Convert video to MP4, WebM and Ogg (VP8) for HTML 5 video tag

Here is a small script, which converts videos to all three HTML5 video formats that can be used in the <video> element. Either avconf or FFmpeg can be used as converters. I’m using both, because avconf does not have builtin support for the Fraunhofer FDK AAC codec in Ubuntu 14.04 (you would need to compile it from source) and the FFmpeg Ubuntu package (https://www.ffmpeg.org/download.html) no support for Theora. For more information see: http://www.w3schools.com/html/html5_video.asp

#!/bin/bash
if [ "$1" == "" ]; then
    echo Please specify a file name.
    exit 1;
fi

# Choose between ffmpeg and avconv:
CONVERTER_WEBM=ffmpeg
CONVERTER_MP4=ffmpeg
CONVERTER_OGG=avconv

# 720p:
HORIZONTAL_RESOLUTION=1280

# 1080p:
#HORIZONTAL_RESOLUTION=1920

#ffmpeg Wiki: https://trac.ffmpeg.org/wiki/

# Parameters:
# -acodec libvorbis: Set aduo codec to Ogg Vorbis. Should be default, but just in case.
# -aq 5: Audio quality between -1 and 10 (good starting point is 5).
# -ac 2: Audio channels: Stereo
# -vcodec libvpx: Should be default
# -qmin: Video quality minimum: Seems to make no difference
# -qmax: Maximum video quality between 0 and 63: Higher value means lower quality (good starting point is 25).
# -b:v 2M: Bitrate of 2MB/s
# -vf scale=$HORIZONTAL_RESOLUTION:-1: Apply scale filter and reduce resolution to 720p (keeping vertical resolution).
rm -f $1.webm
$CONVERTER_WEBM -i $1 -acodec libvorbis -aq 7 -ac 2 -vcodec libvpx -b:v 2M -qmax 16 -vf scale=$HORIZONTAL_RESOLUTION:-1 -threads 4 $1.webm
# Parameters:
# -acodec libfdk_aac: Fraunhofer FDK AAC codec library: Highest AAC encoder available
# -cutoff 18000: By default high frequencies are cut off, so increase cut off level
# -vcodec libx264
# -qmax: Range is 0 to 51 here.
rm -f $1.mp4
$CONVERTER_MP4 -i $1 -acodec libfdk_aac -cutoff 18000 -vcodec libx264 -b:v 2M -qmax 16 -vf scale=$HORIZONTAL_RESOLUTION:-1 -threads 4 $1.mp4

# Parameters:
# -qscale:a 7: Audio quality (range 0 to 10)
# -qscale:v 7: Video quality (range 0 to 10)
rm -f $1.ogv
$CONVERTER_OGG -i $1 -acodec libvorbis -qscale:a 7 -vcodec libtheora -qscale:v 9 -vf scale=$HORIZONTAL_RESOLUTION:-1 -threads 4 $1.ogv

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>