Sub topic 3
Containers and codecs: what browsers can play
This chapter assumes you can write an audio or video element with a src attribute and read a MIME type such as video/mp4. You don't need any encoding experience. By the end you should be able to read a codec string, predict whether a file will play, and pick sensible formats for the web.
Why a file extension is not enough
A file named clip.mp4 tells you little about whether a browser can play it. Two separate things decide that: the container and the codecs.
- A container is a file format that packages one or more streams together with timing information and metadata. MP4, WebM, and Ogg are containers.
- A codec (coder-decoder) is the algorithm that compresses and decompresses one stream. H.264, VP9, AAC, and Opus are codecs.
A typical video file holds one video stream and one audio stream. Each stream uses its own codec, and the container interleaves them so they stay in sync.
The browser must understand the container and every codec inside it. If it supports MP4 but not the video codec in your file, playback fails. If it supports the video codec but not the audio codec, you may get picture with no sound.
Common containers
| Container | Typical extension | MIME type | Usual contents |
|---|---|---|---|
| MP4 | `.mp4`, `.m4v` | `video/mp4` | H.264 or AV1 video, AAC audio |
| MP4 (audio only) | `.m4a` | `audio/mp4` | AAC audio |
| WebM | `.webm` | `video/webm`, `audio/webm` | VP8, VP9, or AV1 video, Opus or Vorbis audio |
| Ogg | `.ogg`, `.ogv`, `.oga` | `video/ogg`, `audio/ogg` | Theora video, Vorbis or Opus audio |
| MP3 | `.mp3` | `audio/mpeg` | MP3 audio |
| WAV | `.wav` | `audio/wav` | Uncompressed PCM audio |
| FLAC | `.flac` | `audio/flac` | Lossless audio |
Some audio formats, such as MP3 and FLAC, are both a codec and a bare container. That is why audio/mpeg needs no codec detail.
Common codecs
Video codecs:
- H.264 (AVC) is the most widely supported video codec. Every current browser plays it, usually with hardware acceleration.
- VP9 is open and royalty-free. It compresses better than H.264 and is supported in all current major browsers.
- AV1 compresses better still, but encoding is slow and older devices lack hardware decoding. Software decoding can drain a laptop battery.
- H.265 (HEVC) compresses well but has patent licensing costs. Support depends on the browser, operating system, and hardware, so don't rely on it.
- VP8 and Theora are older. VP8 still works in WebM, and Theora is rarely used today.
Audio codecs:
- AAC is the standard companion to H.264 in MP4 and works everywhere.
- Opus is open, efficient, and good for both speech and music. It is the best choice for WebM.
- Vorbis is an older open codec. It still works but Opus replaces it.
- MP3 plays in every browser. Its patents have expired, so there is no licensing concern now.
Reading a codec string
A MIME type can carry a codecs parameter that names the exact codecs inside the container. This is how code and markup say "MP4 with H.264 and AAC" instead of just "MP4".
| Codec string | Meaning |
|---|---|
| `avc1.42E01E` | H.264, Baseline profile, level 3.0 |
| `avc1.64001F` | H.264, High profile, level 3.1 |
| `mp4a.40.2` | AAC-LC audio |
| `vp8` | VP8 video |
| `vp09.00.10.08` | VP9, profile 0, level 1.0, 8-bit |
| `vp9` | VP9 in the short form used with WebM |
| `av01.0.05M.08` | AV1, main profile, level 3.1, 8-bit |
| `opus` | Opus audio |
| `vorbis` | Vorbis audio |
For H.264, the six hex digits after avc1. encode the profile, constraint flags, and level. A higher profile and level allow more compression features and larger frames, but may exclude weaker devices. You rarely write these by hand. Tools such as ffprobe report them, and you can copy them.
The type attribute on an element uses the same syntax. The next chapter covers source elements fully. For now, note how the type narrows what the browser must support:
<source src="clip.mp4" type='video/mp4; codecs="avc1.64001F, mp4a.40.2"'>
<source src="clip.webm" type='video/webm; codecs="vp9, opus"'>The codec list is quoted, so the whole attribute value uses single quotes here. With the codecs given, the browser can skip a file without downloading it if it can't decode one of the streams.
A safe default
For broad coverage, encode two files:
- MP4 with H.264 video and AAC audio. This plays on essentially every browser and device.
- WebM with VP9 video and Opus audio. This is often smaller at the same quality.
If you must pick one, pick MP4 with H.264 and AAC. Add AV1 only when file size matters enough to justify the extra encoding work, and always keep an H.264 fallback.
Here are example ffmpeg commands for both files:
ffmpeg -i input.mov \
-c:v libx264 -profile:v high -pix_fmt yuv420p \
-c:a aac -b:a 128k \
-movflags +faststart \
clip.mp4
ffmpeg -i input.mov \
-c:v libvpx-vp9 -crf 32 -b:v 0 \
-c:a libopus -b:a 96k \
clip.webmThree details in these commands prevent common failures:
-pix_fmt yuv420pforces the 4:2:0 chroma format. H.264 files in 4:4:4 or 10-bit formats often fail in browsers.-movflags +faststartmoves the MP4 index to the start of the file so playback can begin before the download finishes. Chapter on serving media returns to this.-b:v 0with-crftells VP9 to use constant-quality mode.
Why support varies
Browser support is not only a matter of software. Four factors interact:
- Hardware decoders. Phones and laptops decode H.264 in dedicated chips. A codec without hardware support falls back to the CPU, which may stutter or heat the device.
- Operating system codecs. Some browsers use codecs built into the OS, so the same browser behaves differently on Windows, macOS, and Linux.
- Licensing. Vendors that avoid royalty-bearing codecs may leave them out of default builds.
- Profiles and levels. "Supports H.264" means supports some profiles and levels. A very high resolution or an unusual profile may fail where a standard 1080p High-profile file works.
Common errors
- Trusting the extension. Renaming
video.movtovideo.mp4does not change its codecs. A QuickTime file with ProRes video will not play. - A wrong
type. If thetypesaysvideo/mp4but the file is WebM, some browsers reject it and others sniff the content. Declare the real type. - Unsupported audio in a supported container. An MP4 with H.264 video and AC-3 or DTS audio often gives silent video.
- Incorrect MIME type from the server. If the server sends
application/octet-streamfor a.webmfile, some browsers refuse it. The serving chapter covers this. - Assuming AV1 or HEVC everywhere. Without a fallback, a share of users sees a blank player.
Use ffprobe to see what a file really contains:
ffprobe -hide_banner clip.mp4The output lists each stream with its codec name, such as Video: h264 (High) and Audio: aac (LC). Compare these against the table above.
Checking support in the browser
You can ask the browser directly whether it can play a type with canPlayType, which a later chapter covers in detail. For now, remember that no static table is final. Support changes with each browser release, so check current compatibility data for any format you depend on.
Exercise
A video file named lecture.mp4 plays with sound in one browser, but another shows the picture with no sound. What is the most likely cause, and how would you confirm it?
Show answer
The container is supported, but the audio codec inside is not supported by the second browser, for example AC-3 or DTS audio in an MP4. Run ffprobe on the file and read the Audio line. If it is not AAC (or another widely supported codec such as Opus or MP3), re-encode the audio with -c:a aac.
Exercise
What is the difference between a container and a codec? Give one example of each, then name the container and both codecs in a typical web-friendly WebM file.
Show answer
A container packages encoded streams and metadata into one file, for example MP4. A codec compresses and decompresses one stream, for example H.264. A typical web-friendly WebM file uses the WebM container with VP9 video and Opus audio.