summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 75dfcdb93a982904e88697323b7d6ad35e563592 (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 vainfo vpx-tools vorbis-tools opus-tools
  20. #
  21. # TODO:
  22. # * improve workflow
  23. # + check initially and fail if all needed tools are not available
  24. # + offer to skip rendering again if an output file exist already
  25. # + maybe support extracting keyframes to aid in picking cover image
  26. # <https://video.stackexchange.com/questions/26066/dump-keyframes-of-a-livestream>
  27. # <https://video.stackexchange.com/questions/21970/speed-up-a-video-30x-using-only-key-frames-with-ffmpeg>
  28. # + offer to produce a full ladder at once
  29. # <https://developers.google.com/media/vp9/settings/vod>
  30. # <https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices>
  31. # * improve configurability
  32. # + support --width and --height,
  33. # resolving other dimension from (explicit or source) aspect ratio
  34. # + make choice of encoders configurable
  35. # * tune defaults
  36. # + adapt VP8/VP9 quantizer based on frame size
  37. # <https://developers.google.com/media/vp9/settings/vod#quality>
  38. # + maybe pass option real_time=-2 to melt
  39. # <https://www.reddit.com/r/kdenlive/comments/ka0aak/kdenlive_gpucpu_use_threads_mlt_and_ffmpeg_tips/>
  40. # + maybe reduce threads to "spend" total available cores only once
  41. # + apply application option when using opusenc
  42. # + use joint stereo for low-bitrate Opus speech
  43. # + maybe refine audio bandwidth algorithms
  44. # <http://trac.ffmpeg.org/wiki/Encode/HighQualityAudio>
  45. # + change VP8/VP9 bandwidth algorithm
  46. # <https://developers.google.com/media/vp9/settings/vod/>
  47. # + tune VP8 parameters:
  48. # <http://www.webmproject.org/docs/encoder-parameters/
  49. # + tune VP9 parameters
  50. # <https://sites.google.com/a/webmproject.org/wiki/ffmpeg/vp9-encoding-guide>
  51. # + compute (or hardcode) html5 codec types, e.g. using mp4v2-utils
  52. # <http://stackoverflow.com/a/16365526>
  53. # + generalize GOP handling, with keyframes every ~8s by default
  54. # which fits 48kHz audio at 25 fps (200f) and 30fps (240f),
  55. # or an "LL" option of 48f (1.92s at 25fps or 1.6s at 30fps)
  56. # + generalize GOP handling,
  57. # with default "normal" option of 200f@25fps or 240f@30fps (both 8s)
  58. # and "low-latency" option of 48f (1.92s at 25fps or 1.6s at 30fps)
  59. # <https://stackoverflow.com/questions/30979714/how-to-change-keyframe-interval-in-ffmpeg/41735741#41735741>
  60. # <http://anton.lindstrom.io/gop-size-calculator/>
  61. # <https://docs.unified-streaming.com/best-practice/content-preparation.html#recommended-fragment-boundaries-are-aligned-across-all-tracks-audio-video-text>
  62. # * players
  63. # + disable flash player by default
  64. # + support HLS player, when HLS is supported
  65. # <https://github.com/video-dev/hls.js>
  66. # * normalize each infile separately when xml fed as infile keeps sync
  67. # maybe as workaround re-feed audio separately from xml, as done at
  68. # <http://bernaerts.dyndns.org/linux/74-ubuntu/214-ubuntu-stabilize-video-melt>
  69. # * support DASH
  70. # <https://sites.google.com/a/webmproject.org/wiki/adaptive-streaming/instructions-to-playback-adaptive-webm-using-dash>
  71. # * support watermark
  72. # * support superstable live streaming, e.g. (maybe faked) endless source
  73. # + lipsync may require single stream of equally chunked tracks
  74. # <https://sites.google.com/a/webmproject.org/wiki/adaptive-streaming/instructions-to-do-webm-live-streaming-via-dash>
  75. # * support adaptive streaming
  76. # + compute ladder from mathematical model
  77. # as in "Optimal design of encoding profiles for ABR streaming"
  78. # <http://www.reznik.org/publications.html>
  79. # * support low-latency streaming a.k.a. "LL", i.e. 3-5s latency
  80. # + LL-HLS
  81. # <https://stackoverflow.com/questions/56700705/how-to-enable-lhls-in-ffmpeg-4-1>
  82. # + CMAF, using either fmpeg or (when released) GPAC
  83. # <https://github.com/FFmpeg/FFmpeg/commit/cc929ce>
  84. # <https://github.com/gpac/gpac/commit/4529c60>
  85. # * support realtime streaming a.k.a. "ULL", i.e. less than 0.5s latency
  86. set -e
  87. PRG=$(basename "$0")
  88. showhelp() {
  89. cat <<EOF
  90. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  91. Encode video file in multiple web-optimized formats, and provide sample
  92. html favoring open formats with optional non-JavaScript Flash fallback.
  93. --video Video style:
  94. [refbpp]
  95. talkinghead 0.1
  96. action 0.15
  97. (default: none)
  98. -p, --profile Video frame size and optional rate, delimited by @
  99. except "p" labels: e.g. 848x480@25 480p25 wvga@25
  100. -s, --size Set video frame size:
  101. [modulus 16]
  102. 320x240 qvga
  103. 640x480 vga 480p 848x480 wvga
  104. 576p 1024x576 wsvga
  105. 1024x768 xga hd 720p 1280x720 wxga
  106. [modulus 8]
  107. 240p 424x240 wqvga
  108. 480x360 hvga nhd 360p 640x360
  109. 800x600 svga
  110. (default: use input size)
  111. -a, --aspect Display Aspect Ratio in melt format: e.g. @16/9
  112. (default: no aspect hinting)
  113. -r, --rate Video framerate: e.g. 25 1001/30000
  114. (default: use input framerate)
  115. --refbpp Bitrate reference: Bits-per-pixel for 360p30 H.264
  116. (default: 0.12)
  117. --formats Container and media formats to use, comma-separated:
  118. [container] [video] [audio]
  119. theora ogg Ogg Theora Vorbis
  120. vp8 webm WebM VP8 Vorbis
  121. vp9 WebM VP9 Opus
  122. avc h264 mp4 MPEG-4 H.264/AVC AAC-LC
  123. (default: vp8,vp9,avc)
  124. --audio Audio style:
  125. [channels] [bitrate per channel]
  126. music max. 2 ~48-64k
  127. speech 1 ~32-64k
  128. silence 0
  129. (default: channel count of first input, ~48-64k)
  130. --audioprefilter Add audio filter before loudness
  131. --loudness Add EBU R128 loudness filter
  132. --loudness-data Add loudness filter, with precomputed results
  133. (default: none - compute results on-the-fly)
  134. --filter Add filter
  135. --stem Stem of output filenames, optionally with path
  136. (default: basename of first input)
  137. -t, --title Title used in html fallback graphics
  138. (default: stem)
  139. --still-frame Frame used as still picture
  140. (default: 0)
  141. --sample Create only a 150 frames long sample
  142. --sample-start Create sample, starting at a specific frame
  143. (default: start at first frame)
  144. --sample-length Create sample of specified length, in frames
  145. (default: 150 frames i.e. approx. 5s)
  146. --compression Prioritize quality, size or render time:
  147. [quality] [bitrate] [speed] [method]
  148. dirty fast CQ
  149. normal medium average medium CQ
  150. hq high exact slow 2xCQ¹
  151. exact medium exact slow 2xCQ¹
  152. quality medium vary medium Q²
  153. ¹ avc uses 1-pass
  154. ² vp8 uses "soft-CQ" (CQ with very high bitrate)
  155. (default: normal)
  156. --quality Quality rate factor: auto [integer] none
  157. suggested, avc: 19 23 27 31 35 39
  158. suggested, vp8/vp9: 27 33 39 45 51 57
  159. (default: auto - 23 for avc, 33 for vp8/vp9)
  160. --gpu Use hardware acceleration: none auto vaapi qsv
  161. (default: auto when compression=dirty, else none)
  162. --gpudevice Device used for hardware acceleration
  163. (default: /dev/dri/renderD128)
  164. -h, --help This help text
  165. Examples:
  166. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  167. $PRG -p 480p15 --filter "grain noise=20" myvideo.dv
  168. Recommendations for best results:
  169. * Use square pixels, modulus 16, max. 480 width (qvga vga 480p 720p).
  170. * Try lower --refbpp while visually acceptable, or raise to max. 0.2.
  171. * Expand and compress noisy speech with ladspa.1075 and ladspa.1916.
  172. More info:
  173. <http://camendesign.com/code/video_for_everybody>
  174. <http://www.streaminglearningcenter.com/articles/configuring-your-streaming-video-(for-newbies).html>
  175. <http://en.wikipedia.org/wiki/HTML5_video>
  176. <http://www.penguinproducer.com/2012/01/ladspa-noise-removal/>
  177. <http://theproaudiofiles.com/mixing-vocals-dynamics-compression-limiting>
  178. <http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/>
  179. EOF
  180. }
  181. exit1() {
  182. response="${1:+Error: }${1:-Internal error!}"
  183. echo >&2 "$response"
  184. exit 1
  185. }
  186. # defaults
  187. formats=vp8,vp9,avc
  188. stillframe=0
  189. samplestart=0
  190. samplelength=150
  191. compression=normal
  192. # establish/verify rendering settings for reference codec
  193. #
  194. # 1) pick reference frame:encoding format: 360p30:h.264
  195. # 2) pick reference codec (incl. normal settings)
  196. # 3) render reference frame format with --compression=quality
  197. # * check that bpp is near "refbpp" of 0.12
  198. # (otherwise adapt settings or do research and update refbpp)
  199. # 4) render misc. frame formats (esp. modulus 16) with --compression=quality
  200. # * check that bpp derives from refbpp by "rule of .75"
  201. # (otherwise adapt settings and/or do research and update rule)
  202. # 5) render misc. frame formats with --compression=normal
  203. # * check that bpp derives from refbpp by "rule of .75"
  204. # 6) optionally pick dirty/hq settings
  205. # * check that --compression=quality has filesize similar as normal
  206. # establish/verify rendering settings for non-reference codec
  207. #
  208. # 1) pick normal settings
  209. # 2) render reference frame format with --compression=quality
  210. # * check that this "codec-bpp" deviates reasonably from refbpp
  211. # 3) render misc. frame formats (esp. modulus 16) with --compression=quality
  212. # * check that bpp derives from codec-bpp by "rule of .75"
  213. # (otherwise adapt settings and/or extend rule)
  214. # 4) render misc. frame formats with --compression=normal
  215. # * check that bpp derives from codec-bpp by "rule of .75"
  216. # 5) optionally pick dirty/hq settings
  217. # * check that --compression=quality has similar filesize as normal
  218. # VP8 is rumored to compress ~20% worse than H.264
  219. factor_vp8=120/100
  220. # VP9 compresses ~30% better than H.264 for practical use-cases
  221. # <https://engineering.fb.com/2018/04/10/video-engineering/av1-beats-x264-and-libvpx-vp9-in-practical-use-case/>
  222. factor_vp9=70/100
  223. # Mimic ReplayGain level when loudness is enabled (EBU R128: -23LUFS)
  224. # <https://auphonic.com/blog/2013/01/07/loudness-targets-mobile-audio-podcasts-radio-tv/>
  225. lufs=-18
  226. # parse cmdline options
  227. 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:,quality:,gpu:,gpudevice: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  228. eval set -- "$TEMP"
  229. while true ; do
  230. case "$1" in
  231. -h|--help) showhelp; exit;;
  232. -p|--profile) profile="$2"; shift 2;;
  233. -s|--size) size="$2"; shift 2;;
  234. -a|--aspect) aspect="$2"; shift 2;;
  235. -r|--rate) framerate="$2"; shift 2;;
  236. --video) video="$2"; shift 2;;
  237. --refbpp) refbpp="$2"; shift 2;;
  238. --formats) formats="$2"; shift 2;;
  239. --audio) audio="$2"; shift 2;;
  240. --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;;
  241. --loudness) loudness=yes; shift;;
  242. --loudness-data) loudness=yes; loudness_data="$2"; shift 2;;
  243. --filter) filters="${filters:+$filters }-filter $2"; shift 2;;
  244. --stem) stem="$2"; shift 2;;
  245. -t|--title) title="$2"; shift 2;;
  246. --still-frame) stillframe="$2"; shift 2;;
  247. --sample) sample=yes; shift;;
  248. --sample-start) sample=yes; samplestart="$2"; shift 2;;
  249. --sample-length) sample=yes; samplelength="$2"; shift 2;;
  250. --compression) compression="$2"; shift 2;;
  251. --quality) quality="$2"; shift 2;;
  252. --gpu) gpu="$2"; shift 2;;
  253. --gpudevice) gpudevice="$2"; shift 2;;
  254. --) shift; break;;
  255. *) exit1 "Internal error resolving options.";;
  256. esac
  257. done
  258. [ -n "$sample" ] || notsample=yes
  259. # Resolve if system has many CPU cores
  260. processors=$(nproc)
  261. [ $processors -gt 2 ] || processors=
  262. # sanitize infiles
  263. infiles=$*
  264. infile_first=$(perl -e 'print shift @ARGV' $infiles)
  265. [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!"
  266. # resolve stem and title (if not explicitly set)
  267. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  268. title=${title:-$stem}
  269. #use_ffmpeg_theora=yes
  270. #use_ffmpeg_vp8=yes
  271. #use_ffmpeg_vp9=yes
  272. #use_ffmpeg_avc=yes
  273. #use_ffmpeg2theora=yes # TODO...
  274. #use_vpxenc=yes
  275. # Avoid discrete audio encoders if possible: may cause sync problems
  276. #use_oggenc=yes
  277. #use_opusenc=yes
  278. # Avoid AAC: Lame arguably better than any free AAC encoder
  279. # Argument for AAC: Safari possibly lacks mp3 support in video container
  280. #use_mp3=yes
  281. [ -n "$use_mp3" ] || use_aac=yes
  282. # Avoid MP3 VBR but seems ABR is fine: Set to CBR for strict correctnes
  283. #use_lame_cbr=yes
  284. [ -n "$use_lame_cbr" ] || use_lame_abr=yes
  285. # resolve quality/speed hints
  286. qkey_theora=q
  287. qkey_vp8=crf
  288. qkey_vp9=crf
  289. qkey_avc=crf
  290. q_theora=5
  291. q_vp8=33
  292. cpu_vp8=2
  293. q_vp9=33
  294. cpu_vp9=2
  295. q_avc=23
  296. preset_avc=medium
  297. profile_vp8=0
  298. profile_vp9=0
  299. profile_avc=main
  300. case "$compression" in
  301. normal) :;;
  302. dirty)
  303. gpu=${gpu:-auto}
  304. q_theora=1
  305. cpu_vp8=4
  306. cpu_vp9=4
  307. preset_avc=veryfast
  308. ;;
  309. hq)
  310. multipass=yes
  311. no_multipass_theora=yes # TODO: try with ffmpeg2theora
  312. no_multipass_avc=yes
  313. q_theora=6
  314. cpu_vp8=0
  315. cpu_vp9=1
  316. preset_avc=veryslow
  317. ;;
  318. exact)
  319. multipass=yes
  320. no_multipass_avc=yes
  321. ;;
  322. quality)
  323. no_bitrate=yes
  324. ;;
  325. *) exit1 "Unknown compression mode \"$compression\".";;
  326. esac
  327. case "$quality" in
  328. auto|'') :;;
  329. none) no_q=yes;;
  330. *) q=$quality;;
  331. esac
  332. gpudevice=${gpudevice:-/dev/dri/renderD128}
  333. if [ auto = "$gpu" ]; then
  334. vainfo=$(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'Driver version:\s*\K.*')
  335. case "$vainfo" in
  336. '') gpu=;;
  337. *)
  338. for vformat in theora vp8 vp9 avc; do
  339. eval "no_gpu_decoder_$vformat=yes"
  340. eval "no_gpu_encoder_$vformat=yes"
  341. done
  342. case $(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'VAProfile\S+\s*:\s*VAEntrypointVLD') in
  343. *VAProfileVP8Version0_3*) no_gpu_decoder_vp8=;;
  344. *VAProfileVP9Profile0*) no_gpu_decoder_vp9=;;
  345. *VAProfileH264Main*) no_gpu_decoder_avc=;;
  346. esac
  347. case $(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'VAProfile\S+\s*:\s*VAEntrypointEncSlice') in
  348. *VAProfileVP8Version0_3*) no_gpu_encoder_vp8=;;
  349. *VAProfileVP9Profile0*) no_gpu_encoder_vp9=;;
  350. *VAProfileH264Main*) no_gpu_encoder_avc=;;
  351. esac
  352. if [ -n "$no_gpu_decoder_vp8$no_gpu_decoder_vp9$no_gpu_decoder_avc" ]; then
  353. case "$vainfo" in
  354. # TODO *"Intel iHD driver"*) gpu=qsv;;
  355. *) gpu=vaapi;;
  356. esac
  357. fi
  358. esac
  359. fi
  360. case "$gpu" in
  361. none|'') gpu=;;
  362. qsv)
  363. multipass=
  364. preset_vp8=medium
  365. preset_vp9=medium
  366. preset_avc=
  367. qkey_avc=qp
  368. ;;
  369. vaapi)
  370. multipass=
  371. preset_avc=
  372. qkey_avc=qp
  373. case "$compression" in
  374. dirty)
  375. preset_avc=
  376. ;;
  377. esac
  378. ;;
  379. *) exit1 "Unknown GPU type \"$gpu\".";;
  380. esac
  381. [ -z "$gpu" ] && no_gpu=yes || eval "gpu_$gpu=yes"
  382. # parse/resolve size and framerate
  383. case "$profile" in
  384. '') :;;
  385. *@*)
  386. while read s r foo; do
  387. profilesize="${size:-$s}"
  388. framerate="${framerate:-$r}"
  389. done << EOF
  390. $(echo "$profile" | perl -F@ -anE 'say join " ", @F')
  391. EOF
  392. ;;
  393. *p*)
  394. while read s r foo; do
  395. profilesize="${size:-${s}p}"
  396. framerate="${framerate:-$r}"
  397. done << EOF
  398. $(echo "$profile" | perl -Fp -anE 'say join " ", @F')
  399. EOF
  400. ;;
  401. *)
  402. profilesize="$profile"
  403. ;;
  404. esac
  405. size=${size:-$profilesize}
  406. case "$size" in
  407. qvga) size=320x240;;
  408. hvga) size=480x360;;
  409. vga) size=640x480;;
  410. svga) size=800x600;;
  411. xga) size=1024x768;;
  412. 240p|wqvga) size=424x240;;
  413. 360p|nhd) size=640x360;;
  414. 480p|wvga) size=848x480;;
  415. 576p|wsvga) size=1024x576;;
  416. 720p|wxga|hd) size=1280x720;;
  417. esac
  418. if [ -n "$size" ]; then
  419. while read w h foo; do
  420. width="${width:-$w}"
  421. height="${height:-$h}"
  422. done << EOF
  423. $(echo "$size" | perl -Fx -anE 'say join " ", @F')
  424. EOF
  425. if [ -z "$width" ] || [ -z "$height" ]; then
  426. exit1 "Failed to parse size \"$size\"."
  427. fi
  428. fi
  429. case "$framerate" in
  430. */*)
  431. while read n d foo; do
  432. framerate_num="${framerate_num:-$n}"
  433. framerate_den="${framerate_den:-$d}"
  434. done << EOF
  435. $(echo "$framerate" | perl -F/ -anE 'say join " ", @F')
  436. EOF
  437. ;;
  438. ?*)
  439. framerate_num="$framerate"
  440. framerate_den=1
  441. ;;
  442. esac
  443. # resolve input size and framerate (needed for computing bitrate)
  444. while read w h r s foo; do
  445. width_in="${width_in:-$w}"
  446. height_in="${height_in:-$h}"
  447. framerate_in="${framerate_in:-$r}"
  448. scantype_in="${scantype_in:-$s}"
  449. done << EOF
  450. $(mediainfo --Inform="Video;%Width% %Height% %FrameRate% %ScanType%" \
  451. "$infile_first")
  452. EOF
  453. [ Progressive = "$scantype_in" ] || do_deinterlace=yes
  454. case "$video" in
  455. talkinghead)
  456. refbpp="${refbpp:-0.1}"
  457. x264tune=film
  458. ;;
  459. action)
  460. refbpp="${refbpp:-0.15}"
  461. x264tune=film
  462. ;;
  463. '')
  464. refbpp="${refbpp:-0.12}"
  465. ;;
  466. *) exit1 "Unknown video style \"$video\".";;
  467. esac
  468. for format in $(echo "$formats" | sed -e 's/,/ /g'); do
  469. case $format in
  470. theora|ogg) ogv=yes;;
  471. avc|h264|mp4) mp4=yes;;
  472. vp8|webm) webm=yes;;
  473. vp9) webm_vp9=yes;;
  474. *) exit1 "Unknown format \"$format\".";;
  475. esac
  476. done
  477. _width="${width:-$width_in}"
  478. _height="${height:-$height_in}"
  479. if [ -n "$_width" ] && [ -n "$_height" ]; then
  480. _pixels="$((_width*_height))"
  481. fi
  482. _frames="${framerate:-$framerate_in}"
  483. qmin_vp8=0
  484. qmin_vp9=0
  485. qmax_vp8=63
  486. qmax_vp9=63
  487. lag_vp8=16
  488. lag_vp9=16
  489. tokenparts_log2_vp8=0
  490. tokenparts_log2_vp9=0
  491. if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then
  492. qmin_vp8=11
  493. qmin_vp9=11
  494. qmax_vp8=51
  495. qmax_vp9=51
  496. tokenparts_log2_vp8=2
  497. tokenparts_log2_vp9=2
  498. if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then
  499. lag_vp8=25
  500. lag_vp9=25
  501. profile_vp8=1
  502. profile_vp9=1
  503. fi
  504. fi
  505. # compute average bitrate from reference data and "power of .75" rule
  506. if [ -n "$_pixels" ] && [ -n "$_frames" ]; then
  507. bitrate_avc=$(perl -E '$refsize=640*360;' \
  508. -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )")
  509. bitrate_theora=$bitrate_avc # FIXME
  510. bitrate_vp8=$(perl -E "say int( +($bitrate_avc*$factor_vp8) )") #"
  511. bitrate_vp9=$(perl -E "say int( +($bitrate_avc*$factor_vp9) )") #"
  512. fi
  513. # default per-codec-channel bitrates
  514. quality_vorbis=3
  515. bitrate_opus=48
  516. quality_lame=6
  517. bitrate_lame=64
  518. bitrate_aac=64
  519. case "$audio" in
  520. music)
  521. opusapp=audio
  522. ;;
  523. speech)
  524. mono=yes
  525. quality_vorbis=2
  526. bitrate_opus=32
  527. quality_lame=7
  528. bitrate_lame=48
  529. opusapp=voip
  530. ;;
  531. silence)
  532. silence=yes
  533. ;;
  534. '')
  535. :
  536. ;;
  537. *) exit1 "Unknown audio style \"$audio\".";;
  538. esac
  539. which ffprobe > /dev/null && ffprobe=ffprobe || ffprobe=avprobe
  540. channels=$($ffprobe -v warning -show_streams "$infile_first" \
  541. | perl -ne 's/channels=// and print $_' || echo -1)
  542. # adapt channel count and flags to reflect actual downmix/silence need
  543. if [ -n "$stereo" ]; then
  544. if [ $channels -gt 2 ]; then
  545. channels=2
  546. else
  547. stereo=
  548. fi
  549. elif [ -n "$mono" ]; then
  550. if [ $channels -gt 1 ]; then
  551. channels=1
  552. else
  553. mono=
  554. fi
  555. elif [ -n "$silence" ]; then
  556. if [ $channels -gt 0 ]; then
  557. channels=0
  558. else
  559. silence=
  560. fi
  561. fi
  562. [ -z "$silence" ] || channels=
  563. if [ -n "$stereo$mono" ]; then
  564. # melt cannot downmix with (stereo?) filters applied (bug#763911)
  565. if [ -n "$audioprefilters$compress$loudness$limit$filters" ]; then
  566. if [ -z "${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}" ]; then
  567. downmix_ffmpeg=yes
  568. else
  569. downmix_oggenc=yes
  570. downmix_oggenc=yes
  571. fi
  572. else
  573. downmix_melt=yes
  574. fi
  575. fi
  576. encoder_vp8=libvpx
  577. encoder_vp9=libvpx-vp9
  578. encoder_avc=libx264
  579. for vformat in theora vp8 vp9 avc; do
  580. [ -z "$no_bitrate" ] || eval "no_bitrate_$vformat=yes"
  581. eval "[ -n \"$no_bitrate\$no_bitrate_$vformat\" ] || do_bitrate_$vformat=yes"
  582. eval "[ -z \"$no_q\$no_q_$vformat\" ] || q_$vformat="
  583. [ -z "$q" ] || eval "q_$vformat=$q"
  584. eval "[ -n \"\$q_$vformat\" ] || no_q_$vformat=yes"
  585. [ -z "$gpu" ] || eval "[ -n \"\$no_gpu_encoder_$vformat\" ] || encoder_$vformat=${vformat}_$gpu"
  586. [ avc = "$vformat" ] || eval "[ -n \"$use_vpxenc\$use_ffmpeg_$vformat\" ] || use_melt_$vformat=yes"
  587. [ -z "$multipass" ] || eval "[ -n \"\$no_multipass_$vformat\" ] || multipass_$vformat=yes"
  588. eval "[ -n \"\$multipass_$vformat\" ] || singlepass_$vformat=yes"
  589. done
  590. if [ -n "$use_ffmpeg_theora$use_ffmpeg_vp8$use_ffmpeg_vp9$use_vpxenc" ]; then
  591. [ -n "$use_oggenc" ] || use_ffmpeg_vorbis=yes
  592. [ -n "$use_opusenc" ] || use_ffmpeg_opus=yes
  593. [ -z "$use_oggenc$use_opusenc" ] || use_wav=yes
  594. [ -z "$use_ffmpeg_vorbis$use_ffmpeg_opus" ] || use_matroska=yes
  595. fi
  596. if [ -n "$use_ffmpeg_avc" ]; then
  597. use_matroska=yes
  598. else
  599. use_melt_avc=yes
  600. fi
  601. use_melt_any="${ogv:+$use_melt_theora}${webm:+$use_melt_vp8}${webm_vp9:+$use_melt_vp9}${mp4:+$use_melt_avc}"
  602. use_ffmpeg_any="${ogv:+$use_ffmpeg_theora}${webm:+$use_ffmpeg_vp8}${webm_vp9:+$use_ffmpeg_vp9}${mp4:+$use_ffmpeg_avc}"
  603. use_vpxenc_any="${webm:+$use_vpxenc}${webm_vp9:+$use_vpxenc}"
  604. use_opusenc_any="${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}"
  605. use_oggenc_any="${ogv:+$use_oggenc}${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}"
  606. [ -z "$use_ffmpeg_any$use_vpxenc_any" ] || use_any_nomelt_video=yes
  607. [ -z "$gpu" ] || echo >&2 "GPU platform enabled: $gpu"
  608. # gpu options
  609. _melt_gpu_init=${gpu:+ ${gpu}_device=$gpudevice}
  610. _ffmpeg_gpu_vaapi_init="-${gpu}_device $gpudevice"
  611. _ffmpeg_gpu_qsv_init="-init_hw_device qsv=hw:$gpudevice -filter_hw_device hw"
  612. _ffmpeg_gpu_init="${gpu_vaapi:+ ${_ffmpeg_gpu_vaapi_init}}${gpu_qsv:+ ${_ffmpeg_gpu_qsv_init}}"
  613. _ffmpeg_gpu_vaapi_filter="-vf 'format=nv12|$gpu,hwupload'"
  614. _ffmpeg_gpu_qsv_filter="-vf 'hwupload=extra_hw_frames=64,format=qsv'"
  615. _ffmpeg_gpu_filter="${gpu_vaapi:+ ${_ffmpeg_gpu_vaapi_filter}}${gpu_qsv:+ ${_ffmpeg_gpu_qsv_filter}}"
  616. # generic options
  617. which ffmpeg > /dev/null && ffmpeg=ffmpeg || ffmpeg=avconv
  618. _melt_infiles="${notsample:+$infiles}${sample:+$infile_first in=$((samplestart)) out=$((samplestart + samplelength))}"
  619. render() {
  620. engine=$1; shift
  621. chained=
  622. echo=
  623. unchained=yes
  624. case "$engine" in
  625. ffmpeg-chained) engine=ffmpeg; chained=yes;;
  626. vpxenc-chained) engine=vpxenc; chained=yes;;
  627. ffmpeg-chained-echo) engine=ffmpeg; chained=yes; echo=yes;;
  628. vpxenc-chained-echo) engine=vpxenc; chained=yes; echo=yes;;
  629. esac
  630. [ -z "$chained" ] || unchained=
  631. case "$engine" in
  632. melt)
  633. set -- -progress$_melt_gpu_init "$@"
  634. ;;
  635. ffmpeg)
  636. engine=$ffmpeg
  637. set -- -threads auto -y -hide_banner -v ${unchained:+info -stats}${chained:+warning}$_ffmpeg_gpu_init "$@"
  638. ;;
  639. vpxenc)
  640. set -- --quiet ${processors:+-t $((processors-1))} "$@"
  641. ;;
  642. oggenc)
  643. set -- "$@"
  644. ;;
  645. opusenc)
  646. set -- "$@"
  647. ;;
  648. *) exit1 "Unknown render engine \"$engine\".";;
  649. esac
  650. ${echo:+echo }"$engine" "$@"
  651. }
  652. # filter options
  653. _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}"
  654. _ffmpeg_video="${no_gpu:+${do_deinterlace:+-filter:v yadif}${framerate:+ -r $framerate_num/$framerate_den}${size:+ -s ${width:+$width}x${height:+$height}}${aspect:+ -aspect $aspect}}"
  655. _melt_downmix="${downmix_melt:+ac=$channels}"
  656. _ffmpeg_downmix="${downmix_ffmpeg:+-ac $channels}"
  657. # FIXME: how to downmix to stereo?
  658. _oggenc_downmix="${downmix_oggenc:+${mono:+--downmix}}"
  659. _opusenc_downmix="${downmix_opusenc:+${stereo:+--downmix-stereo}${mono:+--downmix-mono}}"
  660. # limit (i.e. avoid peaks "clipping")
  661. _melt_postfilters_audio="${limit:+-filter ladspa.1077}"
  662. # format bitrate options
  663. _melt_bitrate_theora="${do_bitrate_theora:+ vb=$bitrate_theora}"
  664. _ffmpeg_bitrate_theora="${do_bitrate_theora:+ -b:v $bitrate_theora}"
  665. _melt_bitrate_vp8="${do_bitrate_vp8:+ vb=$bitrate_vp8 minrate=$((bitrate_vp8/20)) maxrate=$((bitrate_vp8*12))}"
  666. _ffmpeg_bitrate_vp8="${do_bitrate_vp8:+ -b:v $bitrate_vp8 -minrate $((bitrate_vp8/20)) -maxrate $((bitrate_vp8*12))}"
  667. _vpxenc_bitrate_vp8="${do_bitrate_vp8:+ --target-bitrate=$((bitrate_vp8/1000))${multipass_vp8:+ --minsection-pct=5 --maxsection-pct=1200}}"
  668. _melt_bitrate_vp9="${do_bitrate_vp9:+ vb=$bitrate_vp9 minrate=$((bitrate_vp9/20)) maxrate=$((bitrate_vp9*12))}"
  669. _ffmpeg_bitrate_vp9="${do_bitrate_vp9:+ -b:v $bitrate_vp9 -minrate $((bitrate_vp9/20)) -maxrate $((bitrate_vp9*12))}"
  670. _vpxenc_bitrate_vp9="${do_bitrate_vp9:+ --target-bitrate=$((bitrate_vp9/1000))${multipass_vp9:+ --minsection-pct=5 --maxsection-pct=1200}}"
  671. _melt_bitrate_avc="${do_bitrate_avc:+ maxrate=$bitrate_avc${no_gpu:+ bufsize=$((bitrate_avc*2))}}"
  672. _ffmpeg_bitrate_avc="${do_bitrate_avc:+ -maxrate $bitrate_avc${no_gpu:+ -bufsize $((bitrate_avc*2))}}"
  673. # format low-level options
  674. _melt_misc_vp8=" qmin=$qmin_vp8 qmax=$qmax_vp8 g=120 deadline=good cpu-used=$cpu_vp8${profile_vp8:+ vprofile=$profile_vp8} auto-alt-ref=1 lag-in-frames=$lag_vp8 arnr-maxframes=7 arnr-strength=5 arnr-type=centered"
  675. _ffmpeg_misc_vp8=" -qmin $qmin_vp8 -qmax $qmax_vp8 -g 120 -deadline good -cpu-used $cpu_vp8${profile_vp8:+ -profile:v $profile_vp8} -auto-alt-ref 1 -lag-in-frames $lag_vp8 -arnr-maxframes 7 -arnr-strength 5 -arnr-type centered"
  676. _vpxenc_misc_vp8=" --passes=${singlepass_vp8:+1}${multipass_vp8:+2} --min-q=$qmin_vp8 --max-q=$qmax_vp8 --kf-max-dist=120 --good --cpu-used=$cpu_vp8${profile_vp8:+ --profile=$profile_vp8} --token-parts=$tokenparts_log2_vp8 --auto-alt-ref=1 --lag-in-frames=$lag_vp8 --arnr-maxframes=7 --arnr-strength=5 --arnr-type=3"
  677. _melt_misc_vp9=" qmin=$qmin_vp9 qmax=$qmax_vp9 g=120 deadline=good cpu-used=$cpu_vp9${profile_vp9:+ vprofile=$profile_vp9} auto-alt-ref=1 lag-in-frames=$lag_vp9 arnr-maxframes=7 arnr-strength=5 arnr-type=centered"
  678. _ffmpeg_misc_vp9=" -qmin $qmin_vp9 -qmax $qmax_vp9 -g 120 -deadline good -cpu-used $cpu_vp9${profile_vp9:+ -profile:v $profile_vp9} -auto-alt-ref 1 -lag-in-frames $lag_vp9 -arnr-maxframes 7 -arnr-strength 5 -arnr-type centered"
  679. _vpxenc_misc_vp9=" --passes=${singlepass_vp9:+1}${multipass_vp9:+2} --min-q=$qmin_vp9 --max-q=$qmax_vp9 --kf-max-dist=120 --good --cpu-used=$cpu_vp9${profile_vp9:+ --profile=$profile_vp9} --tile-columns=$tokenparts_log2_vp9 --tile-rows=$tokenparts_log2_vp9 --auto-alt-ref=1 --lag-in-frames=$lag_vp9 --arnr-maxframes=7 --arnr-strength=5 --arnr-type=3"
  680. _melt_misc_avc="${no_gpu:+ movflags=+faststart}"
  681. _ffmpeg_misc_avc="${no_gpu:+ -movflags +faststart}"
  682. # format options
  683. # emulate VP8 Constant Quality using "loose" Constrained Quality
  684. _melt_theora="vcodec=libtheora$_melt_bitrate_theora $qkey_theora=$q_theora"
  685. _ffmpeg_theora="-c:v libtheora$_ffmpeg_bitrate_theora -$qkey_theora:v $q_theora"
  686. _melt_vp8="vcodec=$encoder_vp8${pre_vp8:+ vpreset=$pre_vp8}${preset_vp8:+ preset=$preset_vp8}$_melt_bitrate_vp8${q_vp8:+ ${no_bitrate_vp8:+vb=$((bitrate_vp8*10)) }$qkey_vp8=$q_vp8}$_melt_misc_vp8"
  687. _ffmpeg_vp8="-c:v $encoder_vp8${pre_vp8:+ -vpre $pre_vp8}${preset_vp8:+ preset $preset_vp8}$_ffmpeg_bitrate_vp8${q_vp8:+ ${no_bitrate_vp8:+-b:v $((bitrate_vp8*10)) }-$qkey_vp8 $q_vp8}$_ffmpeg_misc_vp8"
  688. _vpxenc_vp8="--codec=vp8$_vpxenc_bitrate_vp8 --end-usage=${no_q_vp8:+vbr}${q_vp8:+cq --cq-level=$q_vp8}$_vpxenc_misc_vp8"
  689. _melt_vp9="vcodec=$encoder_vp9${pre_vp9:+ vpreset=$pre_vp9}${preset_vp9:+ preset=$preset_vp9}$_melt_bitrate_vp9${q_vp9:+ ${no_bitrate_vp9:+vb=0 }$qkey_vp9=$q_vp9}$_melt_misc_vp9"
  690. _ffmpeg_vp9="-c:v $encoder_vp9${pre_vp9:+ -vpre $pre_vp9}${preset_vp9:+ -preset $preset_vp9}$_ffmpeg_bitrate_vp9${q_vp9:+ ${no_bitrate_vp9:+-b:v 0 }-$qkey_vp9 $q_vp9}$_ffmpeg_misc_vp9"
  691. _vpxenc_vp9="--codec=vp9$_vpxenc_bitrate_vp9 --end-usage=${no_q_vp9:+vbr}${q_vp9:+cq --cq-level=$q_vp9}$_vpxenc_misc_vp9"
  692. _melt_avc="vcodec=$encoder_avc${preset_avc:+ vpreset=$preset_avc}${profile_avc:+ vprofile=$profile_avc}${x264tune:+ tune=$x264tune}$_melt_bitrate_avc${q_avc:+ $qkey_avc=$q_avc} threads=0$_melt_misc_avc"
  693. _ffmpeg_avc="-c:v $encoder_avc${preset_avc:+ -preset $preset_avc}${profile_avc:+ -profile:v $profile_avc}${x264tune:+ -tune $x264tune}$_ffmpeg_bitrate_avc${q_avc:+ -$qkey_avc $q_avc}$_ffmpeg_misc_avc"
  694. _melt_pcm="$_melt_downmix acodec=pcm_s16le"
  695. _melt_vorbis="$_melt_downmix acodec=libvorbis aq=$quality_vorbis"
  696. _ffmpeg_vorbis="$_ffmpeg_downmix -c:a libvorbis -q:a $quality_vorbis"
  697. _oggenc_vorbis="$_oggenc_downmix -q $quality_vorbis"
  698. _melt_opus="$_melt_downmix acodec=libopus ab=$((channels*bitrate_opus))k${opusapp:+ application=$opusapp}"
  699. _ffmpeg_opus="$_ffmpeg_downmix -c:a libopus -b:a $((channels*bitrate_opus))k${opusapp:+ -application $opusapp}"
  700. _opusenc_opus="$_opusenc_downmix --bitrate $((channels*bitrate_opus))"
  701. _melt_mp3="$_melt_downmix acodec=libmp3lame${use_lame_abr:+ aq=$quality_lame}${use_lame_cbr:+ ab=$((channels*bitrate_lame))k}"
  702. _ffmpeg_mp3="$_ffmpeg_downmix -c:a libmp3lame${use_lame_abr:+ -q:a $quality_lame}${use_lame_cbr:+ -b:a $((channels*bitrate_lame))k}"
  703. _melt_aac="$_melt_downmix acodec=aac ab=$((channels*bitrate_aac))k"
  704. _ffmpeg_aac="$_ffmpeg_downmix -c:a aac -b:a $((channels*bitrate_aac))k"
  705. # container options
  706. _melt_stdout="-consumer avformat:pipe:1 f=yuv4mpegpipe $_melt_video pix_fmt=yuv420p an=1 audio_off=1"
  707. _ffmpeg_stdin="-f yuv4mpegpipe -i pipe:0$_ffmpeg_gpu_filter"
  708. _ffmpeg_rawvideo="-f rawvideo"
  709. _melt_wav="f=wav $_melt_pcm vn=1 video_off=1"
  710. _ffmpeg_wav_in="-f wav"
  711. _oggenc_wav_in=
  712. _opusenc_wav_in=
  713. _melt_matroska_pcm="f=matroska $_melt_downmix $_melt_pcm vn=1 video_off=1"
  714. _ffmpeg_matroska_pcm_in="-f matroska"
  715. _melt_ogv="f=ogg $_melt_video $_melt_theora ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  716. _ffmpeg_ogv="-f ogg $_ffmpeg_video $_ffmpeg_theora ${silence:+-an}${channels:+ $_ffmpeg_vorbis}"
  717. _ffmpeg_ogv_in="-f ogg"
  718. _ffmpeg_ogv_keepvideo="-f ogg $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_vorbis}"
  719. _ffmpeg_ogg_in="-f ogg"
  720. _ffmpeg_ogv_onlyvideo="-f ogg $_ffmpeg_video $_ffmpeg_theora -an"
  721. _ffmpeg_ogg_vorbis="-f ogg -vn $_ffmpeg_vorbis"
  722. _ffmpeg_ogg_opus="-f ogg -vn $_ffmpeg_opus"
  723. _melt_webm="f=webm $_melt_video $_melt_vp8 ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  724. _ffmpeg_webm="-f webm $_ffmpeg_video $_ffmpeg_vp8 ${silence:+-an}${channels:+ $_ffmpeg_vorbis}"
  725. _ffmpeg_webm_in="-f webm"
  726. _ffmpeg_webm_onlyvideo="-f webm $_ffmpeg_video $_ffmpeg_vp8 -an"
  727. _ffmpeg_webm_keepvideo="-f webm $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_vorbis}"
  728. _ffmpeg_webm_keepvideo_opus="-f webm $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_opus}"
  729. _melt_webm_vp9="f=webm $_melt_video $_melt_vp9 ${silence:+an=1 audio_off=1}${channels:+$_melt_opus}"
  730. _ffmpeg_webm_vp9="-f webm $_ffmpeg_video $_ffmpeg_vp9 ${silence:+-an}${channels:+$_ffmpeg_opus}"
  731. _ffmpeg_webm_vp9_onlyvideo="-f webm $_ffmpeg_video $_ffmpeg_vp9 -an"
  732. _melt_mp4="f=mp4 $_melt_video $_melt_avc ${silence:+an=1 audio_off=1}${channels:+${use_mp3:+$_melt_mp3}${use_aac:+$_melt_aac}}"
  733. _ffmpeg_mp4="-f mp4 $_ffmpeg_video $_ffmpeg_avc ${silence:+-an}${channels:+${use_mp3:+$_ffmpeg_mp3}${use_aac:+$_ffmpeg_aac}}"
  734. _ffmpeg_mp4_onlyvideo="-f mp4 $_ffmpeg_video $_ffmpeg_avc -an"
  735. _ffmpeg_mp4_keepvideo="-f mp4 $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_mp3}"
  736. _ffmpeg_mp4_keepvideo_aac="-f mp4 $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_aac}"
  737. _melt_img="f=image2 $_melt_video"
  738. _melt_loudness="$loudness_data"
  739. if [ -n "$loudness" ] && [ -z "$silence$_melt_loudness" ]; then
  740. echo >&2 "Analyzing audio dynamics..."
  741. render melt $_melt_infiles \
  742. $audioprefilters -filter loudness $filters \
  743. -consumer xml:$stem.xml video_off=1 all=1
  744. _melt_loudness="$(perl -n \
  745. -e 'm!<(property) name="results">([^<]+)</\1>! and print $2' \
  746. $stem.xml)"
  747. echo >&2 "Loudness data: $_melt_loudness"
  748. fi
  749. if [ -n "${ogv:+$multipass_theora}${webm:+$multipass_vp8}${webm_vp9:+$multipass_vp9}${mp4:+$multipass_avc}" ]; then
  750. echo >&2 "Analyzing video complexity..."
  751. render melt $_melt_infiles $filters \
  752. ${webm:+${use_melt_vp8:+-consumer avformat:$stem.webm $_melt_webm \
  753. pass=1 passlogfile=${stem}_vp8}} \
  754. ${webm_vp9:+${use_melt_vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9 \
  755. pass=1 passlogfile=${stem}_vp9}} \
  756. ${mp4:+${use_melt_avc:+-consumer avformat:$stem.mp4 $_melt_mp4 \
  757. pass=1 passlogfile=${stem}_avc}} \
  758. ${use_any_nomelt_video:+$_melt_stdout} \
  759. | pee \
  760. ${use_ffmpeg_any:+"$(render ffmpeg-chained-echo $_ffmpeg_stdin \
  761. ${ogv:+${use_ffmpeg_theora:+$_ffmpeg_rawvideo $_ffmpeg_ogv -an \
  762. -pass 1 -passlogfile ${stem}_theora /dev/null}} \
  763. ${webm:+${use_ffmpeg_vp8:+$_ffmpeg_rawvideo $_ffmpeg_vp8 -an \
  764. -pass 1 -passlogfile ${stem}_vp8 /dev/null}} \
  765. ${webm_vp9:+${use_ffmpeg_vp9:+$_ffmpeg_rawvideo $_ffmpeg_vp9 -an \
  766. -pass 1 -passlogfile ${stem}_vp9 /dev/null}} \
  767. ${mp4:+${use_ffmpeg_avc:+$_ffmpeg_rawvideo $_ffmpeg_avc -an \
  768. -pass 1 -passlogfile ${stem}_avc /dev/null}})"} \
  769. ${webm:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp8 \
  770. --pass=1 --fpf=${stem}_vp8.log -o /dev/null)"}} \
  771. ${webm_vp9:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp9 \
  772. --pass=1 --fpf=${stem}_vp9.log -o /dev/null)"}}
  773. fi
  774. echo >&2 "Encoding video${channels:+ and audio}..."
  775. render melt $_melt_infiles \
  776. ${channels:+$audioprefilters \
  777. ${_melt_loudness:+-filter loudness program=$lufs \
  778. results="$_melt_loudness"}} \
  779. $filters${channels:+ $_melt_postfilters_audio} \
  780. ${ogv:+${use_melt_theora:+-consumer avformat:$stem.ogv $_melt_ogv \
  781. ${multipass_theora:+pass=2 passlogfile=${stem}_theora}}} \
  782. ${webm:+${use_melt_vp8:+-consumer avformat:$stem.webm $_melt_webm \
  783. ${multipass_vp8:+pass=2 passlogfile=${stem}_vp8}}} \
  784. ${webm_vp9:+${use_melt_vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9 \
  785. ${multipass_vp9:+pass=2 passlogfile=${stem}_vp9}}} \
  786. ${mp4:+${use_melt_avc:+-consumer avformat:$stem.mp4 $_melt_mp4
  787. ${multipass_avc:+pass=2 passlogfile=${stem}_avc}}} \
  788. ${channels:+${use_wav:+-consumer avformat:$stem.wav \
  789. $_melt_wav}} \
  790. ${channels:+${use_matroska:+-consumer avformat:$stem.mkv \
  791. $_melt_matroska_pcm}} \
  792. ${use_any_nomelt_video:+$_melt_stdout} \
  793. | pee \
  794. ${use_ffmpeg_any:+"$(render ffmpeg-chained-echo $_ffmpeg_stdin \
  795. ${ogv:+${use_ffmpeg_theora:+$_ffmpeg_ogv_onlyvideo \
  796. ${multipass_theora:+-pass 2 -passlogfile ${stem}_theora} \
  797. ${stem}${channels:+_silent}.ogv}} \
  798. ${webm:+${use_ffmpeg_vp8:+$_ffmpeg_webm_onlyvideo \
  799. ${multipass_vp8:+-pass 2 -passlogfile ${stem}_vp8} \
  800. ${stem}${channels:+_silent}.webm}} \
  801. ${webm_vp9:+${use_ffmpeg_vp9:+$_ffmpeg_webm_vp9_onlyvideo \
  802. ${multipass_vp9:+-pass 2 -passlogfile ${stem}_vp9} \
  803. ${stem}_vp9${channels:+_silent}.webm}} \
  804. ${mp4:+${use_ffmpeg_avc:+$_ffmpeg_mp4_onlyvideo \
  805. ${multipass_avc:+-pass 2 -passlogfile ${stem}_avc} \
  806. ${stem}${channels:+_silent}.mp4}})"} \
  807. ${webm:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp8 \
  808. ${multipass_vp8:+--pass=2 --fpf=${stem}_vp8.log} \
  809. -o ${stem}${channels:+_silent}.webm)"}} \
  810. ${webm_vp9:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp9 \
  811. ${multipass_vp9:+--pass=2 --fpf=${stem}_vp9.log} \
  812. -o ${stem}_vp9${channels:+_silent}.webm)"}}
  813. if [ -n "${ogv:+$use_oggenc$use_ffmpeg_theora}${webm:+$use_vpxenc$use_oggenc}" ] && [ -n "$channels" ]; then
  814. echo >&2 "Encoding Vorbis audio and muxing with video..."
  815. # TODO: Encode once, muxing to containers as needed
  816. if [ -n "$use_oggenc" ]; then
  817. render oggenc $_oggenc_wav_in $_oggenc_vorbis -o - $stem.wav \
  818. | render ffmpeg-chained \
  819. ${ogv:+${use_oggenc:+$_ffmpeg_ogv_in -i ${stem}_silent.ogv \
  820. $_ffmpeg_ogg_in -i pipe:0 \
  821. $_ffmpeg_ogv_keepvideo $stem.ogv}} \
  822. ${webm:+${use_oggenc:+$_ffmpeg_webm_in -i ${stem}_silent.webm \
  823. $_ffmpeg_ogg_in -i pipe:0 \
  824. $_ffmpeg_webm_keepvideo $stem.webm}}
  825. fi
  826. if [ -n "${ogv:+$use_ffmpeg_theora}" ]; then
  827. render ffmpeg $_ffmpeg_ogv_in -i ${stem}_silent.ogv \
  828. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  829. $_ffmpeg_ogv_keepvideo ${stem}.ogv
  830. fi
  831. if [ -n "${webm:+$use_vpxenc}" ]; then
  832. render ffmpeg $_ffmpeg_webm_in -i ${stem}_silent.webm \
  833. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  834. $_ffmpeg_webm_keepvideo ${stem}.webm
  835. fi
  836. fi
  837. if [ -n "${webm_vp9:+$use_vpxenc$use_opusenc}" ] && [ -n "$channels" ]; then
  838. echo >&2 "Encoding Opus audio and muxing with VP9 video..."
  839. if [ -n "$use_opusenc" ]; then
  840. render opusenc $_opusenc_wav_in $_opusenc_opus -o - $stem.wav \
  841. | render ffmpeg-chained \
  842. $_ffmpeg_webm_in -i ${stem}_vp9_silent.webm \
  843. $_ffmpeg_ogg_in -i pipe:0 \
  844. $_ffmpeg_webm_keepvideo_opus ${stem}_vp9.webm
  845. else
  846. render ffmpeg $_ffmpeg_webm_in -i ${stem}_vp9_silent.webm \
  847. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  848. $_ffmpeg_webm_keepvideo_opus ${stem}_vp9.webm
  849. fi
  850. fi
  851. if [ -n "${mp4:+$use_ffmpeg_avc}" ] && [ -n "$channels" ]; then
  852. if [ -n "$use_mp3" ]; then
  853. echo "Encoding MP3 audio and muxing with H.264/AVC video..."
  854. render ffmpeg $_ffmpeg_mp4_in -i ${stem}_silent.mp4 \
  855. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  856. $_ffmpeg_mp4_keepvideo ${stem}.mp4
  857. else
  858. echo "Encoding AAC audio and muxing with H.264/AVC video..."
  859. render ffmpeg $_ffmpeg_mp4_in -i ${stem}_silent.mp4 \
  860. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  861. $_ffmpeg_mp4_keepvideo_aac ${stem}.mp4
  862. fi
  863. fi
  864. # cleanup encoding cruft
  865. rm -f $stem.xml $stem.wav $stem.mkv ${stem}_*.log ${stem}_*silent.webm
  866. # JPEG preview
  867. render melt $infile_first in=$stillframe out=$stillframe \
  868. -group $filters \
  869. -consumer avformat:$stem.jpg $_melt_img
  870. __width="${_width:+ width=\"$_width\"}"
  871. __height="${_height:+ height=\"$_height\"}"
  872. # Flash object needs extra space for controllers
  873. __heightplus=${_height:+ height=\"$((_height+4))\"}
  874. _source_ogv="<source src=\"$stem.ogv\" type='video/ogg; codecs=\"theora, vorbis\"' />"
  875. _source_webm="<source src=\"$stem.webm\" type='video/webm; codecs=\"vp8, vorbis\"' />"
  876. _source_webm_vp9="<source src=\"${stem}_vp9.webm\" type='video/webm; codecs=\"vp9, opus\"' />"
  877. # TODO: add type, with h.264 level inspected after rendering
  878. # <https://stackoverflow.com/questions/16363167/html5-video-tag-codecs-attribute#answer-16365526>
  879. _source_mp4="<source src=\"$stem.mp4\" type=\"video/mp4\" />"
  880. [ -z "$flashplayer" ] || flash=yes
  881. [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled."
  882. _object_flash="<object$__width$__heightplus type=\"application/x-shockwave-flash\" data=\"$flashplayer.swf\">"
  883. _param_name="<param name=\"movie\" value=\"$flashplayer.swf\" />"
  884. _param_flashvars="<param name=\"flashvars\" value=\"image=$stem.jpg&amp;file=$stem.mp4\" />"
  885. __ogvfile=${ogv:+open format <a href=\"$stem.ogv\">Ogg/Thera</a>}
  886. __webmfile=${webm:+open format <a href=\"$stem.webm\">WebM/VP8</a>}
  887. __webm_vp9_file=${vp9:+open format <a href=\"${stem}_vp9.webm\">WebM/VP9</a>}
  888. __mp4file=${mp4:+closed format <a href=\"$stem.mp4\">MPEG-4/AVC</a>}
  889. cat >"$stem.html" <<EOF
  890. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  891. <video$__width$__height preload controls>
  892. ${mp4:+$_source_mp4
  893. }${webm_vp9:+$_source_webm_vp9
  894. }${webm:+$_source_webm
  895. }${ogv:+$_source_ogv
  896. }${flash:+$_object_flash
  897. $_param_name
  898. $_param_flashvars
  899. }<img src="$stem.jpg"$__width$__height alt="$title"
  900. title="No video playback capabilities, please download the video below" />
  901. ${flash:+</object>
  902. }</video>
  903. <p><strong>Download Video:</strong><ul>
  904. ${webm_vp9:+<li>$__webm_vp9_file
  905. }${webm:+<li>$__webmfile
  906. }${ogv:+<li>$__ogvfile
  907. }${mp4:+<li>$__mp4file
  908. }</ul></p>
  909. EOF