Modern video codecs do not compress each frame as if it were an independent photograph. They exploit the fact that, from one frame to the next, most of the picture changes very little. To do this they use three kinds of frame, organised into groups called GOPs (Groups of Pictures). Understanding this structure explains many behaviours that otherwise seem mysterious: why cutting a video without re-encoding is not frame-accurate, why a live stream needs regular keyframes, why a file with lots of motion is heavier.
I-frames: complete pictures
An I-frame (intra) is compressed using only the information it contains, like a JPEG image. It depends on no other frame and can be decoded on its own. This is why it is also called a keyframe.
Characteristics: - it is the heaviest type of frame; - it is the point from which a decoder can start reading; - it stops errors from spreading: if some data is corrupted, the damage ends at the next keyframe.
P-frames: forward prediction
A P-frame (predicted) describes only the differences from a previous frame. The encoder splits the picture into blocks and, for each one, searches the reference frame for a similar area. Instead of storing pixels, it stores a motion vector («this block is the earlier one shifted three pixels to the right») and a residual, the small difference left over.
A P-frame weighs much less than an I-frame, but it cannot be decoded without the frame it depends on.
B-frames: bidirectional prediction
A B-frame (bi-predicted) can use both earlier and later frames as references. Think of an object that moves and uncovers the background: what appears behind the object does not exist in the previous frame, but it does exist in the next one.
To use a future frame, the encoder must transmit it first. This is why, in streams with B-frames, the decoding order differs from the display order, and containers store two separate timestamps, called DTS and PTS.
B-frames are the lightest, but they add complexity and a small delay, which is why they are sometimes disabled in very low-latency applications.
What a GOP is
The GOP is the sequence running from one keyframe to the next. A typical structure might be:
I B B P B B P B B P B B I ...
The GOP length is the number of frames between two keyframes. In a video at 25 frames per second, a GOP of 50 means one keyframe every 2 seconds.
Long or short GOP?
| Long GOP | Short GOP |
|---|---|
| lighter file at equal quality | heavier file |
| less precise seeking | fast, precise jumps |
| errors propagate for longer | quick recovery from errors |
| suited to archiving and distribution | suited to editing and segmented streaming |
Closed and open GOP
In a closed GOP no frame refers to frames in another GOP: each group is self-contained. In an open GOP some B-frames at the start may refer to the previous GOP, gaining a little efficiency but making cutting trickier. Adaptive streaming prefers closed GOPs, because each segment must be decodable on its own.
Why keyframes matter in streaming
In adaptive streaming the video is split into segments a few seconds long, and the player can switch quality only at the start of a segment. Each segment must therefore begin with a keyframe, and keyframes must fall at the same instants in every quality level. That is why a fixed interval is set and automatic keyframe insertion at scene changes is disabled or limited.
With FFmpeg and x264, a 2-second interval at 25 fps is set like this:
ffmpeg -i input.mp4 -c:v libx264 -g 50 -keyint_min 50 -sc_threshold 0 output.mp4
Live streaming platforms also usually ask for a keyframe every few seconds: an interval that is too long delays playback start for viewers who join.
Cutting without re-encoding
When you cut a video with -c copy or with an editor in «lossless» mode, the start point can only fall on a keyframe. If you ask to start at second 12.3 and the nearest keyframe is at second 10, the result will start at 10 or show a corrupted picture until the next keyframe. Some tools solve this by re-encoding only the opening section up to the first keyframe and copying the rest: the so-called smart cut.
Motion has a cost
The more the picture changes, the closer P- and B-frames get to the weight of an I-frame: prediction works poorly and the residual grows. This is why handheld footage with constant shake is noticeably heavier than the same footage stabilised. Stabilising before encoding improves both viewing and compression.
In short
- I-frames are complete, P-frames predict from the past, B-frames from past and future.
- The GOP is the distance between two keyframes and determines size and seeking precision.
- Adaptive streaming requires aligned keyframes and closed GOPs.
- Cutting without re-encoding is only accurate on keyframes.
- Motion and shake reduce the efficiency of temporal compression.