Skip to content
systemcoding.dev
Media

MP4 vs MKV vs WebM: Choosing a Container

Choose between MP4, MKV and WebM by use case: web playback, archiving, editing and streaming, with what each allows and the FFmpeg commands to convert.

6 min read
Summarize in AI
Flat illustration of video frames passing through a codec, with the Docker, FFmpeg and GStreamer logos.

You have a video, and a program is asking which container to save it in. The three names that come up are MP4, MKV and WebM, and all three are fine containers. The question is which one fits the place the file is going. This post is a decision guide organised by purpose: web playback, long-term archiving, editing, streaming and sharing. A separate reference lists exactly which codecs each container accepts in a compatibility table, and this post does not repeat it. Instead, it explains what each container is for, what it gives up and how to switch in one FFmpeg command. It uses FFmpeg 7.1.

What the three are

MP4 (MPEG-4 Part 14, built on the ISO base media file format) is the container of most phone videos, streaming services and download buttons. It is structured as a tree of boxes, with an index (moov) that tells a player where every frame is. It carries H.264, H.265 and AV1 video, AAC and other audio, and simple subtitle tracks, and it is supported by nearly every device and browser made in the last decade.

MKV (Matroska) is an open container built on EBML, a binary format that makes it extensible. It carries almost any video and audio codec, any number of audio and subtitle tracks, chapters, attachments such as fonts, and rich tags. It is the container of choice for archived media libraries, ripped discs and anime. It has weaker native support in mainstream devices and browsers.

WebM is a restricted profile of Matroska, defined by the WebM project for the web. It allows VP8, VP9 and AV1 video with Vorbis and Opus audio, and WebVTT subtitles. It has royalty-free codecs only, and it is designed to be playable by browsers without licensing worry.

Decide by purpose

Publishing video on a web page

Use MP4 with H.264 and AAC if you want the one file that plays on every browser and phone. Use WebM with VP9 or AV1 and Opus as an additional source for browsers that support it, where it often delivers smaller files at the same quality. A page can offer both and let the browser pick the first it supports:

HTML
<video controls width="640">
  <source src="clip.webm" type='video/webm; codecs="vp9, opus"'>
  <source src="clip.mp4"  type='video/mp4; codecs="avc1.640028, mp4a.40.2"'>
</video>

Order matters: the browser picks the first source it can play, so put the preferred, smaller format first. For files that start playing before they finish downloading, the MP4 needs its index at the front, which one FFmpeg flag does:

Shell
ffmpeg -i input.mov -c copy -movflags +faststart output.mp4

+faststart moves the moov box to the start of the file in a second pass, so a browser can begin playback right away. Without it, a progressive download must fetch the end of the file before it can play.

Archiving and keeping a library

Use MKV. It accepts whatever codec the source has, including ones MP4 will not carry, such as FLAC audio, some subtitle formats (SRT, ASS, PGS) and multiple audio tracks, and it stores chapters and attachments. Remuxing a disc rip into MKV with -c copy loses nothing. Media servers such as Jellyfin and Plex read MKV well, though some TV apps have trouble with it, in which case the server transcodes or remuxes on the fly.

Shell
ffmpeg -i disc.m2ts -map 0 -c copy library.mkv

-map 0 includes every stream of the input, not only the default video and audio, which is what you want for an archive.

Editing

Editors prefer an intermediate container: MOV or MP4 with an editing-friendly codec, or MKV is rarely used here. If your editor accepts MP4 with H.264, a plain MP4 works, but long-GOP H.264 is slow to scrub, so many workflows convert to an intra-frame codec in MOV first. This is a codec decision, not a container one: all three containers can hold the data, and editors differ in which they import.

Streaming (HLS and DASH)

Streaming formats do not use one big file. HLS delivers short segments, traditionally MPEG-TS and now often fragmented MP4 (fMP4), and DASH uses fragmented MP4 or WebM segments. MP4 is the common base, and CMAF, the common media application format, standardises fMP4 for both. If you are building a streaming ladder, create MP4 renditions and let a packager (FFmpeg's hls muxer, Shaka Packager, Bento4) cut them. A later post explains the fragmented structure.

Sharing by message or email

Use MP4 with H.264 and AAC. It plays in chat apps, on phones and in email clients without conversion. MKV frequently does not preview, and WebM is not accepted everywhere.

What each one cannot do

Limits are as useful as features, and the table collects the ones that cause trouble in practice.

MP4MKVWebM
Native device and browser supportexcellentlimitedgood in browsers
Arbitrary codecsno, a defined listalmost anyno, VP8/VP9/AV1 and Vorbis/Opus only
Multiple audio and subtitle tracksyes, with limits on subtitle formatsyes, many formatsaudio yes; text only as WebVTT
Chapters and attachmentschapters yes, attachments nobothchapters yes
Streams while downloadingwith +faststartyes by designyes
Royalty concerns for the codecsH.264, H.265 and AAC carry patent licensingdepends on codecnone, all royalty-free

Two details deserve a sentence each. MP4's subtitle support is narrow: text subtitles are stored as mov_text, which is plain and styled poorly, and image subtitles from discs (PGS, VobSub) do not fit, so converting a disc rip to MP4 usually loses them or forces you to burn them into the picture. And MKV can hold codecs such as H.264 with features that some MP4 players reject; the container is not at fault.

Convert between them

Containers differ in what they accept, so the conversion depends on the codecs. If the codecs are allowed in both, remux:

Shell
ffmpeg -i input.mkv -c copy output.mp4

If an MKV holds subtitles that MP4 cannot take, keep video and audio and convert text subtitles to mov_text:

Shell
ffmpeg -i input.mkv -map 0:v -map 0:a -map 0:s? -c:v copy -c:a copy -c:s mov_text output.mp4

The 0:s? with a question mark makes the subtitle mapping optional, so the command does not fail if the file has none. To make a WebM, you need codecs that WebM allows, so re-encode:

Shell
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 32 -b:v 0 -c:a libopus -b:a 96k output.webm

-crf 32 -b:v 0 selects constant-quality mode for VP9. This command takes a while, since VP9 encoding is slower than H.264, and the output file is usually smaller than the H.264 original at similar quality.

A short decision flow

Start with the destination. If it is a web page or a phone, pick MP4 with H.264 and AAC, and add WebM if you want smaller files in browsers that support it. If it is a library you control, pick MKV and remux everything. If it is a streaming service, produce fragmented MP4 renditions. If it is for editing, ask what the editor imports. Then check that your codecs fit the container: if they do, remux; if not, re-encode only the stream that does not fit.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.