summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 1766ca726e9e2d4de034d87d59b8abb929d72e7b (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. #
  25. # Possible flashplayers:
  26. # http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/
  27. #
  28. # TODO: offer to skip rendering again if an output file exist already
  29. # TODO: support --width and --height (resolving the other part from input/forced aspect ratio)
  30. # TODO: support --formats (comma-separated, to allow e.g. excluding webm even if supported)
  31. # TODO: add --speech option, using mono, lower audio rate, and (when solved how) speex
  32. set -e
  33. PRG=$(basename "$0")
  34. showhelp() {
  35. cat <<EOF
  36. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  37. Encode video file in multiple web-optimized formats, and provide sample
  38. html favoring open formats with optional non-JavaScript Flash fallback.
  39. -p, --profile Video format:
  40. [modulus 16]
  41. 320x240 qvga 240p 432x240 wqvga
  42. 640x480 vga 480p 848x480 wvga
  43. 576p 1024x576 wsvga
  44. 1024x768 xga hd 720p 1280x720 wxga
  45. [modulus 8]
  46. 480x360 hvga nhd 360p 640x360
  47. 800x600 svga
  48. -s, --size Output size (ffmpeg): WIDTHxHEIGHT vga qcif etc.
  49. (default: use input size)
  50. -a, --aspect Display Aspect Ratio in melt format e.g. @16/9
  51. (default: no aspect hinting)
  52. -r, --rate Video framerate
  53. (default: 25)
  54. -b, --bitrate Video bitrate in bytes, with optional ISO suffix
  55. (default: none)
  56. --h264profile MPEG-4 AVC target profile: baseline main high
  57. (default: baseline)
  58. --h264preset MPEG-4 AVC target preset: slow ultrafast etc.
  59. (default: medium)
  60. --webmpreset WebM target preset: 360p 720p 720p50_60 etc.
  61. (default: profile-related or none)
  62. --audio Audio style: music speech silence
  63. (default: none - use input channel count)
  64. --stem Stem of output filenames, optionally with path
  65. (default: basename of last input file)
  66. -t, --title Title used in html fallback graphics
  67. (default: stem)
  68. --filter Add melt filter (applied to all input files)
  69. --sample[=frame] Limit output to a 150 frames sample from
  70. beginning or optionally a later start frame
  71. -h, --help This help text
  72. Examples:
  73. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  74. $PRG -p 480p --stem funny -t "Funny guy" myvideo.dv
  75. Options before input files are passed to melt producer, and after to
  76. melt avformat consumer.
  77. When video bitrate is set, 2-pass encoding is used for MPEG-4 output.
  78. Hints:
  79. * Use max. 640x480 pixel size for widest compatibility
  80. (ie. square-pixel 640x360 or anamorphic svcd_ntsc_wide for 16:9)
  81. * Use a modulus 16 profile (qvga vga 480p 720p) for best compression.
  82. * Append these for single-mic speech: ac=1 ar=32000 ab=64k
  83. * Set bitrate for optimal MPEG-4/WebM bits-per-pixel ratio:
  84. + talking head: 0.1
  85. + high-motion or detailed content: 0.15
  86. (inspect result with mediainfo)
  87. More info: <http://camendesign.com/code/video_for_everybody>
  88. <http://www.streaminglearningcenter.com/>
  89. <http://en.wikipedia.org/wiki/HTML5_video>
  90. EOF
  91. }
  92. exit1() {
  93. response="${1:+Error: }${1:-Internal error!}"
  94. echo >&2 "$response"
  95. exit 1
  96. }
  97. # defaults
  98. rate=25
  99. h264profile=baseline
  100. # parse cmdline options
  101. 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."
  102. eval set -- "$TEMP"
  103. while true ; do
  104. case "$1" in
  105. -h|--help) showhelp; exit;;
  106. -p|--profile) profile="$2"; shift 2;;
  107. -s|--size) size="$2"; shift 2;;
  108. -a|--aspect) aspect="$2"; shift 2;;
  109. -r|--rate) rate="$2"; shift 2;;
  110. -b|--bitrate) bitrate="$2"; shift 2;;
  111. --h264profile) h264profile="$2"; shift 2;;
  112. --h264preset) h264preset="$2"; shift 2;;
  113. --webmpreset) webmpreset="$2"; shift 2;;
  114. --audio) audio="$2"; shift 2;;
  115. --stem) stem="$2"; shift 2;;
  116. -t|--title) title="$2"; shift 2;;
  117. --filter) filters="${filters:+$filters }-filter $2"; shift 2
  118. while [ $# -gt 0 ] ; do
  119. case "$1" in
  120. *=*) filters="${filters:+$filters }$1"; shift;;
  121. *) break;;
  122. esac
  123. done
  124. ;;
  125. --sample) sample="in=${2:-0} out=$((${2:-0} + 150))"; shift 2;;
  126. --) shift; break;;
  127. *) exit1 "Internal error resolving options.";;
  128. esac
  129. done
  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|432x240|wqvga)
  179. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  180. _melt_video="${size:-width=432 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="progressive=1 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. ;;
  225. speech)
  226. channels=1
  227. bitrate_vorbis=48
  228. bitrate_aac=64
  229. ;;
  230. silence)
  231. channels=0
  232. ;;
  233. '')
  234. channels=$(avprobe -v warning -show_streams "$infile_first" | perl -ne 's/channels=// and print $_')
  235. ;;
  236. *) exit1 "Unknown audio style \"$audio\".";;
  237. esac
  238. [ $channels -le 2 ] || channels=2
  239. [ $channels -gt 0 ] || channels=
  240. # TODO: Check and fail if all needed tools are not available
  241. # TODO: When verified beneficial, add option real_time=-2
  242. _melt_in="${_melt_in:+$_melt_in }-progress $sample"
  243. _melt_video="$_melt_video ${bitrate:+vb=${bitrate}} ${size:+s=$size} ${aspect:+aspect=$aspect}"
  244. _melt_ogg="f=ogg vcodec=libtheora ${bitrate:-qscale=5}"
  245. _melt_h264="$_melt_h264 ${bitrate:-qscale=5}"
  246. _melt_webm="${webmpreset:+-vpre=libvpx-$webmpreset}"
  247. _melt_audio="${channels:+ac=$channels}"
  248. _melt_vorbis="$_melt_audio acodec=libvorbis ab=$(($channels*$bitrate_vorbis))k"
  249. _melt_aac="$_melt_audio acodec=aac ab=$(($channels*$bitrate_aac))k"
  250. ## Theora/Vorbis/Ogg
  251. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.ogv" $_melt_ogg $_melt_video $_melt_vorbis $_melt_out
  252. ## H.264/AAC/MP4
  253. [ -z "$bitrate" ] || melt -group $_melt_in $infiles -group $filters -consumer avformat:/dev/null properties=x264-medium-pass1 $_melt_h264 $_melt_video $_melt_out
  254. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.mp4" ${bitrate:+pass=2} $_melt_h264 $_melt_video $_melt_aac $_melt_out
  255. # TODO: drop qt-faststart when melt 0.9.2 is stable
  256. mv "$stem.mp4" "$stem.mp4"~
  257. qt-faststart "$stem.mp4"~ "$stem.mp4"
  258. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  259. ## VP8/Vorbis/WebM
  260. # TODO: use two-pass when supported by melt
  261. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.webm" properties=webm $_melt_webm $_melt_video $_melt_vorbis $_melt_out
  262. ## JPEG preview
  263. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  264. # resolve width and height from preview image
  265. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  266. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  267. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  268. heightplus=${height:+$(($height+4))}
  269. # TODO: resolve flash player to use
  270. [ -z "$flashplayer" ] || flash=yes
  271. cat >"$stem.html" <<EOF
  272. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  273. <video width="$width" height="$height" preload controls>
  274. <source src="$stem.mp4" type="video/mp4" />
  275. <source src="$stem.webm" type="video/webm" />
  276. <source src="$stem.ogv" type="video/ogg" />
  277. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  278. <param name="movie" value="$flashplayer.swf" />
  279. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  280. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  281. title="No video playback capabilities, please download the video below" />
  282. ${flash:+</object>
  283. }</video>
  284. <p><strong>Download Video:</strong>
  285. open format <a href="$stem.webm">WebM</a>,
  286. open format <a href="$stem.ogv">Ogg</a>,
  287. closed Format <a href="$stem.mp4">MPEG-4</a>.
  288. </p>
  289. EOF