使用 CMTime 而不进行舍入?
我正在编写一个使用 AVFoundation 和 CMTime 的应用程序。我已记录使用 CMTimeMake()
创建的 CMTime
实例的值。该值似乎四舍五入为最接近的整数。我需要一个具有精确值的 CMTime 实例,无需舍入。
我在 CMTime
参考中看到了舍入常量:
enum {
kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
kCMTimeRoundingMethod_RoundTowardZero = 2,
kCMTimeRoundingMethod_RoundAwayFromZero = 3,
kCMTimeRoundingMethod_QuickTime = 4,
kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,
kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};
没有任何示例说明如何控制将哪些策略应用于 CMTime
实例?或者,如果这不是正确的方法,我如何从 CMTime
实例中提取精确的值?
编辑:
我已经找到并测试了CMTIME_HAS_BEEN_ROUNDED()
。我将 CMTime
实例传递给此函数,它返回 No
(表明该值尚未四舍五入)。那么为什么我会失去精度呢?
I'm writing an application that uses AVFoundation and CMTime. I have logged the values of my CMTime
instances created using CMTimeMake()
. This value seems to be rounded to the nearest integer. I need a CMTime
instance with a precise value, without rounding.
I have seen the rounding constants in the CMTime
reference:
enum {
kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
kCMTimeRoundingMethod_RoundTowardZero = 2,
kCMTimeRoundingMethod_RoundAwayFromZero = 3,
kCMTimeRoundingMethod_QuickTime = 4,
kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,
kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};
There aren't any example of how I can control which of these strategies is applied to a CMTime
instance? Or, if this isn't the right way, how can I extract a precise value from a CMTime
instance?
Edit:
I have found and tested CMTIME_HAS_BEEN_ROUNDED()
. I passed my CMTime
instance to this function and it returns No
(indicating that the value has not been rounded). So why am I losing precision?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您阅读
CMTime 的文档
您将看到它使用分子和分母将时间存储为有理数。分子为int64_t
,分母为int32_t
。分子指定经过了多少个“刻度”,分母指定了每秒多少个“刻度”。
因此 0.5 秒可以存储为:
等等。您使用的方式
是说“每秒有一个刻度”,并且由于分子是整数,因此浮点值被截断,因此仅存储 1。因此,您将时间指定为:
1/1
,已过去一个刻度,每秒一个刻度,因此您实际上存储恰好 1 秒。要解决这个问题,取决于您想要做什么以及您是否关心时间尺度。 Apple 建议时间刻度为 600,但如果您不在乎,则可以这样做:
将时间刻度设置为 1000,即每秒 1000 个刻度,即每个刻度一毫秒。它还将以秒为单位的时间转换为毫秒。请注意,它会截断第 4 位数字,因此如果您有 1.2345,您只会得到 1.234,而不是 1.235。如果这对您很重要,请参阅
roundf
。If you read the documentation for
CMTime
you will see that it stores time as a rational number using a numerator and denominator. The numerator isint64_t
while the denominator isint32_t
.The numerator specifies how many "ticks" have passed, and the denominator specifies how many "ticks" per second.
So 0.5 seconds can be stored as:
And so forth. The way you have done it, using
Is saying "there is one tick per second", and since the numerator is an integer, the float value is truncated so only the 1 is stored. Therefore you are specifying time as:
1/1
, one tick has elapsed, one tick per second, so you are actually storing exactly 1 second.To fix this, it depends on what you want to do and whether you care about the timescale. Apple recommends a timescale of 600, but if you don't care, you can just do this:
That sets timescale to 1000, so 1000 ticks per second, so one millisecond per tick. It also converts the time in seconds to milliseconds. Note that it truncates the 4th digit, so if you had 1.2345 you just get 1.234 not 1.235. If that matters to you, see
roundf
.CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);
这帮助我获得了以毫秒为单位的精确时间。干杯。
CMTimeMake([[Array objectAtIndex:i]floatValue]*1000, 1000);
This helped me to get exact time in milliseconds. Cheers.