如何控制ffmpeg的关键帧生成? [英] How to control key-frame generation of ffmpeg?

查看:419
本文介绍了如何控制ffmpeg的关键帧生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个分割器来介入ffmpeg的write_frame函数,并将输出数据写入单独的文件。每个分段文件包含大约3秒的视频段。



代码如下:

  1  - 获取转码包
2 - 检查是否包含关键帧数据,如果是goto 3.
3 - 检查当前段的持续时间,如果超过3秒,转到4
4 - 关闭文件,并创建新段,将数据包写入段文件,goto-1

一般来说,每个段都至少包含3个秒视频数据,并从关键帧开始。



问题是输出视频的持续时间是非常不同的,有些包含3秒,有些包含5或6。



我怀疑由于ffmpeg在转码过程中生成关键帧的问题。如果两个相邻关键帧之间的距离是6秒,我有6秒的段。



这是我的问题:


  1. 是真的,ffmpeg以不规则的间隔生成关键帧(间隔时间可以达到几秒钟(例如6)?


  2. 我们如何控制ffmpeg关键帧生成?我想应该有一个ffmpeg命令的参数,-force_key_frames可能,但我不知道)



解决方案


  1. ffmpeg可以根据场景变化检测以不规则的间隔生成关键帧。


  2. 关键帧间隔可以由GOP大小控制,可以使用以下选项


-g(FFmpeg)
关键帧间隔,也称为GOP长度,这决定了I帧之间的最大距离,非常高的GOP长度将导致稍高效的压缩,但会使视频中的图像变得更困难。



-keyint_min(FFmpeg)
Mi最小GOP长度,I帧之间的最小距离。


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)

解决方案

  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.

这篇关于如何控制ffmpeg的关键帧生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆