Question

Hello I'm trying to convert an integer which represents a frame rate into CMTime to be able to set minFrameDuration in AVCaptureScreenInput.

Until now I have

int numberOfFrames = [self convertFrameRateStringToInt:[parameters objectForKey:@"frameRate"]];
Float64 frameDuration = 1.0/numberOfFrames;
NSLog(@"numberOfFrames %i - frameDuration %f",numberOfFrames, frameDuration);
NSLog(@"timeshow %f",CMTimeGetSeconds(CMTimeMakeWithSeconds(frameDuration, 1)));
return CMTimeMakeWithSeconds(frameDuration,1);

if the numberOfFrames is set to 15 frames per seconds then the frameDuration is 0.066667 therefore the parameters for CMTimeMakeWithSeconds are 0.066667 and 1 which means the CMTimeGetSeconds should return 0.066667/1 = 0.066667 but instead it returns 0.

Can some one figures out what I'm doing wrong and explain me why ? Thanks.

Edit1: I made it to work changing the function like this :

int numberOfFrames = [self convertFrameRateStringToInt:string];
NSLog(@"timeshow %f",CMTimeGetSeconds(CMTimeMake(1, (int32_t)numberOfFrames)));
return CMTimeMake(1, (int32_t)numberOfFrames);

I still can't understand why the last method didn't work

Was it helpful?

Solution

You should be construction your CMTime like so:

CMTimeMake(1, numberOfFrames);

A CMTime is represented as a rational number, with a numerator (an int64_t value), and a denominator (an int32_t timescale). Conceptually, the timescale specifies the fraction of a second each unit in the numerator occupies. Thus if the timescale is 4, each unit represents a quarter of a second; if the timescale is 10, each unit represents a tenth of a second, and so on.

Your call CMTimeMakeWithSeconds adds kCMTimeFlags_HasBeenRounded to CMTime. Thats why it will round to the nearest full second.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top