Ch. 16 · Containers in Depth

한국어日本語
7 min

EBML in MKV and WebM: A Container with Variable-Length Sizes

Matroska is a tree of variable-length elements called EBML. WebM is a subset of it.

MKV (Matroska) and WebM come from a different lineage than MP4. Their foundation is EBML (Extensible Binary Meta Language), a tree in which elements contain elements, like XML, but written in binary. It is an open format with a public specification, and the element definitions can be checked in the Matroska specification and in the documents standardized by the IETF (RFC 8794 for EBML, RFC 9559 for Matroska).

What a single element looks like

An EBML element is ID + size + data. Its distinguishing feature is that both the ID and the size are written as variable-length integers. The number of leading zeros in the first byte tells you how many bytes the value takes. This contrasts with MP4 boxes, which use a fixed 4-byte size. Like MP4, even an ID you don't recognize can be skipped because you can read its size.

EBML header (DocType) Segment SeekHead Info Tracks (codec setup) Cluster (Block group) Cluster Cues Chapters · Tags · Attachments Cues = seek index (plays part of the role of the sample table in MP4)
Typical top-level structure of Matroska
  • EBML header: states that the file is EBML and whether the DocType is matroska or webm.
  • Segment: the top-level element that contains all the actual content.
  • Tracks: the codec ID and codec configuration for each track.
  • Cluster: a unit that groups a span of time's worth of Blocks (SimpleBlock and others). Each cluster has a reference timestamp, and the blocks inside use values relative to it.
  • Cues: the index for seeking. It is often placed at the end of the file, and SeekHead tells the player where each element is so it can find them.
  • Chapters · Tags · Attachments: chapters, metadata tags, and attached files (such as fonts for subtitles).
A structural difference from MP4

In MKV, both the data (Cluster) and the index (Cues) are elements inside the Segment, so it is easy to keep appending from the front before the file is finished. Even if a recording is cut off partway, the clusters already written can generally still be read, which is why recording software (such as OBS with its MKV recording) recommends MKV. For live use, writing the Segment size as "unknown" is also a technique that is used.

WebM is a subset of MKV

WebM is a profile restricted for the web, built on the Matroska structure. Its DocType is webm, and it limits video to royalty-free codecs such as VP8, VP9, and AV1, and audio to Vorbis and Opus (VP9 and AV1). So "every WebM can be read as MKV, but not every MKV is WebM." Conversely, if you put H.264+AAC in an MKV, that file is not WebM.

MP4MKVWebM
Structural basisISOBMFF boxesEBML elementsEBML (a subset of Matroska)
Size notationFixed 4 bytes (or 64-bit extension)Variable-length integerVariable-length integer
Codecs it holdsMostly those registered in the standardAlmost unrestrictedVP8·VP9·AV1 / Vorbis·Opus, etc.
Browser and device compatibilityWidestVaries by playback environmentBrowser-centered

Compatibility depends on the environment. Check whether a file will actually play with the codec support tool.

EBMLExtensible Binary Meta Language

An element-tree format that writes IDs and sizes with variable lengths. The basis of Matroska and WebM.

ClusterCluster

In Matroska, a unit that groups a span of time's worth of blocks. It has its own reference timestamp.

CuesCues

The element in Matroska that serves as the seek index.

DocTypeDocument type

A value written in the EBML header. It distinguishes matroska from webm.

Check yourself

Which statement correctly describes the relationship between WebM and MKV?