How to Encode WebM Videos for YouTube in Linux

WebM is the new open video file format that Google created for HTML5 video. It is a combination of the VP8 video codec and Ogg Vorbis audio codec, wrapped in a Matroska container. It is free and open source, giving anyone the ability to encode videos without restrictions, and play them in supported browsers (Mozilla Firefox, Google Chrome, and Opera).

Google’s recent announcement that it is dropping support for H.264 video in Chrome means that the primary HTML5 video format on YouTube will eventually be WebM. Now is an excellent opportunity for you to learn how to encode videos in the new format. Furthermore, H.264 is patented and not open, so Linux users should make an effort to avoid it, when possible.

Currently, Apple and Microsoft continue to only use H.264 and refuse to include WebM or Ogg Theora (both of which Mozilla, Google, and Opera support). Since WebM and Ogg Theora are open formats, Google has made it clear that it will prefer those over H.264.

We will cover two different techniques: (1) FFMPEG command line encoding and (2) Graphical encoding with Arista Transcoder.

In order to encode videos in WebM format, you will need ffmpeg version 0.6 or later. If your distribution has an earlier version, you will need to download the binaries from an alternate repository or compile it from source. The version in the Ubuntu 10.10 Maverick Meerkat repository has built-in WebM support.

Once you have it installed and are sure you have working libvpx library support, you can begin encoding. These instructions assume you want to encode in 720p, but you can alter the settings as you like.

First, check to see if you have a libvpx-720p preset in /usr/share/ffmpeg. Assuming you do not, create a file in that directory called “libvpx-720p.ffpreset” and add the following contents (edit as root):

vcodec=libvpx
g=120
rc_lookahead=16
level=216
profile=0
qmax=42
qmin=10
vb=2M
#ignored unless using -pass 2
maxrate=24M
minrate=100k

Save the file, and then enter the follow commands, one after the other:

ffmpeg -i input.mp4 -s 1280x720 -vpre libvpx-720p -b 3900k -pass 1 -an -f webm -y output.webm
ffmpeg -i input.mp4 -s 1280x720 -vpre libvpx-720p -b 3900k -pass 2 -acodec libvorbis -ab 100k -f webm -y output.webm

Replace “input.mp4” with the actual name of your video and “output.webm” with the name of your newly-created WebM video. When both passes finish, you should have a playable video, which you can test in Mplayer.

Arista Transcoder screenshot

Arista Transcoder provides an easy interface to quickly encode video files, including WebM. To use it, follow these instructions:

1. Select the source
2. Choose the device type
3. Select “WebM” as the preset
4. Click “Add to queue”

Arista Transcoder encoding WebM video

Arista will show you a live fast-forwarding preview of the video as it is encoding, and it will allow you to add multiple jobs to the queue if you need to encode several files or the same file in different formats.

Both methods should provide you with video files ready for YouTube or your own websites HTML5 videos. In my tests, the ffmpeg encoding was much slower but produced better quality than Arista Transcoder. Tweaking one or the other may produce better results for each.