AVCaptureSession抓取帧时的准确时间戳 [英] AVCaptureSession exact time stamp when a frame is grabbed

查看:184
本文介绍了AVCaptureSession抓取帧时的准确时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AVCaptureSession中,我需要知道正在捕获的帧的数量。

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 

在此方法中我可以获取帧的CMTime并使用以下方法将其打印到控制台:

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

一个数字和下一个数字之间的差值约为41,659,256,这似乎不代表抓取帧时带小数位的秒数。

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);

25帧的数字相同,只有在达到一整秒时才会改变。

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.

推荐答案


如果我将这个数字除以时间刻度,那么数字将是相同的25帧,只有在达到一整秒时才会改变。

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);

这篇关于AVCaptureSession抓取帧时的准确时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆