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