Skip to content

Instantly share code, notes, and snippets.

@mkoby
Last active January 4, 2016 10:59
Show Gist options
  • Save mkoby/8612349 to your computer and use it in GitHub Desktop.
Save mkoby/8612349 to your computer and use it in GitHub Desktop.
Bash script to encode videos for CodeCasts.tv. Uses ffmpeg to encode to Theory (Ogg), WebM (vp8), and h.264 (x264). This version of the script doesn't require ffmpeg2theora or HandbrakeCLI, so less requirements all around. Need to get the WebM to create a smaller file.
#!/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, libvorbis, libvpx, & x264 installed to run this
# script. ffmpeg can be installed via Homebrew on a Mac.
# Modifications made for resolution and quality where needed.
INPUT_FILE=$1
FILENAME=${INPUT_FILE%%.*}
VIDEO_HEIGHTS=( 480 720 1080 )
INPUT_HEIGHT=`ffprobe -v quiet -show_streams $INPUT_FILE | grep height | cut -d = -f 2`
if [ -z "$INPUT_FILE" ]; then
echo "Usage: encode4codecasts.sh FILENAME"
echo "You need to pass a video to process. Exiting"
exit
fi
echo "Encoding $INPUT_FILE"
echo "$INPUT_FILE Height: $INPUT_HEIGHT"
for height in ${VIDEO_HEIGHTS[@]}
do
# If the input video's height is the same as the height to scale to, skip,
# because we'll encode a "full" version after we handle the scaled videos
if [ $height == $INPUT_HEIGHT ]; then
continue
fi
# We only want to scale if the height we're scaling to is smaller than the
# original video's height. NEVER scale up.
if [ $INPUT_HEIGHT -gt $height ]; then
echo "RUNNING: ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libtheora -c:a libvorbis -q:v 5 -q:a 5 -vf \"scale=-1:$height\" -y ${FILENAME}_${height}.ogv"
ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libtheora -c:a libvorbis -q:v 5 -q:a 5 -vf "scale=-1:$height" -y ${FILENAME}_${height}.ogv
echo "RUNNING: ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libx264 -profile:v main -vf \"scale=-trunc(oh*a/2)*2:min($height\,iw)\" -strict -2 -y ${FILENAME}_${height}.m4v"
ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libx264 -profile:v main -vf "scale=trunc\(oh*a/2\)*2:min\($height\,iw\)" -strict -2 -y ${FILENAME}_${height}.m4v
echo "RUNNING: ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libvpx -c:a libvorbis -quality good -vf \"scale=-1:$height\" -y ${FILENAME}_${height}.webm"
ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libvpx -c:a libvorbis -quality good -vf "scale=-1:$height" -y ${FILENAME}_${height}.webm
fi
done
# Full Size videos. Convert but don't scale.
echo "RUNNING: ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libtheora -c:a libvorbis -q:v 5 -q:a 5 -y ${FILENAME}_full.ogv"
ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libtheora -c:a libvorbis -q:v 5 -q:a 5 -y ${FILENAME}_full.ogv
echo "RUNNING: ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libx264 -profile:v main -strict -2 ${FILENAME}_full.m4v"
ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libx264 -profile:v main -strict -2 -y ${FILENAME}_full.m4v
echo "RUNNING: ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libvpx -c:a libvorbis -quality good -y ${FILENAME}_full.webm"
ffmpeg -i $INPUT_FILE -threads 16 -keyint_min 0 -g 250 -skip_threshold 0 -qmin 1 -qmax 51 -c:v libvpx -c:a libvorbis -quality good -y ${FILENAME}_full.webm
#Copying file to the codecasts-tv_xxxx.mov
echo "COPYING $INPUT_FILE codecasts-tv_$INPUT_FILE"
cp $INPUT_FILE codecasts-tv_$INPUT_FILE
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment