summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 0d5868d01de0647e04c280de50cfc87fe4d87d68 (plain)
  1. #!/bin/sh
  2. # Origins:
  3. # http://diveintohtml5.org/video.html
  4. # http://camendesign.com/code/video_for_everybody
  5. # http://www.streaminglearningcenter.com/articles/so-you-want-to-get-to-know-h264.html
  6. # Possible flashplayers:
  7. # http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/
  8. set -e
  9. PRG=$(basename "$0")
  10. showhelp() {
  11. cat <<EOF
  12. Usage: $PRG --size SIZE [--profile PROFILE] [--title TITLE] INPUTFILE
  13. Encode video file in multiple web-optimized formats, and provide sample
  14. html favoring open formats with optional non-JavaScript Flash fallback.
  15. -s, --size Output size in WIDTHxHEIGHT
  16. (mandatory)
  17. -p, --profile Quality profile (lo, hi, lowide, hiwide)
  18. (default: lo)
  19. -t, --title Title used in html fallback graphics
  20. (default: basename of input file)
  21. -h, --help This help text
  22. Examples:
  23. $PRG -s 320x200 -t "Funny guy" myvideo.dv
  24. NB! use max. 640x480 to support old iPod (e.g. 640x360 for 16:9).
  25. More info: <http://camendesign.com/code/video_for_everybody>
  26. EOF
  27. }
  28. exit1() {
  29. response="${1:+Error: }${1:-Internal error!}"
  30. echo >&2 "$response"
  31. exit 1
  32. }
  33. # parse cmdline options
  34. TEMP="`getopt -s sh -o hs:p:t: -l help,size:,profile:,title: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  35. eval set -- "$TEMP"
  36. while true ; do
  37. case "$1" in
  38. -h|--help) showhelp; exit;;
  39. -s|--size) size="$2"; shift 2;;
  40. -p|--profile) profile="$2"; shift 2;;
  41. -t|--title) title="$2"; shift 2;;
  42. --) shift; break;;
  43. *) exit1 "Internal error resolving options.";;
  44. esac
  45. done
  46. if [ $# -eq 0 ]; then
  47. showhelp
  48. exit1 "Too few parameters!"
  49. fi
  50. if [ $# -gt 1 ]; then
  51. showhelp
  52. exit1 "Too many parameters!"
  53. fi
  54. # input filename (mandatory)
  55. infile="$1"; shift
  56. [ -e "$infile" ] || exit1 "Input file missing!"
  57. filebase=$(echo "$infile" | perl -pe 's/\.[^.]*//')
  58. # output size in WIDTHxHEIGHT (mandatory)
  59. # TODO: Make optional with input size as default
  60. # TODO: Make each half of size optional with default resolved from input aspect ratio
  61. [ -n "$size" ] || exit1 "Unknown output size!"
  62. width=$(echo "$size" | perl -pe 's/x[^x]*//')
  63. height=$(echo "$size" | perl -pe 's/[^x]*x//')
  64. heightplus=${height:+$(($height+4))}
  65. # fallback profile
  66. profile=${profile:-lo}
  67. case "$profile" in
  68. lo)
  69. bitrate=768
  70. aspect=4:3
  71. ;;
  72. lowide)
  73. bitrate=768
  74. aspect=16:9
  75. ;;
  76. hi)
  77. bitrate=1120
  78. aspect=4:3
  79. ;;
  80. hiwide)
  81. bitrate=1120
  82. aspect=16:9
  83. ;;
  84. esac
  85. # fallback graphics title
  86. title=${title:-$filebase}
  87. # TODO: Check and fail if all needed tools are not available
  88. ## Theora/Vorbis/Ogg
  89. ffmpeg2theora --videobitrate "$bitrate" --max_size "$size" --aspect "$aspect" --output "$filebase.ogv" "$infile"
  90. ## H.264/AAC/MP4
  91. HandBrakeCLI --preset "iPhone & iPod Touch" --vb "$bitrate" ${width:+--maxWidth "$width"} ${height:+--maxHeight "$height"} --two-pass --turbo --optimize --input "$infile" --output "$filebase.mp4"
  92. ## VP8/Vorbis/WebM
  93. ! ffmpeg -codecs | grep -iw vp8 || webm=yes
  94. # FIXME: check and fail if logfiles exist already
  95. [ -z "$webm" ] || ffmpeg -pass 1 -passlogfile "$infile" -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i "$infile" -vcodec libvpx -b "$bitrate"k -s "$size" -aspect "$aspect" -an -f webm -y NUL
  96. [ -z "$webm" ] || ffmpeg -pass 2 -passlogfile "$infile" -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i "$infile" -vcodec libvpx -b "$bitrate"k -s "$size" -aspect "$aspect" -acodec libvorbis -ac 2 -y "$filebase.webm"
  97. rm "$infile"-*.log
  98. ## JPEG preview
  99. ffmpegthumbnailer -s0 -i "$filebase.ogv" -o "$filebase.jpg"
  100. # TODO: resolve flash player to use
  101. [ -z "$flashplayer" ] || flash=yes
  102. cat >"$filebase.html" <<EOF
  103. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  104. <video width="$width" height="$height" preload controls>
  105. <source src="$filebase.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  106. <source src="$filebase.ogv" type='video/ogg; codecs="theora, vorbis"' />
  107. ${webm:+<source src="$filebase.webm" type='video/webm; codecs="vp8, vorbis"' />
  108. }${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  109. <param name="movie" value="$flashplayer.swf" />
  110. <param name="flashvars" value="image=$filebase.jpg&amp;file=$filebase.mp4" />
  111. }<img src="$filebase.jpg" width="$width" height="$height" alt="$title"
  112. title="No video playback capabilities, please download the video below" />
  113. ${flash:+</object>
  114. }</video>
  115. <p><strong>Download Video:</strong>
  116. open format <a href="$filebase.ogv">Ogg</a>,
  117. ${webm:+open format <a href="$filebase.webm">WebM</a>,}
  118. closed Format <a href="$filebase.mp4">MP4</a>.
  119. </p>
  120. EOF