6

I am inside of a AVCaptureSession and I need to know the number of the frame being captured.

When the capture starts, I will reset a frame number counter to zero. So, the timestamp of the first frame will be zero. Subsequent frames will have capture timestamps like this, for example:

0
0.033
0.066
0.099
etc

but these are not exact numbers, because frames can be dropped and slight differences may happen. I need to know the exact time when a frame was captured.

I have this method that is called every time a frame is grabbed...

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 

inside this method I can obtain the CMTime of a frame and print it to the console using:

CMTime timestamp = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );
NSLog(@"%lld", timestamp.value);

but this will give me wild numbers like

156317265588132
156317307247388
156317348909678
156317390573453
156317432230603

the difference between one number and the next is about 41,659,256 that does not seem to represent a time in seconds with decimal places when the frame is grabbed.

if I divide this numbers by the timescale, using

NSLog(@"%lld", timestamp.value/timestamp.timescale);

the numbers will be the same for 25 frames and will only change when a full second is reached.

How do I obtain the exact time stamp in seconds with decimal places when a frame is grabbed? thanks.

Duck
  • 34,902
  • 47
  • 248
  • 470

1 Answers1

8

If I divide this numbers by the timescale, the numbers will be the same for 25 frames and will only change when a full second is reached.

Because integer division truncates. How about

NSLog(@"%f", (float)timestamp.value / timestamp.timescale);