WebM is a media container format that is designed for video on the web. It's offered under a permissive licence which means that it has been integrated into many browsers making it a great choice for encoding video.
Because WebM is just a container format, the video itself is encoded using the VP8/VP9 or AV1 codecs. Vorbis or Opus codecs are used for the audio. AV1 and Opus are the most recent codecs to be released and so often perform the best when looking at compression and quality.
If opting to use the AV1 codec, make sure you check what devices you plan to support as only recent iPhones can play them back natively.
Why use WebM?
There are many reasons you might choose to use WebM over other media formats:
- Optimized for the Web - WebM was designed by Google for web use, making it compatible with modern web browsers.
- High Compression Efficiency - WebM achieves high compression ratios which ultimately result in smaller file sizes. This means that videos with start playing faster and be quicker to download and stream.
- Free and Open Source - WebM is an open-source format with no licensing fees, unlike formats like H.264, which may require licensing fees for commercial use.
Creating a WebM file with VP9 and Opus
By default, FFmpeg will create a WebM using the VP9 and Opus codecs if you don't specify any encoder options:
To explicitly specify VP9 and Opus if needed, you can use the following options:
If you want to control the bitrate and adjust the quality of the output, it's recommended that you use "2-pass" mode which can achieve a higher quality result:
We're calling `ffmpeg` twice to achieve this. 2-pass encoding can sometimes be very slow though. You may want to omit it when testing out options or when you don't require a very high quality encode.
Creating a WebM file with AV1 and Opus
AV1 is the most recent and advanced video codec that WebM supports and is usually paired with Opus for audio.
To use AV1 you need to specify which codec to use for encoding, in this case `libaom-av1`. This example also sets the desired video bitrate to 3.5Mbps and chooses the Opus codec for audio (which should also just be the default if omitted):
Similar to VP9, the highest quality output is usually done with 2 passes:
Creating a WebM file with VP8 for legacy support
If you need to support very old devices or browsers you may need to opt for VP8 instead of VP9 or AV1 for the codec. This will likely need to be paired with Vorbis instead of Opus for the audio codec to work correctly also. It would be best to use VP9 or AV1 where possible though.
You can create a WebM with VP8 and Vorbis like this:
Alternatives to using WebM
Containers like MP4 and the H.264 codec are extremely well supported across a range of devices where WebM is still catching up, so you might want to consider alternative formats if you need to support more than just the web.
Here are some guides we have that may be useful for you when choosing an alternative: