Skip to content
systemcoding.dev
Media

Codec vs Container: The Difference Explained Simply

A codec compresses audio or video and a container packages the compressed streams in one file. See the difference with ffprobe and learn to swap containers.

7 min read
Summarize in AI
Flat illustration of shipping containers stacked on a ship, with the Docker, FFmpeg and NixOS logos.

When a video will not play, people say "the format is wrong", and that one word hides two different things. A file like holiday.mp4 has a container, which is the MP4 part, and one or more codecs inside it, which are the part that actually compressed the pictures and sound. Mixing the two up is the source of most confusing advice: "convert it to MP4" may mean a ten-second repackaging or a ten-minute re-encode, depending on what is inside. This post explains each idea with an everyday comparison, shows both parts in a real file with ffprobe, and teaches the two commands that matter: one that changes the container only, and one that changes the codec. It uses FFmpeg 7.1, and the command-line tools ffmpeg and ffprobe.

An everyday picture

Think of a shipping container full of boxes. The boxes hold the goods, and the container is the standard steel shell that lets a crane move them and a port handle them. You can unload the boxes and put them in a different container without opening a single box. The contents are unchanged, and only the packing differs.

A codec (short for coder-decoder) is the way the goods are packed into the boxes: a method for turning raw video frames or raw sound samples into far fewer bytes, and back again. H.264, H.265 (HEVC), VP9 and AV1 are video codecs; AAC, MP3 and Opus are audio codecs. A codec decides how small the data is and how good it looks or sounds, and it decides which devices can play it, because each device needs a matching decoder.

A container (also called a format or wrapper) is the file structure that holds the compressed streams together. It stores the video stream, the audio streams, subtitles, chapter marks and metadata, and it records when each piece should be played so the sound stays aligned with the picture. MP4, MKV (Matroska), WebM, MOV and AVI are containers. A container says nothing about how the pictures were compressed; it just carries whatever compressed data it is given, within the limits of what it supports.

The file extension usually names the container: .mp4, .mkv, .webm. It does not tell you the codecs.

See both in one file

ffprobe ships with FFmpeg and reports exactly what a file holds. Ask for the format and the streams:

Shell
ffprobe -hide_banner -show_entries format=format_name:stream=index,codec_type,codec_name -of default=noprint_wrappers=0 holiday.mp4
Text
[STREAM]
index=0
codec_name=h264
codec_type=video
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_type=audio
[/STREAM]
[FORMAT]
format_name=mov,mp4,m4a,3gp,3g2,mj2
[/FORMAT]

The format name looks odd because FFmpeg uses one demuxer for the whole QuickTime family, and it lists the names it will answer to. The streams are the useful part: stream 0 is video compressed with h264, and stream 1 is audio compressed with aac. The container is MP4, the video codec is H.264, the audio codec is AAC. Three different facts.

For a one-line view that reads naturally, use the default output and look at the lines starting with Stream:

Shell
ffprobe -hide_banner holiday.mp4 2>&1 | grep -E "Input|Stream"
Text
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'holiday.mp4':
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080, 4210 kb/s, 30 fps, 30 tbr, 15360 tbn (default)
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)

The numbers are an example for one file. The line says that the video is H.264 High profile at 1920x1080, and the audio is AAC-LC in stereo.

Change the container, keep the codecs

Suppose a program wants MKV instead of MP4. The codecs are fine, so nothing needs to be decoded or encoded. Tell FFmpeg to copy the compressed data:

Shell
ffmpeg -i holiday.mp4 -c copy holiday.mkv

-c copy means "copy every stream as it is". FFmpeg reads the packets from the MP4 container and writes the same packets into a Matroska container. It is called remuxing. It finishes in the time it takes to read and write the file, and the quality is exactly the same, since no pixel was touched. The result:

Text
Output #0, matroska,webm, to 'holiday.mkv':
  Stream #0:0: Video: h264 (High), yuv420p, 1920x1080, 30 fps, 30 tbr, 1k tbn (default)
  Stream #0:1: Audio: aac (LC), 48000 Hz, stereo, fltp, 128 kb/s (default)
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)

The words (copy) in the stream mapping are your confirmation that nothing was re-encoded. If a command you ran printed (h264 (native) -> h264 (libx264)), it did re-encode.

Change the codec: the slow, lossy way

A different goal is to shrink the file by switching to a more efficient codec, or to make a video play on a device that cannot decode H.264. That needs a re-encode: decode every frame to raw pictures and compress them again with a different codec.

Shell
ffmpeg -i holiday.mp4 -c:v libx265 -crf 28 -c:a copy holiday_hevc.mp4

-c:v libx265 picks the H.265 encoder for the video, -crf 28 sets a constant quality level, and -c:a copy leaves the audio untouched. This keeps the MP4 container, but the video codec changed from H.264 to H.265. The command takes much longer than the remux, since every frame is decoded and encoded, and it loses a little quality because compression is lossy and each generation of encoding discards detail. Do it once, from the best source you have, and not repeatedly.

The two commands produce files that look similar in a file manager and are completely different operations. This is the practical lesson: before converting, ask whether the container or the codec is the problem.

Why a file can be "the right container, the wrong codec"

Container support is not the same as codec support. An MP4 file with H.264 and AAC plays almost everywhere. An MP4 file with an older codec such as MPEG-4 Part 2 video, or a rarely supported audio codec such as an uncommon DTS variant, can play with no sound, or not at all, on a device that decodes the container happily. Likewise, a .mkv file may hold something a TV cannot decode, even though the TV lists MKV among its supported formats.

When a file fails, run ffprobe and read the codec names, then compare them with what the device supports. The fix is one of three things:

  1. Remux to a container the device accepts, when the codecs are already supported: ffmpeg -i in.mkv -c copy out.mp4.

  2. Re-encode only the stream the device cannot decode, and copy the others: ffmpeg -i in.mkv -c:v copy -c:a aac -b:a 160k out.mp4.

  3. Re-encode everything when neither codec is supported.

Option 2 is the most useful in practice, since audio encoding is cheap and video copying is free.

Not every container takes every codec

The reverse limit also exists. A container is a specification, and each one lists the codecs it can carry. WebM is a restricted profile of Matroska that allows VP8, VP9 and AV1 video with Vorbis and Opus audio, and no H.264 or AAC. If you try to copy H.264 into WebM, FFmpeg refuses:

Shell
ffmpeg -i holiday.mp4 -c copy holiday.webm
Text
[webm @ 0x55d0c8a05a80] Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

The error message states the rule, and the fix is to encode to codecs WebM allows, for example -c:v libvpx-vp9 -c:a libopus. A later post compares the common containers in detail.

Where the confusion comes from

Several names blur the line, and knowing the traps helps. MP4 is a container, while MPEG-4 is a family of standards that includes the container (Part 14), the AVC video codec (Part 10) and AAC audio (Part 3). H.264 and AVC are two names for one codec. MOV is Apple's QuickTime container, and MP4 is derived from it. AVI is an old container that has trouble with modern codec features. MP3 is both a codec and a plain audio file format, since MP3 files are a bare stream of MP3 frames with no real container. Matroska is the container behind .mkv (video), .mka (audio) and .webm (a subset).

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.