使用 libx264 压缩一组图像时,为什么帧速率会影响最终输出大小?

When compressing a set of images with libx264, why does frame rate affect final output size?

我正在使用 ffmpeg 将一组图像编码为短延时视频,使用 libx264 编解码器。我的第一次尝试,我以 30 FPS 编码,使用:

ffmpeg -r 30 -pattern_type glob -i "*.jpg" -vcodec libx264 -crf 30 -pix_fmt yuv420p output.mp4

有了 60 帧,我得到了一个 163 KB 的文件,时长 2 秒。然后我意识到我需要它更慢,所以我重新 运行 相同的命令,但将 -r 更改为 2。现在我有一个 30 秒长的文件,但大小跳到了 891 KB!视频质量在感知上看起来是一样的。

如何以较慢的帧速率进行编码,而不会使最终文件大小膨胀?


注释:我的一些理论,以及我检查过的东西。首先,为了确保 ffmpeg 不会在较长的版本中重复帧,我检查了 I/P/B 计数。 30 FPS 文件有:

[libx264 @ 0x7f9b26001c00] frame I:1     Avg QP:30.67  size: 44649
[libx264 @ 0x7f9b26001c00] frame P:15    Avg QP:31.19  size:  5471
[libx264 @ 0x7f9b26001c00] frame B:44    Avg QP:31.45  size:   767

2 FPS 文件有:

[libx264 @ 0x7fcd32842200] frame I:1     Avg QP:21.29  size: 90138
[libx264 @ 0x7fcd32842200] frame P:15    Avg QP:22.48  size: 33686
[libx264 @ 0x7fcd32842200] frame B:44    Avg QP:26.29  size:  6674

因此,I/P/B 计数相同,但 2 FPS 文件的 QP 低得多。为了抵消,我尝试为 2 FPS 文件增加 -crf,以获得大致相同的目标大小,但这只会给我一个非常模糊的视频(必须转到 crf=40)。我尝试搞乱 -minrate、-maxrate、-bt、none 帮助。我猜有一些 x264 编解码器设置是依赖于帧率的,但我不知道是哪一个(据我所知,恒定比特率受帧率影响,但 CRF 不应该,但也许我理解错了

CRF 模式旨在在其编码输出中获得并保持一定的质量水平。如果要以 25 fps 显示同一组帧,则每个帧的持续时间为 40 毫秒,观众将无法完全理解瞬态特征。 x264/x265 等编码器将更积极地优化这些帧。 OTOH,如果以 2 fps 显示,每帧可见半秒,因此在保持感知质量方面没有多少回旋余地。

对于 x264,这是实现该逻辑的 commit 的消息。

VFR/framerate-aware 速率控制,第 2 部分

MB-tree and qcomp complexity estimation now consider the duration of a frame in their calculations. This is very important for visual optimizations, as frames that last longer are inherently more important quality-wise. Improves VFR-aware PSNR as much as 1-2db on extreme test cases, ~0.5db on more ordinary VFR clips (e.g. deduped anime episodes).

WARNING: This change redefines x264's internal quality measurement. x264 will now scale its quality based on the framerate of the video due to the aforementioned frame duration logic. That is, --crf X will give lower quality per frame for a 60fps video than for a 30fps one. This will make --crf closer to constant perceptual quality than previously. The "center" for this change is 25fps: that is, videos lower than 25fps will go up in quality at the same CRF and videos above will go down. This choice is completely arbitrary.

Note that to take full advantage of this, x264 must encode your video at the correct framerate, with the correct timestamps.