summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 1d1564c8a87f542c9db2a7a672fe590496d984b2 (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...] [--] 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. -t, --title Title used in html fallback graphics
  32. (default: basename of first input file)
  33. -h, --help This help text
  34. Examples:
  35. $PRG -s qvga -t "Funny guy" myvideo.dv outro.dv
  36. $PRG -p sdi_486i_5994 -s hd480 -t "Funny guy" myvideo.dv
  37. If multiple input files are provided, they are merged together and
  38. output files are named based on first input file.
  39. Default options are optimal for 320x240 (qvga) and similar.
  40. Suggested bitrates:
  41. 320x240 (qvga): 768k
  42. 640x480 (vga): 1120k
  43. NB! Old iPod require baseline encoding and has 640x480 size limit.
  44. Use e.g. quarter_ntsc_wide, svcd_ntsc_wide or svcd_pal_wide (varying in
  45. framerate and Pixel Aspect Ratio) for widescreen material.
  46. More info: <http://camendesign.com/code/video_for_everybody>
  47. <http://www.streaminglearningcenter.com/>
  48. <http://en.wikipedia.org/wiki/HTML5_video>
  49. EOF
  50. }
  51. exit1() {
  52. response="${1:+Error: }${1:-Internal error!}"
  53. echo >&2 "$response"
  54. exit 1
  55. }
  56. # defaults
  57. bitrate=768k
  58. h264profile=baseline
  59. # parse cmdline options
  60. TEMP="`getopt -s sh -o hs:a:b:p:t: -l help,size:,aspect:,bitrate:,profile:,h264profile,title: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  61. eval set -- "$TEMP"
  62. while true ; do
  63. case "$1" in
  64. -h|--help) showhelp; exit;;
  65. -s|--size) size="$2"; shift 2;;
  66. -a|--aspect) aspect="$2"; shift 2;;
  67. -b|--bitrate) bitrate="$2"; shift 2;;
  68. -p|--profile) profile="$2"; shift 2;;
  69. --h264profile) h264profile="$2"; shift 2;;
  70. -t|--title) title="$2"; shift 2;;
  71. --) shift; break;;
  72. *) exit1 "Internal error resolving options.";;
  73. esac
  74. done
  75. if [ $# -eq 0 ]; then
  76. showhelp
  77. exit1 "Too few parameters!"
  78. fi
  79. # input filename (mandatory)
  80. infile="$1"
  81. [ -e "$infile" ] || exit1 "Input file missing!"
  82. filebase=$(echo "$infile" | perl -pe 's/\.[^.]*//')
  83. # fallback graphics title
  84. title=${title:-$filebase}
  85. # TODO: Check and fail if all needed tools are not available
  86. # TODO: When verified beneficial, add option real_time=-2
  87. opts_common="${bitrate:+b=${bitrate}} ${size:+s=$size} ${aspect:+aspect=$aspect}"
  88. opts_audio="ac=2 ar=44100 ab=96k"
  89. # bail out (before any expensive tasks) if logfiles exists
  90. [ ! -f x264-*.log* ] || exit1 "logfiles already exist. Remove them (or wait for that other process using them to complete) and try again."
  91. ## Theora/Vorbis/Ogg
  92. melt -consumer avformat:"$filebase.ogg" f=ogg vcodec=libtheora $opts_common acodec=libvorbis aq=25 $opts_audio "$@"
  93. ## H.264/AAC/MP4
  94. melt -consumer avformat:/dev/null f=mp4 properties=x264-medium-$h264profile $opts_common pass=1 an=1 fastfirstpass=1 "$@"
  95. melt -consumer avformat:"$filebase.mp4" properties=x264-medium-$h264profile $opts_common pass=2 acodec=libvo_aacenc $opts_audio "$@"
  96. mv "$filebase.mp4" "$filebase.mp4"~
  97. qt-faststart "$filebase.mp4"~ "$filebase.mp4"
  98. [ -f "$filebase.mp4" ] && rm "$filebase.mp4"~ || exit1 "failed to optimize with qt-faststart."
  99. ## VP8/Vorbis/WebM
  100. # TODO: use two-pass when supported by melt
  101. melt -consumer avformat:"$filebase.webm" properties=webm $opts_common $opts_audio "$@"
  102. ## JPEG preview
  103. ffmpegthumbnailer -s0 -i "$filebase.mp4" -o "$filebase.jpg"
  104. # resolve width and height from preview image
  105. size=$(jpeginfo "$filebase.jpg" | perl -ane 'print "$F[1]x$F[3]"')
  106. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  107. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  108. heightplus=${height:+$(($height+4))}
  109. # TODO: resolve flash player to use
  110. [ -z "$flashplayer" ] || flash=yes
  111. cat >"$filebase.html" <<EOF
  112. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  113. <video width="$width" height="$height" preload controls>
  114. <source src="$filebase.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  115. <source src="$filebase.ogg" type='video/ogg; codecs="theora, vorbis"' />
  116. ${webm:+<source src="$filebase.webm" type='video/webm; codecs="vp8, vorbis"' />
  117. }${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  118. <param name="movie" value="$flashplayer.swf" />
  119. <param name="flashvars" value="image=$filebase.jpg&amp;file=$filebase.mp4" />
  120. }<img src="$filebase.jpg" width="$width" height="$height" alt="$title"
  121. title="No video playback capabilities, please download the video below" />
  122. ${flash:+</object>
  123. }</video>
  124. <p><strong>Download Video:</strong>
  125. open format <a href="$filebase.ogg">Ogg</a>,
  126. ${webm:+open format <a href="$filebase.webm">WebM</a>,}
  127. closed Format <a href="$filebase.mp4">MP4</a>.
  128. </p>
  129. EOF