WebM web video encoding tutorial with FFMpeg
Previously I wrote about encoding H.264 video for web. Since video support in browsers is kind of a mess, you’ll be forced to re-encode videos to WebM (VP8/vorbis) format sooner or later if you want HTML5 playback. FFMpeg is able to use the official libvpx library, which makes it pretty easy. However, the support is still slightly buggy and the parameter mapping is non-obvious if you’re used to encoding to other formats.
So, let’s start:
libvpx is a rather new addition to FFmpeg and for quality reasons you really want to use the newest stable version of FFmpeg and libvpx. How to get those was described in my previous article. Just don’t forget to compile in libvpx and libvorbis support.
You’ll have to choose resolution and bitrate into which to encode. For general guidelines check my previous article and add about 10-15% to video bitrate. libfaac and libvorbis output quality is mostly comparable, so you can just keep the same settings.
All the choices being made, now it’s time to encode your video:
(I’m using new-style parameters introduced in latest FFmpegs, old-style parameters are being slowly deprecated because of ambiguity)
Two-pass encoding is of course also possible:
As mentioned in the previous article, FFmpeg parameters are position sensitive, so be sure to maintain the order of parameters.
Now, let’s break down the parameters for the encode:
|-codec:v||Specifies the video encoder to be used, in our case that is libvpx VP8 library|
|-quality good||Sets encoding speed for the VP8 encoder. This works in concert with cpu-used parameter. Available values are |
|-cpu-used [0-5]||Sets “speed” of encoding “ lower value uses more CPU for processing and produces better quality. Larger values trade quality for faster encoding, with 4 and 5 enabling “rate distortion optimization”, which significantly speeds-up encoding with price of big quality hit.|
|-b:v [bitrate]||Sets desired output video bitrate|
|-maxrate/-bufsize||Set upper limits for stream bitrate, maxrate specifying maximum bitrate and bufsize specifying device buffer size. Buffer size tells the encoder how much it can overshoot the maximum bitrate when required. Good default is twice the maxrate for approx. 2sec of buffer.|
|-qmin 10 -qmax 42||This sets minimum and maximum quantization values. Since as of 0.9, FFmpeg sets those values wrong by default, adding these is required for a a decent video quality. Omitting these will produce blocky broken video.|
|-threads [num]||Sets number of encoding threads to use. Set to the number of your CPU cores available.|
|-vf scale=[width:height]||Rescales video to a chosen resolution. Value of “-1″ means “size to keep aspect ratio”, e.g. setting this to “-1:720″ will produce a 720p output with same aspect ratio as input.|
|-codec:a libvorbis||Sets output audio encoder to libvorbis to produce vorbis output. This is required for a valid WebM file.|
|-b:a [bitrate]||Sets bitrate for encoded audio.|
|-an||disables audio, audio processing has no effect on first pass so itís best to disable it to not waste CPU.|
|-f webm||tells FFmpeg the output file format (required only if it cannot be inferred from output file extension).|
|-pass [1 2]||tells FFmpeg to process video in multiple passes and sets the current pass.|
That’s mostly all there is to it “ other FFmpeg mappings for libvpx can be found in the documentation. It’s also worth noting, that libvpx supports the “profile” parameter, which sets complexity of output stream, much like H.264 has. However, since devices with WebM support are very scarce, there currently isn’t much documentation on which devices supports which profile.
These are analogous to the command lines in the H.264 article. Replace the thread count in
-threads parameter with number of your CPU cores for optimal encoding speed.