summaryrefslogtreecommitdiff
path: root/localvideowebencode
blob: 297301215b9a5997dc5beadc9d0ce9bc0272c59f (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: use (openshot and) melt:
  12. # * melt -consumer avformat:outfile.webm properties=consumer/avformat/webm infile.xml
  13. # * melt -consumer avformat:outfile.x264 properties=consumer/avformat/x264-medium-baseline infile.xml
  14. # * melt -consumer avformat:outfile.ogg f=ogg vcodec=libtheora b=1120k acodec=libvorbis aq=25 infile.xml
  15. set -e
  16. PRG=$(basename "$0")
  17. showhelp() {
  18. cat <<EOF
  19. Usage: $PRG --size SIZE [--profile PROFILE] [--title TITLE] INPUTFILE
  20. Encode video file in multiple web-optimized formats, and provide sample
  21. html favoring open formats with optional non-JavaScript Flash fallback.
  22. -s, --size Output size in WIDTHxHEIGHT
  23. -p, --profile Quality profile (lo, hi, lowide, hiwide)
  24. (default: lo)
  25. -t, --title Title used in html fallback graphics
  26. (default: basename of input file)
  27. -h, --help This help text
  28. Examples:
  29. $PRG -s 320x200 -t "Funny guy" myvideo.dv
  30. NB! use max. 640x480 to support old iPod (e.g. 640x360 for 16:9).
  31. More info: <http://camendesign.com/code/video_for_everybody>
  32. EOF
  33. }
  34. exit1() {
  35. response="${1:+Error: }${1:-Internal error!}"
  36. echo >&2 "$response"
  37. exit 1
  38. }
  39. # parse cmdline options
  40. TEMP="`getopt -s sh -o hs:p:t: -l help,size:,profile:,title: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error."
  41. eval set -- "$TEMP"
  42. while true ; do
  43. case "$1" in
  44. -h|--help) showhelp; exit;;
  45. -s|--size) size="$2"; shift 2;;
  46. -p|--profile) profile="$2"; shift 2;;
  47. -t|--title) title="$2"; shift 2;;
  48. --) shift; break;;
  49. *) exit1 "Internal error resolving options.";;
  50. esac
  51. done
  52. if [ $# -eq 0 ]; then
  53. showhelp
  54. exit1 "Too few parameters!"
  55. fi
  56. if [ $# -gt 1 ]; then
  57. showhelp
  58. exit1 "Too many parameters!"
  59. fi
  60. # input filename (mandatory)
  61. infile="$1"; shift
  62. [ -e "$infile" ] || exit1 "Input file missing!"
  63. filebase=$(echo "$infile" | perl -pe 's/\.[^.]*//')
  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. _ffmpeg() {
  88. passes="$1"; shift
  89. ext="$1"; shift
  90. [ ! -f "$filebase.$ext" ] || exit1 "output file \"$filebase.$ext\" already exist. Remove it and try again."
  91. case $passes in
  92. 0)
  93. ffmpeg -i "$infile" $opts "$@" -f "$ext" -y "$filebase.$ext"
  94. ;;
  95. 1)
  96. [ ! -f "$filebase"-*.log ] || exit1 "logfiles already exist. Remove them (or wait for that other process to complete) and try again."
  97. ffmpeg -i "$infile" -pass 1 -passlogfile "$filebase" "$@" -an -f "$ext" -y /dev/null
  98. ;;
  99. 2)
  100. [ -f "$filebase"-*.log ] || exit1 "logfiles missing. Pass 2 cannot succeed without a prior pass 1."
  101. ffmpeg -i "$infile" -pass 2 -passlogfile "$filebase" "$@" -y "$filebase.$ext"
  102. rm "$filebase"-*.log
  103. case $ext in
  104. mp4)
  105. mv "$filebase.$ext" "$filebase.$ext"~
  106. qt-faststart "$filebase.$ext"~ "$filebase.$ext"
  107. [ -f "$filebase.$ext" ] && rm "$filebase.$ext"~ || exit1 "failed to optimize with qt-faststart."
  108. ;;
  109. esac
  110. ;;
  111. esac
  112. }
  113. opts_common="-threads 0 -b ${bitrate}k ${size:+-s $size} ${aspect:+-aspect $aspect}"
  114. opts_audio="-ac 2 -ar 44100 -ab 96k"
  115. opts_webm="-vcodec libvpx -keyint_min 0 -g 250 -skip_threshold 0"
  116. ## Theora/Vorbis/Ogg
  117. _ffmpeg 0 ogg $opts_common -vcodec libtheora -acodec libvorbis $opts_audio
  118. ## H.264/AAC/MP4
  119. _ffmpeg 1 mp4 $opts_common -vcodec libx264 -vpre medium_firstpass -vpre baseline
  120. _ffmpeg 2 mp4 $opts_common -vcodec libx264 -vpre medium -vpre baseline -acodec libvo_aacenc $opts_audio
  121. ## VP8/Vorbis/WebM
  122. ! ffmpeg -codecs | grep -iw vp8 || webm=yes
  123. [ -z "$webm" ] || _ffmpeg 1 webm $opts_common $opts_webm
  124. [ -z "$webm" ] || _ffmpeg 2 webm $opts_common $opts_webm -acodec libvorbis $opts_audio
  125. ## JPEG preview
  126. ffmpegthumbnailer -s0 -i "$filebase.ogg" -o "$filebase.jpg"
  127. # resolve width and height from preview image
  128. size=${size:-$(jpeginfo "$filebase.jpg" | perl -ane 'print "$F[1]x$F[3]"')}
  129. width=$(echo "$size" | perl -Fx -ane 'print $F[0]')
  130. height=$(echo "$size" | perl -Fx -ane 'print $F[1]')
  131. heightplus=${height:+$(($height+4))}
  132. # TODO: resolve flash player to use
  133. [ -z "$flashplayer" ] || flash=yes
  134. cat >"$filebase.html" <<EOF
  135. <!-- Video for Everybody, Kroc Camen of Camen Design -->
  136. <video width="$width" height="$height" preload controls>
  137. <source src="$filebase.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  138. <source src="$filebase.ogg" type='video/ogg; codecs="theora, vorbis"' />
  139. ${webm:+<source src="$filebase.webm" type='video/webm; codecs="vp8, vorbis"' />
  140. }${flash:+<object width="$width" height="$heightplus" type="application/x-shockwave-flash" data="$flashplayer.swf">
  141. <param name="movie" value="$flashplayer.swf" />
  142. <param name="flashvars" value="image=$filebase.jpg&amp;file=$filebase.mp4" />
  143. }<img src="$filebase.jpg" width="$width" height="$height" alt="$title"
  144. title="No video playback capabilities, please download the video below" />
  145. ${flash:+</object>
  146. }</video>
  147. <p><strong>Download Video:</strong>
  148. open format <a href="$filebase.ogg">Ogg</a>,
  149. ${webm:+open format <a href="$filebase.webm">WebM</a>,}
  150. closed Format <a href="$filebase.mp4">MP4</a>.
  151. </p>
  152. EOF