summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 56326857974f630a9f64a10edc790bac7fa91e4f (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 VP8 Vorbis
  125. vp9 webm WebM VP9 Opus
  126. avc h264 mp4 MPEG-4 H.264/AVC AAC-LC
  127. (default: 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=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. # * establish "Bits Per Pixel" and define as refbpp
  203. # currently used: 0.12 (TODO: seems too high - ancient reference)
  204. # 4) render misc. frame formats (esp. modulus 16) with --compression=quality
  205. # * establish and define curve formula
  206. # currently used: "rule of .75" (TODO: seems slightly too high)
  207. # 5) render misc. frame formats with --compression=normal
  208. # * verify that bpp derives from refbpp as expected
  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 at misc --quality
  215. # * establish quality similar to reference encoding, and define as q
  216. # 3) render misc. frame formats (esp. modulus 16) with --compression=quality
  217. # * establish the "Bjontegaard" bitrate distance, and define as b_dbr
  218. # <https://streaminglearningcenter.com/encoding/compute-bd-rate-functions.html>
  219. # * establish and define bpp curve
  220. # 4) render misc. frame formats with --compression=normal
  221. # * verify that content quality is comparable to reference encoding
  222. # * verify that bpp derives from refbpp as expected
  223. # 5) optionally pick dirty/hq settings
  224. # * check that --compression=quality has similar filesize as normal
  225. # Theora seems to compress ~100% worse than H.264
  226. # (reportedly 76% worse in 2010, and libtheora has not improved since)
  227. # <http://compression.ru/video/codec_comparison/h264_2010/>
  228. b_dbr_theora=100
  229. # VP8 is rumored to compress ~20% worse than H.264
  230. b_dbr_vp8=20
  231. # VP9 compresses ~30% better than H.264 for practical use-cases
  232. # <https://engineering.fb.com/2018/04/10/video-engineering/av1-beats-x264-and-libvpx-vp9-in-practical-use-case/>
  233. b_dbr_vp9=-30
  234. # Mimic ReplayGain level when loudness is enabled (EBU R128: -23LUFS)
  235. # <https://auphonic.com/blog/2013/01/07/loudness-targets-mobile-audio-podcasts-radio-tv/>
  236. lufs=-18
  237. # parse cmdline options
  238. 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."
  239. eval set -- "$TEMP"
  240. while true ; do
  241. case "$1" in
  242. -h|--help) showhelp; exit;;
  243. -p|--profile) profile="$2"; shift 2;;
  244. -s|--size) size="$2"; shift 2;;
  245. -a|--aspect) aspect="$2"; shift 2;;
  246. -r|--rate) framerate="$2"; shift 2;;
  247. --video) video="$2"; shift 2;;
  248. --refbpp) refbpp="$2"; shift 2;;
  249. --formats) formats="$2"; shift 2;;
  250. --audio) audio="$2"; shift 2;;
  251. --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;;
  252. --loudness) loudness=yes; shift;;
  253. --loudness-data) loudness=yes; loudness_data="$2"; shift 2;;
  254. --filter) filters="${filters:+$filters }-filter $2"; shift 2;;
  255. --stem) stem="$2"; shift 2;;
  256. -t|--title) title="$2"; shift 2;;
  257. --still-frame) stillframe="$2"; shift 2;;
  258. --sample) sample=yes; shift;;
  259. --sample-start) sample=yes; samplestart="$2"; shift 2;;
  260. --sample-length) sample=yes; samplelength="$2"; shift 2;;
  261. --compression) compression="$2"; shift 2;;
  262. --quality) quality="$2"; shift 2;;
  263. --gpu) gpu="$2"; shift 2;;
  264. --gpudevice) gpudevice="$2"; shift 2;;
  265. --) shift; break;;
  266. *) exit1 "Internal error resolving options.";;
  267. esac
  268. done
  269. [ -n "$sample" ] || notsample=yes
  270. # Resolve if system has many CPU cores
  271. processors=$(nproc)
  272. [ $processors -gt 2 ] || processors=
  273. # sanitize infiles
  274. infiles=$*
  275. infile_first=$(perl -e 'print shift @ARGV' $infiles)
  276. [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!"
  277. # resolve stem and title (if not explicitly set)
  278. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  279. title=${title:-$stem}
  280. #use_ffmpeg_theora=yes
  281. #use_ffmpeg_vp8=yes
  282. #use_ffmpeg_vp9=yes
  283. #use_ffmpeg_avc=yes
  284. #use_ffmpeg2theora=yes # TODO...
  285. #use_vpxenc=yes
  286. # Avoid discrete audio encoders if possible: may cause sync problems
  287. #use_oggenc=yes
  288. #use_opusenc=yes
  289. # Avoid AAC: Lame arguably better than any free AAC encoder
  290. # Argument for AAC: Safari possibly lacks mp3 support in video container
  291. #use_mp3=yes
  292. [ -n "$use_mp3" ] || use_aac=yes
  293. # Avoid MP3 VBR but seems ABR is fine: Set to CBR for strict correctnes
  294. #use_lame_cbr=yes
  295. [ -n "$use_lame_cbr" ] || use_lame_abr=yes
  296. # resolve quality/speed hints
  297. qkey_theora=q
  298. qkey_vp8=crf
  299. qkey_vp9=crf
  300. qkey_avc=crf
  301. q_theora=6
  302. q_vp8=33
  303. cpu_vp8=2
  304. q_vp9=33
  305. cpu_vp9=2
  306. q_avc=23
  307. preset_avc=medium
  308. profile_vp8=0
  309. profile_vp9=0
  310. profile_avc=main
  311. case "$compression" in
  312. normal) :;;
  313. dirty)
  314. gpu=${gpu:-auto}
  315. cpu_vp8=4
  316. cpu_vp9=4
  317. preset_avc=veryfast
  318. ;;
  319. hq)
  320. multipass=yes
  321. no_q_theora=yes
  322. no_multipass_avc=yes
  323. cpu_vp8=0
  324. cpu_vp9=1
  325. # preset_avc=veryslow # faster than medium unneeded as of June 2020
  326. ;;
  327. exact)
  328. multipass=yes
  329. no_multipass_avc=yes
  330. ;;
  331. quality)
  332. no_bitrate=yes
  333. ;;
  334. *) exit1 "Unknown compression mode \"$compression\".";;
  335. esac
  336. case "$quality" in
  337. auto|'') :;;
  338. none) no_q=yes;;
  339. *) q=$quality;;
  340. esac
  341. gpudevice=${gpudevice:-/dev/dri/renderD128}
  342. if [ auto = "$gpu" ]; then
  343. vainfo=$(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'Driver version:\s*\K.*')
  344. case "$vainfo" in
  345. '') gpu=;;
  346. *)
  347. for vformat in theora vp8 vp9 avc; do
  348. eval "no_gpu_decoder_$vformat=yes"
  349. eval "no_gpu_encoder_$vformat=yes"
  350. done
  351. case $(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'VAProfile\S+\s*:\s*VAEntrypointVLD') in
  352. *VAProfileVP8Version0_3*) no_gpu_decoder_vp8=;;
  353. *VAProfileVP9Profile0*) no_gpu_decoder_vp9=;;
  354. *VAProfileH264Main*) no_gpu_decoder_avc=;;
  355. esac
  356. case $(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'VAProfile\S+\s*:\s*VAEntrypointEncSlice') in
  357. *VAProfileVP8Version0_3*) no_gpu_encoder_vp8=;;
  358. *VAProfileVP9Profile0*) no_gpu_encoder_vp9=;;
  359. *VAProfileH264Main*) no_gpu_encoder_avc=;;
  360. esac
  361. if [ -n "$no_gpu_decoder_vp8$no_gpu_decoder_vp9$no_gpu_decoder_avc" ]; then
  362. case "$vainfo" in
  363. # TODO *"Intel iHD driver"*) gpu=qsv;;
  364. *) gpu=vaapi;;
  365. esac
  366. fi
  367. esac
  368. fi
  369. case "$gpu" in
  370. none|'') gpu=;;
  371. qsv)
  372. multipass=
  373. preset_vp8=medium
  374. preset_vp9=medium
  375. preset_avc=
  376. qkey_avc=qp
  377. ;;
  378. vaapi)
  379. multipass=
  380. preset_avc=
  381. qkey_avc=qp
  382. case "$compression" in
  383. dirty)
  384. preset_avc=
  385. ;;
  386. esac
  387. ;;
  388. *) exit1 "Unknown GPU type \"$gpu\".";;
  389. esac
  390. [ -z "$gpu" ] && no_gpu=yes || eval "gpu_$gpu=yes"
  391. # parse/resolve size and framerate
  392. case "$profile" in
  393. '') :;;
  394. *@*)
  395. while read s r foo; do
  396. profilesize="${size:-$s}"
  397. framerate="${framerate:-$r}"
  398. done << EOF
  399. $(echo "$profile" | perl -F@ -anE 'say join " ", @F')
  400. EOF
  401. ;;
  402. *p*)
  403. while read s r foo; do
  404. profilesize="${size:-${s}p}"
  405. framerate="${framerate:-$r}"
  406. done << EOF
  407. $(echo "$profile" | perl -Fp -anE 'say join " ", @F')
  408. EOF
  409. ;;
  410. *)
  411. profilesize="$profile"
  412. ;;
  413. esac
  414. size=${size:-$profilesize}
  415. case "$size" in
  416. qvga) size=320x240;;
  417. hvga) size=480x360;;
  418. vga) size=640x480;;
  419. svga) size=800x600;;
  420. xga) size=1024x768;;
  421. 240p|wqvga) size=424x240;;
  422. 360p|nhd) size=640x360;;
  423. 480p|wvga) size=848x480;;
  424. 576p|wsvga) size=1024x576;;
  425. 720p|wxga|hd) size=1280x720;;
  426. esac
  427. if [ -n "$size" ]; then
  428. while read w h foo; do
  429. width="${width:-$w}"
  430. height="${height:-$h}"
  431. done << EOF
  432. $(echo "$size" | perl -Fx -anE 'say join " ", @F')
  433. EOF
  434. if [ -z "$width" ] || [ -z "$height" ]; then
  435. exit1 "Failed to parse size \"$size\"."
  436. fi
  437. fi
  438. case "$framerate" in
  439. */*)
  440. while read n d foo; do
  441. framerate_num="${framerate_num:-$n}"
  442. framerate_den="${framerate_den:-$d}"
  443. done << EOF
  444. $(echo "$framerate" | perl -F/ -anE 'say join " ", @F')
  445. EOF
  446. ;;
  447. ?*)
  448. framerate_num="$framerate"
  449. framerate_den=1
  450. ;;
  451. esac
  452. # resolve input size and framerate (needed for computing bitrate)
  453. while read w h r s foo; do
  454. width_in="${width_in:-$w}"
  455. height_in="${height_in:-$h}"
  456. framerate_in="${framerate_in:-$r}"
  457. scantype_in="${scantype_in:-$s}"
  458. done << EOF
  459. $(mediainfo --Inform="Video;%Width% %Height% %FrameRate% %ScanType%" \
  460. "$infile_first")
  461. EOF
  462. [ Progressive = "$scantype_in" ] || do_deinterlace=yes
  463. case "$video" in
  464. talkinghead)
  465. refbpp="${refbpp:-0.1}"
  466. x264tune=film
  467. ;;
  468. action)
  469. refbpp="${refbpp:-0.15}"
  470. x264tune=film
  471. ;;
  472. '')
  473. refbpp="${refbpp:-0.12}"
  474. ;;
  475. *) exit1 "Unknown video style \"$video\".";;
  476. esac
  477. for format in $(echo "$formats" | sed -e 's/,/ /g'); do
  478. case $format in
  479. theora|ogg) ogv=yes;;
  480. avc|h264|mp4) mp4=yes;;
  481. vp8) webm=yes;;
  482. vp9|webm) webm_vp9=yes;;
  483. *) exit1 "Unknown format \"$format\".";;
  484. esac
  485. done
  486. _width="${width:-$width_in}"
  487. _height="${height:-$height_in}"
  488. if [ -n "$_width" ] && [ -n "$_height" ]; then
  489. _pixels="$((_width*_height))"
  490. fi
  491. _frames="${framerate:-$framerate_in}"
  492. qmin_vp8=0
  493. qmin_vp9=0
  494. qmax_vp8=63
  495. qmax_vp9=63
  496. lag_vp8=16
  497. lag_vp9=16
  498. tokenparts_log2_vp8=0
  499. tokenparts_log2_vp9=0
  500. if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then
  501. qmin_vp8=11
  502. qmin_vp9=11
  503. qmax_vp8=51
  504. qmax_vp9=51
  505. tokenparts_log2_vp8=2
  506. tokenparts_log2_vp9=2
  507. if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then
  508. lag_vp8=25
  509. lag_vp9=25
  510. profile_vp8=1
  511. profile_vp9=1
  512. fi
  513. fi
  514. # compute average bitrate from reference data and "power of .75" rule
  515. if [ -n "$_pixels" ] && [ -n "$_frames" ]; then
  516. bitrate_avc=$(perl -E '$refsize=640*360;' \
  517. -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )")
  518. bitrate_theora=$(perl -E "say int( +($bitrate_avc*(100+($b_dbr_theora))/100) )") #"
  519. bitrate_vp8=$(perl -E "say int( +($bitrate_avc*(100+($b_dbr_vp8))/100) )") #"
  520. bitrate_vp9=$(perl -E "say int( +($bitrate_avc*(100+($b_dbr_vp9))/100) )") #"
  521. fi
  522. # default per-codec-channel bitrates
  523. quality_vorbis=3
  524. bitrate_opus=48
  525. quality_lame=6
  526. bitrate_lame=64
  527. bitrate_aac=64
  528. case "$audio" in
  529. music)
  530. opusapp=audio
  531. ;;
  532. speech)
  533. mono=yes
  534. quality_vorbis=2
  535. bitrate_opus=32
  536. quality_lame=7
  537. bitrate_lame=48
  538. opusapp=voip
  539. ;;
  540. silence)
  541. silence=yes
  542. ;;
  543. '')
  544. :
  545. ;;
  546. *) exit1 "Unknown audio style \"$audio\".";;
  547. esac
  548. which ffprobe > /dev/null && ffprobe=ffprobe || ffprobe=avprobe
  549. channels=$($ffprobe -v warning -show_streams "$infile_first" \
  550. | perl -ne 's/channels=// and print $_' || echo -1)
  551. # adapt channel count and flags to reflect actual downmix/silence need
  552. if [ -n "$stereo" ]; then
  553. if [ $channels -gt 2 ]; then
  554. channels=2
  555. else
  556. stereo=
  557. fi
  558. elif [ -n "$mono" ]; then
  559. if [ $channels -gt 1 ]; then
  560. channels=1
  561. else
  562. mono=
  563. fi
  564. elif [ -n "$silence" ]; then
  565. if [ $channels -gt 0 ]; then
  566. channels=0
  567. else
  568. silence=
  569. fi
  570. fi
  571. [ -z "$silence" ] || channels=
  572. if [ -n "$stereo$mono" ]; then
  573. # melt cannot downmix with (stereo?) filters applied (bug#763911)
  574. if [ -n "$audioprefilters$compress$loudness$limit$filters" ]; then
  575. if [ -z "${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}" ]; then
  576. downmix_ffmpeg=yes
  577. else
  578. downmix_oggenc=yes
  579. downmix_oggenc=yes
  580. fi
  581. else
  582. downmix_melt=yes
  583. fi
  584. fi
  585. encoder_vp8=libvpx
  586. encoder_vp9=libvpx-vp9
  587. encoder_avc=libx264
  588. for vformat in theora vp8 vp9 avc; do
  589. [ -z "$no_bitrate" ] || eval "no_bitrate_$vformat=yes"
  590. eval "[ -n \"$no_bitrate\$no_bitrate_$vformat\" ] || do_bitrate_$vformat=yes"
  591. eval "[ -z \"$no_q\$no_q_$vformat\" ] || q_$vformat="
  592. [ -z "$q" ] || eval "q_$vformat=$q"
  593. eval "[ -n \"\$q_$vformat\" ] || no_q_$vformat=yes"
  594. [ -z "$gpu" ] || eval "[ -n \"\$no_gpu_encoder_$vformat\" ] || encoder_$vformat=${vformat}_$gpu"
  595. [ avc = "$vformat" ] || eval "[ -n \"$use_vpxenc\$use_ffmpeg_$vformat\" ] || use_melt_$vformat=yes"
  596. [ -z "$multipass" ] || eval "[ -n \"\$no_multipass_$vformat\" ] || multipass_$vformat=yes"
  597. eval "[ -n \"\$multipass_$vformat\" ] || singlepass_$vformat=yes"
  598. done
  599. if [ -n "$use_ffmpeg_theora$use_ffmpeg_vp8$use_ffmpeg_vp9$use_vpxenc" ]; then
  600. [ -n "$use_oggenc" ] || use_ffmpeg_vorbis=yes
  601. [ -n "$use_opusenc" ] || use_ffmpeg_opus=yes
  602. [ -z "$use_oggenc$use_opusenc" ] || use_wav=yes
  603. [ -z "$use_ffmpeg_vorbis$use_ffmpeg_opus" ] || use_matroska=yes
  604. fi
  605. if [ -n "$use_ffmpeg_avc" ]; then
  606. use_matroska=yes
  607. else
  608. use_melt_avc=yes
  609. fi
  610. use_melt_any="${ogv:+$use_melt_theora}${webm:+$use_melt_vp8}${webm_vp9:+$use_melt_vp9}${mp4:+$use_melt_avc}"
  611. use_ffmpeg_any="${ogv:+$use_ffmpeg_theora}${webm:+$use_ffmpeg_vp8}${webm_vp9:+$use_ffmpeg_vp9}${mp4:+$use_ffmpeg_avc}"
  612. use_vpxenc_any="${webm:+$use_vpxenc}${webm_vp9:+$use_vpxenc}"
  613. use_opusenc_any="${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}"
  614. use_oggenc_any="${ogv:+$use_oggenc}${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}"
  615. [ -z "$use_ffmpeg_any$use_vpxenc_any" ] || use_any_nomelt_video=yes
  616. [ -z "$gpu" ] || echo >&2 "GPU platform enabled: $gpu"
  617. # gpu options
  618. _melt_gpu_init=${gpu:+ ${gpu}_device=$gpudevice}
  619. _ffmpeg_gpu_vaapi_init="-${gpu}_device $gpudevice"
  620. _ffmpeg_gpu_qsv_init="-init_hw_device qsv=hw:$gpudevice -filter_hw_device hw"
  621. _ffmpeg_gpu_init="${gpu_vaapi:+ ${_ffmpeg_gpu_vaapi_init}}${gpu_qsv:+ ${_ffmpeg_gpu_qsv_init}}"
  622. _ffmpeg_gpu_vaapi_filter="-vf 'format=nv12|$gpu,hwupload'"
  623. _ffmpeg_gpu_qsv_filter="-vf 'hwupload=extra_hw_frames=64,format=qsv'"
  624. _ffmpeg_gpu_filter="${gpu_vaapi:+ ${_ffmpeg_gpu_vaapi_filter}}${gpu_qsv:+ ${_ffmpeg_gpu_qsv_filter}}"
  625. # generic options
  626. which ffmpeg > /dev/null && ffmpeg=ffmpeg || ffmpeg=avconv
  627. _melt_infiles="${notsample:+$infiles}${sample:+$infile_first in=$((samplestart)) out=$((samplestart + samplelength))}"
  628. render() {
  629. engine=$1; shift
  630. chained=
  631. echo=
  632. unchained=yes
  633. case "$engine" in
  634. ffmpeg-chained) engine=ffmpeg; chained=yes;;
  635. vpxenc-chained) engine=vpxenc; chained=yes;;
  636. ffmpeg-chained-echo) engine=ffmpeg; chained=yes; echo=yes;;
  637. vpxenc-chained-echo) engine=vpxenc; chained=yes; echo=yes;;
  638. esac
  639. [ -z "$chained" ] || unchained=
  640. case "$engine" in
  641. melt)
  642. set -- -progress$_melt_gpu_init "$@"
  643. ;;
  644. ffmpeg)
  645. engine=$ffmpeg
  646. set -- -threads auto -y -hide_banner -v ${unchained:+info -stats}${chained:+warning}$_ffmpeg_gpu_init "$@"
  647. ;;
  648. vpxenc)
  649. set -- --quiet ${processors:+-t $((processors-1))} "$@"
  650. ;;
  651. oggenc)
  652. set -- "$@"
  653. ;;
  654. opusenc)
  655. set -- "$@"
  656. ;;
  657. *) exit1 "Unknown render engine \"$engine\".";;
  658. esac
  659. ${echo:+echo }"$engine" "$@"
  660. }
  661. # filter options
  662. _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}"
  663. _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}}"
  664. _melt_downmix="${downmix_melt:+ac=$channels}"
  665. _ffmpeg_downmix="${downmix_ffmpeg:+-ac $channels}"
  666. # FIXME: how to downmix to stereo?
  667. _oggenc_downmix="${downmix_oggenc:+${mono:+--downmix}}"
  668. _opusenc_downmix="${downmix_opusenc:+${stereo:+--downmix-stereo}${mono:+--downmix-mono}}"
  669. # limit (i.e. avoid peaks "clipping")
  670. _melt_postfilters_audio="${limit:+-filter ladspa.1077}"
  671. # format bitrate options
  672. _melt_bitrate_theora="${do_bitrate_theora:+ vb=$bitrate_theora}"
  673. _ffmpeg_bitrate_theora="${do_bitrate_theora:+ -b:v $bitrate_theora}"
  674. _melt_bitrate_vp8="${do_bitrate_vp8:+ vb=$bitrate_vp8 minrate=$((bitrate_vp8/20)) maxrate=$((bitrate_vp8*12))}"
  675. _ffmpeg_bitrate_vp8="${do_bitrate_vp8:+ -b:v $bitrate_vp8 -minrate $((bitrate_vp8/20)) -maxrate $((bitrate_vp8*12))}"
  676. _vpxenc_bitrate_vp8="${do_bitrate_vp8:+ --target-bitrate=$((bitrate_vp8/1000))${multipass_vp8:+ --minsection-pct=5 --maxsection-pct=1200}}"
  677. _melt_bitrate_vp9="${do_bitrate_vp9:+ vb=$bitrate_vp9 minrate=$((bitrate_vp9/20)) maxrate=$((bitrate_vp9*12))}"
  678. _ffmpeg_bitrate_vp9="${do_bitrate_vp9:+ -b:v $bitrate_vp9 -minrate $((bitrate_vp9/20)) -maxrate $((bitrate_vp9*12))}"
  679. _vpxenc_bitrate_vp9="${do_bitrate_vp9:+ --target-bitrate=$((bitrate_vp9/1000))${multipass_vp9:+ --minsection-pct=5 --maxsection-pct=1200}}"
  680. _melt_bitrate_avc="${do_bitrate_avc:+ maxrate=$bitrate_avc${no_gpu:+ bufsize=$((bitrate_avc*2))}}"
  681. _ffmpeg_bitrate_avc="${do_bitrate_avc:+ -maxrate $bitrate_avc${no_gpu:+ -bufsize $((bitrate_avc*2))}}"
  682. # format low-level options
  683. _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"
  684. _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"
  685. _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"
  686. _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"
  687. _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"
  688. _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"
  689. _melt_misc_avc="${no_gpu:+ movflags=+faststart}"
  690. _ffmpeg_misc_avc="${no_gpu:+ -movflags +faststart}"
  691. # format options
  692. # emulate VP8 Constant Quality using "loose" Constrained Quality
  693. _melt_theora="vcodec=libtheora$_melt_bitrate_theora${q_theora:+ $qkey_theora=$q_theora}"
  694. _ffmpeg_theora="-c:v libtheora$_ffmpeg_bitrate_theora${q_theora:+ -$qkey_theora:v $q_theora}"
  695. _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"
  696. _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"
  697. _vpxenc_vp8="--codec=vp8$_vpxenc_bitrate_vp8 --end-usage=${no_q_vp8:+vbr}${q_vp8:+cq --cq-level=$q_vp8}$_vpxenc_misc_vp8"
  698. _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"
  699. _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"
  700. _vpxenc_vp9="--codec=vp9$_vpxenc_bitrate_vp9 --end-usage=${no_q_vp9:+vbr}${q_vp9:+cq --cq-level=$q_vp9}$_vpxenc_misc_vp9"
  701. _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"
  702. _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"
  703. _melt_pcm="$_melt_downmix acodec=pcm_s16le"
  704. _melt_vorbis="$_melt_downmix acodec=libvorbis aq=$quality_vorbis"
  705. _ffmpeg_vorbis="$_ffmpeg_downmix -c:a libvorbis -q:a $quality_vorbis"
  706. _oggenc_vorbis="$_oggenc_downmix -q $quality_vorbis"
  707. _melt_opus="$_melt_downmix acodec=libopus ab=$((channels*bitrate_opus))k${opusapp:+ application=$opusapp}"
  708. _ffmpeg_opus="$_ffmpeg_downmix -c:a libopus -b:a $((channels*bitrate_opus))k${opusapp:+ -application $opusapp}"
  709. _opusenc_opus="$_opusenc_downmix --bitrate $((channels*bitrate_opus))"
  710. _melt_mp3="$_melt_downmix acodec=libmp3lame${use_lame_abr:+ aq=$quality_lame}${use_lame_cbr:+ ab=$((channels*bitrate_lame))k}"
  711. _ffmpeg_mp3="$_ffmpeg_downmix -c:a libmp3lame${use_lame_abr:+ -q:a $quality_lame}${use_lame_cbr:+ -b:a $((channels*bitrate_lame))k}"
  712. _melt_aac="$_melt_downmix acodec=aac ab=$((channels*bitrate_aac))k"
  713. _ffmpeg_aac="$_ffmpeg_downmix -c:a aac -b:a $((channels*bitrate_aac))k"
  714. # container options
  715. _melt_stdout="-consumer avformat:pipe:1 f=yuv4mpegpipe $_melt_video pix_fmt=yuv420p an=1 audio_off=1"
  716. _ffmpeg_stdin="-f yuv4mpegpipe -i pipe:0$_ffmpeg_gpu_filter"
  717. _ffmpeg_rawvideo="-f rawvideo"
  718. _melt_wav="f=wav $_melt_pcm vn=1 video_off=1"
  719. _ffmpeg_wav_in="-f wav"
  720. _oggenc_wav_in=
  721. _opusenc_wav_in=
  722. _melt_matroska_pcm="f=matroska $_melt_downmix $_melt_pcm vn=1 video_off=1"
  723. _ffmpeg_matroska_pcm_in="-f matroska"
  724. _melt_ogv="f=ogg $_melt_video $_melt_theora ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  725. _ffmpeg_ogv="-f ogg $_ffmpeg_video $_ffmpeg_theora ${silence:+-an}${channels:+ $_ffmpeg_vorbis}"
  726. _ffmpeg_ogv_in="-f ogg"
  727. _ffmpeg_ogv_keepvideo="-f ogg $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_vorbis}"
  728. _ffmpeg_ogg_in="-f ogg"
  729. _ffmpeg_ogv_onlyvideo="-f ogg $_ffmpeg_video $_ffmpeg_theora -an"
  730. _ffmpeg_ogg_vorbis="-f ogg -vn $_ffmpeg_vorbis"
  731. _ffmpeg_ogg_opus="-f ogg -vn $_ffmpeg_opus"
  732. _melt_webm="f=webm $_melt_video $_melt_vp8 ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  733. _ffmpeg_webm="-f webm $_ffmpeg_video $_ffmpeg_vp8 ${silence:+-an}${channels:+ $_ffmpeg_vorbis}"
  734. _ffmpeg_webm_in="-f webm"
  735. _ffmpeg_webm_onlyvideo="-f webm $_ffmpeg_video $_ffmpeg_vp8 -an"
  736. _ffmpeg_webm_keepvideo="-f webm $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_vorbis}"
  737. _ffmpeg_webm_keepvideo_opus="-f webm $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_opus}"
  738. _melt_webm_vp9="f=webm $_melt_video $_melt_vp9 ${silence:+an=1 audio_off=1}${channels:+$_melt_opus}"
  739. _ffmpeg_webm_vp9="-f webm $_ffmpeg_video $_ffmpeg_vp9 ${silence:+-an}${channels:+$_ffmpeg_opus}"
  740. _ffmpeg_webm_vp9_onlyvideo="-f webm $_ffmpeg_video $_ffmpeg_vp9 -an"
  741. _melt_mp4="f=mp4 $_melt_video $_melt_avc ${silence:+an=1 audio_off=1}${channels:+${use_mp3:+$_melt_mp3}${use_aac:+$_melt_aac}}"
  742. _ffmpeg_mp4="-f mp4 $_ffmpeg_video $_ffmpeg_avc ${silence:+-an}${channels:+${use_mp3:+$_ffmpeg_mp3}${use_aac:+$_ffmpeg_aac}}"
  743. _ffmpeg_mp4_onlyvideo="-f mp4 $_ffmpeg_video $_ffmpeg_avc -an"
  744. _ffmpeg_mp4_keepvideo="-f mp4 $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_mp3}"
  745. _ffmpeg_mp4_keepvideo_aac="-f mp4 $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_aac}"
  746. _melt_img="f=image2 $_melt_video"
  747. _melt_loudness="$loudness_data"
  748. if [ -n "$loudness" ] && [ -z "$silence$_melt_loudness" ]; then
  749. echo >&2 "Analyzing audio dynamics..."
  750. render melt $_melt_infiles \
  751. $audioprefilters -filter loudness $filters \
  752. -consumer xml:$stem.xml video_off=1 all=1
  753. _melt_loudness="$(perl -n \
  754. -e 'm!<(property) name="results">([^<]+)</\1>! and print $2' \
  755. $stem.xml)"
  756. echo >&2 "Loudness data: $_melt_loudness"
  757. fi
  758. if [ -n "${ogv:+$multipass_theora}${webm:+$multipass_vp8}${webm_vp9:+$multipass_vp9}${mp4:+$multipass_avc}" ]; then
  759. echo >&2 "Analyzing video complexity..."
  760. render melt $_melt_infiles $filters \
  761. ${ogv:+${use_melt_theora:+-consumer avformat:$stem.ogv $_melt_ogv \
  762. pass=1 passlogfile=${stem}_theora}} \
  763. ${webm:+${use_melt_vp8:+-consumer avformat:$stem.webm $_melt_webm \
  764. pass=1 cpu-used=4 passlogfile=${stem}_vp8}} \
  765. ${webm_vp9:+${use_melt_vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9 \
  766. pass=1 cpu-used=4 passlogfile=${stem}_vp9}} \
  767. ${mp4:+${use_melt_avc:+-consumer avformat:$stem.mp4 $_melt_mp4 \
  768. pass=1 passlogfile=${stem}_avc}} \
  769. ${use_any_nomelt_video:+$_melt_stdout} \
  770. | pee \
  771. ${use_ffmpeg_any:+"$(render ffmpeg-chained-echo $_ffmpeg_stdin \
  772. ${ogv:+${use_ffmpeg_theora:+$_ffmpeg_rawvideo $_ffmpeg_ogv -an \
  773. -pass 1 -passlogfile ${stem}_theora /dev/null}} \
  774. ${webm:+${use_ffmpeg_vp8:+$_ffmpeg_rawvideo $_ffmpeg_vp8 -an \
  775. -pass 1 -cpu-used 4 -passlogfile ${stem}_vp8 /dev/null}} \
  776. ${webm_vp9:+${use_ffmpeg_vp9:+$_ffmpeg_rawvideo $_ffmpeg_vp9 -an \
  777. -pass 1 -cpu-used 4 -passlogfile ${stem}_vp9 /dev/null}} \
  778. ${mp4:+${use_ffmpeg_avc:+$_ffmpeg_rawvideo $_ffmpeg_avc -an \
  779. -pass 1 -passlogfile ${stem}_avc /dev/null}})"} \
  780. ${webm:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp8 \
  781. --pass=1 --cpu-used=4 --fpf=${stem}_vp8.log -o /dev/null)"}} \
  782. ${webm_vp9:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp9 \
  783. --pass=1 --cpu-used=4 --fpf=${stem}_vp9.log -o /dev/null)"}}
  784. fi
  785. echo >&2 "Encoding video${channels:+ and audio}..."
  786. render melt $_melt_infiles \
  787. ${channels:+$audioprefilters \
  788. ${_melt_loudness:+-filter loudness program=$lufs \
  789. results="$_melt_loudness"}} \
  790. $filters${channels:+ $_melt_postfilters_audio} \
  791. ${ogv:+${use_melt_theora:+-consumer avformat:$stem.ogv $_melt_ogv \
  792. ${multipass_theora:+pass=2 passlogfile=${stem}_theora}}} \
  793. ${webm:+${use_melt_vp8:+-consumer avformat:$stem.webm $_melt_webm \
  794. ${multipass_vp8:+pass=2 passlogfile=${stem}_vp8}}} \
  795. ${webm_vp9:+${use_melt_vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9 \
  796. ${multipass_vp9:+pass=2 passlogfile=${stem}_vp9}}} \
  797. ${mp4:+${use_melt_avc:+-consumer avformat:$stem.mp4 $_melt_mp4
  798. ${multipass_avc:+pass=2 passlogfile=${stem}_avc}}} \
  799. ${channels:+${use_wav:+-consumer avformat:$stem.wav \
  800. $_melt_wav}} \
  801. ${channels:+${use_matroska:+-consumer avformat:$stem.mkv \
  802. $_melt_matroska_pcm}} \
  803. ${use_any_nomelt_video:+$_melt_stdout} \
  804. | pee \
  805. ${use_ffmpeg_any:+"$(render ffmpeg-chained-echo $_ffmpeg_stdin \
  806. ${ogv:+${use_ffmpeg_theora:+$_ffmpeg_ogv_onlyvideo \
  807. ${multipass_theora:+-pass 2 -passlogfile ${stem}_theora} \
  808. ${stem}${channels:+_silent}.ogv}} \
  809. ${webm:+${use_ffmpeg_vp8:+$_ffmpeg_webm_onlyvideo \
  810. ${multipass_vp8:+-pass 2 -passlogfile ${stem}_vp8} \
  811. ${stem}${channels:+_silent}.webm}} \
  812. ${webm_vp9:+${use_ffmpeg_vp9:+$_ffmpeg_webm_vp9_onlyvideo \
  813. ${multipass_vp9:+-pass 2 -passlogfile ${stem}_vp9} \
  814. ${stem}_vp9${channels:+_silent}.webm}} \
  815. ${mp4:+${use_ffmpeg_avc:+$_ffmpeg_mp4_onlyvideo \
  816. ${multipass_avc:+-pass 2 -passlogfile ${stem}_avc} \
  817. ${stem}${channels:+_silent}.mp4}})"} \
  818. ${webm:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp8 \
  819. ${multipass_vp8:+--pass=2 --fpf=${stem}_vp8.log} \
  820. -o ${stem}${channels:+_silent}.webm)"}} \
  821. ${webm_vp9:+${use_vpxenc:+"$(render vpxenc-chained-echo - $_vpxenc_vp9 \
  822. ${multipass_vp9:+--pass=2 --fpf=${stem}_vp9.log} \
  823. -o ${stem}_vp9${channels:+_silent}.webm)"}}
  824. if [ -n "${ogv:+$use_oggenc$use_ffmpeg_theora}${webm:+$use_vpxenc$use_oggenc}" ] && [ -n "$channels" ]; then
  825. echo >&2 "Encoding Vorbis audio and muxing with video..."
  826. # TODO: Encode once, muxing to containers as needed
  827. if [ -n "$use_oggenc" ]; then
  828. render oggenc $_oggenc_wav_in $_oggenc_vorbis -o - $stem.wav \
  829. | render ffmpeg-chained \
  830. ${ogv:+${use_oggenc:+$_ffmpeg_ogv_in -i ${stem}_silent.ogv \
  831. $_ffmpeg_ogg_in -i pipe:0 \
  832. $_ffmpeg_ogv_keepvideo $stem.ogv}} \
  833. ${webm:+${use_oggenc:+$_ffmpeg_webm_in -i ${stem}_silent.webm \
  834. $_ffmpeg_ogg_in -i pipe:0 \
  835. $_ffmpeg_webm_keepvideo $stem.webm}}
  836. fi
  837. if [ -n "${ogv:+$use_ffmpeg_theora}" ]; then
  838. render ffmpeg $_ffmpeg_ogv_in -i ${stem}_silent.ogv \
  839. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  840. $_ffmpeg_ogv_keepvideo ${stem}.ogv
  841. fi
  842. if [ -n "${webm:+$use_vpxenc}" ]; then
  843. render ffmpeg $_ffmpeg_webm_in -i ${stem}_silent.webm \
  844. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  845. $_ffmpeg_webm_keepvideo ${stem}.webm
  846. fi
  847. fi
  848. if [ -n "${webm_vp9:+$use_vpxenc$use_opusenc}" ] && [ -n "$channels" ]; then
  849. echo >&2 "Encoding Opus audio and muxing with VP9 video..."
  850. if [ -n "$use_opusenc" ]; then
  851. render opusenc $_opusenc_wav_in $_opusenc_opus -o - $stem.wav \
  852. | render ffmpeg-chained \
  853. $_ffmpeg_webm_in -i ${stem}_vp9_silent.webm \
  854. $_ffmpeg_ogg_in -i pipe:0 \
  855. $_ffmpeg_webm_keepvideo_opus ${stem}_vp9.webm
  856. else
  857. render ffmpeg $_ffmpeg_webm_in -i ${stem}_vp9_silent.webm \
  858. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  859. $_ffmpeg_webm_keepvideo_opus ${stem}_vp9.webm
  860. fi
  861. fi
  862. if [ -n "${mp4:+$use_ffmpeg_avc}" ] && [ -n "$channels" ]; then
  863. if [ -n "$use_mp3" ]; then
  864. echo "Encoding MP3 audio and muxing with H.264/AVC video..."
  865. render ffmpeg $_ffmpeg_mp4_in -i ${stem}_silent.mp4 \
  866. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  867. $_ffmpeg_mp4_keepvideo ${stem}.mp4
  868. else
  869. echo "Encoding AAC audio and muxing with H.264/AVC video..."
  870. render ffmpeg $_ffmpeg_mp4_in -i ${stem}_silent.mp4 \
  871. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  872. $_ffmpeg_mp4_keepvideo_aac ${stem}.mp4
  873. fi
  874. fi
  875. # cleanup encoding cruft
  876. rm -f $stem.xml $stem.wav $stem.mkv ${stem}_*.log ${stem}_*silent.webm
  877. # JPEG preview
  878. render melt $infile_first in=$stillframe out=$stillframe \
  879. -group $filters \
  880. -consumer avformat:$stem.jpg $_melt_img
  881. __width="${_width:+ width=\"$_width\"}"
  882. __height="${_height:+ height=\"$_height\"}"
  883. # Flash object needs extra space for controllers
  884. __heightplus=${_height:+ height=\"$((_height+4))\"}
  885. _source_ogv="<source src=\"$stem.ogv\" type='video/ogg; codecs=\"theora, vorbis\"' />"
  886. _source_webm="<source src=\"$stem.webm\" type='video/webm; codecs=\"vp8, vorbis\"' />"
  887. _source_webm_vp9="<source src=\"${stem}_vp9.webm\" type='video/webm; codecs=\"vp9, opus\"' />"
  888. # TODO: add type, with h.264 level inspected after rendering
  889. # <https://stackoverflow.com/questions/16363167/html5-video-tag-codecs-attribute#answer-16365526>
  890. _source_mp4="<source src=\"$stem.mp4\" type=\"video/mp4\" />"
  891. [ -z "$flashplayer" ] || flash=yes
  892. [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled."
  893. _object_flash="<object$__width$__heightplus type=\"application/x-shockwave-flash\" data=\"$flashplayer.swf\">"
  894. _param_name="<param name=\"movie\" value=\"$flashplayer.swf\" />"
  895. _param_flashvars="<param name=\"flashvars\" value=\"image=$stem.jpg&amp;file=$stem.mp4\" />"
  896. __ogvfile=${ogv:+open format <a href=\"$stem.ogv\">Ogg/Thera</a>}
  897. __webmfile=${webm:+open format <a href=\"$stem.webm\">WebM/VP8</a>}
  898. __webm_vp9_file=${vp9:+open format <a href=\"${stem}_vp9.webm\">WebM/VP9</a>}
  899. __mp4file=${mp4:+closed format <a href=\"$stem.mp4\">MPEG-4/AVC</a>}
  900. cat >"$stem.html" <<EOF
  901. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  902. <video$__width$__height preload controls>
  903. ${mp4:+$_source_mp4
  904. }${webm_vp9:+$_source_webm_vp9
  905. }${webm:+$_source_webm
  906. }${ogv:+$_source_ogv
  907. }${flash:+$_object_flash
  908. $_param_name
  909. $_param_flashvars
  910. }<img src="$stem.jpg"$__width$__height alt="$title"
  911. title="No video playback capabilities, please download the video below" />
  912. ${flash:+</object>
  913. }</video>
  914. <p><strong>Download Video:</strong><ul>
  915. ${webm_vp9:+<li>$__webm_vp9_file
  916. }${webm:+<li>$__webmfile
  917. }${ogv:+<li>$__ogvfile
  918. }${mp4:+<li>$__mp4file
  919. }</ul></p>
  920. EOF