summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 552213ff286aa4ea24b8c511e52e65bcaec22996 (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 mediainfo
  19. # Recommends: moreutils vpx-tools vorbis-tools opus-tools
  20. #
  21. # TODO:
  22. # * Offer to skip rendering again if an output file exist already.
  23. # * Support --width and --height, resolving the other part from input
  24. # or forced aspect ratio.
  25. # * Drop $melt_recent flag when melt 0.9.2 is stable.
  26. # * Check and fail if all needed tools are not available.
  27. # * Test if beneficial to apply real_time=-2.
  28. # * Normalize each infile separately when xml fed as infile keeps sync.
  29. # Maybe as workaround re-feed audio separately from xml, as done at
  30. # <http://bernaerts.dyndns.org/linux/74-ubuntu/214-ubuntu-stabilize-video-melt>.
  31. # * Resolve flash player to use.
  32. # * Make choice of encoders configurable.
  33. # * Figure out how to apply application option when using opusenc.
  34. set -e
  35. PRG=$(basename "$0")
  36. showhelp() {
  37. cat <<EOF
  38. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  39. Encode video file in multiple web-optimized formats, and provide sample
  40. html favoring open formats with optional non-JavaScript Flash fallback.
  41. --video Video style:
  42. ref-bpp
  43. talkinghead 0.1
  44. action 0.15
  45. (default: none)
  46. -p, --profile Video frame size and optional rate, delimited by @
  47. except "p" labels: e.g. 848x480@25 480p25 wvga@25
  48. -s, --size Set video frame size:
  49. [modulus 16]
  50. 320x240 qvga
  51. 640x480 vga 480p 848x480 wvga
  52. 576p 1024x576 wsvga
  53. 1024x768 xga hd 720p 1280x720 wxga
  54. [modulus 8]
  55. 240p 424x240 wqvga
  56. 480x360 hvga nhd 360p 640x360
  57. 800x600 svga
  58. (default: use input size)
  59. -a, --aspect Display Aspect Ratio in melt format: e.g. @16/9
  60. (default: no aspect hinting)
  61. -r, --rate Video framerate: e.g. 25 1001/30000
  62. (default: use input framerate)
  63. --refbpp Bitrate reference: Bits-per-pixel for 360p30 H.264
  64. (default: 0.12)
  65. --formats Containers and codecs to use, comma-separated:
  66. [container] [video codec] [audio codec]
  67. theora ogg Ogg Theora Vorbis
  68. vp8 webm WebM VP8 Vorbis
  69. vp9 WebM VP9 Opus
  70. h264 mp4 MPEG-4 H.264 AAC
  71. (default: webm,vp9,mp4)
  72. --audio Audio style:
  73. [channels] [limit] [Vorbis] [Opus] [AAC]
  74. music max 2 64k 64k audio 96k
  75. hqspeech 1 48k 32k voip 64k
  76. speech 1 X 48k 32k voip 64k
  77. silence 0
  78. (default: none - use channel count of first input)
  79. --audioprefilter Add audio filter before loudness
  80. --loudness Add EBU R128 loudness filter
  81. --loudness-data Add loudness filter, with precomputed results
  82. (default: none - compute results on-the-fly)
  83. --filter Add filter
  84. --stem Stem of output filenames, optionally with path
  85. (default: basename of first input)
  86. -t, --title Title used in html fallback graphics
  87. (default: stem)
  88. --sample Create only a 150 frames long sample
  89. --sample-start Create sample, starting at a specific frame
  90. (default: start at first frame)
  91. --sample-length Create sample of specified length, in frames
  92. (default: 150 frames i.e. approx. 5s)
  93. --compression Optimize for quality or speed: normal dirty hq
  94. (default: normal)
  95. -h, --help This help text
  96. Examples:
  97. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  98. $PRG -p 480p15 --filter "grain noise=20" myvideo.dv
  99. Recommendations for best results:
  100. * Use square pixels, modulus 16, max. 480 width (qvga vga 480p 720p).
  101. * Try lower --ref-bpp while visually acceptable, or raise to max. 0.2.
  102. * Expand and compress noisy speech with ladspa.1075 and ladspa.1916.
  103. More info:
  104. <http://camendesign.com/code/video_for_everybody>
  105. <http://www.streaminglearningcenter.com/articles/configuring-your-streaming-video-(for-newbies).html>
  106. <http://en.wikipedia.org/wiki/HTML5_video>
  107. <http://www.penguinproducer.com/2012/01/ladspa-noise-removal/>
  108. <http://theproaudiofiles.com/mixing-vocals-dynamics-compression-limiting>
  109. <http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/>
  110. EOF
  111. }
  112. exit1() {
  113. response="${1:+Error: }${1:-Internal error!}"
  114. echo >&2 "$response"
  115. exit 1
  116. }
  117. # defaults
  118. formats=webm,vp9,mp4
  119. samplestart=0
  120. samplelength=150
  121. compression=normal
  122. # VP8 is rumored to compress ~20% worse than H.264
  123. factor_vp8=120/100
  124. # VP9 compresses natural video ~27% worse than H.264 (i.e. excluding animation)
  125. # <http://infoscience.epfl.ch/record/200925/files/article-vp9-submited-v2.pdf>
  126. factor_vp9=127/100
  127. # Mimic ReplayGain level when loudness is enabled (EBU R128: -23LUFS)
  128. # <https://auphonic.com/blog/2013/01/07/loudness-targets-mobile-audio-podcasts-radio-tv/>
  129. lufs=-18
  130. # parse cmdline options
  131. TEMP="`getopt -s sh -o hp:s:a:r:b:t: -l help,profile:,size:,aspect:,rate:,video:,refbpp:,formats:,audio:,audioprefilter:,loudness,loudness-data:,filter:,stem:,title:,sample,sample-start:,sample-length:,compression: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  132. eval set -- "$TEMP"
  133. while true ; do
  134. case "$1" in
  135. -h|--help) showhelp; exit;;
  136. -p|--profile) profile="$2"; shift 2;;
  137. -s|--size) size="$2"; shift 2;;
  138. -a|--aspect) aspect="$2"; shift 2;;
  139. -r|--rate) framerate="$2"; shift 2;;
  140. --video) video="$2"; shift 2;;
  141. --refbpp) refbpp="$2"; shift 2;;
  142. --formats) formats="$2"; shift 2;;
  143. --audio) audio="$2"; shift 2;;
  144. --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;;
  145. --loudness) loudness=yes; shift;;
  146. --loudness-data) loudness=yes; loudness_data="$2"; shift 2;;
  147. --filter) filters="${filters:+$filters }-filter $2"; shift 2;;
  148. --stem) stem="$2"; shift 2;;
  149. -t|--title) title="$2"; shift 2;;
  150. --sample) sample=yes; shift;;
  151. --sample-start) sample=yes; samplestart="$2"; shift 2;;
  152. --sample-length) sample=yes; samplelength="$2"; shift 2;;
  153. --compression) compression="$2"; shift 2;;
  154. --) shift; break;;
  155. *) exit1 "Internal error resolving options.";;
  156. esac
  157. done
  158. [ -n "$sample" ] || notsample=yes
  159. # Resolve if system has many CPU cores
  160. processors=$(nproc)
  161. [ $processors -gt 2 ] || processors=
  162. # Resolve if melt is version 0.9.2 or newer
  163. melt_recent=$(melt -query filter=loudness | grep -qi R128 && echo yes)
  164. # sanitize infiles
  165. infiles=$*
  166. infile_first=$(perl -e 'print shift @ARGV' $infiles)
  167. [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!"
  168. # resolve stem and title (if not explicitly set)
  169. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  170. title=${title:-$stem}
  171. # Avoid melt for VP8 and VP9: multipass broken for these codecs
  172. #use_avconv_vp8=yes
  173. #use_avconv_vp9=yes
  174. use_vpxenc=yes
  175. # Avoid discrete audio encoders if possible: may cause sync problems
  176. #use_oggenc=yes
  177. #use_opusenc=yes
  178. if [ -n "$use_avconv_vp8$use_avconv_vp9$use_vpxenc" ]; then
  179. [ -n "$use_oggenc" ] || use_avconv_ogg=yes
  180. [ -n "$use_opusenc" ] || use_avconv_opus=yes
  181. [ -z "$use_oggenc$use_opusenc" ] || use_wav=yes
  182. [ -z "$use_avconv_ogg$use_avconv_opus" ] || use_matroska=yes
  183. fi
  184. # Avoid AAC if possible: Lame arguably better than any free AAC encoder
  185. # Argument for AAC: Safari possibly lacks mp3 support in video container
  186. #use_aac=yes
  187. [ -n "$use_aac" ] || use_mp3=yes
  188. # resolve quality/speed hints
  189. multipass=yes
  190. qscale_theora=5
  191. #crf_vp8=10
  192. cpu_vp8=2
  193. #crf_vp9=10
  194. cpu_vp9=5
  195. crf_h264=23
  196. speedpreset_h264=medium
  197. case "$compression" in
  198. normal) :;;
  199. dirty)
  200. multipass=
  201. qscale_theora=1
  202. cpu_vp8=5
  203. crf_h264=51
  204. ;;
  205. hq)
  206. qscale_theora=6
  207. cpu_vp8=0
  208. cpu_vp9=1
  209. speedpreset_h264=veryslow
  210. ;;
  211. *) exit1 "Unknown compression mode \"$compression\".";;
  212. esac
  213. [ -n "$multipass" ] || singlepass=yes
  214. [ -n "$crf_vp8" ] || vbr_vp8=yes
  215. [ -n "$crf_vp9" ] || vbr_vp9=yes
  216. # parse/resolve size and framerate
  217. case "$profile" in
  218. '') :;;
  219. *@*)
  220. while read s r foo; do
  221. profilesize="${size:-$s}"
  222. framerate="${framerate:-$r}"
  223. done << EOF
  224. $(echo "$profile" | perl -F@ -anE 'say join " ", @F')
  225. EOF
  226. ;;
  227. *p*)
  228. while read s r foo; do
  229. profilesize="${size:-${s}p}"
  230. framerate="${framerate:-$r}"
  231. done << EOF
  232. $(echo "$profile" | perl -Fp -anE 'say join " ", @F')
  233. EOF
  234. ;;
  235. *)
  236. profilesize="$profile"
  237. ;;
  238. esac
  239. size=${size:-$profilesize}
  240. case "$size" in
  241. qvga) size=320x240;;
  242. hvga) size=480x360;;
  243. vga) size=640x480;;
  244. svga) size=800x600;;
  245. xga) size=1024x768;;
  246. 240p|wqvga) size=424x240;;
  247. 360p|nhd) size=640x360;;
  248. 480p|wvga) size=848x480;;
  249. 576p|wsvga) size=1024x576;;
  250. 720p|wxga|hd) size=1280x720;;
  251. esac
  252. if [ -n "$size" ]; then
  253. while read w h foo; do
  254. width="${width:-$w}"
  255. height="${height:-$h}"
  256. done << EOF
  257. $(echo "$size" | perl -Fx -anE 'say join " ", @F')
  258. EOF
  259. if [ -z "$width" ] || [ -z "$height" ]; then
  260. exit1 "Failed to parse size \"$size\"."
  261. fi
  262. fi
  263. case "$framerate" in
  264. */*)
  265. while read n d foo; do
  266. framerate_num="${framerate_num:-$n}"
  267. framerate_den="${framerate_den:-$d}"
  268. done << EOF
  269. $(echo "$framerate" | perl -F/ -anE 'say join " ", @F')
  270. EOF
  271. ;;
  272. ?*)
  273. framerate_num="$framerate"
  274. framerate_den=1
  275. ;;
  276. esac
  277. # resolve input size and framerate (needed for computing bitrate)
  278. while read w h r s foo; do
  279. width_in="${width_in:-$w}"
  280. height_in="${height_in:-$h}"
  281. framerate_in="${framerate_in:-$r}"
  282. scantype_in="${scantype_in:-$s}"
  283. done << EOF
  284. $(mediainfo --Inform="Video;%Width% %Height% %FrameRate% %ScanType%" \
  285. "$infile_first")
  286. EOF
  287. [ Progressive = "$scantype_in" ] || do_deinterlace=yes
  288. case "$video" in
  289. talkinghead)
  290. refbpp="${refbpp:-0.1}"
  291. x264tune=film
  292. ;;
  293. action)
  294. refbpp="${refbpp:-0.15}"
  295. x264tune=film
  296. ;;
  297. '')
  298. refbpp="${refbpp:-0.12}"
  299. ;;
  300. *) exit1 "Unknown video style \"$video\".";;
  301. esac
  302. for format in $(echo "$formats" | sed -e 's/,/ /g'); do
  303. case $format in
  304. theora|ogg) ogg=yes;;
  305. h264|mp4) mp4=yes;;
  306. vp8|webm) webm=yes;;
  307. vp9) vp9=yes;;
  308. *) exit1 "Unknown format \"$format\".";;
  309. esac
  310. done
  311. _width="${width:-$width_in}"
  312. _height="${height:-$height_in}"
  313. if [ -n "$_width" ] && [ -n "$_height" ]; then
  314. _pixels="$((_width*_height))"
  315. fi
  316. _frames="${framerate:-$framerate_in}"
  317. sizepreset_vpx=libvpx-360p
  318. webm_qmin=0
  319. webm_qmax=63
  320. webm_lag=16
  321. webm_tokenparts_log2=0
  322. if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then
  323. sizepreset_vpx=libvpx-720p
  324. webm_qmin=11
  325. web_qmax=51
  326. webm_tokenparts_log2=2
  327. if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then
  328. sizepreset_vpx=libvpx-720p50_60
  329. webm_lag=25
  330. fi
  331. fi
  332. # compute average bitrate from reference data and "power of .75" rule
  333. bitrate_vp8="$bitrate"
  334. bitrate_vp9="$bitrate"
  335. if [ -n "$_pixels" ] && [ -n "$_frames" ]; then
  336. bitrate=$(perl -E '$refsize=640*360;' \
  337. -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )")
  338. bitrate_vp8=$(perl -E "say int( +($bitrate*$factor_vp8) )") #"
  339. bitrate_vp9=$(perl -E "say int( +($bitrate*$factor_vp9) )") #"
  340. fi
  341. # default per-codec-channel bitrates
  342. quality_vorbis=3
  343. bitrate_opus=48
  344. bitrate_mp3=64
  345. bitrate_aac=64
  346. case "$audio" in
  347. music)
  348. opusapp=audio
  349. ;;
  350. hqspeech)
  351. stereo=yes
  352. quality_vorbis=1
  353. bitrate_opus=32
  354. bitrate_mp3=48
  355. opusapp=voip
  356. ;;
  357. speech)
  358. mono=yes
  359. quality_vorbis=1
  360. bitrate_opus=32
  361. bitrate_mp3=48
  362. compress=yes
  363. [ -z "$melt_recent" ] || _melt_loudness="$loudness_data"
  364. limit=yes
  365. opusapp=voip
  366. ;;
  367. silence)
  368. silence=yes
  369. ;;
  370. '')
  371. :
  372. ;;
  373. *) exit1 "Unknown audio style \"$audio\".";;
  374. esac
  375. channels=$(avprobe -v warning -show_streams "$infile_first" \
  376. | perl -ne 's/channels=// and print $_' || echo -1)
  377. # adapt channel count and flags to reflect actual downmix/silence need
  378. if [ -n "$stereo" ]; then
  379. if [ $channels -gt 2 ]; then
  380. channels=2
  381. else
  382. stereo=
  383. fi
  384. elif [ -n "$mono" ]; then
  385. if [ $channels -gt 1 ]; then
  386. channels=1
  387. else
  388. mono=
  389. fi
  390. elif [ -n "$silence" ]; then
  391. if [ $channels -gt 0 ]; then
  392. channels=0
  393. else
  394. silence=
  395. fi
  396. fi
  397. [ -z "$silence" ] || channels=
  398. if [ -n "$stereo$mono" ]; then
  399. # melt cannot downmix with (stereo?) filters applied (bug#763911)
  400. if [ -n "$audioprefilters$compress$loudness$limit$filters" ]; then
  401. if [ -z "${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}" ]; then
  402. downmix_avconv=yes
  403. else
  404. downmix_oggenc=yes
  405. downmix_oggenc=yes
  406. fi
  407. else
  408. downmix_melt=yes
  409. fi
  410. fi
  411. # generic options
  412. melt="melt -progress"
  413. avconv="avconv -threads auto -y -v info -stats"
  414. avconv_chained="avconv -threads auto -y -v warning"
  415. vpxenc_chained="vpxenc --quiet ${processors:+-t $((processors-1))}"
  416. oggenc="oggenc"
  417. opusenc="opusenc"
  418. _melt_infiles="${notsample:+$infiles}${sample:+$infile_first in=$((samplestart)) out=$((samplestart + samplelength))}"
  419. # filter options
  420. _melt_video="progressive=1${framerate:+ frame_rate_num="$framerate_num" frame_rate_den="$framerate_den"}${size:+ s=${width:+$width}x${height:+$height}}${aspect:+ aspect=$aspect}"
  421. _avconv_video="${do_deinterlace:+-filter:v yadif}${framerate:+ -r $framerate_num/$framerate_den}${size:+ -s ${width:+$width}x${height:+$height}}${aspect:+ -aspect $aspect}"
  422. _melt_downmix="${downmix_melt:+ac=$channels}"
  423. _avconv_downmix="${downmix_avconv:+-ac $channels}"
  424. # FIXME: how to downmix to stereo?
  425. _oggenc_downmix="${downmix_oggenc:+${mono:+--downmix}}"
  426. _opusenc_downmix="${downmix_opusenc:+${stereo:+--downmix-stereo}${mono:+--downmix-mono}}"
  427. # limit (i.e. avoid peaks "clipping")
  428. _melt_postfilters_audio="${limit:+-filter ladspa.1077}"
  429. # codec options
  430. _melt_theora="vcodec=libtheora${bitrate:+ vb=$bitrate} qscale=$qscale_theora"
  431. _melt_vp8="vcodec=libvpx vpreset=$sizepreset_vpx${bitrate_vp8:+ vb=$bitrate_vp8 minrate=$((bitrate_vp8/20)) maxrate=$((bitrate_vp8*12))} crf=$crf_vp8 cpu-used=$cpu_vp8"
  432. _avconv_vp8="-c:v libvpx -pre:v $sizepreset_vpx${bitrate_vp8:+ -b:v $bitrate_vp8 -minrate $((bitrate_vp8/20)) -maxrate $((bitrate_vp8*12))} -crf $crf_vp8 -cpu-used $cpu_vp8"
  433. _vpxenc_vpx="--min-q=$webm_qmin --max-q=$webm_qmax${multipass:+ --minsection-pct=5 --maxsection-pct=1200} --kf-max-dist=120 --auto-alt-ref=1 --lag-in-frames=$webm_lag --arnr-maxframes=7 --arnr-strength=5 --arnr-type=3"
  434. _vpxenc_vp8="--codec=vp8${bitrate_vp8:+ --target-bitrate=$((bitrate_vp8/1000))} --good --end-usage=${vbr_vp8:+vbr}${crf_vp8:+cq --cq-level=$crf_vp8} --cpu-used=$cpu_vp8 $_vpxenc_vpx --token-parts=$webm_tokenparts_log2"
  435. _melt_vp9="vcodec=libvpx-vp9 vpreset=$sizepreset_vpx${bitrate_vp9:+ vb=$bitrate_vp9 minrate=$((bitrate_vp9/20)) maxrate=$((bitrate_vp9*12))} crf=$crf_vp9 cpu-used=$cpu_vp9"
  436. _avconv_vp9="-c:v libvpx-vp9 -pre:v $sizepreset_vpx${bitrate_vp9:+ -b:v $bitrate_vp9 -minrate $((bitrate_vp9/20)) -maxrate $((bitrate_vp9*12))} -crf $crf_vp9 -cpu-used $cpu_vp9"
  437. _vpxenc_vp9="--codec=vp9${bitrate_vp9:+ --target-bitrate=$((bitrate_vp9/1000))} --good --end-usage=${vbr_vp9:+vbr}${crf_vp9:+cq --cq-level=$crf_vp9} --cpu-used=$cpu_vp9 $_vpxenc_vpx --tile-columns=$webm_tokenparts_log2 --tile-rows=$webm_tokenparts_log2"
  438. _melt_h264="vcodec=libx264 vpreset=$speedpreset_h264 vprofile=baseline${x264tune:+ tune=$x264tune} threads=0 movflags=+faststart crf=$crf_h264"
  439. _melt_pcm="$_melt_downmix acodec=pcm_s16le"
  440. _melt_vorbis="$_melt_downmix acodec=libvorbis aq=$quality_vorbis"
  441. _avconv_vorbis="$_avconv_downmix -c:a libvorbis -aq $quality_vorbis"
  442. _oggenc_vorbis="$_oggenc_downmix -q $quality_vorbis"
  443. _melt_opus="$_melt_downmix acodec=libopus ab=$((channels*bitrate_opus))k${opusapp:+ application=$opusapp}"
  444. _avconv_opus="$_avconv_downmix -c:a libopus -b:a $((channels*bitrate_opus))k${opusapp:+ -application $opusapp}"
  445. _opusenc_opus="$_opusenc_downmix --bitrate $((channels*bitrate_opus))"
  446. _melt_mp3="$_melt_downmix acodec=libmp3lame ab=$((channels*bitrate_mp3))k"
  447. _melt_aac="$_melt_downmix acodec=aac ab=$((channels*bitrate_aac))k"
  448. # container options
  449. _melt_yuv4mpeg2="f=yuv4mpegpipe $_melt_video pix_fmt=yuv420p an=1 audio_off=1"
  450. _avconv_yuv4mpeg2_in="-f yuv4mpegpipe"
  451. _avconv_rawvideo="-f rawvideo"
  452. _melt_wav="f=wav $_melt_pcm vn=1 video_off=1"
  453. _avconv_wav_in="-f wav"
  454. _oggenc_wav_in=
  455. _opusenc_wav_in=
  456. _melt_matroska_pcm="f=matroska $_melt_downmix $_melt_pcm vn=1 video_off=1"
  457. _avconv_matroska_pcm_in="-f matroska"
  458. _melt_ogg="f=ogg $_melt_video $_melt_theora ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  459. _avconv_ogg_in="-f ogg"
  460. _avconv_ogg_vorbis="-f ogg -vn $_avconv_vorbis"
  461. _avconv_ogg_opus="-f ogg -vn $_avconv_opus"
  462. _melt_webm="f=webm $_melt_video $_melt_vp8 ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  463. _avconv_webm="-f webm $_avconv_video $_avconv_vp8 ${silence:+-an}${channels:+ $_avconv_vorbis}"
  464. _avconv_webm_in="-f webm"
  465. _avconv_webm_onlyvideo="-f webm $_avconv_video $_avconv_vp8 -an}"
  466. _avconv_webm_keepvideo="-f webm $_avconv_video -c:v copy ${silence:+-an}${channels:+$_avconv_vorbis}"
  467. _avconv_webm_keepvideo_opus="-f webm $_avconv_video -c:v copy ${silence:+-an}${channels:+$_avconv_opus}"
  468. _melt_webm_vp9="f=webm $_melt_video $_melt_vp9 ${silence:+an=1 audio_off=1}${channels:+$_melt_opus}"
  469. _avconv_webm_vp9="-f webm $_avconv_video $_avconv_vp9 ${silence:+-an}${channels:+$_avconv_opus}"
  470. _avconv_webm_onlyvideo_vp9="-f webm $_avconv_video $_avconv_vp9 -an}"
  471. _melt_mp4="f=mp4 $_melt_video $_melt_h264 ${silence:+an=1 audio_off=1}${channels:+${use_mp3:+$_melt_mp3}${use_aac:+$_melt_aac}}"
  472. _melt_img="f=image2 $_melt_video"
  473. if [ -n "$loudness" ] && [ -z "$silence$_melt_loudness" ]; then
  474. echo "Analyzing audio dynamics..."
  475. $melt $_melt_infiles \
  476. $audioprefilters -filter loudness $filters \
  477. -consumer xml:$stem.xml video_off=1 all=1
  478. _melt_loudness="$(perl -n \
  479. -e 'm!<(property) name="results">([^<]+)</\1>! and print $2' \
  480. $stem.xml)"
  481. echo "Loudness data: $_melt_loudness"
  482. fi
  483. if [ -n "$multipass" ] && [ -n "$webm$vp9" ]; then
  484. echo "Analyzing video complexity..."
  485. if [ -n "$use_vpxenc" ]; then
  486. $melt $_melt_infiles $filters \
  487. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  488. | pee \
  489. ${webm:+"$vpxenc_chained - $_vpxenc_vp8 \
  490. -p 2 --pass=1 --fpf=${stem}_vp8.log -o /dev/null"} \
  491. ${vp9:+"$vpxenc_chained - $_vpxenc_vp9 \
  492. -p 2 --pass=1 --fpf=${stem}_vp9.log -o /dev/null"}
  493. else
  494. $melt $_melt_infiles $filters \
  495. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  496. | $avconv_chained $_avconv_yuv4mpeg2_in -i pipe:0 \
  497. ${webm:+$_avconv_rawvideo $_avconv_vp8 -an \
  498. -pass 1 -passlogfile ${stem}_vp8 /dev/null} \
  499. ${vp9:+$_avconv_rawvideo $_avconv_vp9 -an \
  500. -pass 1 -passlogfile ${stem}_vp9 /dev/null}
  501. [ -z "$webm" ] || [ -n "$use_avconv_vp8" ] || mv -f ${stem}_vp8-*.log ${stem}_vp8_2pass.log
  502. [ -z "$vp9" ] || [ -n "$use_avconv_vp9" ] || mv -f ${stem}_vp9-*.log ${stem}_vp9_2pass.log
  503. fi
  504. fi
  505. if [ -n "$use_oggenc$use_opusenc$use_vpxenc" ] && [ -n "$webm$vp9" ]; then
  506. echo "Encoding video${channels:+ and extracting audio...}"
  507. $melt $_melt_infiles \
  508. ${channels:+$audioprefilters \
  509. ${_melt_loudness:+-filter loudness program=$lufs \
  510. results="$_melt_loudness"}} \
  511. $filters${channels:+ $_melt_postfilters_audio} \
  512. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  513. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4} \
  514. ${channels:+${use_wav:+-consumer avformat:$stem.wav \
  515. $_melt_wav}} \
  516. ${channels:+${use_matroska:+-consumer avformat:$stem.mkv \
  517. $_melt_matroska_pcm}} \
  518. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  519. | pee \
  520. ${webm:+${use_avconv_vp8:+"$avconv_chained \
  521. $_avconv_yuv4mpeg2_in -i pipe:0 \
  522. $_avconv_webm_onlyvideo \
  523. ${multipass:+-pass 2 -passlogfile ${stem}_vp8} \
  524. ${stem}${channels:+_silent}.webm"}} \
  525. ${webm:+${use_vpxenc:+"$vpxenc_chained - $_vpxenc_vp8 \
  526. ${singlepass:+-p 1} \
  527. ${multipass:+-p 2 --pass=2 --fpf=${stem}_vp8.log} \
  528. -o ${stem}${channels:+_silent}.webm"}} \
  529. ${vp9:+${use_avconv_vp9:+"$avconv_chained \
  530. $_avconv_yuv4mpeg2_in -i pipe:0 \
  531. $_avconv_webm_onlyvideo_vp9 \
  532. ${multipass:+-pass 2 -passlogfile ${stem}_vp9} \
  533. ${stem}_vp9${channels:+_silent}.webm"}} \
  534. ${vp9:+${use_vpxenc:+"$vpxenc_chained - $_vpxenc_vp9 \
  535. ${singlepass:+-p 1} \
  536. ${multipass:+-p 2 --pass=2 --fpf=${stem}_vp9.log} \
  537. -o ${stem}_vp9${channels:+_silent}.webm"}}
  538. if [ -n "$webm" ] && [ -n "$channels" ]; then
  539. echo "Encoding Vorbis audio and muxing with VP8 video..."
  540. if [ -n "$use_oggenc" ]; then
  541. $oggenc $_oggenc_wav_in $_oggenc_vorbis -o - $stem.wav \
  542. | $avconv_chained \
  543. $_avconv_webm_in -i ${stem}_silent.webm \
  544. $_avconv_ogg_in -i pipe:0 \
  545. $_avconv_webm_keepvideo $stem.webm
  546. else
  547. $avconv $_avconv_webm_in -i ${stem}_silent.webm \
  548. $_avconv_matroska_wav_in -i $stem.mkv \
  549. $_avconv_webm_keepvideo ${stem}.webm
  550. fi
  551. fi
  552. if [ -n "$vp9" ] && [ -n "$channels" ]; then
  553. echo "Encoding Opus audio and muxing with WebM/VP9 video..."
  554. if [ -n "$use_opusenc" ]; then
  555. $opusenc $_opusenc_wav_in $_opusenc_opus -o - $stem.wav \
  556. | $avconv_chained \
  557. $_avconv_webm_in -i ${stem}_vp9_silent.webm \
  558. $_avconv_ogg_in -i pipe:0 \
  559. $_avconv_webm_keepvideo_opus ${stem}_vp9.webm
  560. else
  561. $avconv $_avconv_webm_in -i ${stem}_vp9_silent.webm \
  562. $_avconv_matroska_wav_in -i $stem.mkv \
  563. $_avconv_webm_keepvideo_opus ${stem}_vp9.webm
  564. fi
  565. fi
  566. elif [ -n "$use_avconv_vp8$use_avconv_vp9" ] && [ -n "$webm$vp9" ]; then
  567. echo "Encoding video..."
  568. $melt $_melt_infiles \
  569. ${channels:+$audioprefilters \
  570. ${_melt_loudness:+-filter loudness program=$lufs \
  571. results="$_melt_loudness"}} \
  572. $filters${channels:+ $_melt_postfilters_audio} \
  573. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  574. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4} \
  575. ${channels:+-consumer avformat:$stem.wav $_melt_wav} \
  576. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  577. | pee \
  578. ${webm:+${use_avconv_vp8:+"$avconv_chained \
  579. $_avconv_yuv4mpeg2_in -i pipe:0 \
  580. $_avconv_webm \
  581. ${multipass:+-pass 2 -passlogfile ${stem}_vp8} \
  582. ${stem}.webm"}} \
  583. ${vp9:+${use_avconv_vp9:+"$avconv_chained \
  584. $_avconv_yuv4mpeg2_in -i pipe:0 \
  585. $_avconv_webm_vp9 \
  586. ${multipass:+-pass 2 -passlogfile ${stem}_vp9} \
  587. ${stem}_vp9.webm"}}
  588. else
  589. echo "Encoding video..."
  590. $melt $_melt_infiles \
  591. ${channels:+$audioprefilters \
  592. ${_melt_loudness:+-filter loudness program=$lufs \
  593. results="$_melt_loudness"}} \
  594. $filters${channels:+ $_melt_postfilters_audio} \
  595. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  596. ${webm:+-consumer avformat:$stem.webm $_melt_webm \
  597. ${multipass:+pass=2 passlogfile=${stem}_vp8}} \
  598. ${vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9 \
  599. ${multipass:+pass=2 passlogfile=${stem}_vp9}} \
  600. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4}
  601. fi
  602. if [ -n "$mp4" ] && [ -z "$melt_recent" ]; then
  603. mv "$stem.mp4" "$stem.mp4"~
  604. qt-faststart "$stem.mp4"~ "$stem.mp4"
  605. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  606. fi
  607. # cleanup encoding cruft
  608. rm -f $stem.xml $stem.wav $stem.mkv ${stem}_*.log ${stem}_*silent.webm
  609. # JPEG preview
  610. $melt $infile_first in=0 out=0 \
  611. -group $filters \
  612. -consumer avformat:$stem.jpg $_melt_img
  613. __width="${_width:+ width=\"$_width\"}"
  614. __height="${_height:+ height=\"$_height\"}"
  615. # Flash object needs extra space for controllers
  616. __heightplus=${_height:+ height=\"$((_height+4))\"}
  617. _source_ogg="<source src=\"$stem.ogv\" type=\"video/ogg\" />"
  618. _source_webm="<source src=\"$stem.webm\" type=\"video/webm\" />"
  619. _source_vp9="<source src=\"${stem}_vp9.webm\" type='video/ogg; codecs=\"vp9, opus\"' />"
  620. _source_mp4="<source src=\"$stem.mp4\" type=\"video/mp4\" />"
  621. [ -z "$flashplayer" ] || flash=yes
  622. [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled."
  623. _object_flash="<object$__width$__heightplus type=\"application/x-shockwave-flash\" data=\"$flashplayer.swf\">"
  624. _param_name="<param name=\"movie\" value=\"$flashplayer.swf\" />"
  625. _param_flashvars="<param name=\"flashvars\" value=\"image=$stem.jpg&amp;file=$stem.mp4\" />"
  626. __oggfile=${ogg:+open format <a href=\"$stem.ogv\">Ogg</a>}
  627. __webmfile=${webm:+open format <a href=\"$stem.webm\">WebM (VP8)</a>}
  628. __vp9file=${vp9:+open format <a href=\"${stem}_vp9.webm\">WebM (VP9/Opus)</a>}
  629. __mp4file=${mp4:+closed format <a href=\"$stem.mp4\">MPEG-4</a>}
  630. cat >"$stem.html" <<EOF
  631. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  632. <video$__width$__height preload controls>
  633. ${mp4:+$_source_mp4
  634. }${vp9:+$_source_vp9
  635. }${webm:+$_source_webm
  636. }${ogg:+$_source_ogg
  637. }${flash:+$_object_flash
  638. $_param_name
  639. $_param_flashvars
  640. }<img src="$stem.jpg"$__width$__height alt="$title"
  641. title="No video playback capabilities, please download the video below" />
  642. ${flash:+</object>
  643. }</video>
  644. <p><strong>Download Video:</strong><ul>
  645. ${vp9:+<li>$__vp9file
  646. }${webm:+<li>$__webmfile
  647. }${ogg:+<li>$__oggfile
  648. }${mp4:+<li>$__mp4file
  649. }</ul></p>
  650. EOF