为AVCaptureSession设置一个自定义的AVFrameRateRange

我试图每秒用AVCaptureSession 5张图片,我不知道我明白什么AVFrameRange的意思。 目前我有一些设置设备的代码:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

并尝试将activeVideoMinFrameDurationactiveVideoMaxFrameDurationCMTimeMake(1, 5)的自定义值。 苹果告诉我,我只能使用他们提供的AVFrameRanges之一。

当我NSLogged他们,我得到(2,30),(2,60),和(2,24)。 我首先想知道这是什么意思? 这是相机将运行的帧速率还是捕捉帧的间隔(即,我正在尝试做什么)?

如果不是的话,我可以怎样在sampleBufferDelegate方法上每秒保存5帧? 目前它给了我每一个框架,因为每一次都有一个框架调用这个方法,所以我只需要一些指示我如何每秒只能抓取5个。

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

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

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

 - (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]; } 

用于select自定义帧速率的代码如下 – 添加到Apple RosyWriter的检查,以validation当前格式是否支持所选的FPS

 - (void)configureCamera:(AVCaptureDevice *)videoDevice withFrameRate:(int)desiredFrameRate { BOOL isFPSSupported = NO; AVCaptureDeviceFormat *currentFormat = [videoDevice activeFormat]; for ( AVFrameRateRange *range in currentFormat.videoSupportedFrameRateRanges ) { if ( range.maxFrameRate >= desiredFrameRate && range.minFrameRate <= desiredFrameRate ) { isFPSSupported = YES; break; } } if( isFPSSupported ) { if ( [videoDevice lockForConfiguration:NULL] ) { videoDevice.activeVideoMaxFrameDuration = CMTimeMake( 1, desiredFrameRate ); videoDevice.activeVideoMinFrameDuration = CMTimeMake( 1, desiredFrameRate ); [videoDevice unlockForConfiguration]; } } } 

如果当前格式( activeFormat )不支持您select的FPS,请使用以下代码更改activeFormat ,然后selectFPS。 将需要获得格式维度,以满足您的需求。

 - (void)configureCamera:(AVCaptureDevice *)device withFrameRate:(int)desiredFrameRate { AVCaptureDeviceFormat *desiredFormat = nil; for ( AVCaptureDeviceFormat *format in [device formats] ) { for ( AVFrameRateRange *range in format.videoSupportedFrameRateRanges ) { if ( range.maxFrameRate >= desiredFrameRate && range.minFrameRate <= desiredFrameRate ) { desiredFormat = format; goto desiredFormatFound; } } } desiredFormatFound: if ( desiredFormat ) { if ( [device lockForConfiguration:NULL] == YES ) { device.activeFormat = desiredFormat ; device.activeVideoMinFrameDuration = CMTimeMake ( 1, desiredFrameRate ); device.activeVideoMaxFrameDuration = CMTimeMake ( 1, desiredFrameRate ); [device unlockForConfiguration]; } } } 

注:不build议使用AVCaptureConnection videoMinFrameDuration来设置FPS。