After lots of trial and error each time I convert a video to WebM, I finally got around to posting this so I don’t forget next time. In a nutshell, here’s the conversion command that works for me:
avconv -i myvideo.mp4 -acodec libvorbis -aq 5 -ac 2 -qmax 25 -threads 2 myvideo.webm
What is this doing? Let’s go through it bit by bit. Assuming we have a video called myvideo.mp4, the simplest way to convert to WebM is with this little line:
avconv -i myvideo.mp4 myvideo.webm
Easy, but the quality will likely be rubbish hence the use of a few flags. The flags can be divided into three sorts: audio, video and the transcoding itself.
Concentrating on the audio first, we should specify the audio codec which for WebM is Ogg Vorbis:
In later versions the audio codec is Ogg Vorbis by default but personally I specify it just in case.
The quality can be adjusted with the
-aq flag from -1 to 10 with a higher number meaning better quality. I’ve found 4 or 5 to be more than adequate.
The number of channels, e.g. mono (1) and stereo (2), is controlled with the
Moving on to the video quality and thankfully it’s nice and simple. Like the audio, we can specify a quality level. With the libvpx library used for WebM, this is actually a quantization level, set with the
-qmax flags ranging from 0 to 51. In my tests, setting
qmin makes no difference so I ignore it. Setting
qmax effectively controls the maximum compression that will be applied to each frame. In other words, a higher
qmax means higher compression, which results in lower quality and smaller file size. Obviously you should adjust this to whatever’s best for your circumstances, but I’ve found 25 to be a good starting point.
Note that with both the audio and video, setting flags for bitrate (
-ab for audio,
-b for video) makes little or no difference. Instead, setting quality flags indicates the use of a variable bitrate.
Finally, I tend to also use the
-threads flag. This simply sets how many CPU threads to use when transcoding. If your PC has multiple cores then a higher number means faster processing but with less spare capacity for running other programs. Incidentally it’s also possible to do 2-pass encoding with WebM using the
FFmpeg naming confusion
Note that due to what seem to be political reasons, using the ffmpeg command in Ubuntu results in a sad message.
*** THIS PROGRAM IS DEPRECATED *** This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
ffmpeg: command not found
It turns out that FFmpeg is very much alive, as is Libav (avconv), both with similar goals but with slightly different ways of doing it. I don’t follow the details and arguments but for practical purposes, using either the
avconv command is fine for converting videos to WebM with the above flags (at the time of writing). Of course, this may change eventually but for the sake of regular users, I hope not.
Unfortunately whatever FFmpeg/Libav disagreement there was has resulted in ffmpeg being removed from Ubuntu and possibly other Linux distros. For the transcoding commands in this post at least, the parameters are the same so if you have problems using avconv try with ffmpeg and vice versa.