Guest User

Shady

a guest
Jun 23rd, 2009
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.56 KB | None | 0 0
  1. #!/bin/bash
  2. # Ipod/G1 Videoencoder v0.3 by Shady
  3. # Features:
  4. # Calculates new aspect ratios depending on video file. Never will you get a stretched out video!
  5. # Skips videos already encoded
  6. # Adds subtitles wether it is softsub from .mkv or a seperate same-named.srt.
  7. # Specify Language preferences for both Audio and Subtitles
  8. # Recursively finds videos and encodes them.
  9. # Made to work especially well on the Andoid OS!
  10. #-------------
  11. #Set home where to put log.
  12. home="/home/shady"
  13.  
  14. #--------Subtitle And Language
  15. #Set to where font is to use in subtitles
  16. subfontpath="/home/shady/myriadwebpro-condensed.ttf"
  17.  
  18. #Set to what scale to use in font.
  19. subfontscale="3.2"
  20.  
  21. #Subtitle options, will pick the english language sub first, if not there then the
  22. #dutch sub file, otherwise the undefined (dont know language sub).
  23.  
  24. #Set to "" if you don't want subtitles. Mind that if your video doesnt have subtitles nothing will happen.
  25. suboptions="-slang eng,dut,und -font $subfontpath -subfont-text-scale $subfontscale"
  26.  
  27. #edit down here if you know what your doing. Check bottom for mencoder options.
  28. #Audiooptions, set to "" if you don't need audiosettings.
  29. audiooptions="-alang jpn,eng,und"
  30.  
  31. #----------
  32.  
  33. #bash doesn't do float calculations, found this on inet somewhere.
  34. function fgt() {
  35.     LHS=$1
  36.     RHS=$2
  37.  
  38.     min=`(echo $LHS ; echo $RHS) | sort -n | head -1`
  39.     if [ "$min" = "$LHS" ]; then
  40.         return 1
  41.     else
  42.         return 0
  43.     fi
  44. }
  45.  
  46. function flt () {
  47.     LHS=$1
  48.     RHS=$2
  49.  
  50.     min=`(echo $LHS ; echo $RHS) | sort -n | head -1`
  51.     if [ "$min" = "$LHS" ]; then
  52.         return 0
  53.     else
  54.         return 1
  55.     fi
  56. }
  57.  
  58. function true_or_false () {
  59.     FUNC=$1
  60.     ARG1=$2
  61.     ARG2=$3
  62.  
  63.     $FUNC $ARG1 $ARG2
  64.  
  65.     if [ $? -eq 0 ]; then
  66.         echo TRUE
  67.         else
  68.         echo FALSE
  69.     fi
  70. }
  71.  
  72. find "$1" \( -name *.avi -o -name *.mkv -o -name *.mp4 -o -name *.ogm \) -a  \( ! -iname "*Mobile*" \)  -type f -print > $home/.genmob.log
  73.  
  74. cat $home/.genmob.log | while read LINE
  75. do
  76.         #File information and check
  77.         #TODO check if there is a mobile directory inside the directory we are looking.
  78.         vfile=$(basename "$LINE")
  79.         vdir=$(dirname "$LINE")
  80.         wext=$(echo "$vfile" | awk -F . '{print $NF}')
  81.         vfilewext=$(basename "$vfile" ."$wext")
  82.         newfile="$vdir/Mobile/NOTREADYYET-Mobile-$vfilewext".mp4
  83.         if [ ! -d "$vdir/Mobile" ] && [ ! -f "$vdir/Mobile/Mobile-$vfilewext.mp4" ] && [ "$(basename "$vdir")" != "Mobile" ]  
  84.         then
  85.             mkdir "$vdir/Mobile"
  86.             #echo $newfile
  87.  
  88.             #Find aspect ratio
  89.             #Movie info:
  90.             #yes i know this is ugly as hell, i dunno how to split string or whatever in bash.
  91.             vwidth=`expr substr $(mplayer -identify "$LINE" -ao null -vo null -frames 0 2>/dev/null | grep ^ID_VIDEO_WIDTH=) 16 5`
  92.             vheight=`expr substr $(mplayer -identify "$LINE" -ao null -vo null -frames 0 2>/dev/null | grep ^ID_VIDEO_HEIGHT=) 17 5`
  93.             vaspect=`echo "$vwidth / $vheight " | bc -l`
  94.             if [ $(true_or_false fgt $vaspect 1.2) = "TRUE" -a $(true_or_false flt $vaspect 1.4) = "TRUE" ]
  95.             then
  96.                     newaspect="4:3"
  97.                     newoptions="scale=432:324,crop=432:320"
  98.             else
  99.                     if [ $(true_or_false fgt $vaspect 1.64) = "TRUE" -a $(true_or_false flt $vaspect 1.81) = "TRUE" ]
  100.                     then
  101.                            newaspect="16:9"
  102.                             newoptions="scale=480:270,expand=480:272"
  103.                     else
  104.                             if [ $(true_or_false fgt $vaspect 1.83) = "TRUE" -a $(true_or_false flt $vaspect 1.87) = "TRUE" ]
  105.                             then
  106.                                     newaspect="37:20"
  107.                                     newoptions="scale=480:259,expand=480:272"
  108.                             else
  109.                                     if [ $(true_or_false fgt $vaspect 2.3) = "TRUE" -a $(true_or_false flt $vaspect 2.5) = "TRUE" ]
  110.                                     then
  111.                                             newaspect="47:20"
  112.                                             newoptions="scale=480:204,expand=480:208"
  113.                                     fi
  114.                             fi
  115.                     fi
  116.             fi
  117.             echo "Encoding: $vwidth x $vheight with ratio $newaspect : $vfile"
  118.             #Edit this line with your own options
  119.             mencoder "$LINE" -o "$newfile" -of lavf -lavfopts format=mp4 -ss 1 -vf pp=ci,$newoptions,harddup -ovc x264 -x264encopts crf=30.0:nocabac:level_idc=30:global_header:threads=2 -ofps 30000/1001 -oac faac -faacopts mpeg=4:object=2:br=128:raw -channels 2 -srate 44100 -af volnorm $suboptions -sub "$vdir/$vfilewext".srt $audiooptions -msglevel all=-1:statusline=5
  120.             mv "$newfile" "$vdir/Mobile/Mobile-$vfilewext".mp4
  121.         else
  122.             echo "Skipping: $vfile already is in a Mobile directory or already has a Mobile directory."
  123.         fi
  124.  
  125.         :
  126. done
  127.  
  128. #4:3=1.33
  129. #Ex: 640x480
  130. #scale=432:324,crop=432:320
  131. #16:9=1.7778
  132. #Ex: 848x480
  133. #scale=480:270,expand=480:272
  134. #37:20=1.8500
  135. #Ex: 1280x688
  136. #scale=480:259,expand=480:270
  137. #47:20=2.4000
  138. #Ex: 1920x800
  139. #scale=480:204,expand=480:208
Advertisement
Add Comment
Please, Sign In to add comment