summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: c0c0e3a534bdb5b2f4f9f9f02ba2c820ed09b81e (plain)
  1. #!/bin/sh
  2. # Origins:
  3. # http://diveintohtml5.org/video.html
  4. # http://camendesign.com/code/video_for_everybody
  5. # http://www.streaminglearningcenter.com/articles/so-you-want-to-get-to-know-h264.html
  6. # Possible flashplayers:
  7. # http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/
  8. # TODO: offer to skip rendering again if an output file exist already
  9. # TODO: support --width and --height (resolving the other part from input/forced aspect ratio)
  10. # TODO: support --formats (comma-separated, to allow e.g. excluding webm even if supported)
  11. # TODO: add --speech option, using mono, lower audio rate, and (when solved how) speex
  12. set -e
  13. PRG=$(basename "$0")
  14. showhelp() {
  15. cat <<EOF
  16. Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...]
  17. Encode video file in multiple web-optimized formats, and provide sample
  18. html favoring open formats with optional non-JavaScript Flash fallback.
  19. -p, --profile Video format:
  20. [modulus 16]
  21. 320x240 qvga 240p 432x240 wqvga
  22. 640x480 vga 480p 848x480 wvga
  23. 576p 1024x576 wsvga
  24. 1024x768 xga hd 720p 1280x720 wxga
  25. [modulus 8]
  26. 480x360 hvga nhd 360p 640x360
  27. 800x600 svga
  28. -s, --size Output size (ffmpeg): WIDTHxHEIGHT vga qcif etc.
  29. (default: use input size)
  30. -a, --aspect Display Aspect Ratio in melt format e.g. @16/9
  31. (default: no aspect hinting)
  32. -r, --rate Video framerate
  33. (default: 25)
  34. -b, --bitrate Video bitrate in bytes, with optional ISO suffix
  35. (default: none)
  36. --h264profile MPEG-4 AVC target profile (baseline medium)
  37. (default: baseline)
  38. --stem Stem of output filenames, optionally with path
  39. (default: basename of last input file)
  40. -t, --title Title used in html fallback graphics
  41. (default: stem)
  42. --filter Add melt filter (applied to all input files)
  43. --sample[=frame] Limit output to a 150 frames sample from
  44. beginning or optionally a later start frame
  45. -h, --help This help text
  46. Examples:
  47. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  48. $PRG -p 480p --stem funny -t "Funny guy" myvideo.dv
  49. Options before input files are passed to melt producer, and after to
  50. melt avformat consumer.
  51. When video bitrate is set, 2-pass encoding is used for MPEG-4 output.
  52. Hints:
  53. * Use max. 640x480 pixel size for widest compatibility
  54. (ie. square-pixel 640x360 or anamorphic svcd_ntsc_wide for 16:9)
  55. * Use a modulus 16 profile (qvga vga 480p 720p) for best compression.
  56. * Append these for single-mic speech: ac=1 ar=32000 ab=64k
  57. * Set bitrate for optimal MPEG-4/WebM bits-per-pixel ratio:
  58. + talking head: 0.1
  59. + high-motion or detailed content: 0.15
  60. (inspect result with mediainfo)
  61. More info: <http://camendesign.com/code/video_for_everybody>
  62. <http://www.streaminglearningcenter.com/>
  63. <http://en.wikipedia.org/wiki/HTML5_video>
  64. EOF
  65. }
  66. exit1() {
  67. response="${1:+Error: }${1:-Internal error!}"
  68. echo >&2 "$response"
  69. exit 1
  70. }
  71. # defaults
  72. rate=25
  73. h264profile=baseline
  74. # parse cmdline options
  75. TEMP="`getopt -s sh -o hp:s:a:r:b:t: -l help,profile:,size:,aspect:,rate:,bitrate:,h264profile:,stem:,title:,filter:,sample:: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  76. eval set -- "$TEMP"
  77. while true ; do
  78. case "$1" in
  79. -h|--help) showhelp; exit;;
  80. -p|--profile) profile="$2"; shift 2;;
  81. -s|--size) size="$2"; shift 2;;
  82. -a|--aspect) aspect="$2"; shift 2;;
  83. -r|--rate) rate="$2"; shift 2;;
  84. -b|--bitrate) bitrate="$2"; shift 2;;
  85. --h264profile) h264profile="$2"; shift 2;;
  86. --stem) stem="$2"; shift 2;;
  87. -t|--title) title="$2"; shift 2;;
  88. --filter) filters="${filters:+$filters }-filter $2"; shift 2
  89. while [ $# -gt 0 ] ; do
  90. case "$1" in
  91. *=*) filters="${filters:+$filters }$1"; shift;;
  92. *) break;;
  93. esac
  94. done
  95. ;;
  96. --sample) sample="in=${2:-0} out=$((${2:-0} + 150))"; shift 2;;
  97. --) shift; break;;
  98. *) exit1 "Internal error resolving options.";;
  99. esac
  100. done
  101. while [ $# -gt 0 ] ; do
  102. case "$1" in
  103. *=*) _melt_in="${_melt_in:+$_melt_in }$1"; shift;;
  104. *) break;;
  105. esac
  106. done
  107. while [ $# -gt 0 ] ; do
  108. case "$1" in
  109. *=*) _melt_out="${_melt_out:+$_melt_out }$1"; shift;;
  110. *) infiles="${infiles:+$infiles }$1"; shift;;
  111. esac
  112. done
  113. if [ -z "$infiles" ]; then
  114. showhelp
  115. exit1 "Too few parameters!"
  116. fi
  117. # input filename (mandatory)
  118. infile=$(perl -e 'print pop @ARGV' $infiles)
  119. [ -e "$infile" ] || exit1 "Input file missing!"
  120. # resolve stem and title (if not explicitly set)
  121. stem=${stem:-$(basename "$infile" | perl -pe 's/\.[^.]*//')}
  122. title=${title:-$stem}
  123. case "$profile" in
  124. 320x240|qvga)
  125. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  126. _melt="${size:-width=320 height=240}${_melt:+ $_melt}"
  127. ;;
  128. 480x360|hvga)
  129. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  130. _melt="${size:-width=480 height=360}${_melt:+ $_melt}"
  131. ;;
  132. 640x480|vga)
  133. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  134. _melt="${size:-width=640 height=480}${_melt:+ $_melt}"
  135. ;;
  136. 800x600|svga)
  137. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  138. _melt="${size:-width=800 height=600}${_melt:+ $_melt}"
  139. ;;
  140. 1024x768|xga)
  141. _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}"
  142. _melt="${size:-width=1024 height=768}${_melt:+ $_melt}"
  143. ;;
  144. 240p|432x240|wqvga)
  145. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  146. _melt="${size:-width=432 height=240}${_melt:+ $_melt}"
  147. ;;
  148. 360p|640x360|nhd)
  149. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  150. _melt="${size:-width=640 height=360}${_melt:+ $_melt}"
  151. ;;
  152. 480p|848x480|wvga)
  153. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  154. _melt="${size:-width=848 height=480}${_melt:+ $_melt}"
  155. ;;
  156. 576p|1024x576|wsvga)
  157. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  158. _melt="${size:-width=1024 height=576}${_melt:+ $_melt}"
  159. ;;
  160. 720p|1280x720|wxga|hd)
  161. _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}"
  162. ;;
  163. '')
  164. _melt="progressive=1 frame_rate_den=1${_melt:+ $_melt}"
  165. [ "25" != "$rate" ] || _melt="{_melt:+$_melt }frame_rate_num=$rate"
  166. ;;
  167. *) exit1 "Unknown profile \"$profile\" - please specify size and aspect directly.";;
  168. esac
  169. [ "25" = "$rate" ] || _melt="{_melt:+$_melt }frame_rate_num=$rate"
  170. # TODO: Check and fail if all needed tools are not available
  171. # TODO: When verified beneficial, add option real_time=-2
  172. _melt_in="${_melt_in:+$_melt_in }-progress $sample"
  173. _melt="$_melt ${bitrate:+vb=${bitrate}} ${size:+s=$size} ${aspect:+aspect=$aspect}"
  174. _melt_audio="ab=96k"
  175. ## Theora/Vorbis/Ogg
  176. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.ogg" f=ogg vcodec=libtheora $_melt acodec=libvorbis $_melt_audio $_melt_out
  177. ## H.264/AAC/MP4
  178. [ -z "$bitrate" ] || melt -group $_melt_in $infiles -group $filters -consumer avformat:/dev/null properties=x264-medium-pass1 properties=x264-medium-$h264profile $_melt $_melt_out
  179. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.mp4" ${bitrate:+pass=2} properties=x264-medium-$h264profile $_melt acodec=aac $_melt_audio $_melt_out
  180. # TODO: drop qt-faststart when melt 0.9.2 is stable
  181. mv "$stem.mp4" "$stem.mp4"~
  182. qt-faststart "$stem.mp4"~ "$stem.mp4"
  183. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4"
  184. ## VP8/Vorbis/WebM
  185. # TODO: use two-pass when supported by melt
  186. melt -group $_melt_in $infiles -group $filters -consumer avformat:"$stem.webm" properties=webm $_melt $_melt_audio $_melt_out
  187. ## JPEG preview
  188. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  189. # resolve width and height from preview image
  190. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  191. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  192. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  193. heightplus=${height:+$(($height+4))}
  194. # TODO: resolve flash player to use
  195. [ -z "$flashplayer" ] || flash=yes
  196. cat >"$stem.html" <<EOF
  197. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  198. <video width="$width" height="$height" preload controls>
  199. <source src="$stem.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  200. <source src="$stem.ogg" type='video/ogg; codecs="theora, vorbis"' />
  201. <source src="$stem.webm" type='video/webm; codecs="vp8, vorbis"' />
  202. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  203. <param name="movie" value="$flashplayer.swf" />
  204. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  205. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  206. title="No video playback capabilities, please download the video below" />
  207. ${flash:+</object>
  208. }</video>
  209. <p><strong>Download Video:</strong>
  210. open format <a href="$stem.ogg">Ogg</a>,
  211. open format <a href="$stem.webm">WebM</a>,
  212. closed Format <a href="$stem.mp4">MP4</a>.
  213. </p>
  214. EOF