Introduction
Matroska is an open, royalty-free container format, first released in 2002. A Matroska file can hold any number of video, audio and subtitle tracks in almost any codec, together with chapters, tags and attached files such as fonts. That flexibility makes it a common choice for films with several languages and subtitle tracks.
The same format uses .mkv for video, .mka for audio only,
.mks for subtitles only and .mk3d for stereoscopic video. WebM,
used for video on the web, is a restricted form of Matroska.
Structure
Matroska is built on EBML, the Extensible Binary Meta Language, a binary form of XML. Every element is an ID, a size and the data. IDs and sizes are variable-length integers: the number of leading zero bits in the first byte says how many bytes follow, so small values take little space. Elements nest, so the file is a tree.
A file starts with an EBML header, ID
1A 45 DF A3, whose DocType is matroska or webm.
Then comes one Segment holding everything else: a
SeekHead index and Info element with
the timescale and duration, the Tracks
element describing each track, and
Clusters of encoded frames, each cluster
starting with a timestamp. Cues
near the end let a player seek quickly. Chapters
and tags are optional, and each track names its codec by a
Codec ID.
0EBML header: EBML version and DocTypevaries…Segment holds all the contentvaries…SeekHead where the other top-level elements arevaries…Info timescale, duration and titlevaries…Tracks one entry per track: type, codec, settingsvaries…Cluster a timestamp then blocks of framesrepeated…Cues seek points; also Chapters, Tags, Attachmentsvaries
Not described
Known, not yet covered
- Reference: the byte layout of the Block header and its flag bit positions were taken from the lacing section and from the Matroska notes rather than read from RFC 9559 sections 10.1 and 10.2.
- Viewer: video is not decoded. The tracks, their codec settings and every frame (offset, size, timecode, flags, lacing) are listed, but no picture is drawn for H.264, HEVC, VP8, VP9, AV1 or any other video codec.
- Viewer: only Opus, Vorbis and DTS (
A_DTSandA_DTS/LOSSLESS: the core and its XCh, XXCh, X96, XBR and XLL (lossless) extensions) sound is decoded and played. AAC, FLAC, MP3, AC-3, PCM and the other audio codecs are named and their frames listed, with the note that their sound is not decoded here (shared decoders for some of them exist and are not yet connected). - Viewer: content encodings are not applied. A track with ContentCompression (zlib, bzlib, lzo1x, header stripping) or ContentEncryption is shown as it is stored, and its frames are not unpacked or decrypted.
- Viewer: a track cannot yet be saved or opened as a file of its own (for example an Opus track as an Ogg stream); only attached files open as entries. The CodecPrivate of codecs other than Opus, Vorbis, FLAC, AAC, AVC, HEVC, VP9, AV1, VfW and ACM is shown as bytes.
- Viewer: chained and linked Segments (several Segments in one file, hard and soft linking, ordered chapters, Matroska Script and DVD menu chapter commands) are listed as they are stored and are not followed or run.
Links
Software
- VLC media player – free media player that plays MKV with all its tracks (Windows, macOS, Linux, mobile)
- mpv – free, minimal media player with strong MKV and subtitle support (Windows, macOS, Linux)
- MKVToolNix – free tools for creating, splitting and editing Matroska files (Windows, macOS, Linux)
- Kodi – free media centre that plays MKV libraries (Windows, macOS, Linux, mobile)
- HandBrake – free video converter that reads and writes MKV (Windows, macOS, Linux)
- FFmpeg – command-line tools that read, write and convert MKV (Windows, macOS, Linux)
- DaVinci Resolve – video editor and colour grading app that imports MKV (Windows, macOS, Linux)
Developers
- libmatroska – the reference Matroska library, built on libebml (C++)
- libebml – the reference EBML reader and writer (C++)
- libavformat – FFmpeg’s container library, which reads and writes MKV (C)
Reference
- Matroska element specification – every Matroska element, its ID and its meaning, on matroska.org
- RFC 8794 – the Extensible Binary Meta Language that Matroska is built on