summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 7cb3c43e17b18e0a600ae7c570a3f2e7a837cd85 (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: offer to skip rendering again if an output file exist already
  9. # TODO: support --width and --height (resolving the other part from input/forced aspect ratio)
  10. # TODO: support --formats (comma-separated, to allow e.g. excluding webm even if supported)
  11. # TODO: add --speech option, using mono, lower audio rate, and (when solved how) speex
  12. set -e
  13. PRG=$(basename "$0")
  14. showhelp() {
  15. cat <<EOF
  16. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  17. Encode video file in multiple web-optimized formats, and provide sample
  18. html favoring open formats with optional non-JavaScript Flash fallback.
  19. -p, --profile Video format:
  20. [modulus 16]
  21. 320x240 qvga 240p 432x240 wqvga
  22. 640x480 vga 480p 848x480 wvga
  23. 576p 1024x576 wsvga
  24. 1024x768 xga hd 720p 1280x720 wxga
  25. [modulus 8]
  26. 480x360 hvga nhd 360p 640x360
  27. 800x600 svga
  28. -s, --size Output size (ffmpeg): WIDTHxHEIGHT vga qcif etc.
  29. (default: use input size)
  30. -a, --aspect Display Aspect Ratio in melt format e.g. @16/9
  31. (default: no aspect hinting)
  32. -r, --rate Video framerate
  33. (default: 25)
  34. -b, --bitrate Video bitrate in bytes, with optional ISO suffix
  35. (default: none)
  36. --h264profile MPEG-4 AVC target profile: baseline main high
  37. (default: baseline)
  38. --h264preset MPEG-4 AVC target preset: slow ultrafast etc.
  39. (default: medium)
  40. --webmpreset WebM target preset: 360p 720p 720p50_60 etc.
  41. (default: profile-related or none)
  42. --stem Stem of output filenames, optionally with path
  43. (default: basename of last input file)
  44. -t, --title Title used in html fallback graphics
  45. (default: stem)
  46. --filter Add melt filter (applied to all input files)
  47. --sample[=frame] Limit output to a 150 frames sample from
  48. beginning or optionally a later start frame
  49. -h, --help This help text
  50. Examples:
  51. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  52. $PRG -p 480p --stem funny -t "Funny guy" myvideo.dv
  53. Options before input files are passed to melt producer, and after to
  54. melt avformat consumer.
  55. When video bitrate is set, 2-pass encoding is used for MPEG-4 output.
  56. Hints:
  57. * Use max. 640x480 pixel size for widest compatibility
  58. (ie. square-pixel 640x360 or anamorphic svcd_ntsc_wide for 16:9)
  59. * Use a modulus 16 profile (qvga vga 480p 720p) for best compression.
  60. * Append these for single-mic speech: ac=1 ar=32000 ab=64k
  61. * Set bitrate for optimal MPEG-4/WebM bits-per-pixel ratio:
  62. + talking head: 0.1
  63. + high-motion or detailed content: 0.15
  64. (inspect result with mediainfo)
  65. More info: <http://camendesign.com/code/video_for_everybody>
  66. <http://www.streaminglearningcenter.com/>
  67. <http://en.wikipedia.org/wiki/HTML5_video>
  68. EOF
  69. }
  70. exit1() {
  71. response="${1:+Error: }${1:-Internal error!}"
  72. echo >&2 "$response"
  73. exit 1
  74. }
  75. # defaults
  76. rate=25
  77. h264profile=baseline
  78. # parse cmdline options
  79. TEMP="`getopt -s sh -o hp:s:a:r:b:t: -l help,profile:,size:,aspect:,rate:,bitrate:,h264profile:,h264preset:,webmpreset:,stem:,title:,filter:,sample:: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  80. eval set -- "$TEMP"
  81. while true ; do
  82. case "$1" in
  83. -h|--help) showhelp; exit;;
  84. -p|--profile) profile="$2"; shift 2;;
  85. -s|--size) size="$2"; shift 2;;
  86. -a|--aspect) aspect="$2"; shift 2;;
  87. -r|--rate) rate="$2"; shift 2;;
  88. -b|--bitrate) bitrate="$2"; shift 2;;
  89. --h264profile) h264profile="$2"; shift 2;;
  90. --h264preset) h264preset="$2"; shift 2;;
  91. --webmpreset) webmpreset="$2"; shift 2;;
  92. --stem) stem="$2"; shift 2;;
  93. -t|--title) title="$2"; shift 2;;
  94. --filter) filters="${filters:+$filters }-filter $2"; shift 2
  95. while [ $# -gt 0 ] ; do
  96. case "$1" in
  97. *=*) filters="${filters:+$filters }$1"; shift;;
  98. *) break;;
  99. esac
  100. done
  101. ;;
  102. --sample) sample="in=${2:-0} out=$((${2:-0} + 150))"; shift 2;;
  103. --) shift; break;;
  104. *) exit1 "Internal error resolving options.";;
  105. esac
  106. done
  107. while [ $# -gt 0 ] ; do
  108. case "$1" in
  109. *=*) _melt_in="${_melt_in:+$_melt_in }$1"; shift;;
  110. *) break;;
  111. esac
  112. done
  113. while [ $# -gt 0 ] ; do
  114. case "$1" in
  115. *=*) _melt_out="${_melt_out:+$_melt_out }$1"; shift;;
  116. *) infiles="${infiles:+$infiles }$1"; shift;;
  117. esac
  118. done
  119. if [ -z "$infiles" ]; then
  120. showhelp
  121. exit1 "Too few parameters!"
  122. fi
  123. # input filename (mandatory)
  124. infile=$(perl -e 'print pop @ARGV' $infiles)
  125. [ -e "$infile" ] || exit1 "Input file missing!"
  126. # resolve stem and title (if not explicitly set)
  127. stem=${stem:-$(basename "$infile" | perl -pe 's/\.[^.]*//')}
  128. title=${title:-$stem}
  129. case "$profile" in
  130. 320x240|qvga)
  131. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  132. _melt="${size:-width=320 height=240}${_melt:+ $_melt}"
  133. webmpreset="${webmpreset:-360p}"
  134. ;;
  135. 480x360|hvga)
  136. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  137. _melt="${size:-width=480 height=360}${_melt:+ $_melt}"
  138. webmpreset="${webmpreset:-360p}"
  139. ;;
  140. 640x480|vga)
  141. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  142. _melt="${size:-width=640 height=480}${_melt:+ $_melt}"
  143. webmpreset="${webmpreset:-360p}"
  144. ;;
  145. 800x600|svga)
  146. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  147. _melt="${size:-width=800 height=600}${_melt:+ $_melt}"
  148. webmpreset="${webmpreset:-360p}"
  149. ;;
  150. 1024x768|xga)
  151. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  152. _melt="${size:-width=1024 height=768}${_melt:+ $_melt}"
  153. webmpreset="${webmpreset:-720p}"
  154. ;;
  155. 240p|432x240|wqvga)
  156. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  157. _melt="${size:-width=432 height=240}${_melt:+ $_melt}"
  158. webmpreset="${webmpreset:-360p}"
  159. ;;
  160. 360p|640x360|nhd)
  161. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  162. _melt="${size:-width=640 height=360}${_melt:+ $_melt}"
  163. webmpreset="${webmpreset:-360p}"
  164. ;;
  165. 480p|848x480|wvga)
  166. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  167. _melt="${size:-width=848 height=480}${_melt:+ $_melt}"
  168. webmpreset="${webmpreset:-360p}"
  169. ;;
  170. 576p|1024x576|wsvga)
  171. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  172. _melt="${size:-width=1024 height=576}${_melt:+ $_melt}"
  173. webmpreset="${webmpreset:-360p}"
  174. ;;
  175. 720p|1280x720|wxga|hd)
  176. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  177. webmpreset="${webmpreset:-720p}"
  178. ;;
  179. '')
  180. _melt="progressive=1 frame_rate_den=1${_melt:+ $_melt}"
  181. [ "25" != "$rate" ] || _melt="{_melt:+$_melt }frame_rate_num=$rate"
  182. ;;
  183. *) exit1 "Unknown profile \"$profile\" - please specify size and aspect directly.";;
  184. esac
  185. [ "25" = "$rate" ] || _melt="{_melt:+$_melt }frame_rate_num=$rate"
  186. case "$h264profile" in
  187. baseline|main)
  188. _melt_h264="properties=x264-medium-$h264profile ${h264preset:+-vpre=libx264-$h264preset}"
  189. ;;
  190. high)
  191. _melt_h264="properties=x264-medium ${h264preset:+-vpre=libx264-$h264preset}"
  192. ;;
  193. *) exit1 "Unknown MPEG-4 AVC profile \"$h264profile\".";;
  194. esac
  195. # TODO: Check and fail if all needed tools are not available
  196. # TODO: When verified beneficial, add option real_time=-2
  197. _melt_in="${_melt_in:+$_melt_in }-progress $sample"
  198. _melt="$_melt ${bitrate:+vb=${bitrate}} ${size:+s=$size} ${aspect:+aspect=$aspect}"
  199. _melt_audio="ab=96k"
  200. _melt_webm="${webmpreset:+-vpre=libvpx-$webmpreset}"
  201. ## Theora/Vorbis/Ogg
  202. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.ogv" f=ogg vcodec=libtheora $_melt acodec=libvorbis $_melt_audio $_melt_out
  203. ## H.264/AAC/MP4
  204. [ -z "$bitrate" ] || melt -group $_melt_in $infiles -group $filters -consumer avformat:/dev/null properties=x264-medium-pass1 $_melt_h264 $_melt $_melt_out
  205. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.mp4" ${bitrate:+pass=2} $_melt_h264 $_melt acodec=aac $_melt_audio $_melt_out
  206. # TODO: drop qt-faststart when melt 0.9.2 is stable
  207. mv "$stem.mp4" "$stem.mp4"~
  208. qt-faststart "$stem.mp4"~ "$stem.mp4"
  209. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  210. ## VP8/Vorbis/WebM
  211. # TODO: use two-pass when supported by melt
  212. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.webm" properties=webm $_melt_webm $_melt $_melt_audio $_melt_out
  213. ## JPEG preview
  214. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  215. # resolve width and height from preview image
  216. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  217. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  218. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  219. heightplus=${height:+$(($height+4))}
  220. # TODO: resolve flash player to use
  221. [ -z "$flashplayer" ] || flash=yes
  222. cat >"$stem.html" <<EOF
  223. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  224. <video width="$width" height="$height" preload controls>
  225. <source src="$stem.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  226. <source src="$stem.webm" type='video/webm; codecs="vp8, vorbis"' />
  227. <source src="$stem.ogv" type='video/ogg; codecs="theora, vorbis"' />
  228. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  229. <param name="movie" value="$flashplayer.swf" />
  230. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  231. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  232. title="No video playback capabilities, please download the video below" />
  233. ${flash:+</object>
  234. }</video>
  235. <p><strong>Download Video:</strong>
  236. open format <a href="$stem.webm">WebM</a>,
  237. open format <a href="$stem.ogv">Ogg</a>,
  238. closed Format <a href="$stem.mp4">MPEG-4</a>.
  239. </p>
  240. EOF