ios 试图理解 CMTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12902410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:45:26  来源:igfitidea点击:

Trying to understand CMTime

iosavcapturesessioncmtime

提问by RileyE

I have seen some examplesofCMTime(Three separate links), but I still don't get it. I'm using an AVCaptureSession with AVCaptureVideoDataOutput and I want to set the max and min frame rate of the the output. My problem is I just don't understand the CMTime struct.

我见过一些例子CMTime(三个独立的链接),但我还是不明白这一点。我将 AVCaptureSession 与 AVCaptureVideoDataOutput 一起使用,我想设置输出的最大和最小帧速率。我的问题是我只是不理解 CMTime 结构。

Apparently CMTimeMake(value, timeScale) should give me value frames every 1/timeScale seconds for a total of value/timeScale seconds, or am I getting that wrong?

显然 CMTimeMake(value, timeScale) 应该每 1/timeScale 秒给我一个值帧,总计 value/timeScale 秒,还是我弄错了?

Why isn't this documented anywhere in order to explain what this does?

为什么没有记录在任何地方以解释它的作用?

If it does truly work like that, how would I get it to have an indefinite number of frames?

如果它真的像那样工作,我如何让它具有无限数量的帧?

If its really simple, I'm sorry, but nothing has clicked just yet.

如果它真的很简单,我很抱歉,但还没有点击任何东西。

回答by Martin R

A CMTimestruct represents a length of time that is stored as rational number (see CMTime Reference). CMTimehas a valueand a timescalefield, and represents the time value/timescale seconds.

CMTime结构表示存储为有理数(见的时间长度CMTime参考)。CMTime有一个value和一个timescale字段,代表时间value/timescale seconds

CMTimeMakeis a function that returns a CMTimestructure, for example:

CMTimeMake是一个返回CMTime结构的函数,例如:

CMTime t1 = CMTimeMake(1, 10); // 1/10 second = 0.1 second
CMTime t2 = CMTimeMake(2, 1);  // 2 seconds
CMTime t3 = CMTimeMake(3, 4);  // 3/4 second = 0.75 second
CMTime t4 = CMTimeMake(6, 8);  // 6/8 second = 0.75 second

The last two time values t3and t4represent the same time value, therefore

最后两个时间值t3t4代表相同的时间值,因此

CMTimeCompare(t3, t4) == 0

If you set the videoMinFrameDurationof a AVCaptureSessionis does not make a difference if you set

如果你设置了videoMinFrameDurationa AVCaptureSessionis 并没有什么不同,如果你设置

connection.videoMinFrameDuration = CMTimeMake(1, 20); // or
connection.videoMinFrameDuration = CMTimeMake(2, 40);

In both cases the minimum time interval between frames is set to 1/20 = 0.05 seconds.

在这两种情况下,帧之间的最小时间间隔都设置为 1/20 = 0.05 秒。

回答by Doug Voss

My experience differs.

我的经历不同。

For let testTime = CMTime(seconds: 3.83, preferredTimescale: 100)

为了 let testTime = CMTime(seconds: 3.83, preferredTimescale: 100)

If you set a breakpoint and look in the debugger side window it says:

如果您设置断点并查看调试器侧窗口,它会显示:

"383 100ths of a second"

“383 100 分之一秒”

Testing by seeking to a fixed offset in a video in AVPlayer has confirmed this.

通过在 AVPlayer 中寻找视频中的固定偏移量进行的测试已经证实了这一点。

So put the actual number of seconds in the seconds field, and the precision in the preferredTimescale field. So 100 means precision of hundredths of a second.

所以在 seconds 字段中输入实际秒数,在 preferredTimescale 字段中输入精度。所以100意味着百分之一秒的精度。

Doing let testTime = CMTime(seconds: 3.83, preferredTimescale: 100)

正在做 let testTime = CMTime(seconds: 3.83, preferredTimescale: 100)

Still seeks to the same place in the video, but it displays in the debugger side window as "3833 1000ths of a second"

仍然在视频中寻找相同的位置,但它在调试器侧窗口中显示为“3833 1000ths of a second”

Doing let testTime = CMTime(seconds: 3.83, preferredTimescale: 1)

正在做 let testTime = CMTime(seconds: 3.83, preferredTimescale: 1)

Does not seek to the same place in the video, because it's been truncated, and it displays in the debugger side window as "3 seconds". Notice that the .833 part has been lost due to the preferredTimescale.

不寻找视频中的相同位置,因为它已被截断,并且在调试器侧窗口中显示为“3 秒”。请注意,由于首选时间刻度,.833 部分已丢失。