summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 2d349ff16efd528525c3271a9ae739750324ea88 (plain)
  1. #!/bin/sh
  2. # Copyright © 2010-2014 Jonas Smedegaard <dr@jones.dk>
  3. # Description: Recode a video into web-optimized format(s)
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # Depends: libav-tools melt ffmpegthumbnailer
  19. #
  20. # Origins:
  21. # http://diveintohtml5.org/video.html
  22. # http://camendesign.com/code/video_for_everybody
  23. # http://www.streaminglearningcenter.com/articles/so-you-want-to-get-to-know-h264.html
  24. # http://www.penguinproducer.com/2012/01/ladspa-noise-removal/
  25. #
  26. # Possible flashplayers:
  27. # http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/
  28. #
  29. # TODO: offer to skip rendering again if an output file exist already
  30. # TODO: support --width and --height (resolving the other part from input/forced aspect ratio)
  31. # TODO: support --formats (comma-separated, to allow e.g. excluding webm even if supported)
  32. # TODO: add --speech option, using mono, lower audio rate, and (when solved how) speex
  33. set -e
  34. PRG=$(basename "$0")
  35. showhelp() {
  36. cat <<EOF
  37. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  38. Encode video file in multiple web-optimized formats, and provide sample
  39. html favoring open formats with optional non-JavaScript Flash fallback.
  40. -p, --profile Video format:
  41. [modulus 16]
  42. 320x240 qvga
  43. 640x480 vga 480p 848x480 wvga
  44. 576p 1024x576 wsvga
  45. 1024x768 xga hd 720p 1280x720 wxga
  46. [modulus 8]
  47. 240p 424x240 wqvga
  48. 480x360 hvga nhd 360p 640x360
  49. 800x600 svga
  50. -s, --size Output size (ffmpeg): WIDTHxHEIGHT vga qcif etc.
  51. (default: use input size)
  52. -a, --aspect Display Aspect Ratio in melt format e.g. @16/9
  53. (default: no aspect hinting)
  54. -r, --rate Video framerate
  55. (default: 25)
  56. -b, --bitrate Video bitrate in bytes, with optional ISO suffix
  57. (default: none)
  58. --h264profile MPEG-4 AVC target profile: baseline main high
  59. (default: baseline)
  60. --h264preset MPEG-4 AVC target preset: slow ultrafast etc.
  61. (default: medium)
  62. --webmpreset WebM target preset: 360p 720p 720p50_60 etc.
  63. (default: profile-related or none)
  64. --audio Audio style:
  65. channels compress normalize limit
  66. music max 2 X X
  67. speech 1 X X X
  68. silence 0
  69. (default: none - use input channel count)
  70. --stem Stem of output filenames, optionally with path
  71. (default: basename of last input file)
  72. -t, --title Title used in html fallback graphics
  73. (default: stem)
  74. --filter Add melt filter (applied to all input files)
  75. --sample[=frame] Limit output to a 150 frames sample from
  76. beginning or optionally a later start frame
  77. -h, --help This help text
  78. Examples:
  79. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  80. $PRG -p 480p --stem funny -t "Funny guy" myvideo.dv
  81. Options before input files are passed to melt producer, and after to
  82. melt avformat consumer.
  83. When video bitrate is set, 2-pass encoding is used for MPEG-4 output.
  84. Hints:
  85. * Use max. 640x480 pixel size for widest compatibility
  86. (ie. square-pixel 640x360 or anamorphic svcd_ntsc_wide for 16:9)
  87. * Use a modulus 16 profile (qvga vga 480p 720p) for best compression.
  88. * Append these for single-mic speech: ac=1 ar=32000 ab=64k
  89. * Set bitrate for optimal MPEG-4/WebM bits-per-pixel ratio:
  90. + talking head: 0.1
  91. + high-motion or detailed content: 0.15
  92. (inspect result with mediainfo)
  93. More info: <http://camendesign.com/code/video_for_everybody>
  94. <http://www.streaminglearningcenter.com/>
  95. <http://en.wikipedia.org/wiki/HTML5_video>
  96. EOF
  97. }
  98. exit1() {
  99. response="${1:+Error: }${1:-Internal error!}"
  100. echo >&2 "$response"
  101. exit 1
  102. }
  103. # defaults
  104. rate=25
  105. h264profile=baseline
  106. # parse cmdline options
  107. 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."
  108. eval set -- "$TEMP"
  109. while true ; do
  110. case "$1" in
  111. -h|--help) showhelp; exit;;
  112. -p|--profile) profile="$2"; shift 2;;
  113. -s|--size) size="$2"; shift 2;;
  114. -a|--aspect) aspect="$2"; shift 2;;
  115. -r|--rate) rate="$2"; shift 2;;
  116. -b|--bitrate) bitrate="$2"; shift 2;;
  117. --h264profile) h264profile="$2"; shift 2;;
  118. --h264preset) h264preset="$2"; shift 2;;
  119. --webmpreset) webmpreset="$2"; shift 2;;
  120. --audio) audio="$2"; shift 2;;
  121. --stem) stem="$2"; shift 2;;
  122. -t|--title) title="$2"; shift 2;;
  123. --filter) filters="${filters:+$filters }-filter $2"; shift 2
  124. while [ $# -gt 0 ] ; do
  125. case "$1" in
  126. *=*) filters="${filters:+$filters }$1"; shift;;
  127. *) break;;
  128. esac
  129. done
  130. ;;
  131. --sample) sample="in=${2:-0} out=$((${2:-0} + 150))"; shift 2;;
  132. --) shift; break;;
  133. *) exit1 "Internal error resolving options.";;
  134. esac
  135. done
  136. # Resolve if melt is version 0.9.2 or newer
  137. # TODO: drop when melt 0.9.2 is stable
  138. melt_recent=$(melt -query filter=loudness | grep -i R128)
  139. while [ $# -gt 0 ] ; do
  140. case "$1" in
  141. *=*) _melt_in="${_melt_in:+$_melt_in }$1"; shift;;
  142. *) break;;
  143. esac
  144. done
  145. while [ $# -gt 0 ] ; do
  146. case "$1" in
  147. *=*) _melt_out="${_melt_out:+$_melt_out }$1"; shift;;
  148. *) infiles="${infiles:+$infiles }$1"; shift;;
  149. esac
  150. done
  151. if [ -z "$infiles" ]; then
  152. showhelp
  153. exit1 "Too few parameters!"
  154. fi
  155. # input filename (mandatory)
  156. infile_first=$(perl -e 'print pop @ARGV' $infiles)
  157. [ -e "$infile_first" ] || exit1 "Input file missing!"
  158. # resolve stem and title (if not explicitly set)
  159. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  160. title=${title:-$stem}
  161. case "$profile" in
  162. 320x240|qvga)
  163. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  164. _melt_video="${size:-width=320 height=240}${_melt_video:+ $_melt_video}"
  165. webmpreset="${webmpreset:-360p}"
  166. ;;
  167. 480x360|hvga)
  168. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  169. _melt_video="${size:-width=480 height=360}${_melt_video:+ $_melt_video}"
  170. webmpreset="${webmpreset:-360p}"
  171. ;;
  172. 640x480|vga)
  173. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  174. _melt_video="${size:-width=640 height=480}${_melt_video:+ $_melt_video}"
  175. webmpreset="${webmpreset:-360p}"
  176. ;;
  177. 800x600|svga)
  178. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  179. _melt_video="${size:-width=800 height=600}${_melt_video:+ $_melt_video}"
  180. webmpreset="${webmpreset:-360p}"
  181. ;;
  182. 1024x768|xga)
  183. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  184. _melt_video="${size:-width=1024 height=768}${_melt_video:+ $_melt_video}"
  185. webmpreset="${webmpreset:-720p}"
  186. ;;
  187. 240p|424x240|wqvga)
  188. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  189. _melt_video="${size:-width=424 height=240}${_melt_video:+ $_melt_video}"
  190. webmpreset="${webmpreset:-360p}"
  191. ;;
  192. 360p|640x360|nhd)
  193. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  194. _melt_video="${size:-width=640 height=360}${_melt_video:+ $_melt_video}"
  195. webmpreset="${webmpreset:-360p}"
  196. ;;
  197. 480p|848x480|wvga)
  198. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  199. _melt_video="${size:-width=848 height=480}${_melt_video:+ $_melt_video}"
  200. webmpreset="${webmpreset:-360p}"
  201. ;;
  202. 576p|1024x576|wsvga)
  203. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  204. _melt_video="${size:-width=1024 height=576}${_melt_video:+ $_melt_video}"
  205. webmpreset="${webmpreset:-360p}"
  206. ;;
  207. 720p|1280x720|wxga|hd)
  208. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  209. webmpreset="${webmpreset:-720p}"
  210. ;;
  211. '')
  212. _melt_video="frame_rate_den=1${_melt_video:+ $_melt_video}"
  213. [ "25" != "$rate" ] || _melt_video="{_melt_video:+$_melt_video }frame_rate_num=$rate"
  214. ;;
  215. *) exit1 "Unknown profile \"$profile\" - please specify size and aspect directly.";;
  216. esac
  217. [ "25" = "$rate" ] || _melt_video="{_melt_video:+$_melt_video }frame_rate_num=$rate"
  218. case "$h264profile" in
  219. baseline|main)
  220. _melt_h264="properties=x264-medium-$h264profile ${h264preset:+-vpre=libx264-$h264preset}"
  221. ;;
  222. high)
  223. _melt_h264="properties=x264-medium ${h264preset:+-vpre=libx264-$h264preset}"
  224. ;;
  225. *) exit1 "Unknown MPEG-4 AVC profile \"$h264profile\".";;
  226. esac
  227. # default per-codec-channel bitrates
  228. bitrate_vorbis=64
  229. bitrate_aac=96
  230. case "$audio" in
  231. music)
  232. channels=2
  233. # limit (i.e. avoid peaks "clipping")
  234. _melt_audio_filters="-filter ladspa.1077"
  235. ;;
  236. speech)
  237. channels=1
  238. bitrate_vorbis=48
  239. bitrate_aac=64
  240. # expand and compress (i.e. lower volume on silent and loud passages)
  241. _melt_audio_filters_early="-filter ladspa.1075 -filter ladspa.1073"
  242. # limit (i.e. avoid peaks "clipping")
  243. _melt_audio_filters="-filter ladspa.1077"
  244. ;;
  245. silence)
  246. channels=0
  247. ;;
  248. '')
  249. channels=$(avprobe -v warning -show_streams "$infile_first" | perl -ne 's/channels=// and print $_')
  250. ;;
  251. *) exit1 "Unknown audio style \"$audio\".";;
  252. esac
  253. [ $channels -le 2 ] || channels=2
  254. [ $channels -gt 0 ] || channels=
  255. # TODO: Check and fail if all needed tools are not available
  256. # TODO: When verified beneficial, add option real_time=-2
  257. melt="melt -progress"
  258. _melt_in="${_melt_in:+$_melt_in }$sample"
  259. _melt_video="progressive=1 $_melt_video${bitrate:+ vb=${bitrate}}${size:+ s=$size}${aspect:+ aspect=$aspect}"
  260. _melt_ogg="$_melt_video f=ogg vcodec=libtheora${bitrate:- qscale=5}"
  261. _melt_h264="$_melt_video $_melt_h264${bitrate:- qscale=5}"
  262. _melt_webm="$_melt_video${webmpreset:+ vpre=libvpx-$webmpreset}"
  263. _melt_audio="${channels:+ac=$channels}"
  264. _melt_vorbis="$_melt_audio acodec=libvorbis ab=$(($channels*$bitrate_vorbis))k"
  265. _melt_aac="$_melt_audio acodec=aac ab=$(($channels*$bitrate_aac))k"
  266. # resolve EBU R128 audio normalizing
  267. # TODO: normalize each infile separately when xml fed as infile keeps sync
  268. if [ -n "$melt_recent" ] && [ -n "$channels" ]; then
  269. $melt -group $_melt_in $infiles -group $_melt_audio_filters_early -filter loudness -consumer xml:$stem.xml $_melt_audio video_off=1 all=1
  270. _melt_loudness="$(perl -ne 'm!<property name="results">([^<]+)</property>! and print $1' $stem.xml)"
  271. fi
  272. ## Theora/Vorbis/Ogg
  273. $melt -group $_melt_in $infiles -group ${channels:+$_melt_audio_filters_early${_melt_loudness:+ -filter loudness results="$_melt_loudness"} $_melt_audio_filters }$filters -consumer avformat:"$stem.ogv" $_melt_ogg $_melt_vorbis $_melt_out
  274. ## H.264/AAC/MP4
  275. [ -z "$bitrate" ] || $melt -group $_melt_in $infiles -group $filters -consumer avformat:/dev/null properties=x264-medium-pass1 $_melt_h264 $_melt_out
  276. $melt -group $_melt_in $infiles -group ${channels:+$_melt_audio_filters_early${_melt_loudness:+ -filter loudness results="$_melt_loudness"} $_melt_audio_filters }$filters -consumer avformat:"$stem.mp4" ${bitrate:+pass=2} $_melt_h264 $_melt_aac $_melt_out
  277. if [ -z "$melt_recent" ]; then
  278. mv "$stem.mp4" "$stem.mp4"~
  279. qt-faststart "$stem.mp4"~ "$stem.mp4"
  280. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  281. fi
  282. ## VP8/Vorbis/WebM
  283. # TODO: use two-pass when supported by melt
  284. $melt -group $_melt_in $infiles -group ${channels:+$_melt_audio_filters_early${_melt_loudness:+ -filter loudness results="$_melt_loudness"} $_melt_audio_filters }$filters -consumer avformat:"$stem.webm" properties=webm $_melt_webm $_melt_vorbis $_melt_out
  285. # cleanup audio normalize hinting
  286. rm -f $stem.xml
  287. ## JPEG preview
  288. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  289. # resolve width and height from preview image
  290. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  291. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  292. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  293. heightplus=${height:+$(($height+4))}
  294. # TODO: resolve flash player to use
  295. [ -z "$flashplayer" ] || flash=yes
  296. cat >"$stem.html" <<EOF
  297. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  298. <video width="$width" height="$height" preload controls>
  299. <source src="$stem.mp4" type="video/mp4" />
  300. <source src="$stem.webm" type="video/webm" />
  301. <source src="$stem.ogv" type="video/ogg" />
  302. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  303. <param name="movie" value="$flashplayer.swf" />
  304. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  305. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  306. title="No video playback capabilities, please download the video below" />
  307. ${flash:+</object>
  308. }</video>
  309. <p><strong>Download Video:</strong>
  310. open format <a href="$stem.webm">WebM</a>,
  311. open format <a href="$stem.ogv">Ogg</a>,
  312. closed Format <a href="$stem.mp4">MPEG-4</a>.
  313. </p>
  314. EOF