summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 324c05cfcb51dd7b73864e20bc43b68e1189a0b2 (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. --audio Audio style: music speech silence
  43. (default: none - use input channel count)
  44. --stem Stem of output filenames, optionally with path
  45. (default: basename of last input file)
  46. -t, --title Title used in html fallback graphics
  47. (default: stem)
  48. --filter Add melt filter (applied to all input files)
  49. --sample[=frame] Limit output to a 150 frames sample from
  50. beginning or optionally a later start frame
  51. -h, --help This help text
  52. Examples:
  53. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  54. $PRG -p 480p --stem funny -t "Funny guy" myvideo.dv
  55. Options before input files are passed to melt producer, and after to
  56. melt avformat consumer.
  57. When video bitrate is set, 2-pass encoding is used for MPEG-4 output.
  58. Hints:
  59. * Use max. 640x480 pixel size for widest compatibility
  60. (ie. square-pixel 640x360 or anamorphic svcd_ntsc_wide for 16:9)
  61. * Use a modulus 16 profile (qvga vga 480p 720p) for best compression.
  62. * Append these for single-mic speech: ac=1 ar=32000 ab=64k
  63. * Set bitrate for optimal MPEG-4/WebM bits-per-pixel ratio:
  64. + talking head: 0.1
  65. + high-motion or detailed content: 0.15
  66. (inspect result with mediainfo)
  67. More info: <http://camendesign.com/code/video_for_everybody>
  68. <http://www.streaminglearningcenter.com/>
  69. <http://en.wikipedia.org/wiki/HTML5_video>
  70. EOF
  71. }
  72. exit1() {
  73. response="${1:+Error: }${1:-Internal error!}"
  74. echo >&2 "$response"
  75. exit 1
  76. }
  77. # defaults
  78. rate=25
  79. h264profile=baseline
  80. # parse cmdline options
  81. TEMP="`getopt -s sh -o hp:s:a:r:b:t: -l help,profile:,size:,aspect:,rate:,bitrate:,h264profile:,h264preset:,webmpreset:,audio:,stem:,title:,filter:,sample:: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  82. eval set -- "$TEMP"
  83. while true ; do
  84. case "$1" in
  85. -h|--help) showhelp; exit;;
  86. -p|--profile) profile="$2"; shift 2;;
  87. -s|--size) size="$2"; shift 2;;
  88. -a|--aspect) aspect="$2"; shift 2;;
  89. -r|--rate) rate="$2"; shift 2;;
  90. -b|--bitrate) bitrate="$2"; shift 2;;
  91. --h264profile) h264profile="$2"; shift 2;;
  92. --h264preset) h264preset="$2"; shift 2;;
  93. --webmpreset) webmpreset="$2"; shift 2;;
  94. --audio) audio="$2"; shift 2;;
  95. --stem) stem="$2"; shift 2;;
  96. -t|--title) title="$2"; shift 2;;
  97. --filter) filters="${filters:+$filters }-filter $2"; shift 2
  98. while [ $# -gt 0 ] ; do
  99. case "$1" in
  100. *=*) filters="${filters:+$filters }$1"; shift;;
  101. *) break;;
  102. esac
  103. done
  104. ;;
  105. --sample) sample="in=${2:-0} out=$((${2:-0} + 150))"; shift 2;;
  106. --) shift; break;;
  107. *) exit1 "Internal error resolving options.";;
  108. esac
  109. done
  110. while [ $# -gt 0 ] ; do
  111. case "$1" in
  112. *=*) _melt_in="${_melt_in:+$_melt_in }$1"; shift;;
  113. *) break;;
  114. esac
  115. done
  116. while [ $# -gt 0 ] ; do
  117. case "$1" in
  118. *=*) _melt_out="${_melt_out:+$_melt_out }$1"; shift;;
  119. *) infiles="${infiles:+$infiles }$1"; shift;;
  120. esac
  121. done
  122. if [ -z "$infiles" ]; then
  123. showhelp
  124. exit1 "Too few parameters!"
  125. fi
  126. # input filename (mandatory)
  127. infile=$(perl -e 'print pop @ARGV' $infiles)
  128. [ -e "$infile" ] || exit1 "Input file missing!"
  129. # resolve stem and title (if not explicitly set)
  130. stem=${stem:-$(basename "$infile" | perl -pe 's/\.[^.]*//')}
  131. title=${title:-$stem}
  132. case "$profile" in
  133. 320x240|qvga)
  134. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  135. _melt="${size:-width=320 height=240}${_melt:+ $_melt}"
  136. webmpreset="${webmpreset:-360p}"
  137. ;;
  138. 480x360|hvga)
  139. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  140. _melt="${size:-width=480 height=360}${_melt:+ $_melt}"
  141. webmpreset="${webmpreset:-360p}"
  142. ;;
  143. 640x480|vga)
  144. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  145. _melt="${size:-width=640 height=480}${_melt:+ $_melt}"
  146. webmpreset="${webmpreset:-360p}"
  147. ;;
  148. 800x600|svga)
  149. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  150. _melt="${size:-width=800 height=600}${_melt:+ $_melt}"
  151. webmpreset="${webmpreset:-360p}"
  152. ;;
  153. 1024x768|xga)
  154. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  155. _melt="${size:-width=1024 height=768}${_melt:+ $_melt}"
  156. webmpreset="${webmpreset:-720p}"
  157. ;;
  158. 240p|432x240|wqvga)
  159. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  160. _melt="${size:-width=432 height=240}${_melt:+ $_melt}"
  161. webmpreset="${webmpreset:-360p}"
  162. ;;
  163. 360p|640x360|nhd)
  164. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  165. _melt="${size:-width=640 height=360}${_melt:+ $_melt}"
  166. webmpreset="${webmpreset:-360p}"
  167. ;;
  168. 480p|848x480|wvga)
  169. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  170. _melt="${size:-width=848 height=480}${_melt:+ $_melt}"
  171. webmpreset="${webmpreset:-360p}"
  172. ;;
  173. 576p|1024x576|wsvga)
  174. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  175. _melt="${size:-width=1024 height=576}${_melt:+ $_melt}"
  176. webmpreset="${webmpreset:-360p}"
  177. ;;
  178. 720p|1280x720|wxga|hd)
  179. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  180. webmpreset="${webmpreset:-720p}"
  181. ;;
  182. '')
  183. _melt="progressive=1 frame_rate_den=1${_melt:+ $_melt}"
  184. [ "25" != "$rate" ] || _melt="{_melt:+$_melt }frame_rate_num=$rate"
  185. ;;
  186. *) exit1 "Unknown profile \"$profile\" - please specify size and aspect directly.";;
  187. esac
  188. [ "25" = "$rate" ] || _melt="{_melt:+$_melt }frame_rate_num=$rate"
  189. case "$h264profile" in
  190. baseline|main)
  191. _melt_h264="properties=x264-medium-$h264profile ${h264preset:+-vpre=libx264-$h264preset}"
  192. ;;
  193. high)
  194. _melt_h264="properties=x264-medium ${h264preset:+-vpre=libx264-$h264preset}"
  195. ;;
  196. *) exit1 "Unknown MPEG-4 AVC profile \"$h264profile\".";;
  197. esac
  198. case "$audio" in
  199. music)
  200. channels=2
  201. bitrate_vorbis=64
  202. bitrate_aac=96
  203. ;;
  204. speech)
  205. channels=1
  206. bitrate_vorbis=48
  207. bitrate_aac=64
  208. ;;
  209. silence)
  210. channels=0
  211. ;;
  212. '')
  213. channels=$(avprobe -v warning -show_streams "$infile" | perl -ne 's/channels=// and print $_')
  214. ;;
  215. *) exit1 "Unknown audio style \"$audio\".";;
  216. esac
  217. [ $channels -le 2 ] || channels=2
  218. [ $channels -gt 0 ] || channels=
  219. # TODO: Check and fail if all needed tools are not available
  220. # TODO: When verified beneficial, add option real_time=-2
  221. _melt_in="${_melt_in:+$_melt_in }-progress $sample"
  222. _melt="$_melt ${bitrate:+vb=${bitrate}} ${size:+s=$size} ${aspect:+aspect=$aspect}"
  223. _melt_ogg="f=ogg vcodec=libtheora ${bitrate:-qscale=5}"
  224. _melt_h264="$_melt_h264 ${bitrate:-qscale=5}"
  225. _melt_webm="${webmpreset:+-vpre=libvpx-$webmpreset}"
  226. _melt_audio="${channels:+ac=$channels}"
  227. _melt_vorbis="$_melt_audio acodec=libvorbis ab=$(($channels*$bitrate_vorbis))k"
  228. _melt_aac="$_melt_audio acodec=libvorbis ab=$(($channels*$bitrate_aac))k"
  229. ## Theora/Vorbis/Ogg
  230. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.ogv" $_melt_ogg $_melt $_melt_vorbis $_melt_out
  231. ## H.264/AAC/MP4
  232. [ -z "$bitrate" ] || melt -group $_melt_in $infiles -group $filters -consumer avformat:/dev/null properties=x264-medium-pass1 $_melt_h264 $_melt $_melt_out
  233. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.mp4" ${bitrate:+pass=2} $_melt_h264 $_melt $_melt_aac $_melt_out
  234. # TODO: drop qt-faststart when melt 0.9.2 is stable
  235. mv "$stem.mp4" "$stem.mp4"~
  236. qt-faststart "$stem.mp4"~ "$stem.mp4"
  237. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  238. ## VP8/Vorbis/WebM
  239. # TODO: use two-pass when supported by melt
  240. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.webm" properties=webm $_melt_webm $_melt $_melt_vorbis $_melt_out
  241. ## JPEG preview
  242. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  243. # resolve width and height from preview image
  244. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  245. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  246. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  247. heightplus=${height:+$(($height+4))}
  248. # TODO: resolve flash player to use
  249. [ -z "$flashplayer" ] || flash=yes
  250. cat >"$stem.html" <<EOF
  251. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  252. <video width="$width" height="$height" preload controls>
  253. <source src="$stem.mp4" type="video/mp4" />
  254. <source src="$stem.webm" type="video/webm" />
  255. <source src="$stem.ogv" type="video/ogg" />
  256. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  257. <param name="movie" value="$flashplayer.swf" />
  258. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  259. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  260. title="No video playback capabilities, please download the video below" />
  261. ${flash:+</object>
  262. }</video>
  263. <p><strong>Download Video:</strong>
  264. open format <a href="$stem.webm">WebM</a>,
  265. open format <a href="$stem.ogv">Ogg</a>,
  266. closed Format <a href="$stem.mp4">MPEG-4</a>.
  267. </p>
  268. EOF