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