Tammo
Tammo

Reputation: 47

How do you set the framerate when recording video on the iPhone?

I would like to write a camera application where you record video using the iPhone's camera, but I can't find a way to alter the framerate of the recorded video. For example, I'd like to record at 25 frames per second instead of the default 30.

Is it possible to set this framerate in any way, and if yes how?

Upvotes: 3

Views: 3223

Answers (3)

jgh
jgh

Reputation: 2027

[deprecated on iOS, only supported on MacOS]

You can use AVCaptureConnection's videoMaxFrameDuration and videoMinFrameDuration properties. See http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVCaptureConnection_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009522

Additionally, there is an SO question that addresses this (with a good code example): I want to throttle video capture frame rate in AVCapture framework

Upvotes: 1

Praxiteles
Praxiteles

Reputation: 6010

You will need AVCaptureDevice.h

Here is working code here:

- (void)attemptToConfigureFPS
{

    NSError *error;
    if (![self lockForConfiguration:&error]) {
        NSLog(@"Could not lock device %@ for configuration: %@", self, error);
        return;
    }

    AVCaptureDeviceFormat *format = self.activeFormat;
    double epsilon = 0.00000001;

    int desiredFrameRate = 30;

    for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {

        NSLog(@"Pre Minimum frame rate: %f  Max = %f", range.minFrameRate, range.maxFrameRate);


        if (range.minFrameRate <= (desiredFrameRate + epsilon) &&
            range.maxFrameRate >= (desiredFrameRate - epsilon)) {

            NSLog(@"Setting Frame Rate.");

            self.activeVideoMaxFrameDuration = (CMTime){
                .value = 1,
                .timescale = desiredFrameRate,
                .flags = kCMTimeFlags_Valid,
                .epoch = 0,
            };
            self.activeVideoMinFrameDuration = (CMTime){
                .value = 1,
                .timescale = desiredFrameRate,
                .flags = kCMTimeFlags_Valid,
                .epoch = 0,
            };

            // self.activeVideoMinFrameDuration = self.activeVideoMaxFrameDuration;

            // NSLog(@"Post Minimum frame rate: %f  Max = %f", range.minFrameRate, range.maxFrameRate);

            break;
        }
    }

    [self unlockForConfiguration];


    // Audit the changes
    for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {

        NSLog(@"Post Minimum frame rate: %f  Max = %f", range.minFrameRate, range.maxFrameRate);

    }



}

Upvotes: 1

donkim
donkim

Reputation: 13137

As far as I could tell, you can't set the FPS for recording. Look at the WWDC 2010 video for AVFoundation. It seems to suggest that you can but, again, as far as I can tell, that only works for capturing frame data.

I'd love to be proven wrong, but I'm pretty sure that you can't. Sorry!

Upvotes: 0

Related Questions