Sub topic 3
Video basics: codecs, containers and bitrate
This chapter assumes you know basic web architecture, HTTP, and how to read short shell and Python snippets. It covers three ideas that every later chapter depends on: codecs, containers and bitrate. You need them before you can design the transcoding pipeline.
Codecs compress, containers package
A raw video stream is far too large to store or send. A 1080p video at 30 frames per second, with 3 bytes per pixel, is about 187 MB per second. A codec (coder-decoder) compresses it, and the player decompresses it. Common video codecs are H.264 (AVC), H.265 (HEVC), VP9 and AV1. Common audio codecs are AAC and Opus.
A container is the file format that holds the compressed video, the compressed audio, timing data and metadata in one place. MP4, MPEG-TS, WebM and fragmented MP4 (fMP4) are containers. The container does not change the picture quality. The codec does.
| Layer | Question it answers | Examples |
|---|---|---|
| Codec | How is the picture or sound compressed? | H.264, AV1, AAC |
| Container | How are the streams stored together? | MP4, MPEG-TS, WebM |
A file name such as video.mp4 does not tell you the codec inside. Two MP4 files can use different codecs, and a device may play one but not the other. This is why the master playlist in a later chapter lists a CODECS value for each rendition.
How compression works, briefly
H.264 and similar codecs store three kinds of frames:
- An I-frame (keyframe) is a full picture that can be decoded alone.
- A P-frame stores only the changes from earlier frames.
- A B-frame stores changes relative to both earlier and later frames.
The frames between two keyframes form a group of pictures (GOP). A player can only start decoding, or switch quality, at a keyframe. This is why streaming platforms control the GOP length, as the next sections show.
Bitrate
Bitrate is the amount of data used per second of video, usually in kilobits or megabits per second (kbps, Mbps). It sets file size and the network speed a viewer needs:
file size (bytes) = bitrate (bits/s) × duration (s) ÷ 8A 10-minute video at 3 Mbps is 3,000,000 × 600 ÷ 8 = 225 MB.
Bitrate has a direct link to quality, but the link is not linear. Doubling the bitrate gives less and less visible improvement. Newer codecs such as AV1 reach the same quality at a lower bitrate than H.264, but they cost more compute to encode and not every device can decode them.
There are two common rate-control modes:
- Constant bitrate (CBR) keeps the rate nearly flat. It is predictable for networks but wastes bits on simple scenes.
- Variable bitrate (VBR) spends more bits on complex scenes and fewer on simple ones. With a cap (
-maxrate), it suits streaming well.
Seeing it in a command
The transcoding command used throughout this tutorial shows all three ideas at once:
ffmpeg -i input.mp4 \
-vf scale=-2:720 \
-c:v libx264 -preset medium -b:v 2800k -maxrate 3000k -bufsize 6000k \
-g 48 -keyint_min 48 -sc_threshold 0 \
-c:a aac -b:a 128k \
-f hls -hls_time 4 -hls_playlist_type vod \
-hls_segment_filename "720p/seg_%04d.ts" \
720p/index.m3u8-c:v libx264and-c:a aacchoose the codecs.-b:v 2800k,-maxrate 3000kand-bufsize 6000kset a capped variable bitrate.-g 48 -keyint_min 48 -sc_threshold 0force a keyframe every 48 frames. At 24 frames per second that is every 2 seconds, so each 4-second segment starts on a keyframe.-f hlsand the.tssegment names choose the container and packaging.
Common errors
- Assuming the file extension tells you the codec.
- Using a codec that target devices cannot decode in hardware, which drains batteries or fails to play.
- Ignoring
-bufsize. A buffer that is too small makes quality swing, and one that is too large lets bitrate spikes through. - Forgetting audio. It is small, around 128 kbps, but it must be present in every rendition.
Exercise
A source video lasts 20 minutes. You encode it at 4 Mbps video plus 128 kbps audio. About how large is the file in megabytes, and why can a player not start playing from any frame it likes?
Show answer
Total bitrate is 4,128,000 bits/s. File size is 4,128,000 × 1200 ÷ 8 = 619,200,000 bytes, about 619 MB. A player can only start decoding at a keyframe (I-frame), because P-frames and B-frames depend on other frames and cannot be decoded alone.