iosobjective-cdecoderavassetreadercmsamplebufferref

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


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:

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


Solution

  • 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