What's the best HTML video format to serve?
The short answer
An MPEG-4 (.mp4
) container with H.264 codec will get you 97% coverage on current browsers across desktop and mobile, although there are some caveats. To address those you could also serve WebM (.webm
) container with VP9 codec.
In this post:
- What video formats do browsers support?
- How to convert video to MP4
- How to convert video to WebM
- How to generate a video thumbnail
- How to embed video
- More information
What video formats do browsers support?
MPEG-4/H.264 | WebM | AV1 | Ogg/Theora | HEVC/H.265 | |
---|---|---|---|---|---|
% Support | 96.62% | 95.38% | 72.89% | 36.98% | 19.52% |
IE 11 | Yes | Partial#2 | No | Partial | Almost#7 |
Edge 96 | Yes | Yes | Yes | Yes | No |
FF 95 | Yes | Yes | Yes | Yes | No |
Chr. 96 | Yes | Yes | Yes | Yes | No |
Saf. 15.2 | Yes | Almost#3 | No | No | Yes |
Op. 82 | Yes | Yes | Yes | Yes | No |
iOS 15.2 | Yes | Almost#4 #5 | No | No | Yes |
O.Mini all | No | No | No | No | No |
And. 96 | Yes | Yes | Yes | No | No#8 |
BB 10 | Yes | No | No | No | No |
O.Mob 64 | Yes | Yes | Yes | No | No#8 |
Chr/And. 96 | Yes | Yes | Yes | No | No#8 |
FF/And. 95 | Almost#1 | Yes | No#6 | Yes | No |
IE.Mob 11 | Yes | Partial | No | Partial | No |
UC 12.12 | Almost | Yes | No | Yes | No |
SS 15.0 | Yes | Yes | Yes | No | No#8 |
QQ 10.4 | Yes | Yes | No | Yes | No |
baidu 7.12 | Yes | Yes | No | No | No |
Kai 2.5 | Yes | Yes | No | Yes | No |
Every browser supports MPEG-4/H.264 (MPEG-4 container with H.264 codec) except Opera Mini, which doesn’t support video at all, so no great loss there.
Firefox on Android supports MPEG-4/H.264 but doesn’t support hardware acceleration. Videos will work but will have an impact on battery life and possibly playback performance.
UC Browser was popular on mobile devices in 2017 in India and China but has since lost almost all its market share to Chrome. It’s unlikely you need to worry about this browser.
How to convert video to MP4
I find it easiest to use the command line. For that ffmpeg is the best tool. Install it on MacOS with:
brew install ffmpeg
Then run the following command:
1
2
3
4
5
6
7
8
ffmpeg \
-i "input.mov" \
-vcodec libx264 \
-pix_fmt yuv420p \
-profile:v baseline \
-level 3 \
-movflags faststart \
"output.mp4"
- Run
ffmpeg
. The trailing\
means “continue the command on the next line”. - Your input file.
- Sets the codec to H.264.
- Ensure compatibility with Apple Quicktime.
- Use only base H.264 features to ensure widest compatibility.
- Set H.264 quality level. 1 is the lowest and 6 the highest.
- Move video metadata to the start of the file, so the browser can immediately know the dimensions and length of the video. Find more details here.
How to convert video to WebM
1
2
3
4
5
6
ffmpeg -i input.mov -c:v libvpx-vp9 -pass 1 -b:v 1000K -threads 8 -speed 4 \
-tile-columns 6 -frame-parallel 1 \
-an -f webm /dev/null
ffmpeg -i input.mov -c:v libvpx-vp9 -pass 2 -b:v 1000K -threads 8 -speed 1 \
-tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 \
-c:a libopus -b:a 64k -f webm output.webm
The recomended approach for WebM/VP9 is a two-pass approach, hence the two calls to ffmpeg
above.
- Call
ffmpeg
with VP9 codec, for the first pass, at the given video bitrate, with 8 threads, at a faster speed. - Enable parallel processing
- Disable audio for first pass, use webm format, and output nothing.
- The first pass creates a log file called
ffmpeg2pass-0.log
. The second pass uses this information.
- The first pass creates a log file called
- Same as 1 but a higher-quality speed.
- Same as 2, and set
auto-alt-ref
andlag-in-frames
to enhance quality. - Use Opus audio codec, with given audio bitrate.
How to generate a video thumbnail
You can also generate a thumbnail/poster image that the browser shows before it downloads the video:
1
2
3
4
5
ffmpeg \
-i input.mp4 \
-ss 00:00:14.435 \
-vframes 1 \
out.png
- Run
ffmpeg
. - Your input file.
- The time through the video to create the image from, in Hours:Minutes:Seconds.Milliseconds.
- Extract one frame of video.
How to embed video
The minimal code, with video controls, but without a thumbnail:
<video controls src="output.mp4" type="video/mp4">
Your browser doesn't support video.
</video>
And to add a thumbnail image, and multiple video formats:
<video controls poster="out.png" >
<source src="output.webm" type="video/webm" />
<source src="output.mp4" type="video/mp4">
Your browser doesn't support video.
</video>
The browser uses the first source it supports, and so WebM/VP9 should be listed first as it is higher quality than MPEG-4/H.264.
More information
You can find more (very detailed!) information at the following links: