164

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?

1
  • 1
    if your video is 48 FPS (frame per second) then you would easily do CMTimeMake(1, 48) to run a block of code every 1/48 of a second ie 1 block per frame
    – mfaani
    May 4, 2017 at 14:27

5 Answers 5

186

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.

2
  • 27
    Why apple's reference don't say this?
    – vgonisanz
    Nov 28, 2013 at 8:41
  • 6
    Now it says: value/timescale = seconds explicitly.
    – JP Illanes
    Feb 21, 2014 at 3:08
151

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.

2
  • I wonder why setMaxRecordedDuration needs the preferredTimeScale when frame_rate is set differently. Everyone is just blindly using 600, and no good explanation as to exactly what it is doing.
    – dashesy
    Sep 11, 2015 at 1:26
  • 6
    @dashesy 600 is a multiple of the common video frame rates. Warren Moore explains it at warrenmoore.net/understanding-cmtime pretty well.
    – danimal
    Sep 6, 2016 at 23:21
41

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".

12

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.