summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: bb6c897b1f2e000622615ab6736e9639fdc4f89a (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 WebM 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 Containers and codecs to use, comma-separated:
  84. [container] [video codec] [audio codec]
  85. theora ogg Ogg Theora Vorbis
  86. vp8 webm WebM VP8 Vorbis
  87. vp9 WebM VP9 Opus
  88. h264 mp4 MPEG-4 H.264 AAC
  89. (default: webm,vp9,mp4)
  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. (vp8 uses "soft-CQ" instead of unsupported Q)
  114. [quality] [bitrate] [speed] [method,pass]
  115. dirty fast CQ,1
  116. normal medium average medium CQ,1
  117. hq high exact very slow CQ,2
  118. exact medium exact slow CQ,2
  119. quality medium vary medium Q,1
  120. (default: normal)
  121. --quality Quality rate factor: auto [integer] none
  122. suggested, h264: 19 23 27 31 35 39
  123. suggested, vp8/vp9: 27 33 39 45 51 57
  124. (default: auto - 23 for h264, 33 for vp8/vp9)
  125. --gpu Use hardware acceleration: none auto vaapi qsv
  126. (default: auto when compression=dirty, else none)
  127. --gpudevice Device used for hardware acceleration
  128. (default: /dev/dri/renderD128)
  129. -h, --help This help text
  130. Examples:
  131. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  132. $PRG -p 480p15 --filter "grain noise=20" myvideo.dv
  133. Recommendations for best results:
  134. * Use square pixels, modulus 16, max. 480 width (qvga vga 480p 720p).
  135. * Try lower --refbpp while visually acceptable, or raise to max. 0.2.
  136. * Expand and compress noisy speech with ladspa.1075 and ladspa.1916.
  137. More info:
  138. <http://camendesign.com/code/video_for_everybody>
  139. <http://www.streaminglearningcenter.com/articles/configuring-your-streaming-video-(for-newbies).html>
  140. <http://en.wikipedia.org/wiki/HTML5_video>
  141. <http://www.penguinproducer.com/2012/01/ladspa-noise-removal/>
  142. <http://theproaudiofiles.com/mixing-vocals-dynamics-compression-limiting>
  143. <http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/>
  144. EOF
  145. }
  146. exit1() {
  147. response="${1:+Error: }${1:-Internal error!}"
  148. echo >&2 "$response"
  149. exit 1
  150. }
  151. # defaults
  152. formats=webm,vp9,mp4
  153. stillframe=0
  154. samplestart=0
  155. samplelength=150
  156. compression=normal
  157. # establish/verify rendering settings for reference codec
  158. #
  159. # 1) pick reference frame:encoding format: 360p30:h.264
  160. # 2) pick reference codec (incl. normal settings)
  161. # 3) render reference frame format with --compression=quality
  162. # * check that bpp is near "refbpp" of 0.12
  163. # (otherwise adapt settings or do research and update refbpp)
  164. # 4) render misc. frame formats (esp. modulus 16) with --compression=quality
  165. # * check that bpp derives from refbpp by "rule of .75"
  166. # (otherwise adapt settings and/or do research and update rule)
  167. # 5) render misc. frame formats with --compression=normal
  168. # * check that bpp derives from refbpp by "rule of .75"
  169. # 6) optionally pick dirty/hq settings
  170. # * check that --compression=quality has filesize similar as normal
  171. # establish/verify rendering settings for non-reference codec
  172. #
  173. # 1) pick normal settings
  174. # 2) render reference frame format with --compression=quality
  175. # * check that this "codec-bpp" deviates reasonably from refbpp
  176. # 3) render misc. frame formats (esp. modulus 16) with --compression=quality
  177. # * check that bpp derives from codec-bpp by "rule of .75"
  178. # (otherwise adapt settings and/or extend rule)
  179. # 4) render misc. frame formats with --compression=normal
  180. # * check that bpp derives from codec-bpp by "rule of .75"
  181. # 5) optionally pick dirty/hq settings
  182. # * check that --compression=quality has similar filesize as normal
  183. # VP8 is rumored to compress ~20% worse than H.264
  184. factor_vp8=120/100
  185. # VP9 compresses ~30% better than H.264 for practical use-cases
  186. # <https://engineering.fb.com/2018/04/10/video-engineering/av1-beats-x264-and-libvpx-vp9-in-practical-use-case/>
  187. factor_vp9=70/100
  188. # Mimic ReplayGain level when loudness is enabled (EBU R128: -23LUFS)
  189. # <https://auphonic.com/blog/2013/01/07/loudness-targets-mobile-audio-podcasts-radio-tv/>
  190. lufs=-18
  191. # parse cmdline options
  192. 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."
  193. eval set -- "$TEMP"
  194. while true ; do
  195. case "$1" in
  196. -h|--help) showhelp; exit;;
  197. -p|--profile) profile="$2"; shift 2;;
  198. -s|--size) size="$2"; shift 2;;
  199. -a|--aspect) aspect="$2"; shift 2;;
  200. -r|--rate) framerate="$2"; shift 2;;
  201. --video) video="$2"; shift 2;;
  202. --refbpp) refbpp="$2"; shift 2;;
  203. --formats) formats="$2"; shift 2;;
  204. --audio) audio="$2"; shift 2;;
  205. --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;;
  206. --loudness) loudness=yes; shift;;
  207. --loudness-data) loudness=yes; loudness_data="$2"; shift 2;;
  208. --filter) filters="${filters:+$filters }-filter $2"; shift 2;;
  209. --stem) stem="$2"; shift 2;;
  210. -t|--title) title="$2"; shift 2;;
  211. --still-frame) stillframe="$2"; shift 2;;
  212. --sample) sample=yes; shift;;
  213. --sample-start) sample=yes; samplestart="$2"; shift 2;;
  214. --sample-length) sample=yes; samplelength="$2"; shift 2;;
  215. --compression) compression="$2"; shift 2;;
  216. --quality) quality="$2"; shift 2;;
  217. --gpu) gpu="$2"; shift 2;;
  218. --gpudevice) gpudevice="$2"; shift 2;;
  219. --) shift; break;;
  220. *) exit1 "Internal error resolving options.";;
  221. esac
  222. done
  223. [ -n "$sample" ] || notsample=yes
  224. # Resolve if system has many CPU cores
  225. processors=$(nproc)
  226. [ $processors -gt 2 ] || processors=
  227. # sanitize infiles
  228. infiles=$*
  229. infile_first=$(perl -e 'print shift @ARGV' $infiles)
  230. [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!"
  231. # resolve stem and title (if not explicitly set)
  232. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  233. title=${title:-$stem}
  234. #use_ffmpeg_vp8=yes
  235. #use_ffmpeg_vp9=yes
  236. #use_ffmpeg_h264=yes
  237. #use_vpxenc=yes
  238. # Avoid discrete audio encoders if possible: may cause sync problems
  239. #use_oggenc=yes
  240. #use_opusenc=yes
  241. # Avoid AAC: Lame arguably better than any free AAC encoder
  242. # Argument for AAC: Safari possibly lacks mp3 support in video container
  243. #use_mp3=yes
  244. [ -n "$use_mp3" ] || use_aac=yes
  245. # Avoid MP3 VBR but seems ABR is fine: Set to CBR for strict correctnes
  246. #use_lame_cbr=yes
  247. [ -n "$use_lame_cbr" ] || use_lame_abr=yes
  248. # resolve quality/speed hints
  249. qkey_vp8=crf
  250. qkey_vp9=crf
  251. qkey_h264=crf
  252. q_theora=5
  253. q_vp8=33
  254. cpu_vp8=2
  255. q_vp9=33
  256. cpu_vp9=2
  257. q_h264=23
  258. preset_h264=medium
  259. profile_vp8=0
  260. profile_vp9=0
  261. profile_h264=baseline
  262. case "$compression" in
  263. normal) :;;
  264. dirty)
  265. gpu=${gpu:-auto}
  266. q_theora=1
  267. cpu_vp8=4
  268. cpu_vp9=4
  269. preset_h264=veryfast
  270. ;;
  271. hq)
  272. multipass=yes
  273. q_theora=6
  274. cpu_vp8=0
  275. cpu_vp9=1
  276. preset_h264=veryslow
  277. ;;
  278. exact)
  279. multipass=yes
  280. ;;
  281. quality)
  282. no_bitrate=yes
  283. ;;
  284. *) exit1 "Unknown compression mode \"$compression\".";;
  285. esac
  286. case "$quality" in
  287. auto|'') :;;
  288. none) no_q=yes;;
  289. *) q=$quality;;
  290. esac
  291. gpudevice=${gpudevice:-/dev/dri/renderD128}
  292. if [ auto = "$gpu" ]; then
  293. vainfo=$(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'Driver version:\s*\K.*')
  294. case "$vainfo" in
  295. '') gpu=;;
  296. *)
  297. for vformat in vp8 vp9 h264; do
  298. eval "no_gpu_decoder_$vformat=yes"
  299. eval "no_gpu_encoder_$vformat=yes"
  300. done
  301. case $(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'VAProfile\S+\s*:\s*VAEntrypointVLD') in
  302. *VAProfileVP8Version0_3*) no_gpu_decoder_vp8=;;
  303. *VAProfileVP9Profile0*) no_gpu_decoder_vp9=;;
  304. *VAProfileH264ConstrainedBaseline*) no_gpu_decoder_h264=;;
  305. esac
  306. case $(vainfo --display drm --device "$gpudevice" 2>/dev/null | grep -Po 'VAProfile\S+\s*:\s*VAEntrypointEncSlice') in
  307. *VAProfileVP8Version0_3*) no_gpu_encoder_vp8=;;
  308. *VAProfileVP9Profile0*) no_gpu_encoder_vp9=;;
  309. *VAProfileH264ConstrainedBaseline*) no_gpu_encoder_h264=;;
  310. esac
  311. if [ -n "$no_gpu_decoder_vp8$no_gpu_decoder_vp9$no_gpu_decoder_h264" ]; then
  312. case "$vainfo" in
  313. # TODO *"Intel iHD driver"*) gpu=qsv;;
  314. *) gpu=vaapi;;
  315. esac
  316. fi
  317. esac
  318. fi
  319. case "$gpu" in
  320. none|'') gpu=;;
  321. qsv)
  322. multipass=
  323. preset_vp8=medium
  324. preset_vp9=medium
  325. preset_h264=
  326. profile_h264=578
  327. qkey_h264=qp
  328. ;;
  329. vaapi)
  330. multipass=
  331. preset_h264=
  332. profile_h264=578
  333. qkey_h264=qp
  334. case "$compression" in
  335. dirty)
  336. preset_h264=
  337. ;;
  338. esac
  339. ;;
  340. *) exit1 "Unknown GPU type \"$gpu\".";;
  341. esac
  342. [ -n "$multipass" ] || singlepass=yes
  343. [ -z "$gpu" ] && no_gpu=yes || eval "gpu_$gpu=yes"
  344. # parse/resolve size and framerate
  345. case "$profile" in
  346. '') :;;
  347. *@*)
  348. while read s r foo; do
  349. profilesize="${size:-$s}"
  350. framerate="${framerate:-$r}"
  351. done << EOF
  352. $(echo "$profile" | perl -F@ -anE 'say join " ", @F')
  353. EOF
  354. ;;
  355. *p*)
  356. while read s r foo; do
  357. profilesize="${size:-${s}p}"
  358. framerate="${framerate:-$r}"
  359. done << EOF
  360. $(echo "$profile" | perl -Fp -anE 'say join " ", @F')
  361. EOF
  362. ;;
  363. *)
  364. profilesize="$profile"
  365. ;;
  366. esac
  367. size=${size:-$profilesize}
  368. case "$size" in
  369. qvga) size=320x240;;
  370. hvga) size=480x360;;
  371. vga) size=640x480;;
  372. svga) size=800x600;;
  373. xga) size=1024x768;;
  374. 240p|wqvga) size=424x240;;
  375. 360p|nhd) size=640x360;;
  376. 480p|wvga) size=848x480;;
  377. 576p|wsvga) size=1024x576;;
  378. 720p|wxga|hd) size=1280x720;;
  379. esac
  380. if [ -n "$size" ]; then
  381. while read w h foo; do
  382. width="${width:-$w}"
  383. height="${height:-$h}"
  384. done << EOF
  385. $(echo "$size" | perl -Fx -anE 'say join " ", @F')
  386. EOF
  387. if [ -z "$width" ] || [ -z "$height" ]; then
  388. exit1 "Failed to parse size \"$size\"."
  389. fi
  390. fi
  391. case "$framerate" in
  392. */*)
  393. while read n d foo; do
  394. framerate_num="${framerate_num:-$n}"
  395. framerate_den="${framerate_den:-$d}"
  396. done << EOF
  397. $(echo "$framerate" | perl -F/ -anE 'say join " ", @F')
  398. EOF
  399. ;;
  400. ?*)
  401. framerate_num="$framerate"
  402. framerate_den=1
  403. ;;
  404. esac
  405. # resolve input size and framerate (needed for computing bitrate)
  406. while read w h r s foo; do
  407. width_in="${width_in:-$w}"
  408. height_in="${height_in:-$h}"
  409. framerate_in="${framerate_in:-$r}"
  410. scantype_in="${scantype_in:-$s}"
  411. done << EOF
  412. $(mediainfo --Inform="Video;%Width% %Height% %FrameRate% %ScanType%" \
  413. "$infile_first")
  414. EOF
  415. [ Progressive = "$scantype_in" ] || do_deinterlace=yes
  416. case "$video" in
  417. talkinghead)
  418. refbpp="${refbpp:-0.1}"
  419. x264tune=film
  420. ;;
  421. action)
  422. refbpp="${refbpp:-0.15}"
  423. x264tune=film
  424. ;;
  425. '')
  426. refbpp="${refbpp:-0.12}"
  427. ;;
  428. *) exit1 "Unknown video style \"$video\".";;
  429. esac
  430. for format in $(echo "$formats" | sed -e 's/,/ /g'); do
  431. case $format in
  432. theora|ogg) ogg=yes;;
  433. h264|mp4) mp4=yes;;
  434. vp8|webm) webm=yes;;
  435. vp9) vp9=yes;;
  436. *) exit1 "Unknown format \"$format\".";;
  437. esac
  438. done
  439. _width="${width:-$width_in}"
  440. _height="${height:-$height_in}"
  441. if [ -n "$_width" ] && [ -n "$_height" ]; then
  442. _pixels="$((_width*_height))"
  443. fi
  444. _frames="${framerate:-$framerate_in}"
  445. webm_qmin=0
  446. webm_qmax=63
  447. webm_lag=16
  448. webm_tokenparts_log2=0
  449. if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then
  450. webm_qmin=11
  451. webm_qmax=51
  452. webm_tokenparts_log2=2
  453. if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then
  454. webm_lag=25
  455. profile_vp8=1
  456. profile_vp9=1
  457. fi
  458. fi
  459. # compute average bitrate from reference data and "power of .75" rule
  460. if [ -n "$_pixels" ] && [ -n "$_frames" ]; then
  461. bitrate_h264=$(perl -E '$refsize=640*360;' \
  462. -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )")
  463. bitrate_theora=$bitrate_h264 # FIXME
  464. bitrate_vp8=$(perl -E "say int( +($bitrate_h264*$factor_vp8) )") #"
  465. bitrate_vp9=$(perl -E "say int( +($bitrate_h264*$factor_vp9) )") #"
  466. fi
  467. # default per-codec-channel bitrates
  468. quality_vorbis=3
  469. bitrate_opus=48
  470. quality_lame=6
  471. bitrate_lame=64
  472. bitrate_aac=64
  473. case "$audio" in
  474. music)
  475. opusapp=audio
  476. ;;
  477. speech)
  478. mono=yes
  479. quality_vorbis=2
  480. bitrate_opus=32
  481. quality_lame=7
  482. bitrate_lame=48
  483. opusapp=voip
  484. ;;
  485. silence)
  486. silence=yes
  487. ;;
  488. '')
  489. :
  490. ;;
  491. *) exit1 "Unknown audio style \"$audio\".";;
  492. esac
  493. which ffprobe > /dev/null && ffprobe=ffprobe || ffprobe=avprobe
  494. channels=$($ffprobe -v warning -show_streams "$infile_first" \
  495. | perl -ne 's/channels=// and print $_' || echo -1)
  496. # adapt channel count and flags to reflect actual downmix/silence need
  497. if [ -n "$stereo" ]; then
  498. if [ $channels -gt 2 ]; then
  499. channels=2
  500. else
  501. stereo=
  502. fi
  503. elif [ -n "$mono" ]; then
  504. if [ $channels -gt 1 ]; then
  505. channels=1
  506. else
  507. mono=
  508. fi
  509. elif [ -n "$silence" ]; then
  510. if [ $channels -gt 0 ]; then
  511. channels=0
  512. else
  513. silence=
  514. fi
  515. fi
  516. [ -z "$silence" ] || channels=
  517. if [ -n "$stereo$mono" ]; then
  518. # melt cannot downmix with (stereo?) filters applied (bug#763911)
  519. if [ -n "$audioprefilters$compress$loudness$limit$filters" ]; then
  520. if [ -z "${webm:+$use_oggenc}${webm_vp9:+$use_opusenc}" ]; then
  521. downmix_ffmpeg=yes
  522. else
  523. downmix_oggenc=yes
  524. downmix_oggenc=yes
  525. fi
  526. else
  527. downmix_melt=yes
  528. fi
  529. fi
  530. encoder_vp8=libvpx
  531. encoder_vp9=libvpx-vp9
  532. encoder_h264=libx264
  533. for vformat in vp8 vp9 h264; do
  534. [ -z "$no_bitrate" ] || eval "no_bitrate_$vformat=yes"
  535. eval "[ -n \"$no_bitrate\$no_bitrate_$vformat\" ] || do_bitrate_$vformat=yes"
  536. eval "[ -z \"$no_q\$no_q_$vformat\" ] || q_$vformat="
  537. [ -z "$q" ] || eval "q_$vformat=$q"
  538. eval "[ -n \"\$q_$vformat\" ] || no_q_$vformat=yes"
  539. [ -z "$gpu" ] || eval "[ -n \"\$no_gpu_encoder_$vformat\" ] || encoder_$vformat=${vformat}_$gpu"
  540. # melt supports dual-pass only for h.264
  541. [ h264 = "$vformat" ] || [ -n "$vpxenc" ] || eval "[ -z \"$multipass\" ] || use_ffmpeg_$vformat=yes"
  542. # melt (before v6.26) does not support encode-only hardware acceleration
  543. [ h264 = "$vformat" ] || [ -n "$vpxenc$gpu_vaapi" ] || eval "[ -z \"$multipass\" ] || use_ffmpeg_$vformat=yes"
  544. done
  545. if [ -n "$use_ffmpeg_vp8$use_ffmpeg_vp9$use_vpxenc" ]; then
  546. [ -n "$use_oggenc" ] || use_ffmpeg_ogg=yes
  547. [ -n "$use_opusenc" ] || use_ffmpeg_opus=yes
  548. [ -z "$use_oggenc$use_opusenc" ] || use_wav=yes
  549. [ -z "$use_ffmpeg_ogg$use_ffmpeg_opus" ] || use_matroska=yes
  550. fi
  551. if [ -n "$use_ffmpeg_h264" ]; then
  552. use_matroska=yes
  553. else
  554. use_melt_h264=yes
  555. fi
  556. [ -z "$gpu" ] || echo >&2 "GPU platform enabled: $gpu"
  557. # gpu options
  558. _melt_gpu_init=${gpu:+ ${gpu}_device=$gpudevice}
  559. _ffmpeg_gpu_vaapi_init="-${gpu}_device $gpudevice"
  560. _ffmpeg_gpu_qsv_init="-init_hw_device qsv=hw:$gpudevice -filter_hw_device hw"
  561. _ffmpeg_gpu_init="${gpu_vaapi:+ ${_ffmpeg_gpu_vaapi_init}}${gpu_qsv:+ ${_ffmpeg_gpu_qsv_init}}"
  562. _ffmpeg_gpu_vaapi_filter="-vf 'format=nv12|$gpu,hwupload'"
  563. _ffmpeg_gpu_qsv_filter="-vf 'hwupload=extra_hw_frames=64,format=qsv'"
  564. _ffmpeg_gpu_filter="${gpu_vaapi:+ ${_ffmpeg_gpu_vaapi_filter}}${gpu_qsv:+ ${_ffmpeg_gpu_qsv_filter}}"
  565. # generic options
  566. melt="melt -progress$_melt_gpu_init"
  567. which ffmpeg > /dev/null && ffmpeg=ffmpeg || ffmpeg=avconv
  568. ffmpeg_alone="$ffmpeg -threads auto -y -v info -stats$_ffmpeg_gpu_init"
  569. ffmpeg_chained="$ffmpeg -threads auto -y -v warning$_ffmpeg_gpu_init"
  570. vpxenc_chained="vpxenc --quiet ${processors:+-t $((processors-1))}"
  571. oggenc="oggenc"
  572. opusenc="opusenc"
  573. _melt_infiles="${notsample:+$infiles}${sample:+$infile_first in=$((samplestart)) out=$((samplestart + samplelength))}"
  574. # filter options
  575. _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}"
  576. _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}}"
  577. _melt_downmix="${downmix_melt:+ac=$channels}"
  578. _ffmpeg_downmix="${downmix_ffmpeg:+-ac $channels}"
  579. # FIXME: how to downmix to stereo?
  580. _oggenc_downmix="${downmix_oggenc:+${mono:+--downmix}}"
  581. _opusenc_downmix="${downmix_opusenc:+${stereo:+--downmix-stereo}${mono:+--downmix-mono}}"
  582. # limit (i.e. avoid peaks "clipping")
  583. _melt_postfilters_audio="${limit:+-filter ladspa.1077}"
  584. # codec bitrate options
  585. _melt_bitrate_vp8="${do_bitrate_vp8:+ vb=$bitrate_vp8} minrate=$((bitrate_vp8/20)) maxrate=$((bitrate_vp8*12))"
  586. _ffmpeg_bitrate_vp8="${do_bitrate_vp8:+-b:v $bitrate_vp8} -minrate $((bitrate_vp8/20)) -maxrate $((bitrate_vp8*12))"
  587. _vpxenc_bitrate_vp8="--target-bitrate=$((bitrate_vp8/1000)) --minsection-pct=5 --maxsection-pct=1200"
  588. _melt_bitrate_vp9="${do_bitrate_vp9:+ vb=$bitrate_vp9} minrate=$((bitrate_vp9/20)) maxrate=$((bitrate_vp9*12))"
  589. _ffmpeg_bitrate_vp9="${do_bitrate_vp9:+-b:v $bitrate_vp9} -minrate $((bitrate_vp9/20)) -maxrate $((bitrate_vp9*12))"
  590. _vpxenc_bitrate_vp9="--target-bitrate=$((bitrate_vp9/1000)) --minsection-pct=5 --maxsection-pct=1200"
  591. # codec low-level options
  592. _melt_misc_vp8="qmin=$webm_qmin qmax=$webm_qmax g=120 deadline=good cpu-used=$cpu_vp8${profile_vp8:+ vprofile=$profile_vp8} auto-alt-ref=1 lag-in-frames=$webm_lag arnr-maxframes=7 arnr-strength=5 arnr-type=centered"
  593. _ffmpeg_misc_vp8="-qmin $webm_qmin -qmax $webm_qmax -g 120 -deadline good -cpu-used $cpu_vp8${profile_vp8:+ -profile:v $profile_vp8} -auto-alt-ref 1 -lag-in-frames $webm_lag -arnr-maxframes 7 -arnr-strength 5 -arnr-type centered"
  594. _vpxenc_misc_vp8="--min-q=$webm_qmin --max-q=$webm_qmax --kf-max-dist=120 --good --cpu-used=$cpu_vp8${profile_vp8:+ --profile=$profile_vp8} --token-parts=$webm_tokenparts_log2 --auto-alt-ref=1 --lag-in-frames=$webm_lag --arnr-maxframes=7 --arnr-strength=5 --arnr-type=3"
  595. _melt_misc_vp9="qmin=$webm_qmin qmax=$webm_qmax g=120 deadline=good cpu-used=$cpu_vp9${profile_vp9:+ vprofile=$profile_vp9} auto-alt-ref=1 lag-in-frames=$webm_lag arnr-maxframes=7 arnr-strength=5 arnr-type=centered"
  596. _ffmpeg_misc_vp9="-qmin $webm_qmin -qmax $webm_qmax -g 120 -deadline good -cpu-used $cpu_vp9${profile_vp9:+ -profile:v $profile_vp9} -auto-alt-ref 1 -lag-in-frames $webm_lag -arnr-maxframes 7 -arnr-strength 5 -arnr-type centered"
  597. _vpxenc_misc_vp9="--min-q=$webm_qmin --max-q=$webm_qmax --kf-max-dist=120 --good --cpu-used=$cpu_vp9${profile_vp9:+ --profile=$profile_vp9} --tile-columns=$webm_tokenparts_log2 --tile-rows=$webm_tokenparts_log2 --auto-alt-ref=1 --lag-in-frames=$webm_lag --arnr-maxframes=7 --arnr-strength=5 --arnr-type=3"
  598. # codec options
  599. # emulate VP8 Constant Quality using "loose" Constrained Quality
  600. _melt_theora="vcodec=libtheora${bitrate_theora:+ vb=$bitrate_theora} qscale=$q_theora"
  601. _melt_vp8="vcodec=$encoder_vp8${pre_vp8:+ vpreset=$pre_vp8}${preset_vp8:+ preset=$preset_vp8}${do_bitrate_vp8:+ $_melt_bitrate_vp8}${q_vp8:+ ${no_bitrate_vp8:+vb=$((bitrate_vp8*10)) }$qkey_vp8=$q_vp8} $_melt_misc_vp8"
  602. _ffmpeg_vp8="-c:v $encoder_vp8${pre_vp8:+ -vpre $pre_vp8}${preset_vp8:+ preset $preset_vp8}${do_bitrate_vp8:+ $_ffmpeg_bitrate_vp8}${q_vp8:+ ${no_bitrate_vp8:+-b:v $((bitrate_vp8*10)) }-$qkey_vp8 $q_vp8} $_ffmpeg_misc_vp8"
  603. _vpxenc_vp8="--codec=vp8${do_bitrate_vp8:+ $_vpxenc_bitrate_vp8} --end-usage=${no_q_vp8:+vbr}${q_vp8:+cq --cq-level=$q_vp8} $_vpxenc_misc_vp8"
  604. _melt_vp9="vcodec=$encoder_vp9${pre_vp9:+ vpreset=$pre_vp9}${preset_vp9:+ preset=$preset_vp9}${do_bitrate_vp9:+ $_melt_bitrate_vp9}${q_vp9:+ ${no_bitrate_vp9:+vb=0 }$qkey_vp9=$q_vp9} $_melt_misc_vp9"
  605. _ffmpeg_vp9="-c:v $encoder_vp9${pre_vp9:+ -vpre $pre_vp9}${preset_vp9:+ -preset $preset_vp9}${do_bitrate_vp9:+ $_ffmpeg_bitrate_vp9}${q_vp9:+ ${no_bitrate_vp9:+-b:v 0 }-$qkey_vp9 $q_vp9} $_ffmpeg_misc_vp9"
  606. _vpxenc_vp9="--codec=vp9${do_bitrate_vp9:+ $_vpxenc_bitrate_vp9} --end-usage=${no_q_vp9:+vbr}${q_vp9:+cq --cq-level=$q_vp9} $_vpxenc_misc_vp8"
  607. _melt_h264="vcodec=$encoder_h264${preset_h264:+ vpreset=$preset_h264}${profile_h264:+ vprofile=$profile_h264}${x264tune:+ tune=$x264tune}${do_bitrate_h264:+ maxrate=$bitrate_h264${no_gpu:+ bufsize=$((bitrate_h264*2))}}${q_h264:+ $qkey_h264=$q_h264} threads=0${no_gpu:+ movflags=+faststart}"
  608. _ffmpeg_h264="-c:v $encoder_h264${preset_h264:+ -preset $preset_h264}${profile_h264:+ -profile:v $profile_h264}${x264tune:+ -tune $x264tune}${do_bitrate_h264:+ -maxrate $bitrate_h264${no_gpu:+ -bufsize $((bitrate_h264*2))}}${q_h264:+ -$qkey_h264 $q_h264}${no_gpu:+ -movflags +faststart}"
  609. _melt_pcm="$_melt_downmix acodec=pcm_s16le"
  610. _melt_vorbis="$_melt_downmix acodec=libvorbis aq=$quality_vorbis"
  611. _ffmpeg_vorbis="$_ffmpeg_downmix -c:a libvorbis -aq $quality_vorbis"
  612. _oggenc_vorbis="$_oggenc_downmix -q $quality_vorbis"
  613. _melt_opus="$_melt_downmix acodec=libopus ab=$((channels*bitrate_opus))k${opusapp:+ application=$opusapp}"
  614. _ffmpeg_opus="$_ffmpeg_downmix -c:a libopus -b:a $((channels*bitrate_opus))k${opusapp:+ -application $opusapp}"
  615. _opusenc_opus="$_opusenc_downmix --bitrate $((channels*bitrate_opus))"
  616. _melt_mp3="$_melt_downmix acodec=libmp3lame${use_lame_abr:+ aq=$quality_lame}${use_lame_cbr:+ ab=$((channels*bitrate_lame))k}"
  617. _ffmpeg_mp3="$_ffmpeg_downmix -c:a libmp3lame${use_lame_abr:+ -q:a $quality_lame}${use_lame_cbr:+ -b:a $((channels*bitrate_lame))k}"
  618. _melt_aac="$_melt_downmix acodec=aac ab=$((channels*bitrate_aac))k"
  619. _ffmpeg_aac="$_ffmpeg_downmix -c:a aac -b:a $((channels*bitrate_aac))k"
  620. # container options
  621. _melt_stdout="-consumer avformat:pipe:1 f=yuv4mpegpipe $_melt_video pix_fmt=yuv420p an=1 audio_off=1"
  622. _ffmpeg_stdin="-f yuv4mpegpipe -i pipe:0$_ffmpeg_gpu_filter"
  623. _ffmpeg_rawvideo="-f rawvideo"
  624. _melt_wav="f=wav $_melt_pcm vn=1 video_off=1"
  625. _ffmpeg_wav_in="-f wav"
  626. _oggenc_wav_in=
  627. _opusenc_wav_in=
  628. _melt_matroska_pcm="f=matroska $_melt_downmix $_melt_pcm vn=1 video_off=1"
  629. _ffmpeg_matroska_pcm_in="-f matroska"
  630. _melt_ogg="f=ogg $_melt_video $_melt_theora ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  631. _ffmpeg_ogg_in="-f ogg"
  632. _ffmpeg_ogg_vorbis="-f ogg -vn $_ffmpeg_vorbis"
  633. _ffmpeg_ogg_opus="-f ogg -vn $_ffmpeg_opus"
  634. _melt_webm="f=webm $_melt_video $_melt_vp8 ${silence:+an=1 audio_off=1}${channels:+$_melt_vorbis}"
  635. _ffmpeg_webm="-f webm $_ffmpeg_video $_ffmpeg_vp8 ${silence:+-an}${channels:+ $_ffmpeg_vorbis}"
  636. _ffmpeg_webm_in="-f webm"
  637. _ffmpeg_webm_onlyvideo="-f webm $_ffmpeg_video $_ffmpeg_vp8 -an"
  638. _ffmpeg_webm_keepvideo="-f webm $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_vorbis}"
  639. _ffmpeg_webm_keepvideo_opus="-f webm $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_opus}"
  640. _melt_webm_vp9="f=webm $_melt_video $_melt_vp9 ${silence:+an=1 audio_off=1}${channels:+$_melt_opus}"
  641. _ffmpeg_webm_vp9="-f webm $_ffmpeg_video $_ffmpeg_vp9 ${silence:+-an}${channels:+$_ffmpeg_opus}"
  642. _ffmpeg_webm_onlyvideo_vp9="-f webm $_ffmpeg_video $_ffmpeg_vp9 -an"
  643. _melt_mp4="f=mp4 $_melt_video $_melt_h264 ${silence:+an=1 audio_off=1}${channels:+${use_mp3:+$_melt_mp3}${use_aac:+$_melt_aac}}"
  644. _ffmpeg_mp4="-f mp4 $_ffmpeg_video $_ffmpeg_h264 ${silence:+-an}${channels:+${use_mp3:+$_ffmpeg_mp3}${use_aac:+$_ffmpeg_aac}}"
  645. _ffmpeg_mp4_onlyvideo="-f mp4 $_ffmpeg_video $_ffmpeg_h264 -an"
  646. _ffmpeg_mp4_keepvideo="-f mp4 $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_mp3}"
  647. _ffmpeg_mp4_keepvideo_aac="-f mp4 $_ffmpeg_video -c:v copy ${silence:+-an}${channels:+$_ffmpeg_aac}"
  648. _melt_img="f=image2 $_melt_video"
  649. _melt_loudness="$loudness_data"
  650. if [ -n "$loudness" ] && [ -z "$silence$_melt_loudness" ]; then
  651. echo >&2 "Analyzing audio dynamics..."
  652. $melt $_melt_infiles \
  653. $audioprefilters -filter loudness $filters \
  654. -consumer xml:$stem.xml video_off=1 all=1
  655. _melt_loudness="$(perl -n \
  656. -e 'm!<(property) name="results">([^<]+)</\1>! and print $2' \
  657. $stem.xml)"
  658. echo >&2 "Loudness data: $_melt_loudness"
  659. fi
  660. if [ -n "$multipass" ] && [ -n "${webm:+$use_vpxenc$use_ffmpeg_vp8$use_oggenc}${vp9:+$use_vpxenc$use_ffmpeg_vp9$use_opusenc}${mp4:+$use_ffmpeg_h264}" ]; then
  661. echo >&2 "Analyzing video complexity..."
  662. if [ -n "$use_vpxenc" ]; then
  663. $melt $_melt_infiles $filters $_melt_stdout \
  664. | pee \
  665. ${webm:+"$vpxenc_chained - $_vpxenc_vp8 \
  666. -p 2 --pass=1 --fpf=${stem}_vp8.log -o /dev/null"} \
  667. ${vp9:+"$vpxenc_chained - $_vpxenc_vp9 \
  668. -p 2 --pass=1 --fpf=${stem}_vp9.log -o /dev/null"} \
  669. ${mp4:+${use_ffmpeg_h264:+$_ffmpeg_rawvideo $_ffmpeg_h264 -an \
  670. -pass 1 -passlogfile ${stem}_h264 /dev/null}}
  671. [ -z "$mp4" ] || [ -n "$use_ffmpeg_h264" ] || mv -f ${stem}_h264-*.log ${stem}_h264_2pass.log
  672. else
  673. $melt $_melt_infiles $filters $_melt_stdout \
  674. | $ffmpeg_chained $_ffmpeg_stdin \
  675. ${webm:+$_ffmpeg_rawvideo $_ffmpeg_vp8 -an \
  676. -pass 1 -passlogfile ${stem}_vp8 /dev/null} \
  677. ${vp9:+$_ffmpeg_rawvideo $_ffmpeg_vp9 -an \
  678. -pass 1 -passlogfile ${stem}_vp9 /dev/null} \
  679. ${mp4:+${use_ffmpeg_h264:+$_ffmpeg_rawvideo $_ffmpeg_h264 -an \
  680. -pass 1 -passlogfile ${stem}_h264 /dev/null}}
  681. [ -z "$webm" ] || [ -n "$use_ffmpeg_vp8" ] || mv -f ${stem}_vp8-*.log ${stem}_vp8_2pass.log
  682. [ -z "$vp9" ] || [ -n "$use_ffmpeg_vp9" ] || mv -f ${stem}_vp9-*.log ${stem}_vp9_2pass.log
  683. [ -z "$mp4" ] || [ -n "$use_ffmpeg_h264" ] || mv -f ${stem}_h264-*.log ${stem}_h264_2pass.log
  684. fi
  685. fi
  686. if [ -n "${webm:+$use_vpxenc$use_ffmpeg_vp8$use_oggenc}${vp9:+$use_vpxenc$use_ffmpeg_vp9$use_opusenc}${mp4:+$use_ffmpeg_h264}" ]; then
  687. echo >&2 "Encoding video${channels:+ and extracting audio}, as discrete files..."
  688. $melt $_melt_infiles \
  689. ${channels:+$audioprefilters \
  690. ${_melt_loudness:+-filter loudness program=$lufs \
  691. results="$_melt_loudness"}} \
  692. $filters${channels:+ $_melt_postfilters_audio} \
  693. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  694. ${mp4:+${use_melt_h264:+-consumer avformat:$stem.mp4 $_melt_mp4}} \
  695. ${channels:+${use_wav:+-consumer avformat:$stem.wav \
  696. $_melt_wav}} \
  697. ${channels:+${use_matroska:+-consumer avformat:$stem.mkv \
  698. $_melt_matroska_pcm}} \
  699. $_melt_stdout \
  700. | pee \
  701. ${webm:+${use_ffmpeg_vp8:+"$ffmpeg_chained \
  702. $_ffmpeg_stdin $_ffmpeg_webm_onlyvideo \
  703. ${multipass:+-pass 2 -passlogfile ${stem}_vp8} \
  704. ${stem}${channels:+_silent}.webm"}} \
  705. ${webm:+${use_vpxenc:+"$vpxenc_chained - $_vpxenc_vp8 \
  706. ${singlepass:+-p 1} \
  707. ${multipass:+-p 2 --pass=2 --fpf=${stem}_vp8.log} \
  708. -o ${stem}${channels:+_silent}.webm"}} \
  709. ${vp9:+${use_ffmpeg_vp9:+"$ffmpeg_chained \
  710. $_ffmpeg_stdin $_ffmpeg_webm_onlyvideo_vp9 \
  711. ${multipass:+-pass 2 -passlogfile ${stem}_vp9} \
  712. ${stem}_vp9${channels:+_silent}.webm"}} \
  713. ${vp9:+${use_vpxenc:+"$vpxenc_chained - $_vpxenc_vp9 \
  714. ${singlepass:+-p 1} \
  715. ${multipass:+-p 2 --pass=2 --fpf=${stem}_vp9.log} \
  716. -o ${stem}_vp9${channels:+_silent}.webm"}} \
  717. ${mp4:+${use_ffmpeg_h264:+"$ffmpeg_chained \
  718. $_ffmpeg_stdin $_ffmpeg_mp4_onlyvideo \
  719. ${multipass:+-pass 2 -passlogfile ${stem}_h264} \
  720. ${stem}${channels:+_silent}.mp4"}}
  721. if [ -n "${webm:+$use_vpxenc$use_ffmpeg_vp8$use_oggenc}" ] && [ -n "$channels" ]; then
  722. echo >&2 "Encoding Vorbis audio and muxing with VP8 video..."
  723. if [ -n "$use_oggenc" ]; then
  724. $oggenc $_oggenc_wav_in $_oggenc_vorbis -o - $stem.wav \
  725. | $ffmpeg_chained \
  726. $_ffmpeg_webm_in -i ${stem}_silent.webm \
  727. $_ffmpeg_ogg_in -i pipe:0 \
  728. $_ffmpeg_webm_keepvideo $stem.webm
  729. else
  730. $ffmpeg_alone $_ffmpeg_webm_in -i ${stem}_silent.webm \
  731. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  732. $_ffmpeg_webm_keepvideo ${stem}.webm
  733. fi
  734. fi
  735. if [ -n "${vp9:+$use_vpxenc$use_ffmpeg_vp9$use_opusenc}" ] && [ -n "$channels" ]; then
  736. echo >&2 "Encoding Opus audio and muxing with WebM/VP9 video..."
  737. if [ -n "$use_opusenc" ]; then
  738. $opusenc $_opusenc_wav_in $_opusenc_opus -o - $stem.wav \
  739. | $ffmpeg_chained \
  740. $_ffmpeg_webm_in -i ${stem}_vp9_silent.webm \
  741. $_ffmpeg_ogg_in -i pipe:0 \
  742. $_ffmpeg_webm_keepvideo_opus ${stem}_vp9.webm
  743. else
  744. $ffmpeg_alone $_ffmpeg_webm_in -i ${stem}_vp9_silent.webm \
  745. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  746. $_ffmpeg_webm_keepvideo_opus ${stem}_vp9.webm
  747. fi
  748. fi
  749. if [ -n "${mp4:+$use_ffmpeg_h264}" ] && [ -n "$channels" ]; then
  750. if [ -n "$use_mp3" ]; then
  751. echo "Encoding MP3 audio and muxing with h.264 video..."
  752. $ffmpeg_alone $_ffmpeg_mp4_in -i ${stem}_silent.mp4 \
  753. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  754. $_ffmpeg_mp4_keepvideo ${stem}.mp4
  755. else
  756. echo "Encoding AAC audio and muxing with h.264 video..."
  757. $ffmpeg_alone $_ffmpeg_mp4_in -i ${stem}_silent.mp4 \
  758. $_ffmpeg_matroska_wav_in -i $stem.mkv \
  759. $_ffmpeg_mp4_keepvideo_aac ${stem}.mp4
  760. fi
  761. fi
  762. else
  763. echo >&2 "Encoding video${channels:+ and audio}, at once..."
  764. $melt $_melt_infiles \
  765. ${channels:+$audioprefilters \
  766. ${_melt_loudness:+-filter loudness program=$lufs \
  767. results="$_melt_loudness"}} \
  768. $filters${channels:+ $_melt_postfilters_audio} \
  769. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  770. ${webm:+-consumer avformat:$stem.webm $_melt_webm \
  771. ${multipass:+pass=2 passlogfile=${stem}_vp8}} \
  772. ${vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9 \
  773. ${multipass:+pass=2 passlogfile=${stem}_vp9}} \
  774. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4}
  775. fi
  776. # cleanup encoding cruft
  777. rm -f $stem.xml $stem.wav $stem.mkv ${stem}_*.log ${stem}_*silent.webm
  778. # JPEG preview
  779. $melt $infile_first in=$stillframe out=$stillframe \
  780. -group $filters \
  781. -consumer avformat:$stem.jpg $_melt_img
  782. __width="${_width:+ width=\"$_width\"}"
  783. __height="${_height:+ height=\"$_height\"}"
  784. # Flash object needs extra space for controllers
  785. __heightplus=${_height:+ height=\"$((_height+4))\"}
  786. _source_ogg="<source src=\"$stem.ogv\" type=\"video/ogg\" />"
  787. _source_webm="<source src=\"$stem.webm\" type=\"video/webm\" />"
  788. _source_vp9="<source src=\"${stem}_vp9.webm\" type='video/ogg; codecs=\"vp9, opus\"' />"
  789. _source_mp4="<source src=\"$stem.mp4\" type=\"video/mp4\" />"
  790. [ -z "$flashplayer" ] || flash=yes
  791. [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled."
  792. _object_flash="<object$__width$__heightplus type=\"application/x-shockwave-flash\" data=\"$flashplayer.swf\">"
  793. _param_name="<param name=\"movie\" value=\"$flashplayer.swf\" />"
  794. _param_flashvars="<param name=\"flashvars\" value=\"image=$stem.jpg&amp;file=$stem.mp4\" />"
  795. __oggfile=${ogg:+open format <a href=\"$stem.ogv\">Ogg</a>}
  796. __webmfile=${webm:+open format <a href=\"$stem.webm\">WebM (VP8)</a>}
  797. __vp9file=${vp9:+open format <a href=\"${stem}_vp9.webm\">WebM (VP9/Opus)</a>}
  798. __mp4file=${mp4:+closed format <a href=\"$stem.mp4\">MPEG-4</a>}
  799. cat >"$stem.html" <<EOF
  800. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  801. <video$__width$__height preload controls>
  802. ${mp4:+$_source_mp4
  803. }${vp9:+$_source_vp9
  804. }${webm:+$_source_webm
  805. }${ogg:+$_source_ogg
  806. }${flash:+$_object_flash
  807. $_param_name
  808. $_param_flashvars
  809. }<img src="$stem.jpg"$__width$__height alt="$title"
  810. title="No video playback capabilities, please download the video below" />
  811. ${flash:+</object>
  812. }</video>
  813. <p><strong>Download Video:</strong><ul>
  814. ${vp9:+<li>$__vp9file
  815. }${webm:+<li>$__webmfile
  816. }${ogg:+<li>$__oggfile
  817. }${mp4:+<li>$__mp4file
  818. }</ul></p>
  819. EOF