summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 753ea8050ca721f500248733d4eb59783532fecd (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. # TODO: support --width and --height (resolving the other part from input/forced aspect ratio)
  9. set -e
  10. PRG=$(basename "$0")
  11. showhelp() {
  12. cat <<EOF
  13. Usage: $PRG --size SIZE [--profile PROFILE] [--title TITLE] INPUTFILE
  14. Encode video file in multiple web-optimized formats, and provide sample
  15. html favoring open formats with optional non-JavaScript Flash fallback.
  16. -s, --size Output size in WIDTHxHEIGHT
  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. # fallback profile
  59. profile=${profile:-lo}
  60. case "$profile" in
  61. lo)
  62. bitrate=768
  63. aspect=4:3
  64. ;;
  65. lowide)
  66. bitrate=768
  67. aspect=16:9
  68. ;;
  69. hi)
  70. bitrate=1120
  71. aspect=4:3
  72. ;;
  73. hiwide)
  74. bitrate=1120
  75. aspect=16:9
  76. ;;
  77. esac
  78. # fallback graphics title
  79. title=${title:-$filebase}
  80. # TODO: Check and fail if all needed tools are not available
  81. _ffmpeg() {
  82. passes="$1"; shift
  83. ext="$1"; shift
  84. [ ! -f "$filebase.$ext" ] || exit1 "output file \"$filebase.$ext\" already exist. Remove it and try again."
  85. case $passes in
  86. 0)
  87. ffmpeg -i "$infile" $opts "$@" -f "$ext" -y "$filebase.$ext"
  88. ;;
  89. 1)
  90. [ ! -f "$filebase"-*.log ] || exit1 "logfiles already exist. Remove them (or wait for that other process to complete) and try again."
  91. ffmpeg -i "$infile" -pass 1 -passlogfile "$filebase" "$@" -an -f "$ext" -y /dev/null
  92. ;;
  93. 2)
  94. [ -f "$filebase"-*.log ] || exit1 "logfiles missing. Pass 2 cannot succeed without a prior pass 1."
  95. ffmpeg -i "$infile" -pass 2 -passlogfile "$filebase" "$@" -y "$filebase.$ext"
  96. rm "$filebase"-*.log
  97. case $ext in
  98. mp4)
  99. mv "$filebase.$ext" "$filebase.$ext"~
  100. qt-faststart "$filebase.$ext"~ "$filebase.$ext"
  101. [ -f "$filebase.$ext" ] && rm "$filebase.$ext"~ || exit1 "failed to optimize with qt-faststart."
  102. ;;
  103. esac
  104. ;;
  105. esac
  106. }
  107. opts_common="-threads 0 -b ${bitrate}k ${size:+-s $size} ${aspect:+-aspect $aspect}"
  108. opts_audio="-ac 2 -ar 44100 -ab 96k"
  109. opts_webm="-vcodec libvpx -keyint_min 0 -g 250 -skip_threshold 0"
  110. ## Theora/Vorbis/Ogg
  111. _ffmpeg 0 ogg $opts_common -vcodec libtheora -acodec libvorbis $opts_audio
  112. ## H.264/AAC/MP4
  113. _ffmpeg 1 mp4 $opts_common -vcodec libx264 -vpre medium_firstpass -vpre baseline
  114. _ffmpeg 2 mp4 $opts_common -vcodec libx264 -vpre medium -vpre baseline -acodec libvo_aacenc $opts_audio
  115. ## VP8/Vorbis/WebM
  116. ! ffmpeg -codecs | grep -iw vp8 || webm=yes
  117. [ -z "$webm" ] || _ffmpeg 1 webm $opts_common $opts_webm
  118. [ -z "$webm" ] || _ffmpeg 2 webm $opts_common $opts_webm -acodec libvorbis $opts_audio
  119. ## JPEG preview
  120. ffmpegthumbnailer -s0 -i "$filebase.ogg" -o "$filebase.jpg"
  121. # resolve width and height from preview image
  122. size=${size:-$(jpeginfo "$filebase.jpg" | perl -ane 'print "$F[1]x$F[3]"')}
  123. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  124. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  125. heightplus=${height:+$(($height+4))}
  126. # TODO: resolve flash player to use
  127. [ -z "$flashplayer" ] || flash=yes
  128. cat >"$filebase.html" <<EOF
  129. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  130. <video width="$width" height="$height" preload controls>
  131. <source src="$filebase.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  132. <source src="$filebase.ogg" type='video/ogg; codecs="theora, vorbis"' />
  133. ${webm:+<source src="$filebase.webm" type='video/webm; codecs="vp8, vorbis"' />
  134. }${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  135. <param name="movie" value="$flashplayer.swf" />
  136. <param name="flashvars" value="image=$filebase.jpg&amp;file=$filebase.mp4" />
  137. }<img src="$filebase.jpg" width="$width" height="$height" alt="$title"
  138. title="No video playback capabilities, please download the video below" />
  139. ${flash:+</object>
  140. }</video>
  141. <p><strong>Download Video:</strong>
  142. open format <a href="$filebase.ogg">Ogg</a>,
  143. ${webm:+open format <a href="$filebase.webm">WebM</a>,}
  144. closed Format <a href="$filebase.mp4">MP4</a>.
  145. </p>
  146. EOF