summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 9925c8696d5a39eddee8d7d9b41b62d239baa672 (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. # parse cmdline options
  128. 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."
  129. eval set -- "$TEMP"
  130. while true ; do
  131. case "$1" in
  132. -h|--help) showhelp; exit;;
  133. -p|--profile) profile="$2"; shift 2;;
  134. -s|--size) size="$2"; shift 2;;
  135. -a|--aspect) aspect="$2"; shift 2;;
  136. -r|--rate) framerate="$2"; shift 2;;
  137. --video) video="$2"; shift 2;;
  138. --refbpp) refbpp="$2"; shift 2;;
  139. --formats) formats="$2"; shift 2;;
  140. --audio) audio="$2"; shift 2;;
  141. --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;;
  142. --loudness) loudness=yes; shift;;
  143. --loudness-data) loudness=yes; loudness_data="$2"; shift 2;;
  144. --filter) filters="${filters:+$filters }-filter $2"; shift 2;;
  145. --stem) stem="$2"; shift 2;;
  146. -t|--title) title="$2"; shift 2;;
  147. --sample) sample=yes; shift;;
  148. --sample-start) sample=yes; samplestart="$2"; shift 2;;
  149. --sample-length) sample=yes; samplelength="$2"; shift 2;;
  150. --compression) compression="$2"; shift 2;;
  151. --) shift; break;;
  152. *) exit1 "Internal error resolving options.";;
  153. esac
  154. done
  155. # Resolve if system has many CPU cores
  156. processors=$(nproc)
  157. [ $processors -gt 2 ] || processors=
  158. # Resolve if melt is version 0.9.2 or newer
  159. melt_recent=$(melt -query filter=loudness | grep -qi R128 && echo yes)
  160. # sanitize infiles
  161. infiles=$*
  162. infile_first=$(perl -e 'print shift @ARGV' $infiles)
  163. [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!"
  164. # resolve stem and title (if not explicitly set)
  165. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  166. title=${title:-$stem}
  167. # Use vpxenc for VP8 and VP9: CRF is apparently broken with libav
  168. use_vpxenc=yes
  169. #use_oggenc=yes
  170. #use_opusenc=yes
  171. # resolve quality/speed hints
  172. multipass=yes
  173. qscale_theora=5
  174. crf_vp8=55
  175. cpu_vp8=0
  176. crf_vp9=35
  177. cpu_vp9=5
  178. crf_h264=23
  179. speedpreset_h264=medium
  180. case "$compression" in
  181. normal) :;;
  182. dirty)
  183. qscale_theora=1
  184. crf_vp8=63
  185. cpu_vp8=5
  186. crf_vp9=63
  187. crf_h264=51
  188. ;;
  189. hq)
  190. qscale_theora=6
  191. crf_vp8=40
  192. cpu_vp8=0
  193. crf_vp9=40
  194. cpu_vp9=1
  195. speedpreset_h264=veryslow
  196. ;;
  197. *) exit1 "Unknown compression mode \"$compression\".";;
  198. esac
  199. [ -n "$multipass" ] || singlepass=yes
  200. # parse/resolve size and framerate
  201. case "$profile" in
  202. '') :;;
  203. *@*)
  204. while read s r foo; do
  205. profilesize="${size:-$s}"
  206. framerate="${framerate:-$r}"
  207. done << EOF
  208. $(echo "$profile" | perl -F@ -anE 'say join " ", @F')
  209. EOF
  210. ;;
  211. *p*)
  212. while read s r foo; do
  213. profilesize="${size:-${s}p}"
  214. framerate="${framerate:-$r}"
  215. done << EOF
  216. $(echo "$profile" | perl -Fp -anE 'say join " ", @F')
  217. EOF
  218. ;;
  219. *)
  220. profilesize="$profile"
  221. ;;
  222. esac
  223. size=${size:-$profilesize}
  224. case "$size" in
  225. qvga) size=320x240;;
  226. hvga) size=480x360;;
  227. vga) size=640x480;;
  228. svga) size=800x600;;
  229. xga) size=1024x768;;
  230. 240p|wqvga) size=424x240;;
  231. 360p|nhd) size=640x360;;
  232. 480p|wvga) size=848x480;;
  233. 576p|wsvga) size=1024x576;;
  234. 720p|wxga|hd) size=1280x720;;
  235. esac
  236. if [ -n "$size" ]; then
  237. while read w h foo; do
  238. width="${width:-$w}"
  239. height="${height:-$h}"
  240. done << EOF
  241. $(echo "$size" | perl -Fx -anE 'say join " ", @F')
  242. EOF
  243. if [ -z "$width" ] || [ -z "$height" ]; then
  244. exit1 "Failed to parse size \"$size\"."
  245. fi
  246. fi
  247. case "$framerate" in
  248. */*)
  249. while read n d foo; do
  250. framerate_num="${framerate_num:-$n}"
  251. framerate_den="${framerate_den:-$d}"
  252. done << EOF
  253. $(echo "$framerate" | perl -F/ -anE 'say join " ", @F')
  254. EOF
  255. ;;
  256. ?*)
  257. framerate_num="$framerate"
  258. framerate_den=1
  259. ;;
  260. esac
  261. # resolve input size and framerate (needed for computing bitrate)
  262. while read w h r s foo; do
  263. width_in="${width_in:-$w}"
  264. height_in="${height_in:-$h}"
  265. framerate_in="${framerate_in:-$r}"
  266. scantype_in="${scantype_in:-$s}"
  267. done << EOF
  268. $(mediainfo --Inform="Video;%Width% %Height% %FrameRate% %ScanType%" "$infile_first")
  269. EOF
  270. [ Progressive = "$scantype_in" ] || do_deinterlace=yes
  271. case "$video" in
  272. talkinghead)
  273. refbpp="${refbpp:-0.1}"
  274. x264tune=film
  275. ;;
  276. action)
  277. refbpp="${refbpp:-0.15}"
  278. x264tune=film
  279. ;;
  280. '')
  281. refbpp="${refbpp:-0.12}"
  282. ;;
  283. *) exit1 "Unknown video style \"$video\".";;
  284. esac
  285. for format in $(echo "$formats" | sed -e 's/,/ /g'); do
  286. case $format in
  287. theora|ogg) ogg=yes;;
  288. h264|mp4) mp4=yes;;
  289. vp8|webm) webm=yes;;
  290. vp9) vp9=yes;;
  291. *) exit1 "Unknown format \"$format\".";;
  292. esac
  293. done
  294. _width="${width:-$width_in}"
  295. _height="${height:-$height_in}"
  296. if [ -n "$_width" ] && [ -n "$_height" ]; then
  297. _pixels="$(($_width*$_height))"
  298. fi
  299. _frames="${framerate:-$framerate_in}"
  300. sizepreset_vpx=libvpx-360p
  301. webm_qmin=0
  302. webm_qmax=63
  303. webm_lag=16
  304. webm_tokenparts_log2=0
  305. if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then
  306. sizepreset_vpx=libvpx-720p
  307. webm_qmin=11
  308. web_qmax=51
  309. webm_tokenparts_log2=2
  310. if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then
  311. sizepreset_vpx=libvpx-720p50_60
  312. webm_lag=25
  313. fi
  314. fi
  315. # compute average bitrate from reference data and "power of .75" rule
  316. bitrate_vp8="$bitrate"
  317. bitrate_vp9="$bitrate"
  318. if [ -n "$_pixels" ] && [ -n "$_frames" ]; then
  319. bitrate=$(perl -E '$refsize=640*360;' \
  320. -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )")
  321. bitrate_vp8=$(perl -E "say int( +($bitrate*$factor_vp8) )") #"
  322. bitrate_vp9=$(perl -E "say int( +($bitrate*$factor_vp9) )") #"
  323. fi
  324. # default per-codec-channel bitrates
  325. quality_vorbis=3
  326. bitrate_opus=48
  327. bitrate_aac=64
  328. channels=-1
  329. maxchannels=2
  330. case "$audio" in
  331. music)
  332. opusapp=audio
  333. ;;
  334. hqspeech)
  335. channels=1
  336. quality_vorbis=1
  337. bitrate_opus=32
  338. opusapp=voip
  339. ;;
  340. speech)
  341. channels=1
  342. quality_vorbis=1
  343. bitrate_opus=32
  344. compress=yes
  345. [ -z "$melt_recent" ] || _melt_loudness="$loudness_data"
  346. limit=yes
  347. opusapp=voip
  348. ;;
  349. silence)
  350. channels=0
  351. ;;
  352. '')
  353. :
  354. ;;
  355. *) exit1 "Unknown audio style \"$audio\".";;
  356. esac
  357. [ $channels -ge 0 ] || channels=$(avprobe -v warning -show_streams "$infile_first" | perl -ne 's/channels=// and print $_' || echo -1)
  358. [ $channels -le $maxchannels ] || channels=$maxchannels
  359. [ $channels -gt 0 ] || channels=
  360. # generic options
  361. melt="melt -progress"
  362. avconv="avconv -threads auto -y -v warning"
  363. vpxenc="vpxenc --quiet ${processors:+-t $((processors-1))}"
  364. oggenc="oggenc --quiet"
  365. opusenc="opusenc --quiet"
  366. _melt_sample="$infile_first ${sample:+in=${samplestart:-0} out=$((${samplestart:-0} + samplelength))}"
  367. _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}"
  368. _avconv_video="${do_deinterlace:+-filter:v yadif}${framerate:+ -r $framerate_num/$framerate_den}${size:+ -s ${width:+$width}x${height:+$height}}${aspect:+ -aspect $aspect}"
  369. _melt_audio="${channels:+ac=$channels}"
  370. _avconv_audio="${channels:+-ac $channels}"
  371. # filter options
  372. # limit (i.e. avoid peaks "clipping")
  373. _melt_postfilters_audio="${limit:+-filter ladspa.1077}"
  374. # codec options
  375. _melt_theora="vcodec=libtheora${bitrate:+ vb=$bitrate} qscale=$qscale_theora"
  376. _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"
  377. _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"
  378. _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"
  379. _vpxenc_vp8="--codec=vp8${bitrate_vp8:+ --target-bitrate=$bitrate_vp8} --good --end-usage=cq --cq-level=$crf_vp8 --cpu-used=$cpu_vp8 $_vpxenc_vpx --token-parts=$webm_tokenparts_log2"
  380. _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"
  381. _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"
  382. _vpxenc_vp9="--codec=vp9${bitrate_vp9:+ --target-bitrate=$bitrate_vp9} --good --end-usage=cq --cq-level=$crf_vp9 --cpu-used=$cpu_vp9 $_vpxenc_vpx --tile-columns=$webm_tokenparts_log2 --tile-rows=$webm_tokenparts_log2"
  383. _melt_h264="vcodec=libx264 vpreset=$speedpreset_h264 vprofile=baseline${x264tune:+ tune=$x264tune} threads=0 movflags=+faststart crf=$crf_h264"
  384. _melt_vorbis="$_melt_audio acodec=libvorbis aq=$quality_vorbis"
  385. _avconv_vorbis="-c:a libvorbis -aq $quality_vorbis"
  386. _oggenc_vorbis="-q $quality_vorbis"
  387. _melt_opus="$_melt_audio acodec=libopus${channels:+ ab=$(($channels*$bitrate_opus))k}${opusapp:+ application=$opusapp}"
  388. _avconv_opus="-c:a libopus${channels:+ -b:a $(($channels*$bitrate_opus))k}${opusapp:+ -application $opusapp}"
  389. _opusenc_opus="${channels:+--bitrate $((channels*bitrate_opus))}"
  390. _melt_aac="$_melt_audio acodec=aac${channels:+ ab=$(($channels*$bitrate_aac))k}"
  391. # container options
  392. _melt_yuv4mpeg2="f=yuv4mpegpipe $_melt_video pix_fmt=yuv420p an=1 audio_off=1"
  393. _avconv_yuv4mpeg2_in="-f yuv4mpegpipe"
  394. _avconv_rawvideo="-f rawvideo"
  395. _melt_wav="f=wav $_melt_audio vn=1 video_off=1"
  396. _avconv_wav_in="-f wav $_avconv_audio"
  397. _oggenc_wav_in=
  398. _opusenc_wav_in=
  399. _melt_ogg="f=ogg $_melt_video $_melt_theora $_melt_vorbis"
  400. _avconv_ogg_in="-f ogg"
  401. _avconv_ogg_vorbis="-f ogg -vn $_avconv_vorbis"
  402. _avconv_ogg_opus="-f ogg -vn $_avconv_opus"
  403. _melt_webm="f=webm $_melt_video $_melt_vp8 $_melt_vorbis"
  404. _avconv_webm="-f webm $_avconv_video $_avconv_vp8 $_avconv_vorbis"
  405. _avconv_webm_in="-f webm"
  406. _avconv_webm_keepvideo="-f webm $_avconv_video -c:v copy $_avconv_vorbis"
  407. _avconv_webm_keepvideo_opus="-f webm $_avconv_video -c:v copy $_avconv_opus"
  408. _melt_webm_vp9="f=webm $_melt_video $_melt_vp9 $_melt_opus"
  409. _avconv_webm_vp9="-f webm $_avconv_video $_avconv_vp9 $_avconv_opus"
  410. _melt_mp4="f=mp4 $_melt_video $_melt_h264 $_melt_aac"
  411. _melt_img="f=image2 $_melt_video"
  412. [ -z "$loudness" ] || [ -n "$_melt_loudness" ] || do_loudness=yes
  413. if [ -n "$multipass" ] && [ -n "$webm$vp9" ]; then
  414. echo "Analyzing video complexity${do_loudness:+ and audio dynamics}..."
  415. if [ -n "$use_vpxenc" ]; then
  416. $melt ${_melt_sample:-$infiles} \
  417. ${do_loudness:+$audioprefilters -filter loudness} \
  418. $filters \
  419. ${do_loudness:+-consumer xml:$stem.xml $_melt_audio video_off=1 all=1} \
  420. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  421. | pee \
  422. ${webm:+"$vpxenc - $_vpxenc_vp8 -p 2 --pass=1 --fpf=${stem}_vp8.log -o /dev/null"} \
  423. ${vp9:+"$vpxenc - $_vpxenc_vp9 -p 2 --pass=1 --fpf=${stem}_vp9.log -o /dev/null"}
  424. else
  425. $melt ${_melt_sample:-$infiles} \
  426. ${do_loudness:+$audioprefilters -filter loudness} \
  427. $filters \
  428. ${do_loudness:+-consumer xml:$stem.xml $_melt_audio video_off=1 all=1} \
  429. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  430. | $avconv $_avconv_yuv4mpeg2_in -i pipe:0 \
  431. ${webm:+$_avconv_rawvideo $_avconv_vp8 -an -pass 1 -passlogfile ${stem}_vp8 /dev/null} \
  432. ${vp9:+$_avconv_rawvideo $_avconv_vp9 -an -pass 1 -passlogfile ${stem}_vp9 /dev/null}
  433. [ -z "$webm" ] || mv -f ${stem}_vp8-*.log ${stem}_vp8_2pass.log
  434. [ -z "$vp9" ] || mv -f ${stem}_vp9-*.log ${stem}_vp9_2pass.log
  435. fi
  436. if [ -n "$do_loudness" ]; then
  437. _melt_loudness="$(perl -ne 'm!<property name="results">([^<]+)</property>! and print $1' $stem.xml)"
  438. echo "Loudness data: $_melt_loudness"
  439. fi
  440. fi
  441. if [ -n "$use_oggenc$use_opusenc$use_vpxenc" ] && [ -n "$webm$vp9" ]; then
  442. echo "Encoding raw video and extracting audio..."
  443. $melt ${_melt_sample:-$infiles} \
  444. ${channels:+$audioprefilters${_melt_loudness:+ -filter loudness results="$_melt_loudness"}} \
  445. $filters${channels:+ $_melt_postfilters_audio} \
  446. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  447. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4} \
  448. ${channels:+-consumer avformat:$stem.wav $_melt_wav} \
  449. -consumer avformat:pipe:1 $_melt_yuv4mpeg2 \
  450. | pee \
  451. ${webm:+"$vpxenc - $_vpxenc_vp8${singlepass:+ -p 1}${multipass:+ -p 2 --pass=2 --fpf=${stem}_vp8.log} -o ${stem}_silent.webm"} \
  452. ${vp9:+"$vpxenc - $_vpxenc_vp9${singlepass:+ -p 1}${multipass:+ -p 2 --pass=2 --fpf=${stem}_vp9.log} -o ${stem}_vp9_silent.webm"}
  453. if [ -n "$webm" ]; then
  454. echo "Encoding Vorbis audio and muxing with VP8 video..."
  455. if [ -n "$use_oggenc" ]; then
  456. $oggenc $_oggenc_wav_in $_oggenc_vorbis -o - $stem.wav \
  457. | $avconv $_avconv_webm_in -i ${stem}_silent.webm $_avconv_ogg_in -i pipe:0 $_avconv_webm_keepvideo $stem.webm
  458. else
  459. $avconv $_avconv_webm_in -i ${stem}_silent.webm $_avconv_wav_in -i $stem.wav $_avconv_webm_keepvideo ${stem}.webm
  460. fi
  461. fi
  462. if [ -n "$vp9" ]; then
  463. echo "Encoding Opus audio and muxing with WebM/VP9 video..."
  464. if [ -n "$use_opusenc" ]; then
  465. $opusenc $_opusenc_wav_in $_opusenc_opus -o - $stem.wav \
  466. | $avconv $_avconv_webm_in -i ${stem}_vp9_silent.webm $_avconv_ogg_in -i pipe:0 $_avconv_webm_keepvideo_opus ${stem}_vp9.webm
  467. else
  468. $avconv $_avconv_webm_in -i ${stem}_vp9_silent.webm $_avconv_wav_in -i $stem.wav $_avconv_webm_keepvideo_opus ${stem}_vp9.webm
  469. fi
  470. fi
  471. else
  472. echo "Encoding video..."
  473. $melt ${_melt_sample:-$infiles} \
  474. ${channels:+$audioprefilters${_melt_loudness:+ -filter loudness results="$_melt_loudness"}} \
  475. $filters${channels:+ $_melt_postfilters_audio} \
  476. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  477. ${webm:+-consumer avformat:$stem.webm $_melt_webm${multipass:+ pass=2 passlogfile=${stem}_vp8}} \
  478. ${vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9${multipass:+ pass=2 passlogfile=${stem}_vp9}} \
  479. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4}
  480. fi
  481. if [ -n "$mp4" ] && [ -z "$melt_recent" ]; then
  482. mv "$stem.mp4" "$stem.mp4"~
  483. qt-faststart "$stem.mp4"~ "$stem.mp4"
  484. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  485. fi
  486. # cleanup encoding cruft
  487. rm -f $stem.xml $stem.wav ${stem}_vp8.log ${stem}_vp9.log ${stem}_silent.webm ${stem}_vp9_silent.webm
  488. # JPEG preview
  489. $melt $infile_first in=0 out=0 \
  490. -group $filters \
  491. -consumer avformat:$stem.jpg $_melt_img
  492. __width="${_width:+ width=\"$_width\"}"
  493. __height="${_height:+ height=\"$_height\"}"
  494. # Flash object needs extra space for controllers
  495. __heightplus=${_height:+ height=\"$(($_height+4))\"}
  496. _source_ogg="<source src=\"$stem.ogv\" type=\"video/ogg\" />"
  497. _source_webm="<source src=\"$stem.webm\" type=\"video/webm\" />"
  498. _source_vp9="<source src=\"${stem}_vp9.webm\" type='video/ogg; codecs=\"vp9, opus\"' />"
  499. _source_mp4="<source src=\"$stem.mp4\" type=\"video/mp4\" />"
  500. [ -z "$flashplayer" ] || flash=yes
  501. [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled."
  502. _object_flash="<object$__width$__heightplus type=\"application/x-shockwave-flash\" data=\"$flashplayer.swf\">"
  503. _param_name="<param name=\"movie\" value=\"$flashplayer.swf\" />"
  504. _param_flashvars="<param name=\"flashvars\" value=\"image=$stem.jpg&amp;file=$stem.mp4\" />"
  505. __oggfile=${ogg:+open format <a href=\"$stem.ogv\">Ogg</a>}
  506. __webmfile=${webm:+open format <a href=\"$stem.webm\">WebM (VP8)</a>}
  507. __vp9file=${vp9:+open format <a href=\"${stem}_vp9.webm\">WebM (VP9/Opus)</a>}
  508. __mp4file=${mp4:+closed format <a href=\"$stem.mp4\">MPEG-4</a>}
  509. cat >"$stem.html" <<EOF
  510. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  511. <video$__width$__height preload controls>
  512. ${mp4:+$_source_mp4
  513. }${vp9:+$_source_vp9
  514. }${webm:+$_source_webm
  515. }${ogg:+$_source_ogg
  516. }${flash:+$_object_flash
  517. $_param_name
  518. $_param_flashvars
  519. }<img src="$stem.jpg"$__width$__height alt="$title"
  520. title="No video playback capabilities, please download the video below" />
  521. ${flash:+</object>
  522. }</video>
  523. <p><strong>Download Video:</strong><ul>
  524. ${vp9:+<li>$__vp9file
  525. }${webm:+<li>$__webmfile
  526. }${ogg:+<li>$__oggfile
  527. }${mp4:+<li>$__mp4file
  528. }</ul></p>
  529. EOF