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

孤者浪人 提交于 2019-12-11 06:02:01

问题


Scenario:
I am writing an iOS app to try decode a videoFile.mp4. I am using AVAssetReaderTrackOutput with AVAssetReader to decode frames from the video file. This works very well. I get each & every frame from videoFile.mp4 basically using the following logic at the core.

Code:

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

sampleBuffer is the buffer of each video frame here.

Question:

  • How can I get the timestamp of each video frame here ?
  • In other words & more detail, how can I get the timestamp of each sampleBuffer that i am returned from copyNextSampleBuffer?

PS:
Please note that I need the timestamp in milliseconds.


回答1:


I got the answer to my question finally. Following 2 lines can get the frame timestamp of the sampleBuffer returned from copyNextSampleBuffer

CMTime frameTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
double frameTimeMillisecs = CMTimeGetSeconds(frameTime) * 1000;

Timestamp is returned in seconds. Hence multiplying it by 1000 to convert to milliseconds



来源:https://stackoverflow.com/questions/48485983/how-to-get-the-timestamp-of-each-video-frame-in-ios-while-decoding-a-video-mp4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!