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