Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change keyframe interval?

How to set the keyframe interval to 5 seconds using FFmpeg?

like image 932
Samarth Misra Avatar asked Jun 22 '15 12:06

Samarth Misra


People also ask

What should I set keyframe interval to?

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.

What should my keyframe interval be for 60fps?

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.

What should I set my keyframe interval be on OBS?

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.

What is keyframe frequency?

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).


2 Answers

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.

like image 140
Ely Avatar answered Sep 17 '22 15:09

Ely


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.

like image 44
Navilor Avatar answered Sep 18 '22 15:09

Navilor