AVSampleBufferDisplayLayer 播放速度太快

AVSampleBufferDisplayLayer plays too fast

所以我整理了一个示例项目 https://github.com/liuxuan30/TestH264.git,它使用 VideoToolBox 来拥有一个 H264 示例解码器来显示从摄像机捕获的流文件。

使用VideoToolBox的H264解码器是从网上抄来的,不是我写的,当我尝试播放我的h264流文件时,它播放速度太快了,与ffmpeg或ffplay相比,两者都是正常播放速度。

我想问一下,如何解决这个问题?谢谢

这是因为这个常量 kCMSampleAttachmentKey_DisplayImmediately:

If this key is present, the sample should be displayed as soon as possible rather than
according to its presentation timestamp. Use this attachment at run time to request this
behavior from a display pipeline such as the AVSampleBufferDisplayLayer class.
This attachment is not written to media files. from Apple documation

所以你有两种显示方式:

  1. 立即显示 - 这可能对实时流有好处,当您需要尽快显示帧时

  2. 显示特定时间戳的帧

*comparing to ffmpeg or ffplay, which both played back at a normal speed.

ffplay和ffmpeg此时可能使用时间戳。

我从你的测试 H.264 文件中得到了与你相同的结果,但它的发生是因为你一次获得了所有解码帧,所以解码器会立即显示它。

您可以观看此视频以了解有关 VideoToolbox 框架的更多信息: Direct Access to Video Encoding and Decoding