Understanding CMTime basics and how it works

The first thing you need to keep in mind when learning to understand CMTime is that 1 second in time is still 1 second in time. It always has been and it always will be.

Apple defines a CMTime as ‘a rational number, with a numerator (an int64_t - the time value), and a denominator (an int32_t - the timescale)’.

For those that are not mathematically inclined a rational number is ’a number that can be made by dividing two integers’. So 1/2 is a rational number (1 divided by 2, or the ratio of 1 to 2)

Apple continues to say:

Conceptually, the timescale specifies the fraction of a second each unit/slice in the numerator occupies.

Thus if the timescale is 4, each unit/slice represents a quarter of a second (example A); if the timescale is 10, each unit/slice represents a tenth of a second (example B).

What is the timescale in example C?


CMTime is a struct with four members.

We are only concerned with value and timescale

value - is an Int64, which represents your seconds.

timescale - is an Int32, and this represents your timescale (the slices that your seconds are cut into)

The second thing to keep in mind is that iOS mediatypes, that play back (video and audio) have a duration (in seconds, eventually).

So any asset with a video or audio media type will have a duration.

Depending on the type of asset you have, you will get a CMTime .

There are 2 types of media assets in iOS, PHAsset and AVAsset. You can explore these at your leisure, and i will write about these in a future post

You can however, use the TimeInterval of a PHAsset to create a CMTime using something like this: CMTimeMakeWithSeconds(PHAssetTimeIntervalDuration, 600)... see below


CMTime - get asset duration


Apple gives you handy functions that let you play with CMTime. I have found that you should always get back to a CMTime value and use these functions to manipulate you time values.

Looking at media and dealing with durations when done correctly is quite easy.

The two best functions to use to get or view time are CMTimeGetSeconds or CMTimeShow

CMTimeGetSeconds is handy when you want to see the duration of your CMTime inseconds.

CMTimeShow i use to display the actual CMTime values (it's time value and timescale)

There is also a special constant duration: kCMTimeZero:CMTime - Use this constant to initialize a CMTime to 0.

CMTime - make time


So you can also make CMTime.

You can create CMTime using CMTimeMake or CMTimeMakeWithSeconds

You can use CMTimeMake to create a CMTime, but this is much more complicated as you need to know the number of slices you need before and work backwards

CMTimeMakeWithSeconds is a lot easier to use as you will (hopefully always) know the seconds you want. So you just need a timescale..

timescale is usually 600 units, because it suits many different kinds of media.

600 is a common multiple of most standard frame rates: 24 fps for film, 25 fps for PAL (European) TV, 30 fps for NTSC (North America and Japan) TV. All these frame rates divide every into 600.

Make this a constant in your code and always reference it.

CMTime - Manipulation

You can also manipulate CMTime using CMTimeAdd or CMTimeSubtract

You can use CMTimeAdd to add 2 CMTime values.

You use CMTimeSubtract to subtract two value. You should also ensure that your result is not a negative value.