Sub topic 3
Containers and Codecs: What Browsers Can Play
This chapter assumes you can write basic HTML, read a simple JavaScript snippet, and already know how to place an <audio> or <video> element on a page. Here you will learn why a file that plays on one device can fail on another, and how to choose formats that work widely.
Container versus codec
A media file has two layers, and people often mix them up.
- A container is the file format that packs streams together. Examples are MP4, WebM, Ogg, and MP3. It holds the video stream, the audio stream, timing data, and metadata such as captions.
- A codec (coder-decoder) is the method used to compress and decompress one stream. Examples are H.264, VP9, AV1, AAC, and Opus.
The file extension names the container only. A .mp4 file can hold H.264 video with AAC audio, or it can hold AV1 video with Opus audio. A browser may play the first and refuse the second, even though both end in .mp4.
Common combinations
Support changes over time, so check current compatibility tables before you rely on a format. These pairings are widely supported in current browsers.
| Container | Typical video codec | Typical audio codec | Notes |
|---|---|---|---|
| MP4 | H.264 | AAC | Plays almost everywhere. H.264 is patent-encumbered. |
| WebM | VP8 or VP9 | Vorbis or Opus | Open and royalty-free. |
| MP4 or WebM | AV1 | Opus or AAC | Better compression, but slower to encode. Hardware decoding is not universal. |
| MP3 | none | MP3 | Audio only, universally supported. |
| Ogg | none | Vorbis or Opus | Audio only. Safari support is limited. |
For video, a safe plan is an MP4 with H.264 and AAC as the broad fallback. Optionally add a WebM file with VP9 and Opus as a smaller option for browsers that support it. For audio, MP3 or AAC in MP4 covers nearly every browser.
Naming the codec in the type attribute
The type attribute on a <source> element tells the browser what a file contains before it downloads anything. A bare container type such as video/mp4 is only a hint. Adding the codecs parameter is more precise.
<video controls width="640">
<source src="clip.webm" type='video/webm; codecs="vp9, opus"'>
<source src="clip.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
</video>The browser walks the list in order and uses the first source it believes it can play. It skips the rest without downloading them. Note the quoting: the codecs value contains double quotes, so the whole attribute uses single quotes.
Common errors
- Trusting the extension. Renaming
movie.movtomovie.mp4does not change its container or codecs. - Wrong codec inside the right container. An MP4 with an unsupported codec gives a media error, or plays audio with a black picture.
- Omitting
type. The browser must then download part of each file to find out whether it can play it, which wastes bandwidth. - Incorrect codec strings. A mistyped string such as
avc1.xxxxcan make the browser skip a file it could play.
Exercise
A file named lecture.mp4 plays sound but shows no picture in one browser. What is the most likely cause, and how would you confirm it?
Show answer
The container (MP4) is supported, but the video codec inside it (for example AV1 or HEVC) is not supported by that browser, while the audio codec is. Confirm by running ffprobe lecture.mp4 to see the video codec, then re-encode the video as H.264 or add a second <source> in a format the browser supports.