Advertisement
julian_hughes

vid4rb

Nov 30th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.33 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # vid4rb
  4. #
  5. # REQUIREMENTS:
  6. # Mplayer & Mencoder
  7.  
  8. # This script encodes video for playback on Rockboxed players.  It
  9. # automatically detects and applies good crop values.  It automatically
  10. # scales the video to a suitable size.  It detects the audio type in the
  11. # source video and transcodes it only if necessary.  There are options
  12. # to make sample encodes (tests), to specify output directory, to
  13. # disable autocropping and to choose higher quality encoding.
  14. # Run the script with no options or arguments for some short
  15. # instructions & read the comments at the end of the script.
  16.  
  17. SCREENW=320
  18.  
  19. function usage ()
  20. {
  21. printf "\n\tEdit the script and change the value of SCREENW to the width
  22. \tof your player's screen.
  23. \n\tScreen Sizes WxH of various Rockbox Supported Players:\n
  24. \tiPod Video, Gigabeat, Fuze+\t\t\t320x240
  25. \tiriver H300, iPod Color/Photo & Sansa e200\t220x176
  26. \tiPod Nano\t\t\t\t\t176x132
  27. \tiAudio X5 / M5 and iriver H10 20GB / H120     160x128
  28. \tiPod Mini 1G / 2G\t\t\t\t138x110
  29. \tiriver H10 5/6GB\t\t\t\t128x128
  30. \tSansa c200\t\t\t\t\t132x80
  31. \n\nUSAGE:\n\t$(basename "$0") -o <full path to output dire\
  32. ctory> [videofile1 videofile2 videofile3...]
  33. \tIf -o is not used then new files are written to input directories.\n
  34. \t-t encode 120 second test sample
  35. \t-f # set fps to # (defaults to 25)
  36. \t-c disable crop detection and cropping (useful for short clips)
  37. \t-q Higher quality - Video at 400 kbs & Audio at 160 kbs\n
  38. \tConverted movies have old extension removed and are saved as
  39. \t<movie>_rb.mpeg or <movie>_sample_rb.mpeg\n\n"
  40. }
  41.  
  42. function checkaudio ()
  43. {
  44.     AUD=$(mplayer -noconfig all -frames 0 -vo null -ao null -channels 8 \
  45.     -identify "$MOVIE" 2>/dev/null |awk -F "ID_" '{print $2}'|sort -u|\
  46.     sed '/^$/d' |grep -ci -e AUDIO_CODEC=mpg123 -e AUDIO_RATE=44100)
  47. }
  48.  
  49. function cropdetect ()
  50. {
  51.     CROP="1"
  52.     TOTAL_LOOPS="10"
  53.  
  54.     A=0
  55.     while [ "$A" -lt "$TOTAL_LOOPS" ] ; do
  56.     A="$(( $A + 1 ))"
  57.     SKIP_SECS="$(( 30 * $A ))"
  58.  
  59.     mplayer "$MOVIE" -ss $SKIP_SECS \
  60.     -frames 20 -vo md5sum -nosound -nocache \
  61.     -vf cropdetect=20:16 2>&1 > mplayer.log < /dev/null
  62.  
  63.     CROP[$A]=`awk -F 'crop=' '/crop/ {print $2}' < mplayer.log |\
  64.     awk -F ')' '{print $1}' | tail -n 1`
  65.  
  66. done
  67.     rm md5sums mplayer.log
  68.  
  69.  
  70.     B=0
  71.     while [ "$B" -lt "$TOTAL_LOOPS" ] ; do
  72.     B="$(( $B + 1 ))"
  73.  
  74.     C=0
  75.     while [ "$C" -lt "$TOTAL_LOOPS" ] ; do
  76.     C="$(( $C + 1 ))"
  77.  
  78. if [ "${CROP[$B]}" == "${CROP[$C]}" ] ; then
  79.     COUNT_CROP[$B]="$(( ${COUNT_CROP[$B]} + 1 ))"
  80. fi
  81. done  
  82. done
  83.  
  84.     HIGHEST_COUNT=0
  85.  
  86.     D=0
  87.     while [ "$D" -lt "$TOTAL_LOOPS" ] ; do
  88.     D="$(( $D + 1 ))"
  89.  
  90. if [ "${COUNT_CROP[$D]}" -gt "$HIGHEST_COUNT" ] ; then
  91.     HIGHEST_COUNT="${COUNT_CROP[$D]}"
  92.     GREATEST="$D"
  93. fi
  94. done
  95.  
  96.     CROP="crop=${CROP[$GREATEST]}"
  97.     echo -e "$CROP"
  98.    
  99. }
  100.  
  101. while getopts 'co:qtF' OPTION
  102. do
  103.     case $OPTION in
  104.     c)  cflag=1
  105.         ;;
  106.     f)  fflag=1
  107.         fval=$OPTARG
  108.         ;;
  109.     o)  oflag=1
  110.         oval=$OPTARG
  111.         ;;
  112.     q)  qflag=1
  113.         ;;
  114.     t)  tflag=1
  115.         ;;
  116.     esac
  117. done
  118. shift $(($OPTIND-1))
  119.  
  120. if [ $# -lt 1 ]; then
  121.     usage
  122.     exit 1
  123. fi
  124.  
  125. printf "\n\tMovies to be converted are:\n"
  126. printf "\n\t\t%s" "$@"
  127.  
  128. let CORES=$(cat /proc/cpuinfo | grep processor | wc -l)
  129.  
  130. for MOVIE in "$@" ; do
  131.  
  132. if [ $oflag ]; then
  133.     VIDOUT=$(echo ${MOVIE%.*}| sed 's,.*/,,')
  134.     VIDOUT="$oval"/"$VIDOUT"
  135.     else
  136.     VIDOUT="${MOVIE%.*}"
  137. fi
  138.  
  139. if [ $tflag ]; then
  140.     SAMPLE=" -ss 120 -endpos 120"
  141.     EXTN=_sample_rb.mpeg
  142.     else
  143.     EXTN=_rb.mpeg
  144. fi
  145.  
  146. if [ $qflag ]; then
  147.     TRANSCODE="-af resample=44100:0:0:volnorm=2,volume=2 -oac mp3lame \
  148. -lameopts cbr:preset=160"
  149.     VBITRATE="vbitrate=400"
  150.     else
  151.     TRANSCODE="-af resample=44100:0:0:volnorm=2,volume=2 -oac mp3lame \
  152. -lameopts cbr:preset=96"
  153.     VBITRATE="vbitrate=224"
  154. fi
  155.  
  156. # if audio is already mp1/2/3 at 44.1 then keep it, otherwise transcode.
  157. checkaudio
  158. if  [ $AUD -eq 2 ]; then
  159.     AUDIO="-oac copy"
  160.     else
  161.     AUDIO=$TRANSCODE
  162. fi
  163.  
  164. printf "\n\n\tConverting $(basename "$MOVIE")\n\t
  165. Calculating Cropping, Scaling, Audio...wait\n\n"
  166.  
  167. if [ -z $cflag ]; then
  168. # Run cropdetect, discard all the crap, get the crop value
  169.     CROP="-vf crop=$(cropdetect| awk -F 'crop=' '/crop/ {print $2}')"
  170.     echo $CROP
  171.     else
  172.     CROP=""
  173. fi
  174.  
  175. # Do the encoding
  176. mencoder "$MOVIE"$SAMPLE \
  177. -of mpeg -ofps ${fval:-25} $CROP -vf scale=$SCREENW:-2 \
  178. -ovc lavc -lavcopts vcodec=mpeg2video:vmax_b_frames=4:$VBITRATE:threads=$CORES -mc 0 -noskip \
  179. -alang en $AUDIO \
  180. -o "$VIDOUT"$EXTN
  181. done
  182.  
  183. # Cropping
  184. #
  185. # The autocrop will often cause a failure if the video is much shorter
  186. # than 5 minutes.  Use -c to disable the autocropping.
  187.  
  188. # Audio
  189. #
  190. # Rockbox's MPEGplayer can play video with mp1, mp2 or mp3 audio, but it
  191. # can only play audio with a sample rate of 44.1 KHz.  It's quite common
  192. # to find mp3 audio at 48 KHz in video clips.  This script detects if
  193. # audio is mp1/mp2/mp3 at 44.1. If so then the audio is retained. If not
  194. # then it is transcoded.
  195.  
  196. # Quality
  197. #
  198. # Default settings are video bitrate at 224 and audio at 96.
  199. # Use -q to switch this to video at 400 and audio at 160.
  200. # If the existing audio is already mp1/mp2/mp3 at 44.1 then it will not
  201. # be transcoded even when the -q option is used, only the video bitrate
  202. # will be increased. This prevents loss of audio quality where possible.
  203. #
  204. # Both picture quality and file size increase a lot when raising video
  205. # bitrate to 400.  Above 400 you don't really see any quality
  206. # improvement on tiny screens, only bigger files.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement