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