Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Paste Description for mkvid-interactive.sh

This Bash script transcodes videos primarily for Rockbox.

mkvid-interactive.sh
Tuesday, April 8th, 2008 at 5:44:01pm UTC 

  1. #! /usr/bin/env bash
  2. # interactive program to convert videos to MPEG so they can play in Rockbox
  3.  
  4. # these two values depend on the platform!
  5. screenwidth=220; screenheight=176
  6.  
  7. # Players                                     LCD size  4:3 video  16:9 video
  8. # iPod Video & Gigabeat                       320x240   320x240    320x180
  9. # iriver H300, iPod Color/Photo & Sansa e200  220x176   220x166    220x124
  10. # iPod Nano                                   176x132   176x128    176x100
  11. # iAudio X5 / M5 and iriver H10 20GB / H120   160x128   160x120    160x90
  12. # iPod Mini 1G / 2G                           138x110   138x104    138x78
  13. # iriver H10 5/6GB                            128x128   128x96     128x72
  14. # Sansa c200                                  132x80    106x80     132x74
  15.  
  16. # default values
  17. framerate=25
  18. audiobr=64
  19. videobr=256
  20. croptb=0
  21. croplr=0
  22.  
  23.  
  24. [ $# -ge 2 ] || {
  25.         echo "Usage: $0 { infile outfile } [ ... ]"
  26.         exit
  27. }
  28.  
  29. opts=
  30.  
  31. getscreensize()
  32. {
  33.         local w h
  34.         printf "Screen width (default $screenwidth): "
  35.         read w
  36.         screenwidth=${w:-$screenwidth}
  37.        
  38.         printf "Screen height (default $screenheight): "
  39.         read h
  40.         screenheight=${h:-$screenheight}
  41.        
  42.         echo screenwidth=$screenwidth screenheight=$screenheight
  43. }
  44.  
  45. getaspectratio()
  46. {
  47.         local ar x y eof=true
  48.         echo "Output aspect ratio (5:4 is full screen):"
  49.         select ar in "4:3" "5:4" "16:9" "221:100" "other"; do
  50.                 eof=false
  51.                 case "$ar" in
  52.                 '4:3') x=4; y=3; break;;
  53.                 '5:4') x=5; y=4; break;;
  54.                 '16:9') x=16; y=9; break;;
  55.                 '221:100') x=221; y=100; break;;
  56.                 'other') printf 'Other ratio (width height): '; read x y; break;;
  57.                 *) echo "please select one of the options!";;
  58.                 esac
  59.                 eof=true
  60.         done
  61.         $eof && return 1
  62.        
  63.         # "x*h > y*w" is mathematically equivalent to "x/w > y/h", but the
  64.         # former compares only integers (no division hence no fractions)
  65.         local xy yx
  66.         let xy='x*screenheight'
  67.         let yx='y*screenwidth'
  68.        
  69.         if [ $xy -gt $yx ]; then
  70.                 # limit width to the screen width
  71.                 width=$screenwidth
  72.                 # /2*2 makes height a multiple of 2
  73.                 let height='(width*y+x)/(2*x)*2'
  74.         else
  75.                 # limit height to the screen height
  76.                 height=$screenheight
  77.                 # /2*2 makes width a multiple of 2
  78.                 let width='(height*x+y)/(2*y)*2'
  79.         fi
  80.         echo width=$width height=$height
  81. }
  82.  
  83. getyesno()
  84. {
  85.         local yn
  86.         while :; do
  87.                 printf "%s" "$1"
  88.                 read yn
  89.                 case "$yn" in
  90.                 [Yy]*) return 0;;
  91.                 [Nn]*) return 1;;
  92.                 *) echo "Please answer 'y' or 'n'";;
  93.                 esac
  94.         done
  95. }
  96.  
  97. getcrop()
  98. {
  99.         # if there are black bars on the top and bottom (such as a 16:9 video
  100.         # playing inside a 4:3 video), the amount to crop is
  101.         #   (h - (w / ar)) / 2
  102.         # where h is the source height, w is the source width, and ar is the
  103.         # output aspect ratio. For 320x240 source video containing a 16:9
  104.         # video, use 30 for top/bottom crop value.
  105.         # (240 - (320 / (16/9))) / 2 = 30.
  106.         printf "Crop top/bottom by amount (in pixels) (default $croptb): "
  107.         read croptb
  108.        
  109.         # the same applies to cropping the sides, except the formula is
  110.         # inverted (I can't be arsed to do that right now :)
  111.         printf "Crop left/right by amount (in pixels) (default $croplr): "
  112.         read croplr
  113.         echo crop top/bottom=$croptb left/right=$croplr
  114. }
  115.  
  116. getframerate()
  117. {
  118.         local fr r eof=true
  119.         echo "Output frame rate ($framerate):"
  120.         select fr in "24" "25" "29.97" "30" "other"; do
  121.                 eof=false
  122.                 case "$fr" in
  123.                 '24') framerate=24; break;;
  124.                 '25') framerate=25; break;;
  125.                 '29.97') framerate=29.97; break;;
  126.                 '30') framerate=30; break;;
  127.                 'other')
  128.                         printf 'Other frame rate: '
  129.                         read r
  130.                         framerate=${r:-$framerate}
  131.                         break;;
  132.                 *) echo "please select one of the options!";;
  133.                 esac
  134.                 eof=true
  135.         done
  136.         $eof && return 1
  137.         echo framerate=$framerate
  138. }
  139.  
  140. getvb()
  141. {
  142.         local vb
  143.         printf "Output video bitrate ($videobr): "
  144.         read vb
  145.         if [ -n "$vb" ]; then videobr="$vb"; fi
  146.         echo videobr=$videobr
  147. }
  148.  
  149. getab()
  150. {
  151.         local ab
  152.         printf "Output audio bitrate ($audiobr): "
  153.         read ab
  154.         if [ -n "$ab" ]; then audiobr="$ab"; fi
  155.         echo audiobr=$audiobr
  156. }
  157.  
  158. while [ $# -ge 2 ]; do
  159.         case "$1" in
  160.         -o) echo got option >&2; opts="$2"; shift 2; continue;;
  161.         esac
  162.         infile="$1"
  163.         outfile="$2"
  164.         shift 2
  165.         echo
  166.         echo "Configuration for $infile"
  167.         echo
  168.         if ! { getscreensize &&
  169.                getaspectratio &&
  170.                getcrop &&
  171.                getframerate &&
  172.                getvb &&
  173.                getab; }; then
  174.                 echo "processing next file..."
  175.                 continue
  176.         fi
  177.         ffmpeg -i "$infile" -deinterlace -vcodec mpeg2video -ac 2 -ar 44100 -acodec libmp3lame ${framerate:+-r $framerate} ${croptb:+-croptop $croptb -cropbottom $croptb} ${croplr:+-cropleft $croplr -cropright $croplr} -s ${width}x${height} -vb ${videobr}k -ab ${audiobr}k $opts "$outfile"
  178. done
  179. exit

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right