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