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