为AVCaptureSession设置自定义AVFrameRateRange [英] Set a custom AVFrameRateRange for an AVCaptureSession

查看:328
本文介绍了为AVCaptureSession设置自定义AVFrameRateRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用AVCaptureSession每秒拍摄5张照片,我不确定我理解AVFrameRange的含义。目前我有一些设置设备的代码:

I'm trying to take 5 pictures every second with AVCaptureSession and I'm not sure I understand what AVFrameRange means. Currently I have some code that sets up the device:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

并尝试设置 activeVideoMinFrameDuration activeVideoMaxFrameDuration 自定义值 CMTimeMake(1,5)。 Apple告诉我,我只能使用他们提供的AVFrameRanges之一。

and tries to set the activeVideoMinFrameDuration and activeVideoMaxFrameDuration to a custom value of CMTimeMake(1, 5). Apple tells me I can only use one of the AVFrameRanges that they've provided.

当我对它们进行NSLog时,我得到(2,30),(2,60) ,和(2,24)。我首先想知道这意味着什么?这是相机运行的帧速率还是捕获帧的间隔(即我正在尝试做的事情)?

When I NSLogged them, I get (2, 30), (2,60), and (2,24). I first want to know what this means? Is this the frame rate at which the camera will run or an interval for capturing frames (i.e. what I'm trying to do)?

如果不是,那是什么我可以在sampleBufferDelegate方法上每秒保存5帧吗?目前它给了我每一帧,因为这个方法每次都有一个帧被调用,所以我只需要一些指针来说明我每秒只能抓5个。

If it isn't, what can I do to save 5 frames every second on my sampleBufferDelegate method? Currently it gives me every single frame because the method is called every single time there is a frame, so I just need some pointer on how I can grab just 5 at each second.

推荐答案

以下是我们使用的工作代码,它将帧速率设置为每秒5帧。

Here is working code we have used that sets the frame rate at 5 per second.

如果在使用此代码时测量对CaptureOutput的调用,您可以看到相机帧每200毫秒调用一次(即每秒5帧)。(我们只是测试了这一点以确认。)

If you measure calls to CaptureOutput while using this code, you can see that the camera frames are called every 200 msecs (i.e. which is 5 frames per second.) (We just tested this to confirm.)

更改desiredFrameRate以获得其他相机帧速率。

Change desiredFrameRate to get other camera frame rates.

- (void)attemptToConfigure5FPS
{
    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 = 5;

    for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {

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

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

    [self unlockForConfiguration];
}

这篇关于为AVCaptureSession设置自定义AVFrameRateRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆