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