“FFmpeg: Setting a Fixed Keyframe Interval with -g and -sc_threshold 0”
To place keyframes at a fixed interval when encoding to H.264 (libx264) with FFmpeg, write the frame count with -g and add -sc_threshold 0. For a 30fps video, -g 60 -sc_threshold 0 put keyframes exactly every 2 seconds. This setting is useful when you split video every few seconds for HLS, or when you want a video that is easy to seek through. The FFmpeg HLS output documentation says that HLS splits the video at the first keyframe after the specified duration has passed.
-g 60 alone did not keep keyframes at 2-second intervals. libx264 also inserts a keyframe at each scene change and restarts the count from there.
If you want the interval in seconds, use -force_key_frames, which does not depend on the frame rate.
The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), on a virtual machine with 4 CPU cores. The source was a 60-second video (1280×720, 30fps, no audio) made by joining four FFmpeg test patterns (testsrc2, mandelbrot, life, cellauto), 7 seconds each, so the scene changes clearly every 7 seconds. I used only libx264, and all quality settings except -crf 23 were left at their defaults. I did not test camera footage, other encoders such as libx265 or VP9, or GPU encoders.
- 1. Checking keyframe positions with ffprobe
- 2. Without any options, keyframes appear only at scene changes
- 3. With -g, the count restarts at each scene change
- 4. Adding -sc_threshold 0 makes the interval fixed
- 5. To set the interval in seconds, use -force_key_frames
- 6. Shorter intervals made the file larger
Checking keyframe positions with ffprobe
A keyframe is a frame where playback or seeking can start in the middle of the video. To see where keyframes are placed, print the packet information with ffprobe. Packets whose flags contain K are keyframes. In this article, I checked the positions with the following script (kf.sh), which prints the keyframe times in seconds on one line.
#!/bin/sh
# Print the times of keyframes (packets whose flags contain K) on one line
ffprobe -v error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=p=0 "$1" | awk -F, '$2 ~ /K/ {printf "%s%s", sep, $1+0; sep=" "} END {print ""}'
Run it with the file you want to check, for example ./kf.sh out.mp4.
Without any options, keyframes appear only at scene changes
First, I converted the video without specifying any keyframe options.
ffmpeg -i src.mp4 -c:v libx264 -crf 23 default.mp4
0 7 14.0667 21 28 35 42.0667 49 56
Keyframes appeared only at the start and at the 7-second scene changes. At the scene changes at 14 and 42 seconds, the keyframes were placed 2 frames later (14.07 and 42.07 seconds).
Running strings default.mp4 shows that libx264 writes its encoding settings into the file as text. Here it showed keyint=250 keyint_min=25 scenecut=40. This means the maximum keyframe interval is 250 frames (about 8.3 seconds at 30fps), and scenecut, which controls scene change detection, is 40. The value 250 is the default in x264 itself. When -g is not given, the FFmpeg libx264 wrapper uses the x264 default as it is (the default values in FFmpeg 6.1.1's libx264.c, where values are passed to x264, and common/base.c in x264, where the x264 defaults are set). In this source, scene changes came every 7 seconds, so the next keyframe was always inserted before the 250-frame limit was reached.
With -g, the count restarts at each scene change
The FFmpeg libx264 documentation says that -g corresponds to x264's keyint, which is the maximum GOP length, meaning the upper limit on the keyframe interval. The unit is frames, so for 2-second intervals at 30fps, specify 60.
ffmpeg -i src.mp4 -c:v libx264 -crf 23 -g 60 g60.mp4
0 2 4 6 7 9 11 13 14 16 18 20 21 23 25 27 28 30 32 34 35 37 39 41 42 44 46 48 49 51 53 55 56 58
Up to 6 seconds, keyframes were every 2 seconds. A keyframe was then inserted at the 7-second scene change, and the count restarted from there, giving 9, 11, 13 seconds and so on. There were 34 keyframes in total. -g sets an upper limit, meaning "do not go longer than this." It does not prevent keyframes from being inserted at scene changes.
Adding -sc_threshold 0 makes the interval fixed
To stop keyframes from being inserted at scene changes, add -sc_threshold 0. -sc_threshold corresponds to x264's scenecut, the threshold used to detect scene changes.
ffmpeg -i src.mp4 -c:v libx264 -crf 23 -g 60 -sc_threshold 0 g60sc0.mp4
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58
Exactly 30 keyframes appeared, every 2 seconds. The settings written to the file were keyint=60 keyint_min=6 scenecut=0.

Writing the same settings as x264 options, -x264-params keyint=60:scenecut=0, produced the same keyframe positions and the same written settings.
Raising -keyint_min does not stop scene-change keyframes
There is also -keyint_min, which sets the minimum GOP length (it corresponds to x264's min-keyint). When I specified -g 60 -keyint_min 60, the written setting was keyint_min=31, with or without -sc_threshold 0. x264 limits the minimum length to the maximum length divided by two, plus one (encoder/encoder.c in x264).
When I converted with only -g 60 -keyint_min 60, without -sc_threshold, the keyframes were 2 seconds apart for this source. At the scene changes (7, 21, 35, and 49 seconds), the frames were I-frames but not keyframes. x264 does not turn a scene-change I-frame into a keyframe unless it is at least keyint_min frames after the previous keyframe (encoder/slicetype.c in x264). In this source, every scene change came 1 second (30 frames) after the previous keyframe, which was not enough to reach 31 frames.
However, when I removed the first 1.5 seconds of the source so that the scene changes fell 1.5 seconds after a keyframe, the result changed.
ffmpeg -ss 1.5 -i src.mp4 -c:v libx264 -crf 23 -g 60 -keyint_min 60 shift_min60.mp4
0 2 4 5.5 7.5 9.5 11.5 13.5 15.5 17.5 19.5 21.5 23.5 25.5 27.5 29.5 31.5 33.5 35.5 37.5 39.5 41.5 43.5 45.5 47.5 49.5 51.5 53.5 55.5 57.5
A keyframe was inserted at the 5.5-second scene change, and every keyframe after that was shifted by 0.5 seconds. With the same source and -g 60 -sc_threshold 0, keyframes stayed at 2-second intervals from 0 seconds. To fix the interval, you need -sc_threshold 0.
To set the interval in seconds, use -force_key_frames
-g is a frame count, so the interval in seconds changes when the frame rate changes. The expression syntax described in the -force_key_frames section of the ffmpeg command documentation lets you specify the interval in seconds. n_forced is the number of keyframes inserted so far. The following option means "insert a keyframe when the time t reaches n_forced × 2 seconds or more," so keyframes are placed every 2 seconds.
ffmpeg -i src.mp4 -c:v libx264 -crf 23 -force_key_frames "expr:gte(t,n_forced*2)" -sc_threshold 0 fkf_sc0.mp4
These results compare different frame rates for the 30fps source, changed with -r. All runs used -sc_threshold 0.
| Frame rate | Interval with -g 60 |
2-second interval with -force_key_frames |
|---|---|---|
24fps (-r 24) |
2.5 seconds (0, 2.5, 5…) | 2 seconds (0, 2, 4…) |
| 30fps (unchanged) | 2 seconds | 2 seconds |
60fps (-r 60) |
1 second (0, 1, 2…) | 2 seconds (0, 2, 4…) |
-force_key_frames also required -sc_threshold 0. Without it, keyframes were inserted every 2 seconds and also at the scene changes at 7, 21, 35, and 49 seconds, for a total of 34 (the bottom row of the diagram above).
The official documentation also notes that forcing keyframes too often can hurt lookahead processing in some encoders, and that options that fix the GOP structure are more efficient. If the frame rate is fixed, use -g with -sc_threshold 0. If you want videos with different frame rates to have the same interval in seconds, use -force_key_frames. Either way, you can keep the interval fixed.
Shorter intervals made the file larger
Keyframes use more data than other frames. In the video converted with -g 60 -sc_threshold 0, the average size of the 30 keyframes was 257,379 bytes, and the average of the other 1,770 frames was 47,317 bytes, a difference of more than 5 times.
The table below shows file sizes when only -g was changed with -crf 23 -sc_threshold 0. The percentages are relative to -g 250, the same as libx264's default. The image quality is the average SSIM against the original video, measured with the ssim filter.
-g |
Interval (30fps) | Number of keyframes | Size | Compared with -g 250 |
SSIM |
|---|---|---|---|---|---|
| 15 | 0.5 seconds | 120 | 106.0 MB | +22.1% | 0.98938 |
| 30 | 1 second | 60 | 97.2 MB | +11.9% | 0.98885 |
| 60 | 2 seconds | 30 | 91.5 MB | +5.3% | 0.98830 |
| 120 | 4 seconds | 15 | 87.7 MB | +1.0% | 0.98790 |
| 250 | About 8.3 seconds | 8 | 86.9 MB | Baseline | 0.98740 |
| 600 | 20 seconds | 3 | 85.1 MB | -2.0% | 0.98747 |
For this source, 2-second intervals (-g 60) made the file 5.3% larger, and 0.5-second intervals (-g 15) made it 22.1% larger. All of these files are large, at 85 MB or more for 60 seconds. From -g 15 to -g 250, SSIM rose slightly as the interval got shorter, but the difference between the highest (-g 15) and the lowest (-g 250) was only about 0.002.
Because shorter intervals make the file larger, keep the keyframe interval only as short as your segment length or seeking needs require.



