尝试了解 CMTime 和 CMTimeMake

发布于 2024-09-28 13:38:46 字数 299 浏览 2 评论 0原文

1) CMTimeMake(1,10) 表示持续时间为 1 秒,时间刻度为 10,即每秒 10 帧。这意味着 10 帧的视频持续时间为 1 秒?

2)

CMTime lastTime=CMTimeMake(1,10);
CMTime frameTime=CMTimeMake(1, 10);
CMTime currentTime=CMTimeAdd(lastTime, frameTime)

= (2, 10) ?

当前时间每秒 10 帧的 2 秒视频?

1) CMTimeMake(1,10) means duration of 1 second and timescale of 10, or 10 frames per second. This means 1s duration of video with 10 frames?

2)

CMTime lastTime=CMTimeMake(1,10);
CMTime frameTime=CMTimeMake(1, 10);
CMTime currentTime=CMTimeAdd(lastTime, frameTime)

= (2, 10) ?

2 seconds of video and with 10 frames per second of the currentTime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾挽 2024-10-05 13:38:46

1) CMTimeMake(1,10) 实际上表示值 1 和时间刻度 10。它们是分子和分母,因此是 1/10 秒,而不是 1 秒。

2) 结果类似于 CMTimeMake(2, 10),即 2/10 秒。

1) CMTimeMake(1,10) actually means a value of 1 and a timescale of 10. They are a numerator and denominator, so it is 1/10 of a second, not 1 second.

2) The result will be like CMTimeMake(2, 10), which is 2/10ths of a second.

指尖凝香 2024-10-05 13:38:46

彼得是对的。
下面的代码使这个概念更加清晰:

1)

Float64 seconds = 5; 
int32_t preferredTimeScale = 600;
CMTime inTime = CMTimeMakeWithSeconds(seconds, preferredTimeScale);
CMTimeShow(inTime);

上面的代码给出:
{3000/600 = 5.000}

这意味着总持续时间为 5 秒,包含 3000 帧,时间尺度为每秒 600 帧。

2)

int64_t value = 10000;
int32_t preferredTimeScale = 600;
CMTime inTime = CMTimeMake(value, preferredTimeScale);
CMTimeShow(inTime);

这个给出 {10000/600 = 16.667}

这意味着总持续时间为 16.667 秒,其中 10000 帧,时间尺度为每秒 600 帧。

注意 CMTimeMake(int64_t value, int32_t timescale) 之间的区别
和 CMTimeMakeWithSeconds(Float64 Seconds, int32_t PreferredTimeScale)

希望这个解释有帮助。如需进一步说明,请随时在本文中提出更多问题。

Peter is right.
The following code makes the concept more clear:

1)

Float64 seconds = 5; 
int32_t preferredTimeScale = 600;
CMTime inTime = CMTimeMakeWithSeconds(seconds, preferredTimeScale);
CMTimeShow(inTime);

The above code gives:
{3000/600 = 5.000}

Which means a total duration of 5 seconds, with 3000 frames with a timescale of 600 frames per second.

2)

int64_t value = 10000;
int32_t preferredTimeScale = 600;
CMTime inTime = CMTimeMake(value, preferredTimeScale);
CMTimeShow(inTime);

This one gives {10000/600 = 16.667}

Which means a total duration of 16.667 seconds, with 10000 frames with a timescale of 600 frames per second.

Notice the difference between CMTimeMake(int64_t value, int32_t timescale)
and CMTimeMakeWithSeconds(Float64 seconds, int32_t preferredTimeScale)

Hope this explanation helps. For further clarifications, please don't hesitate to post further questions on this post.

仄言 2024-10-05 13:38:46

使用 CMTimeMake(A, B) 您可以存储一个有理数,即 A / B 秒的精确分数

  • CMTimeMake(1, 4) ->时间间隔 0.25 秒

使用 CMTimeMakeWithSeconds(A, B),您可以将 A 存储为 B 步的分辨率

  • CMTimeMakeWithSeconds(0.25, ...) ->时间间隔 0.25 秒

您通常会看到CMTimeMakeWithSeconds(time, NSEC_PER_SEC)NSEC_PER_SEC 实际上意味着“最大分辨率”。

With CMTimeMake(A, B) you store a rational number, an exact fraction A / B seconds

  • CMTimeMake(1, 4) -> the time interval 0.25 seconds

With CMTimeMakeWithSeconds(A, B) you store A seconds to a resolution of B steps

  • CMTimeMakeWithSeconds(0.25, ...) -> the time interval 0.25 seconds

You commonly see CMTimeMakeWithSeconds(time, NSEC_PER_SEC). The NSEC_PER_SEC effectively means "max resolution".

回心转意 2024-10-05 13:38:46

1 秒的间隔

如果您只想知道如何设置 1 秒的间隔(像我一样),这就是您的答案:

int seconds = 1;

CMTime interval = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC);

Interval for 1 Second

If you only want to know how to make an interval for 1 second (like me), this is your answer:

int seconds = 1;

CMTime interval = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC);
羅雙樹 2024-10-05 13:38:46

CMTime 结构表示存储为有理数的时间长度。CMTime 有一个值和一个时间刻度字段,并表示时间值/时间刻度秒。

请参阅查看这个明确的答案

A CMTime struct represents a length of time that is stored as rational number. CMTime has a value and a timescale field, and represents the time value/timescale seconds .

See See this SO Answer which is clear

墨落成白 2024-10-05 13:38:46

Swift CMTime

当你操作时间时很重要以节省准确性。 CMTime 建议您使用两个整数(valuetimescale)来表示浮点数。与浮点运算相比,附加函数简化了 CMTime 数学计算

重要的 CMTime 属性包括:

  • timescale - 一秒

的等份数公式为

Int64 value(numerator) / Int32 timescale(denominator) = Float64 seconds

//for example
//value 1 / timescale 10 = 1/10(0.1) seconds

下一个示例显示下一项:

  • 您可以使用构造函数(init())或CMTimeMake函数创建CMTime。秒
  • 相同
  • 与CMTimeGetSeconds(time)数学
import CoreMedia

func testTime() {
   let value: Int64 = 1
   let timescale: Int32 = 10
   let secondsResult: Double = 0.1 // 1/10 sec
   
   let time1: CMTime = CMTime(value: value, timescale: timescale)
   let time2: CMTime = CMTime(seconds: secondsResult, preferredTimescale: timescale)
   
   let time3: CMTime = CMTimeMake(value: value, timescale: timescale)
   let time4: CMTime = CMTimeMakeWithSeconds(secondsResult, preferredTimescale: timescale)
   
   let seconds1: Double = time1.seconds
   let seconds2: Float64 = CMTimeGetSeconds(time1)
   assert(seconds1 == seconds2)
   assert(seconds1 == secondsResult)
   
   assert(time1 == time2)
   assert(time1 == time3)
   assert(time1 == time4)
        
   assert(CMTimeCompare(time1, time2) == 0)
}

- CMTimeCompare,CMTimeAdd... 使用秒创建CMTime

您可以创建CMTime 使用时间刻度,其中将计算(值/时间刻度)。这种方法很清晰,而且您还可以使用preferredTimescale创建CMTime。在这种情况下,您的可以不同,并需要重新计算以满足公式(必须为Int64),这就是它可以四舍五入的原因,结果也被重新计算

//preset - seconds: 0.1, preferredTimescale: 10, expect value == 1
//result - seconds: 0.1, value: 1
let time5: CMTime = CMTime(seconds: 0.1, preferredTimescale: 10)
assert(time5.value == 1)
assert(time5.seconds == 0.1)

//preset - seconds: 0.1, preferredTimescale: 1, expect value == 0.1
//result - seconds: 0, value: 0
let time6: CMTime = CMTime(seconds: 0.1, preferredTimescale: 1)
assert(time6.value == 0)
assert(time6.seconds == 0)

//preset - seconds: 1.31, preferredTimescale: 10, expect value == 13.1
//result - seconds: 1.3, value: 13
let time7: CMTime = CMTime(seconds: 1.31, preferredTimescale: 10)
assert(time7.value == 13)
assert(time7.seconds == 1.3)

Swift CMTime

When you operate time it is important to save accuracy. CMTime propose you to use two integers(value and timescale) to present a float number. Additional functions simplify CMTime mathematics in contrast to float operations

Important CMTime properties are:

  • value
  • timescale - number of equal parts of one second
  • seconds

The formula is

Int64 value(numerator) / Int32 timescale(denominator) = Float64 seconds

//for example
//value 1 / timescale 10 = 1/10(0.1) seconds

Next example shows next items:

  • You can create CMTime using constructor(init()) or CMTimeMake function
  • time.seconds is the same as CMTimeGetSeconds(time)
  • mathematics with CMTime - CMTimeCompare, CMTimeAdd...
import CoreMedia

func testTime() {
   let value: Int64 = 1
   let timescale: Int32 = 10
   let secondsResult: Double = 0.1 // 1/10 sec
   
   let time1: CMTime = CMTime(value: value, timescale: timescale)
   let time2: CMTime = CMTime(seconds: secondsResult, preferredTimescale: timescale)
   
   let time3: CMTime = CMTimeMake(value: value, timescale: timescale)
   let time4: CMTime = CMTimeMakeWithSeconds(secondsResult, preferredTimescale: timescale)
   
   let seconds1: Double = time1.seconds
   let seconds2: Float64 = CMTimeGetSeconds(time1)
   assert(seconds1 == seconds2)
   assert(seconds1 == secondsResult)
   
   assert(time1 == time2)
   assert(time1 == time3)
   assert(time1 == time4)
        
   assert(CMTimeCompare(time1, time2) == 0)
}

Create CMTime using seconds

You are able to create CMTime using value and timescale where seconds will be calculated(value / timescale). This approach is clear but also you are able to create CMTime using seconds and preferredTimescale. In this case your value and seconds can be different and be recalculated to satisfy formula(value must be Int64) that is why it can be rounded, as a result seconds also is recalculated

//preset - seconds: 0.1, preferredTimescale: 10, expect value == 1
//result - seconds: 0.1, value: 1
let time5: CMTime = CMTime(seconds: 0.1, preferredTimescale: 10)
assert(time5.value == 1)
assert(time5.seconds == 0.1)

//preset - seconds: 0.1, preferredTimescale: 1, expect value == 0.1
//result - seconds: 0, value: 0
let time6: CMTime = CMTime(seconds: 0.1, preferredTimescale: 1)
assert(time6.value == 0)
assert(time6.seconds == 0)

//preset - seconds: 1.31, preferredTimescale: 10, expect value == 13.1
//result - seconds: 1.3, value: 13
let time7: CMTime = CMTime(seconds: 1.31, preferredTimescale: 10)
assert(time7.value == 13)
assert(time7.seconds == 1.3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文