6

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.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

5

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

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • hi, what would it be frameTimeMIllisecs? Is that the duration of the buffer? or the seconds from the referenceDate? something else? – mikey Mar 13 '19 at 02:17
  • `frameTimeMillisecs` is frame timestamp in milliseconds. Not seconds from reference date. It is rather seconds for 0 where 0 is the reference time of the start of the video – TheWaterProgrammer Mar 13 '19 at 08:43
  • Thank you! And how would you know the start of the video timestamp? Is it when startsession is called? – mikey Mar 13 '19 at 11:06