summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 00613e172aabacf1df20450791336446b8bd0a15 (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 ffmpegthumbnailer
  19. #
  20. # TODO: offer to skip rendering again if an output file exist already
  21. # TODO: support --width and --height (resolving the other part from input/forced aspect ratio)
  22. # TODO: support --formats (comma-separated, to allow e.g. excluding webm even if supported)
  23. set -e
  24. PRG=$(basename "$0")
  25. showhelp() {
  26. cat <<EOF
  27. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  28. Encode video file in multiple web-optimized formats, and provide sample
  29. html favoring open formats with optional non-JavaScript Flash fallback.
  30. -p, --profile Video format:
  31. [modulus 16]
  32. 320x240 qvga
  33. 640x480 vga 480p 848x480 wvga
  34. 576p 1024x576 wsvga
  35. 1024x768 xga hd 720p 1280x720 wxga
  36. [modulus 8]
  37. 240p 424x240 wqvga
  38. 480x360 hvga nhd 360p 640x360
  39. 800x600 svga
  40. -s, --size Output size (ffmpeg): WIDTHxHEIGHT vga qcif etc.
  41. (default: use input size)
  42. -a, --aspect Display Aspect Ratio in melt format e.g. @16/9
  43. (default: no aspect hinting)
  44. -r, --rate Video framerate
  45. (default: 25)
  46. -b, --bitrate Video bitrate in bytes, with optional ISO suffix
  47. (default: none)
  48. --h264profile MPEG-4 AVC target profile: baseline main high
  49. (default: baseline)
  50. --h264preset MPEG-4 AVC target preset: slow ultrafast etc.
  51. (default: medium)
  52. --webmpreset WebM target preset: 360p 720p 720p50_60 etc.
  53. (default: profile-related or none)
  54. --audio Audio style:
  55. channels compress normalize limit
  56. music max 2 X X
  57. speech 1 X X X
  58. silence 0
  59. (default: none - use input channel count)
  60. --stem Stem of output filenames, optionally with path
  61. (default: basename of last input file)
  62. -t, --title Title used in html fallback graphics
  63. (default: stem)
  64. --filter Add melt filter (applied to all input files)
  65. --sample[=frame] Limit output to a 150 frames sample from
  66. beginning or optionally a later start frame
  67. -h, --help This help text
  68. Examples:
  69. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  70. $PRG -p 480p --stem funny -t "Funny guy" myvideo.dv
  71. Options before input files are passed to melt producer, and after to
  72. melt avformat consumer.
  73. When video bitrate is set, 2-pass encoding is used for MPEG-4 output.
  74. Hints:
  75. * Use square-pixel max. 480 width (vga 480p) for widest compatibility.
  76. * Use a modulus 16 profile (qvga vga 480p 720p) for best compression.
  77. * Set bitrate for optimal MPEG-4/WebM bits-per-pixel ratio:
  78. + talking head: 0.1
  79. + high-motion or detailed content: 0.15
  80. (inspect bits-per-pixel ratio with mediainfo)
  81. More info:
  82. <http://camendesign.com/code/video_for_everybody>
  83. <http://www.streaminglearningcenter.com/articles/configuring-your-streaming-video-(for-newbies).html>
  84. <http://en.wikipedia.org/wiki/HTML5_video>
  85. <http://www.penguinproducer.com/2012/01/ladspa-noise-removal/>
  86. <http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/>
  87. EOF
  88. }
  89. exit1() {
  90. response="${1:+Error: }${1:-Internal error!}"
  91. echo >&2 "$response"
  92. exit 1
  93. }
  94. # defaults
  95. rate=25
  96. h264profile=baseline
  97. # parse cmdline options
  98. TEMP="`getopt -s sh -o hp:s:a:r:b:t: -l help,profile:,size:,aspect:,rate:,bitrate:,h264profile:,h264preset:,webmpreset:,audio:,stem:,title:,filter:,sample:: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  99. eval set -- "$TEMP"
  100. while true ; do
  101. case "$1" in
  102. -h|--help) showhelp; exit;;
  103. -p|--profile) profile="$2"; shift 2;;
  104. -s|--size) size="$2"; shift 2;;
  105. -a|--aspect) aspect="$2"; shift 2;;
  106. -r|--rate) rate="$2"; shift 2;;
  107. -b|--bitrate) bitrate="$2"; shift 2;;
  108. --h264profile) h264profile="$2"; shift 2;;
  109. --h264preset) h264preset="$2"; shift 2;;
  110. --webmpreset) webmpreset="$2"; shift 2;;
  111. --audio) audio="$2"; shift 2;;
  112. --stem) stem="$2"; shift 2;;
  113. -t|--title) title="$2"; shift 2;;
  114. --filter) filters="${filters:+$filters }-filter $2"; shift 2
  115. while [ $# -gt 0 ] ; do
  116. case "$1" in
  117. *=*) filters="${filters:+$filters }$1"; shift;;
  118. *) break;;
  119. esac
  120. done
  121. ;;
  122. --sample) sample="in=${2:-0} out=$((${2:-0} + 150))"; shift 2;;
  123. --) shift; break;;
  124. *) exit1 "Internal error resolving options.";;
  125. esac
  126. done
  127. # Resolve if melt is version 0.9.2 or newer
  128. # TODO: drop when melt 0.9.2 is stable
  129. melt_recent=$(melt -query filter=loudness | grep -i R128)
  130. while [ $# -gt 0 ] ; do
  131. case "$1" in
  132. *=*) _melt_in="${_melt_in:+$_melt_in }$1"; shift;;
  133. *) break;;
  134. esac
  135. done
  136. while [ $# -gt 0 ] ; do
  137. case "$1" in
  138. *=*) _melt_out="${_melt_out:+$_melt_out }$1"; shift;;
  139. *) infiles="${infiles:+$infiles }$1"; shift;;
  140. esac
  141. done
  142. if [ -z "$infiles" ]; then
  143. showhelp
  144. exit1 "Too few parameters!"
  145. fi
  146. # input filename (mandatory)
  147. infile_first=$(perl -e 'print pop @ARGV' $infiles)
  148. [ -e "$infile_first" ] || exit1 "Input file missing!"
  149. # resolve stem and title (if not explicitly set)
  150. stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')}
  151. title=${title:-$stem}
  152. case "$profile" in
  153. 320x240|qvga)
  154. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  155. _melt_video="${size:-width=320 height=240}${_melt_video:+ $_melt_video}"
  156. webmpreset="${webmpreset:-360p}"
  157. ;;
  158. 480x360|hvga)
  159. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  160. _melt_video="${size:-width=480 height=360}${_melt_video:+ $_melt_video}"
  161. webmpreset="${webmpreset:-360p}"
  162. ;;
  163. 640x480|vga)
  164. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  165. _melt_video="${size:-width=640 height=480}${_melt_video:+ $_melt_video}"
  166. webmpreset="${webmpreset:-360p}"
  167. ;;
  168. 800x600|svga)
  169. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  170. _melt_video="${size:-width=800 height=600}${_melt_video:+ $_melt_video}"
  171. webmpreset="${webmpreset:-360p}"
  172. ;;
  173. 1024x768|xga)
  174. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  175. _melt_video="${size:-width=1024 height=768}${_melt_video:+ $_melt_video}"
  176. webmpreset="${webmpreset:-720p}"
  177. ;;
  178. 240p|424x240|wqvga)
  179. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  180. _melt_video="${size:-width=424 height=240}${_melt_video:+ $_melt_video}"
  181. webmpreset="${webmpreset:-360p}"
  182. ;;
  183. 360p|640x360|nhd)
  184. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  185. _melt_video="${size:-width=640 height=360}${_melt_video:+ $_melt_video}"
  186. webmpreset="${webmpreset:-360p}"
  187. ;;
  188. 480p|848x480|wvga)
  189. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  190. _melt_video="${size:-width=848 height=480}${_melt_video:+ $_melt_video}"
  191. webmpreset="${webmpreset:-360p}"
  192. ;;
  193. 576p|1024x576|wsvga)
  194. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  195. _melt_video="${size:-width=1024 height=576}${_melt_video:+ $_melt_video}"
  196. webmpreset="${webmpreset:-360p}"
  197. ;;
  198. 720p|1280x720|wxga|hd)
  199. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  200. webmpreset="${webmpreset:-720p}"
  201. ;;
  202. '')
  203. _melt_video="frame_rate_den=1${_melt_video:+ $_melt_video}"
  204. [ "25" != "$rate" ] || _melt_video="{_melt_video:+$_melt_video }frame_rate_num=$rate"
  205. ;;
  206. *) exit1 "Unknown profile \"$profile\" - please specify size and aspect directly.";;
  207. esac
  208. [ "25" = "$rate" ] || _melt_video="{_melt_video:+$_melt_video }frame_rate_num=$rate"
  209. case "$h264profile" in
  210. baseline|main)
  211. _melt_h264="properties=x264-medium-$h264profile ${h264preset:+-vpre=libx264-$h264preset}"
  212. ;;
  213. high)
  214. _melt_h264="properties=x264-medium ${h264preset:+-vpre=libx264-$h264preset}"
  215. ;;
  216. *) exit1 "Unknown MPEG-4 AVC profile \"$h264profile\".";;
  217. esac
  218. # default per-codec-channel bitrates
  219. bitrate_vorbis=64
  220. bitrate_aac=96
  221. case "$audio" in
  222. music)
  223. channels=2
  224. # limit (i.e. avoid peaks "clipping")
  225. _melt_audio_filters="-filter ladspa.1077"
  226. ;;
  227. speech)
  228. channels=1
  229. bitrate_vorbis=48
  230. bitrate_aac=64
  231. # expand and compress (i.e. lower volume on silent and loud passages)
  232. _melt_audio_filters_early="-filter ladspa.1075 -filter ladspa.1073"
  233. # limit (i.e. avoid peaks "clipping")
  234. _melt_audio_filters="-filter ladspa.1077"
  235. ;;
  236. silence)
  237. channels=0
  238. ;;
  239. '')
  240. channels=$(avprobe -v warning -show_streams "$infile_first" | perl -ne 's/channels=// and print $_')
  241. ;;
  242. *) exit1 "Unknown audio style \"$audio\".";;
  243. esac
  244. [ $channels -le 2 ] || channels=2
  245. [ $channels -gt 0 ] || channels=
  246. # TODO: Check and fail if all needed tools are not available
  247. # TODO: When verified beneficial, add option real_time=-2
  248. melt="melt -progress"
  249. _melt_in="${_melt_in:+$_melt_in }$sample"
  250. _melt_video="progressive=1 $_melt_video${bitrate:+ vb=${bitrate}}${size:+ s=$size}${aspect:+ aspect=$aspect}"
  251. _melt_ogg="$_melt_video f=ogg vcodec=libtheora${bitrate:- qscale=5}"
  252. _melt_h264="$_melt_video $_melt_h264${bitrate:- qscale=5}"
  253. _melt_webm="$_melt_video${webmpreset:+ vpre=libvpx-$webmpreset}"
  254. _melt_audio="${channels:+ac=$channels}"
  255. _melt_vorbis="$_melt_audio acodec=libvorbis ab=$(($channels*$bitrate_vorbis))k"
  256. _melt_aac="$_melt_audio acodec=aac ab=$(($channels*$bitrate_aac))k"
  257. # resolve EBU R128 audio normalizing
  258. # TODO: normalize each infile separately when xml fed as infile keeps sync
  259. if [ -n "$melt_recent" ] && [ -n "$channels" ]; then
  260. $melt -group $_melt_in $infiles -group $_melt_audio_filters_early -filter loudness -consumer xml:$stem.xml $_melt_audio video_off=1 all=1
  261. _melt_loudness="$(perl -ne 'm!<property name="results">([^<]+)</property>! and print $1' $stem.xml)"
  262. fi
  263. ## Theora/Vorbis/Ogg
  264. $melt -group $_melt_in $infiles -group ${channels:+$_melt_audio_filters_early${_melt_loudness:+ -filter loudness results="$_melt_loudness"} $_melt_audio_filters }$filters -consumer avformat:"$stem.ogv" $_melt_ogg $_melt_vorbis $_melt_out
  265. ## H.264/AAC/MP4
  266. [ -z "$bitrate" ] || $melt -group $_melt_in $infiles -group $filters -consumer avformat:/dev/null properties=x264-medium-pass1 $_melt_h264 $_melt_out
  267. $melt -group $_melt_in $infiles -group ${channels:+$_melt_audio_filters_early${_melt_loudness:+ -filter loudness results="$_melt_loudness"} $_melt_audio_filters }$filters -consumer avformat:"$stem.mp4" ${bitrate:+pass=2} $_melt_h264 $_melt_aac $_melt_out
  268. if [ -z "$melt_recent" ]; then
  269. mv "$stem.mp4" "$stem.mp4"~
  270. qt-faststart "$stem.mp4"~ "$stem.mp4"
  271. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  272. fi
  273. ## VP8/Vorbis/WebM
  274. # TODO: use two-pass when supported by melt
  275. $melt -group $_melt_in $infiles -group ${channels:+$_melt_audio_filters_early${_melt_loudness:+ -filter loudness results="$_melt_loudness"} $_melt_audio_filters }$filters -consumer avformat:"$stem.webm" properties=webm $_melt_webm $_melt_vorbis $_melt_out
  276. # cleanup audio normalize hinting
  277. rm -f $stem.xml
  278. ## JPEG preview
  279. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  280. # resolve width and height from preview image
  281. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  282. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  283. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  284. heightplus=${height:+$(($height+4))}
  285. # TODO: resolve flash player to use
  286. [ -z "$flashplayer" ] || flash=yes
  287. cat >"$stem.html" <<EOF
  288. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  289. <video width="$width" height="$height" preload controls>
  290. <source src="$stem.mp4" type="video/mp4" />
  291. <source src="$stem.webm" type="video/webm" />
  292. <source src="$stem.ogv" type="video/ogg" />
  293. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  294. <param name="movie" value="$flashplayer.swf" />
  295. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  296. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  297. title="No video playback capabilities, please download the video below" />
  298. ${flash:+</object>
  299. }</video>
  300. <p><strong>Download Video:</strong>
  301. open format <a href="$stem.webm">WebM</a>,
  302. open format <a href="$stem.ogv">Ogg</a>,
  303. closed Format <a href="$stem.mp4">MPEG-4</a>.
  304. </p>
  305. EOF