summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 1e4cc7efb98ff4ea74531087c129fb9cc9aa2b53 (plain)
  1. #!/bin/sh
  2. # Origins:
  3. # http://diveintohtml5.org/video.html
  4. # http://camendesign.com/code/video_for_everybody
  5. # Possible flashplayers:
  6. # http://www.internetmarketingnotes.com/2010/07/free-embeddable-flash-video-flv-players-for-commercial-use/
  7. set -e
  8. PRG=$(basename "$0")
  9. showhelp() {
  10. cat <<EOF
  11. Usage: $PRG --size SIZE [--title TITLE] INPUTFILE
  12. Encode video file in multiple web-optimized formats, and provide sample
  13. html favoring open formats with optional non-JavaScript Flash fallback.
  14. -s, --size Output size in WIDTHxHEIGHT
  15. (mandatory)
  16. -t, --title Title used in html fallback graphics
  17. (default: basename of input file)
  18. -h, --help This help text
  19. Examples:
  20. $PRG -s 320x200 -t "Funny guy" myvideo.dv
  21. More info: http://camendesign.com/code/video_for_everybody
  22. EOF
  23. }
  24. exit1() {
  25. response="${1:+Error: }${1:-Internal error!}"
  26. echo >&2 "$response"
  27. exit 1
  28. }
  29. # parse cmdline options
  30. TEMP="`getopt -s sh -o hs:t: -l help,size:,title: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  31. eval set -- "$TEMP"
  32. while true ; do
  33. case "$1" in
  34. -h|--help) showhelp; exit;;
  35. -s|--size) size="$2"; shift 2;;
  36. -t|--title) title="$2"; shift 2;;
  37. --) shift; break;;
  38. *) exit1 "Internal error resolving options.";;
  39. esac
  40. done
  41. if [ $# -eq 0 ]; then
  42. showhelp
  43. exit1 "Too few parameters!"
  44. fi
  45. if [ $# -gt 1 ]; then
  46. showhelp
  47. exit1 "Too many parameters!"
  48. fi
  49. # input filename (mandatory)
  50. infile="$1"; shift
  51. [ -e "$infile" ] || exit1 "Input file missing!"
  52. filebase=$(echo "$infile" | perl -pe 's/\.[^.]*//')
  53. # output size in WIDTHxHEIGHT (mandatory)
  54. # TODO: Make optional with input size as default
  55. # TODO: Make each half of size optional with default resolved from input aspect ratio
  56. [ -n "$size" ] || exit1 "Unknown output size!"
  57. width=$(echo "$size" | perl -pe 's/[^x]*x//')
  58. height=$(echo "$size" | perl -pe 's/x[^x]*//')
  59. heightplus=${height:+$(($height+4))}
  60. # fallback graphics title
  61. title=${title:-$filebase}
  62. # TODO: Check and fail if all needed tools are not available
  63. ## Theora/Vorbis/Ogg
  64. ffmpeg2theora --videobitrate 200 --max_size "$size" --output "$filebase.ogv" "$infile"
  65. ## H.264/AAC/MP4
  66. HandBrakeCLI --preset "iPhone & iPod Touch" --vb 200 ${width:+--maxWidth "$width"} ${height:+--maxHeight "$height"} --two-pass --turbo --optimize --input "$infile" --output "$filebase.mp4"
  67. ## VP8/Vorbis/WebM
  68. ! ffmpeg -codecs | grep -iw vp8 || webm=yes
  69. # FIXME: check and fail if logfiles exist already
  70. [ -z "$webm" ] || ffmpeg -pass 1 -passlogfile "$infile" -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i "$infile" -vcodec libvpx -b 204800 -s "$size" -aspect 4:3 -an -f webm -y NUL
  71. [ -z "$webm" ] || ffmpeg -pass 2 -passlogfile "$infile" -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i "$infile" -vcodec libvpx -b 204800 -s "$size" -aspect 4:3 -acodec libvorbis -ac 2 -y "$filebase.webm"
  72. rm "$infile"-*.log
  73. ## JPEG preview
  74. ffmpegthumbnailer -s0 -i "$filebase.ogv" -o "$filebase.jpg"
  75. # TODO: resolve flash player to use
  76. [ -z "$flashplayer" ] || flash=yes
  77. cat >"$filebase.html" <<EOF
  78. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  79. <video width="$width" height="$height" preload controls>
  80. <source src="$filebase.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  81. <source src="$filebase.ogv" type='video/ogg; codecs="theora, vorbis"' />
  82. ${webm:+<source src="$filebase.webm" type='video/webm; codecs="vp8, vorbis"' />
  83. }${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  84. <param name="movie" value="$flashplayer.swf" />
  85. <param name="flashvars" value="image=$filebase.jpg&amp;file=$filebase.mp4" />
  86. }<img src="$filebase.jpg" width="$width" height="$height" alt="$title"
  87. title="No video playback capabilities, please download the video below" />
  88. ${flash:+</object>
  89. }</video>
  90. <p><strong>Download Video:</strong>
  91. open format <a href="$filebase.ogv">Ogg</a>,
  92. ${webm:+open format <a href="$filebase.webm">WebM</a>,}
  93. closed Format <a href="$filebase.mp4">MP4</a>.
  94. </p>
  95. EOF