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