summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: eeac39c9277267c4987977c1384d57ef0b459f43 (plain)
  1. #!/bin/sh
  2. # Copyright © 2010-2014 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: libav-tools melt mediainfo
  19. #
  20. # TODO:
  21. # * Offer to skip rendering again if an output file exist already.
  22. # * Support --width and --height, resolving the other part from input
  23. # or forced aspect ratio.
  24. # * Drop $melt_recent flag when melt 0.9.2 is stable.
  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. set -e
  32. PRG=$(basename "$0")
  33. showhelp() {
  34. cat <<EOF
  35. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  36. Encode video file in multiple web-optimized formats, and provide sample
  37. html favoring open formats with optional non-JavaScript Flash fallback.
  38. --video Video style:
  39. ref-bpp
  40. talkinghead 0.1
  41. action 0.15
  42. (default: none)
  43. -p, --profile Video frame size and optional rate, delimited by @
  44. except "p" labels: e.g. 848x480@25 480p25 wvga@25
  45. -s, --size Set video frame size:
  46. [modulus 16]
  47. 320x240 qvga
  48. 640x480 vga 480p 848x480 wvga
  49. 576p 1024x576 wsvga
  50. 1024x768 xga hd 720p 1280x720 wxga
  51. [modulus 8]
  52. 240p 424x240 wqvga
  53. 480x360 hvga nhd 360p 640x360
  54. 800x600 svga
  55. (default: use input size)
  56. -a, --aspect Display Aspect Ratio in melt format: e.g. @16/9
  57. (default: no aspect hinting)
  58. -r, --rate Video framerate: e.g. 25 1001/30000
  59. (default: use input framerate)
  60. --refbpp Bitrate reference: Bits-per-pixel for 360p30 H.264
  61. (default: 0.12)
  62. --formats Containers and codecs to use, comma-separated:
  63. [container] [video codec] [audio codec]
  64. theora ogg Ogg Theora Vorbis
  65. vp8 webm WebM VP8 Vorbis
  66. vp9 WebM VP9 Opus
  67. h264 mp4 MPEG-4 H.264 AAC
  68. (default: webm,vp9,mp4)
  69. --audio Audio style:
  70. [channels] [limit] [Vorbis] [Opus] [AAC]
  71. music max 2 64k 64k audio 96k
  72. hqspeech 1 48k 32k voip 64k
  73. speech 1 X 48k 32k voip 64k
  74. silence 0
  75. (default: none - use channel count of first input)
  76. --audioprefilter Add audio filter before loudness
  77. --loudness Add EBU R128 loudness filter
  78. --loudness-data Add loudness filter, with precomputed results
  79. (default: none - compute results on-the-fly)
  80. --filter Add filter
  81. --stem Stem of output filenames, optionally with path
  82. (default: basename of first input)
  83. -t, --title Title used in html fallback graphics
  84. (default: stem)
  85. --sample Create only a 150 frames long sample
  86. --sample-start Create sample, starting at a specific frame
  87. (default: start at first frame)
  88. --sample-length Create sample of specified length, in frames
  89. (default: 150 frames i.e. approx. 5s)
  90. --compression Optimize for quality or speed: normal dirty hq
  91. (default: normal)
  92. -h, --help This help text
  93. Examples:
  94. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  95. $PRG -p 480p15 --filter "grain noise=20" myvideo.dv
  96. Recommendations for best results:
  97. * Use square pixels, modulus 16, max. 480 width (qvga vga 480p 720p).
  98. * Try lower --ref-bpp while visually acceptable, or raise to max. 0.2.
  99. * Expand and compress noisy speech with ladspa.1075 and ladspa.1916.
  100. More info:
  101. <http://camendesign.com/code/video_for_everybody>
  102. <http://www.streaminglearningcenter.com/articles/configuring-your-streaming-video-(for-newbies).html>
  103. <http://en.wikipedia.org/wiki/HTML5_video>
  104. <http://www.penguinproducer.com/2012/01/ladspa-noise-removal/>
  105. <http://theproaudiofiles.com/mixing-vocals-dynamics-compression-limiting>
  106. <http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/>
  107. EOF
  108. }
  109. exit1() {
  110. response="${1:+Error: }${1:-Internal error!}"
  111. echo >&2 "$response"
  112. exit 1
  113. }
  114. # defaults
  115. formats=webm,vp9,mp4
  116. samplestart=0
  117. samplelength=150
  118. compression=normal
  119. # VP8 is rumored to compress ~20% worse than H.264
  120. factor_vp8=120/100
  121. # VP9 compresses natural video ~27% worse than H.264 (i.e. excluding animation)
  122. # <http://infoscience.epfl.ch/record/200925/files/article-vp9-submited-v2.pdf>
  123. factor_vp9=127/100
  124. # parse cmdline options
  125. 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:,sample,sample-start:,sample-length:,compression: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  126. eval set -- "$TEMP"
  127. while true ; do
  128. case "$1" in
  129. -h|--help) showhelp; exit;;
  130. -p|--profile) profile="$2"; shift 2;;
  131. -s|--size) size="$2"; shift 2;;
  132. -a|--aspect) aspect="$2"; shift 2;;
  133. -r|--rate) framerate="$2"; shift 2;;
  134. --video) video="$2"; shift 2;;
  135. --refbpp) refbpp="$2"; shift 2;;
  136. --formats) formats="$2"; shift 2;;
  137. --audio) audio="$2"; shift 2;;
  138. --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;;
  139. --loudness) loudness=yes; shift;;
  140. --loudness-data) loudness=yes; loudness_data="$2"; shift 2;;
  141. --filter) filters="${filters:+$filters }-filter $2"; shift 2;;
  142. --stem) stem="$2"; shift 2;;
  143. -t|--title) title="$2"; shift 2;;
  144. --sample) sample=yes; shift;;
  145. --sample-start) sample=yes; samplestart="$2"; shift 2;;
  146. --sample-length) sample=yes; samplelength="$2"; shift 2;;
  147. --compression) compression="$2"; shift 2;;
  148. --) shift; break;;
  149. *) exit1 "Internal error resolving options.";;
  150. esac
  151. done
  152. # Resolve if melt is version 0.9.2 or newer
  153. melt_recent=$(melt -query filter=loudness | grep -qi R128 && echo yes)
  154. # sanitize infiles
  155. infiles=$*
  156. infile_first=$(perl -e 'print shift @ARGV' $infiles)
  157. [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!"
  158. # resolve stem and title (if not explicitly set)
  159. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  160. title=${title:-$stem}
  161. # resolve quality/speed hints
  162. multipass=yes
  163. speedpreset_h264=medium
  164. crf_vp8=10
  165. crf_vp9=10
  166. crf_h264=23
  167. qscale_theora=5
  168. cpu_vp8=3
  169. cpu_vp9=5
  170. case "$compression" in
  171. normal) :;;
  172. dirty)
  173. multipass=
  174. speedpreset_h264=veryfast
  175. crf_vp8=0
  176. crf_vp9=0
  177. qscale_theora=3
  178. cpu_vp8=5
  179. ;;
  180. hq)
  181. speedpreset_h264=veryslow
  182. crf_vp8=10
  183. crf_vp9=10
  184. qscale_theora=6
  185. cpu_vp8=0
  186. cpu_vp9=1
  187. ;;
  188. *) exit1 "Unknown compression mode \"$compression\".";;
  189. esac
  190. # parse/resolve size and framerate
  191. case "$profile" in
  192. '') :;;
  193. *@*)
  194. while read s r foo; do
  195. profilesize="${size:-$s}"
  196. framerate="${framerate:-$r}"
  197. done << EOF
  198. $(echo "$profile" | perl -F@ -anE 'say join " ", @F')
  199. EOF
  200. ;;
  201. *p*)
  202. while read s r foo; do
  203. profilesize="${size:-${s}p}"
  204. framerate="${framerate:-$r}"
  205. done << EOF
  206. $(echo "$profile" | perl -Fp -anE 'say join " ", @F')
  207. EOF
  208. ;;
  209. *)
  210. profilesize="$profile"
  211. ;;
  212. esac
  213. size=${size:-$profilesize}
  214. case "$size" in
  215. qvga) size=320x240;;
  216. hvga) size=480x360;;
  217. vga) size=640x480;;
  218. svga) size=800x600;;
  219. xga) size=1024x768;;
  220. 240p|wqvga) size=424x240;;
  221. 360p|nhd) size=640x360;;
  222. 480p|wvga) size=848x480;;
  223. 576p|wsvga) size=1024x576;;
  224. 720p|wxga|hd) size=1280x720;;
  225. esac
  226. if [ -n "$size" ]; then
  227. while read w h foo; do
  228. width="${width:-$w}"
  229. height="${height:-$h}"
  230. done << EOF
  231. $(echo "$size" | perl -Fx -anE 'say join " ", @F')
  232. EOF
  233. if [ -z "$width" ] || [ -z "$height" ]; then
  234. exit1 "Failed to parse size \"$size\"."
  235. fi
  236. fi
  237. case "$framerate" in
  238. */*)
  239. while read n d foo; do
  240. framerate_num="${framerate_num:-$n}"
  241. framerate_den="${framerate_den:-$d}"
  242. done << EOF
  243. $(echo "$framerate" | perl -F/ -anE 'say join " ", @F')
  244. EOF
  245. ;;
  246. ?*)
  247. framerate_num="$framerate"
  248. framerate_den=1
  249. ;;
  250. esac
  251. # resolve input size and framerate (needed for computing bitrate)
  252. while read w h r foo; do
  253. width_in="${width_in:-$w}"
  254. height_in="${height_in:-$h}"
  255. framerate_in="${framerate_in:-$r}"
  256. done << EOF
  257. $(mediainfo --Inform="Video;%Width% %Height% %FrameRate%" "$infile_first")
  258. EOF
  259. case "$video" in
  260. talkinghead)
  261. refbpp="${refbpp:-0.1}"
  262. x264tune=film
  263. ;;
  264. action)
  265. refbpp="${refbpp:-0.15}"
  266. x264tune=film
  267. ;;
  268. '')
  269. refbpp="${refbpp:-0.12}"
  270. ;;
  271. *) exit1 "Unknown video style \"$video\".";;
  272. esac
  273. for format in $(echo "$formats" | sed -e 's/,/ /g'); do
  274. case $format in
  275. theora|ogg) ogg=yes;;
  276. h264|mp4) mp4=yes;;
  277. vp8|webm) webm=yes;;
  278. vp9) vp9=yes;;
  279. *) exit1 "Unknown format \"$format\".";;
  280. esac
  281. done
  282. _width="${width:-$width_in}"
  283. _height="${height:-$height_in}"
  284. if [ -n "$_width" ] && [ -n "$_height" ]; then
  285. _pixels="$(($_width*$_height))"
  286. fi
  287. _frames="${framerate:-$framerate_in}"
  288. sizepreset_vpx=libvpx-360p
  289. if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then
  290. sizepreset_vpx=libvpx-720p
  291. if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then
  292. sizepreset_vpx=libvpx-720p50_60
  293. fi
  294. fi
  295. # compute average bitrate from reference data and "power of .75" rule
  296. bitrate_vp8="$bitrate"
  297. bitrate_vp9="$bitrate"
  298. if [ -n "$_pixels" ] && [ -n "$_frames" ]; then
  299. bitrate=$(perl -E '$refsize=640*360;' \
  300. -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )")
  301. bitrate_vp8=$(perl -E "say int( +($bitrate*$factor_vp8) )") #"
  302. bitrate_vp9=$(perl -E "say int( +($bitrate*$factor_vp9) )") #"
  303. fi
  304. # default per-codec-channel bitrates
  305. quality_vorbis=3
  306. bitrate_opus=48
  307. bitrate_aac=64
  308. channels=-1
  309. maxchannels=2
  310. case "$audio" in
  311. music)
  312. opusapp=audio
  313. ;;
  314. hqspeech)
  315. channels=1
  316. quality_vorbis=1
  317. bitrate_opus=32
  318. opusapp=voip
  319. ;;
  320. speech)
  321. channels=1
  322. quality_vorbis=1
  323. bitrate_opus=32
  324. compress=yes
  325. [ -z "$melt_recent" ] || _melt_loudness="$loudness_data"
  326. limit=yes
  327. opusapp=voip
  328. ;;
  329. silence)
  330. channels=0
  331. ;;
  332. '')
  333. :
  334. ;;
  335. *) exit1 "Unknown audio style \"$audio\".";;
  336. esac
  337. [ $channels -ge 0 ] || channels=$(avprobe -v warning -show_streams "$infile_first" | perl -ne 's/channels=// and print $_' || echo -1)
  338. [ $channels -le $maxchannels ] || channels=$maxchannels
  339. [ $channels -gt 0 ] || channels=
  340. # generic options
  341. melt="melt -progress"
  342. avconv="avconv -threads auto -y -v warning"
  343. _melt_sample="$infile_first ${sample:+in=${samplestart:-0} out=$((${samplestart:-0} + samplelength))}"
  344. _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}"
  345. _melt_audio="${channels:+ac=$channels}"
  346. # filter options
  347. # limit (i.e. avoid peaks "clipping")
  348. _melt_postfilters_audio="${limit:+-filter ladspa.1077}"
  349. # codec options
  350. _melt_theora="vcodec=libtheora${bitrate:+ vb=$bitrate} qscale=$qscale_theora"
  351. _melt_vp8="vcodec=libvpx vpreset=$sizepreset_vpx${bitrate_vp8:+ vb=$bitrate_vp8 minrate=$((bitrate_vp8/20)) maxrate=$((bitrate_vp8*12))} crf=$crf_vp8 cpu-used=$cpu_vp8"
  352. _avconv_vp8="-c:v libvpx -pre:v $sizepreset_vpx${bitrate_vp8:+ -b:v $bitrate_vp8 -minrate $((bitrate_vp8/20)) -maxrate $((bitrate_vp8*12))} -crf $crf_vp8 -cpu-used $cpu_vp8"
  353. # CRF ignored with libvpx 1.3
  354. _melt_vp9="vcodec=libvpx-vp9 vpreset=$sizepreset_vpx${bitrate_vp9:+ vb=$bitrate_vp9 minrate=$((bitrate_vp9/20)) maxrate=$((bitrate_vp9*12))} crf=$crf_vp9 cpu-used=$cpu_vp9"
  355. _avconv_vp9="-c:v libvpx-vp9 -pre:v $sizepreset_vpx${bitrate_vp9:+ -b:v $bitrate_vp9 -minrate $((bitrate_vp9/20)) -maxrate $((bitrate_vp9*12))} -crf $crf_vp9 -cpu-used $cpu_vp9"
  356. _melt_h264="vcodec=libx264 vpreset=$speedpreset_h264 vprofile=baseline${x264tune:+ tune=$x264tune} threads=0 movflags=+faststart crf=$crf_h264"
  357. _melt_vorbis="$_melt_audio acodec=libvorbis aq=$quality_vorbis"
  358. _melt_opus="$_melt_audio acodec=libopus${channels:+ ab=$(($channels*$bitrate_opus))k}${opusapp:+ application=$opusapp}"
  359. _melt_aac="$_melt_audio acodec=aac${channels:+ ab=$(($channels*$bitrate_aac))k}"
  360. # container options
  361. _melt_ogg="f=ogg $_melt_video $_melt_theora $_melt_vorbis"
  362. _melt_webm="f=webm $_melt_video $_melt_vp8 $_melt_vorbis"
  363. _melt_webm_vp9="f=webm $_melt_video $_melt_vp9 $_melt_opus"
  364. _melt_mp4="f=mp4 $_melt_video $_melt_h264 $_melt_aac"
  365. _melt_img="f=image2 $_melt_video"
  366. # resolve EBU R128 audio normalizing
  367. if [ -n "$loudness" ] && [ -z "$_melt_loudness" ]; then
  368. echo "Analyzing loudness data..."
  369. $melt ${_melt_sample:-$infiles} $audioprefilters -filter loudness -consumer xml:$stem.xml $_melt_audio video_off=1 all=1
  370. _melt_loudness="$(perl -ne 'm!<property name="results">([^<]+)</property>! and print $1' $stem.xml)"
  371. echo "Loudness data: $_melt_loudness"
  372. fi
  373. if [ -n "$multipass" ] && [ -n "$webm$vp9" ]; then
  374. echo "Analyzing complexity for WebM..."
  375. $melt ${_melt_sample:-$infiles} $filters \
  376. -consumer avformat:pipe:1 $_melt_video f=yuv4mpegpipe pix_fmt=yuv420p an=1 audio_off=1 \
  377. | $avconv -i pipe:0 \
  378. ${webm:+-f rawvideo $_avconv_vp8 -an -pass 1 -passlogfile ${stem}_vp8 /dev/null} \
  379. ${vp9:+-f rawvideo $_avconv_vp9 -an -pass 1 -passlogfile ${stem}_vp9 /dev/null}
  380. [ -z "$webm" ] || mv -f ${stem}_vp8-*.log ${stem}_vp8_2pass.log
  381. [ -z "$vp9" ] || mv -f ${stem}_vp9-*.log ${stem}_vp9_2pass.log
  382. fi
  383. echo "Encoding video..."
  384. $melt ${_melt_sample:-$infiles} \
  385. ${channels:+$audioprefilters${_melt_loudness:+ -filter loudness results="$_melt_loudness"}} \
  386. $filters${channels:+ $_melt_postfilters_audio} \
  387. ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg} \
  388. ${webm:+-consumer avformat:$stem.webm $_melt_webm${multipass:+ pass=2 passlogfile=${stem}_vp8}} \
  389. ${vp9:+-consumer avformat:${stem}_vp9.webm $_melt_webm_vp9${multipass:+ pass=2 passlogfile=${stem}_vp9}} \
  390. ${mp4:+-consumer avformat:$stem.mp4 $_melt_mp4}
  391. if [ -n "$mp4" ] && [ -z "$melt_recent" ]; then
  392. mv "$stem.mp4" "$stem.mp4"~
  393. qt-faststart "$stem.mp4"~ "$stem.mp4"
  394. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  395. fi
  396. # cleanup audio normalize hinting
  397. rm -f $stem.xml
  398. # JPEG preview
  399. $melt $infile_first in=0 out=0 \
  400. -group $filters \
  401. -consumer avformat:$stem.jpg $_melt_img
  402. __width="${_width:+ width=\"$_width\"}"
  403. __height="${_height:+ height=\"$_height\"}"
  404. # Flash object needs extra space for controllers
  405. __heightplus=${_height:+ height=\"$(($_height+4))\"}
  406. _source_ogg="<source src=\"$stem.ogv\" type=\"video/ogg\" />"
  407. _source_webm="<source src=\"$stem.webm\" type=\"video/webm\" />"
  408. _source_vp9="<source src=\"${stem}_vp9.webm\" type='video/ogg; codecs=\"vp9, opus\"' />"
  409. _source_mp4="<source src=\"$stem.mp4\" type=\"video/mp4\" />"
  410. [ -z "$flashplayer" ] || flash=yes
  411. [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled."
  412. _object_flash="<object$__width$__heightplus type=\"application/x-shockwave-flash\" data=\"$flashplayer.swf\">"
  413. _param_name="<param name=\"movie\" value=\"$flashplayer.swf\" />"
  414. _param_flashvars="<param name=\"flashvars\" value=\"image=$stem.jpg&amp;file=$stem.mp4\" />"
  415. __oggfile=${ogg:+open format <a href=\"$stem.ogv\">Ogg</a>}
  416. __webmfile=${webm:+open format <a href=\"$stem.webm\">WebM (VP8)</a>}
  417. __vp9file=${vp9:+open format <a href=\"${stem}_vp9.webm\">WebM (VP9/Opus)</a>}
  418. __mp4file=${mp4:+closed format <a href=\"$stem.mp4\">MPEG-4</a>}
  419. cat >"$stem.html" <<EOF
  420. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  421. <video$__width$__height preload controls>
  422. ${mp4:+$_source_mp4
  423. }${vp9:+$_source_vp9
  424. }${webm:+$_source_webm
  425. }${ogg:+$_source_ogg
  426. }${flash:+$_object_flash
  427. $_param_name
  428. $_param_flashvars
  429. }<img src="$stem.jpg"$__width$__height alt="$title"
  430. title="No video playback capabilities, please download the video below" />
  431. ${flash:+</object>
  432. }</video>
  433. <p><strong>Download Video:</strong><ul>
  434. ${vp9:+<li>$__vp9file
  435. }${webm:+<li>$__webmfile
  436. }${ogg:+<li>$__oggfile
  437. }${mp4:+<li>$__mp4file
  438. }</ul></p>
  439. EOF