The keyframe interval is the distance between I-frames in the encoded video. A smaller interval improves seek performance and recovery from packet loss; a larger interval improves compression efficiency. The default is 250 frames (about 10 seconds at 25 fps), but for web video, a 2-second interval is the standard.

The command

ffmpeg -i in.mp4 -c:v libx264 -crf 20 -g 60 -keyint_min 60 out.mp4

What it does

The `-g 60` flag sets the maximum keyframe interval to 60 frames (about 2 seconds at 30 fps). The `-keyint_min 60` flag sets the minimum interval to 60 frames, so the encoder cannot place a keyframe more frequently than every 2 seconds. The result is a file with a keyframe every 2 seconds, which is the standard for web video (YouTube, Vimeo, HLS). The flag also helps with seeking: a 2-second keyframe interval means a seek to any point in the file is at most 2 seconds off.

Requirements

Any video input. The flag is an encoder option, so the encoder must support it (most do). The output is a normal MP4 with the keyframe interval set. For HLS streaming, the standard is a 2-second keyframe interval; for archival, a 10-second interval is fine.

Things to avoid

  • Setting a very small interval. A 1-frame keyframe interval (every frame is a keyframe) is the I-frame-only mode, which produces a huge file with no inter-frame compression.
  • Confusing `-g` with `-keyint_min`. `-g` is the maximum interval; `-keyint_min` is the minimum. For a fixed 2-second interval, set both to 60 (at 30 fps).
  • Forgetting that the flag is in frames, not seconds. `-g 60` is 60 frames, not 60 seconds. At 30 fps, that is 2 seconds; at 25 fps, it is 2.4 seconds.
  • Combining `-g` with `-bf` (B-frames). B-frames reference both past and future frames, so a keyframe followed by a long B-frame chain can confuse the seek logic of some players.