summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: bd4b198b73b3052c2259e136cbb7a569f44bf869 (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 [INPUTFILE...]
  17. Encode video file in multiple web-optimized formats, and provide sample
  18. html favoring open formats with optional non-JavaScript Flash fallback.
  19. -s, --size Output size in WIDTHxHEIGHT or ffmpeg
  20. abbreviation like vga qvga qcif hd480 etc.
  21. (default: use input size)
  22. -a, --aspect Display Aspect Ratio in melt format e.g. @16/9
  23. (default: no aspect hinting)
  24. -b, --bitrate Bitrate in bytes, optionally ISO appreviated
  25. (default: 768k)
  26. -p, --profile Melt profile like square_ntsc quarter_pal_wide
  27. qcif_15 etc.
  28. (default: none - reuse input profile)
  29. --h264profile MPEG-4 AVC target profile (baseline medium)
  30. (default: baseline)
  31. --stem Stem of output filenames, optionally with path
  32. (default: basename of last input file)
  33. -t, --title Title used in html fallback graphics
  34. (default: stem)
  35. -h, --help This help text
  36. Examples:
  37. $PRG -s qvga -t "Funny guy" intro.dv myvideo.dv
  38. $PRG -p sdi_486i_5994 --stem funny -t "Funny guy" myvideo.dv
  39. Arguments can be passed directly to the melt avformat consumer.
  40. Multiple input files merged together.
  41. Default options are optimal for 320x240 (qvga) and similar.
  42. Suggested bitrates:
  43. 320x240 (qvga): 768k
  44. 640x480 (vga): 1120k
  45. NB! Old iPod require baseline encoding and has 640x480 size limit.
  46. Use e.g. quarter_ntsc_wide, svcd_ntsc_wide or svcd_pal_wide (varying in
  47. framerate and Pixel Aspect Ratio) for widescreen material.
  48. More info: <http://camendesign.com/code/video_for_everybody>
  49. <http://www.streaminglearningcenter.com/>
  50. <http://en.wikipedia.org/wiki/HTML5_video>
  51. EOF
  52. }
  53. exit1() {
  54. response="${1:+Error: }${1:-Internal error!}"
  55. echo >&2 "$response"
  56. exit 1
  57. }
  58. # defaults
  59. bitrate=768k
  60. h264profile=baseline
  61. # parse cmdline options
  62. TEMP="`getopt -s sh -o hs:a:b:p:t: -l help,size:,aspect:,bitrate:,profile:,h264profile:,stem:,title: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  63. eval set -- "$TEMP"
  64. while true ; do
  65. case "$1" in
  66. -h|--help) showhelp; exit;;
  67. -s|--size) size="$2"; shift 2;;
  68. -a|--aspect) aspect="$2"; shift 2;;
  69. -b|--bitrate) bitrate="$2"; shift 2;;
  70. -p|--profile) profile="$2"; shift 2;;
  71. --h264profile) h264profile="$2"; shift 2;;
  72. --stem) stem="$2"; shift 2;;
  73. -t|--title) title="$2"; shift 2;;
  74. --) shift; break;;
  75. *) exit1 "Internal error resolving options.";;
  76. esac
  77. done
  78. if [ $# -eq 0 ]; then
  79. showhelp
  80. exit1 "Too few parameters!"
  81. fi
  82. # input filename (mandatory)
  83. infile=$(perl -e 'print pop @ARGV' "$@")
  84. [ -e "$infile" ] || exit1 "Input file missing!"
  85. # resolve stem and title (if not explicitly set)
  86. stem=${stem:-$(basename "$infile" | perl -pe 's/\.[^.]*//')}
  87. title=${title:-$stem}
  88. # TODO: Check and fail if all needed tools are not available
  89. # TODO: When verified beneficial, add option real_time=-2
  90. opts="${profile:+-profile $profile}"
  91. args=" ${bitrate:+b=${bitrate}} ${size:+s=$size} ${aspect:+aspect=$aspect}"
  92. args_audio="ac=2 ar=44100 ab=96k"
  93. ## Theora/Vorbis/Ogg
  94. melt $opts -consumer avformat:"$stem.ogg" f=ogg vcodec=libtheora $args acodec=libvorbis aq=25 $args_audio "$@"
  95. ## H.264/AAC/MP4
  96. melt $opts -consumer avformat:/dev/null f=mp4 properties=x264-medium-$h264profile $args pass=1 an=1 fastfirstpass=1 "$@"
  97. melt $opts -consumer avformat:"$stem.mp4" properties=x264-medium-$h264profile $args pass=2 acodec=libvo_aacenc $args_audio "$@"
  98. mv "$stem.mp4" "$stem.mp4"~
  99. qt-faststart "$stem.mp4"~ "$stem.mp4"
  100. [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || exit1 "failed to optimize with qt-faststart."
  101. ## VP8/Vorbis/WebM
  102. # TODO: use two-pass when supported by melt
  103. melt $opts -consumer avformat:"$stem.webm" properties=webm $args $args_audio "$@"
  104. ## JPEG preview
  105. ffmpegthumbnailer -s0 -i "$stem.mp4" -o "$stem.jpg"
  106. # resolve width and height from preview image
  107. size=$(jpeginfo "$stem.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  108. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  109. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  110. heightplus=${height:+$(($height+4))}
  111. # TODO: resolve flash player to use
  112. [ -z "$flashplayer" ] || flash=yes
  113. cat >"$stem.html" <<EOF
  114. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  115. <video width="$width" height="$height" preload controls>
  116. <source src="$stem.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  117. <source src="$stem.ogg" type='video/ogg; codecs="theora, vorbis"' />
  118. <source src="$stem.webm" type='video/webm; codecs="vp8, vorbis"' />
  119. ${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  120. <param name="movie" value="$flashplayer.swf" />
  121. <param name="flashvars" value="image=$stem.jpg&amp;file=$stem.mp4" />
  122. }<img src="$stem.jpg" width="$width" height="$height" alt="$title"
  123. title="No video playback capabilities, please download the video below" />
  124. ${flash:+</object>
  125. }</video>
  126. <p><strong>Download Video:</strong>
  127. open format <a href="$stem.ogg">Ogg</a>,
  128. open format <a href="$stem.webm">WebM</a>,
  129. closed Format <a href="$stem.mp4">MP4</a>.
  130. </p>
  131. EOF