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