I-Frames, P-Frames, B-Frames and GOP Structure Explained
Learn how I, P and B frames and the GOP work in H.264 video, why keyframes enable seeking, and how to inspect and set them with ffprobe and x264 options.

A video file is not a stack of photographs. Thirty full pictures per second, uncompressed, would be enormous, and most of each picture is the same as the one before it. Video codecs exploit this: they store a complete picture occasionally and describe the changes in between. The names for the three kinds of stored picture, I-frames, P-frames and B-frames, and for the repeating pattern they form, the GOP, appear in every encoder setting and every streaming guide. This post explains each frame type with the idea behind it, shows a real frame sequence read out of a file with ffprobe, explains why keyframes control seeking and streaming start-up, and shows the options that set the pattern in x264. It uses H.264 as the example and FFmpeg 7.1.
The idea: describe the difference
Imagine a news broadcast where the presenter sits still and only the lips move. Frame after frame, the background and the suit are identical. Instead of storing the background thirty times a second, the encoder stores it once and, for the following frames, stores only what changed, with instructions such as "copy this block from the previous frame, shifted two pixels to the right". Those instructions are small compared with the picture.
This creates a dependency. A frame that is described as "the previous frame plus changes" cannot be shown until the previous frame is known. The three frame types are three ways to handle that dependency.
| Type | Name | Depends on | Size |
|---|---|---|---|
| I | Intra-coded frame | nothing; self-contained | largest |
| P | Predicted frame | earlier frames | medium |
| B | Bi-predicted frame | earlier and later frames | smallest |
I-frames: the anchors
An I-frame is coded using only its own content, like a JPEG image. A decoder can start from an I-frame without having seen anything before it. That makes it the seek point: a player that jumps to the five-minute mark must find the nearest I-frame before that time, decode it, and then decode forward to the exact frame. It is also the entry point for streaming: a viewer who joins a live stream can only start displaying at the next I-frame.
In H.264 there are two kinds of intra picture, and the difference matters. An IDR frame (instantaneous decoder refresh) is an I-frame that also resets the decoder's memory: no later frame may refer to any frame before it. A plain I-frame that is not IDR allows later frames to reference frames from before it, so it is not a clean cut. Real seek points and stream splits need IDR frames. Most encoders default to making their keyframes IDR frames, and tools call them "keyframes" without distinguishing.
P-frames: look back
A P-frame is coded by prediction from earlier frames. The encoder splits the picture into blocks and, for each block, finds the best-matching area in a reference frame and stores the motion vector pointing to it, plus a small correction (the residual). P-frames are much smaller than I-frames, typically a fraction of the size, though the ratio depends entirely on the content: a static scene compresses far better than a scene with a camera pan.
A P-frame needs the frame it references, so decoding must go in order. If the reference is lost or damaged, the error spreads to every frame that depends on it until the next I-frame, which is why packet loss in a stream causes smearing that cleans up at the next keyframe.
B-frames: look back and forward
A B-frame can reference frames both before and after it in display time. Since the future frame must be decoded first, the order in which frames are stored and transmitted is not the order in which they are shown. This is the single most confusing point for beginners.
Consider a display order I B B P. To decode the two B-frames, the decoder needs both the I-frame before and the P-frame after. So the stream stores I P B B: the P-frame is moved ahead. The decoder reconstructs the frames in that decode order and a reorder buffer puts them back in display order. Each frame carries two timestamps for this: a DTS (decode time stamp) and a PTS (presentation time stamp). With B-frames the two differ; without them they are equal.
B-frames give the best compression, since bidirectional prediction finds good matches, and cost two things: a delay, because the encoder must wait for the future frame, and complexity. That is why low-latency video such as video calls uses no B-frames, while on-demand video uses several.
The GOP: one anchor to the next
A GOP (group of pictures) is the run of frames from one keyframe up to, but not including, the next. The GOP length is the distance between keyframes. A typical on-demand setting is a keyframe every two seconds. At 30 frames per second, that is a GOP of 60 frames, such as:
I B B P B B P B B P B B P B B P ... (then the next I)Within a GOP, the pattern of P and B frames is the GOP structure. The common structure with two B-frames between references is written in display order as I B B P B B P. Longer GOPs compress better, because there are fewer large I-frames, but they hurt in three ways: seeking is coarser, a stream's start-up wait is longer, and one error persists longer. Streaming formats such as HLS and DASH need keyframes aligned with segment boundaries, since each segment must start with a keyframe so it can be played alone.
See the frames in a real file
ffprobe can print the picture type of every frame. Ask for the picture type and timestamps of the first dozen frames in the video stream:
ffprobe -v error -select_streams v:0 -show_entries frame=pict_type,pts_time,pkt_dts_time -of csv=p=0 -read_intervals "%+#12" input.mp40.000000,I,-0.200000
0.133333,P,-0.133333
0.066667,B,-0.066667
0.100000,B,0.000000
0.266667,P,0.066667
0.200000,B,0.133333
0.233333,B,0.200000The columns are pts_time, pict_type and pkt_dts_time. The output shown is an example, and a few lines are enough to see the key facts. The first frame is an I with PTS 0. The second frame printed has PTS 0.133333, which is later than the third and fourth lines (0.066667 and 0.100000): the stream is stored in decode order I P B B P B B, but display order sorts by PTS, giving I B B P B B P. The DTS values increase by one frame duration per line, and the first DTS is negative, which is how a container records the start-up delay that B-frame reordering needs.
To list only the keyframes and see the GOP length of a file:
ffprobe -v error -select_streams v:0 -show_entries frame=pts_time,pict_type -of csv=p=0 input.mp4 | grep ",I"0.000000,I
2.000000,I
4.000000,I
6.000000,IKeyframes every 2.0 seconds. That is a GOP of two seconds, or 48 frames at 24 fps.
Set the structure with x264
libx264 exposes the pattern through a few options. In FFmpeg you can pass them as -g and -bf or inside -x264-params:
ffmpeg -i input.mp4 -c:v libx264 -g 48 -bf 2 -sc_threshold 0 -c:a copy output.mp4-g 48 sets the maximum GOP length, a keyframe at least every 48 frames. -bf 2 allows up to two B-frames between references. -sc_threshold 0 disables scene-cut detection, which otherwise inserts extra keyframes where the picture changes abruptly; for streaming with fixed segment lengths you want predictable keyframes, so you turn it off. The equivalent in x264's own vocabulary:
ffmpeg -i input.mp4 -c:v libx264 -x264-params keyint=48:min-keyint=48:bframes=2:scenecut=0 output.mp4For a video call or other live use, remove the delay of B-frames:
ffmpeg -i input.mp4 -c:v libx264 -tune zerolatency -g 30 -bf 0 output.mp4-tune zerolatency turns off B-frames and look-ahead, and -bf 0 states it explicitly. Compression is worse than with B-frames at the same quality, which is the trade you make for low delay.
What it means in practice
If seeking is slow or imprecise in a player, the GOP is long: check the keyframe distance with the command above and shorten it. If a live stream takes seconds to start for each new viewer, the keyframe interval is the reason, because the viewer must wait for the next keyframe. If a file's size is larger than expected for its quality, scene-cut detection or a short GOP may be inserting too many I-frames. If a player shows garbage that clears up after a moment, a reference was lost, and the picture recovers at the next keyframe. These four observations explain a large part of everyday video behaviour.


