Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

libav - How to control key-frame generation of ffmpeg?

I'm making a segmenter that intervene ffmpeg's write_frame function and write output data to separate files. Each segmented file contains segment of about 3 seconds video.

The code does following:

1 - Get transcoded packet  
2 - Check if it contains key frame data, if yes goto 3. 
3 - Check the duration of current segment, if it exceed 3 seconds, goto 4 
4 - Close file, and create new segment, write packet to segment file, goto-1

General speaking, every segment contains at least 3 seconds video data, and it starts with a key frame.

The problem is that the output video's duration are very different, some contain 3 seconds, some contain 5 or 6.

I suspect that the problem due to how ffmpeg generate key frames during transcoding. If the "distance" between two adjacent keyframes are 6s, i got 6 seconds segment.

Here is my questions:

  1. is that true that ffmpeg generate keyframes at irregular intervals (and interval time can be up to few second (eg. 6)?

  2. How can we control the ffmpeg key frame generation? (i guess there should be a ffmpeg command's argument for this, -force_key_frames maybe, but I'm not sure)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. It is possible for ffmpeg to generate Key frame at irregular interval, based on scene change detection.

  2. key frame interval can be controlled by GOP size. the following options can be used

-g (FFmpeg) Keyframe interval, also known as GOP length. This determines the maximum distance between I-frames. Very high GOP lengths will result in slightly more efficient compression, but will make seeking in the video somewhat more difficult.

-keyint_min (FFmpeg) Minimum GOP length, the minimum distance between I-frames.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...