如何在解码 video.mp4 时获取 iOS 中每个视频帧的时间戳

How to get the timestamp of each video frame in iOS while decoding a video.mp4

场景:
我正在编写 iOS 应用程序 来尝试解码 videoFile.mp4。我正在使用 AVAssetReaderTrackOutput with AVAssetReader 解码视频文件中的帧。这很好用。我基本上使用以下核心逻辑从 videoFile.mp4 获取每一帧。

代码:

AVAssetReader * videoFileReader;
AVAssetReaderTrackOutput * assetReaderOutput = [videoFileReader.outputs objectAtIndex:0];
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];

sampleBuffer这里就是每个视频帧的buffer。

问题:

PS:
请注意,我需要 毫秒 .

中的时间戳

我终于得到了问题的答案。下面两行可以得到copyNextSampleBuffer

返回的sampleBufferframe timestamp
CMTime frameTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
double frameTimeMillisecs = CMTimeGetSeconds(frameTime) * 1000;

时间戳在 seconds 中返回。因此将它乘以 1000 转换为 milliseconds