How to set the keyframe interval to 5 seconds using FFmpeg?
We recommend a framerate of 25 Frames Per Second (FPS) and a keyframe interval of 2 seconds (or 50 frames). If this isn't possible, you can also use a framerate of 30 FPS instead. The keyframe interval should never be more than 4 seconds.
To get the number of frames for the keyframe interval, multiply the desired number of seconds with the fps of the video. So if you want a keyframe interval of 2 seconds, as recommended by Youtube for uploads, and you're creating a 60 fps video, multiply 2 with 60 and get 120 as keyframe interval.
We typically recommend a keyframe interval of 2 seconds. Please note that if you set the keyframe interval to 0 seconds in OBS Studio, this does not mean 0 seconds, it will instead instruct OBS to change the keyframe interval to “AUTO”. A Setting of 0 or AUTO is around 8 seconds.
The recommended keyframe frequency is four seconds. The current keyframe frequency is X seconds. Note that ingestion errors can cause incorrect GOP sizes. Some encoders allow you to change the "GOP" to Open (variable) or Closed (fixed).
You'll need to reencode. Set x264's keyint parameter to 5*fps and disable scenecut. If your fps is 24 for example :
ffmpeg -i <input> -vcodec libx264 -x264-params keyint=120:scenecut=0 -acodec copy out.mp4
This is obviously not optimal for quality but it'll match your demand.
Edited to change no-scenecut
to scenecut=0
, as per sigh-boy suggestion.
Sigh.
The misinformation regarding the no-scenecut
option has been going on for longer than I can remember. Never enter a value for no-scenecut
.
A link to documentation can be found here.
For FFmpeg you need to use the following two switches:
-g 120
will define a GOP of 120 frames to create a five second GOP for 23.976fps content. This works in conjunction with the no-scenecut
option.
-x264opts no-scenecut
will force keyframes to be created per the GOP value that FFmpeg uses. The default for libx264
is to create a keyframe when it detects a scene change. If you inspect an output file using MediInfo without that option will see scenecut=40
. When done properly that will be scenecut=0
. If this option is not used then keyframes will be misaligned for ABR content and segment sizes will be unpredictable.
Do not take my word for it, please run the following under a bash shell where $inputfile is the name of the file you want to analyze. If you use the two switches shown above then you will see a very even cadence of keyframes dump to the command prompt.
ffprobe -select_streams v -show_frames -show_entries \ frame=pict_type -of csv $inputfile | grep -n I | cut -d ':' -f 1
You can also reference an article that I wrote regarding how to create proper ABR frame aligned content here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With