Skip to content

Instantly share code, notes, and snippets.

@mkoby
Created February 18, 2012 20:26
Show Gist options
  • Select an option

  • Save mkoby/1860922 to your computer and use it in GitHub Desktop.

Select an option

Save mkoby/1860922 to your computer and use it in GitHub Desktop.
Encode video to HTML5 compatible videos
#!/bin/bash
# This shell script will process & encode a single video file, and output 3
# seperate files in the following formats: WebM, Ogg Theora, & h.264 for
# streaming via an HTML5 player.
#
# You'll need ffmpeg, ffmpeg2theora, & HandbrakeCLI installed to run this
# script. ffmpeg and ffmpeg2theora can be installed via Homebrew on a Mac.
# If you're using Ubuntu, use the PPA at
# https://launchpad.net/~jon-severinsson/+archive/ffmpeg
# for newer versions of ffmpeg and libvpx (WebM codec).
#
# For the HandbrakeCLI, please visit the Handbrake website at
# http://www.handbrake.fr
#
# Command line arguments taken from:
# http://diveintohtml5.info/video.html#example
#
# Modifications made for resolution and quality where needed.
INPUT_FILE=$1
FILENAME=${INPUT_FILE%%.*}
VIDEO_WIDTH="960"
VIDEO_HEIGHT="540"
VIDEO_DIM=$VIDEO_WIDTH"x"$VIDEO_HEIGHT
if [ -z "$INPUT_FILE" ]; then
echo "Usage: encode4codecasts.sh FILENAME"
echo "You need to pass a video to process. Exiting"
exit
fi
echo "Converting to Ogg/Theora"
echo "RUNNING: ffmpeg2theora --videoquality 7 --width $VIDEO_WIDTH --output $FILENAME.ogv $INPUT_FILE"
ffmpeg2theora --videoquality 7 --width $VIDEO_WIDTH --output $FILENAME.ogv $INPUT_FILE
echo "Converting to h.264"
echo "RUNNING: HandBrakeCLI --preset "iPhone 4" --width $VIDEO_WIDTH --two-pass --turbo --optimize --input $INPUT_FILE --output $FILENAME.mp4"
HandBrakeCLI --preset "iPhone 4" --width $VIDEO_WIDTH --two-pass --turbo --optimize --input $INPUT_FILE --output $FILENAME.mp4
echo "Converting to WebM"
echo "RUNNING: ffmpeg -pass 1 -passlogfile $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i $INPUT_FILE -vcodec libvpx -b 204800 -s $VIDEO_DIM -an -f webm -y NUL"
ffmpeg -pass 1 -passlogfile $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i $INPUT_FILE -vcodec libvpx -b:v 204800 -s $VIDEO_DIM -an -f webm -y $FILENAME.webm
echo "WebM - First pass complete"
echo "WebM - Starting second pass"
echo "RUNNING: ffmpeg -pass 2 -passlogfile $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i $INPUT_FILE -vcodec libvpx -b 204800 -s $VIDEO_DIM -acodec libvorbis -ac 2 -y $FILENAME.webm"
ffmpeg -pass 2 -passlogfile $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -i $INPUT_FILE -vcodec libvpx -b:v 204800 -s $VIDEO_DIM -acodec libvorbis -ac 2 -y $FILENAME.webm
echo "DONE"
@mkoby

mkoby commented Feb 20, 2012

Copy link
Copy Markdown
Author

I added a small change that uses the input file's name when creating the output file, and now creates the output files without the input file's extension. I also fixed a bug where the first pass for the WebM file had it's log and input files hardcoded (oops!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment