我想限制AVCapture框架中的视频捕获帧速率 [英] I want to throttle video capture frame rate in AVCapture framework

查看:283
本文介绍了我想限制AVCapture框架中的视频捕获帧速率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试限制应用程序的视频捕获帧速率,因为我发现它会影响VoiceOver性能。

I am trying to throttle my video capture framerate for my application, as I have found that it is impacting VoiceOver performance.

目前,它捕获了来自摄像机,然后使用OpenGL例程尽快处理这些帧。我想在捕获过程中设置一个特定的帧率。

At the moment, it captures frames from the video camera, and then processes the frames using OpenGL routines as quickly as possible. I would like to set a specific framerate in the capture process.

我希望能够通过使用videoMinFrameDuration或minFrameDuration来做到这一点,但这似乎没有什么区别性能。有想法吗?

I was expecting to be able to do this by using videoMinFrameDuration or minFrameDuration, but this seems to make no difference to performance. Any ideas?

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) 
{
    if ([device position] == AVCaptureDevicePositionBack) 
    {
        backFacingCamera = device;
                    //  SET SOME OTHER PROPERTIES
    }
}


// Create the capture session
captureSession = [[AVCaptureSession alloc] init];

// Add the video input  
NSError *error = nil;
videoInput = [[[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error] autorelease];

// Add the video frame output   
videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setAlwaysDiscardsLateVideoFrames:YES];

[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];



// Start capturing
if([backFacingCamera supportsAVCaptureSessionPreset:AVCaptureSessionPreset1920x1080])
{
    [captureSession setSessionPreset:AVCaptureSessionPreset1920x1080]; 
    captureDeviceWidth = 1920; 
    captureDeviceHeight = 1080;
    #if defined(VA_DEBUG)
    NSLog(@"Video AVCaptureSessionPreset1920x1080");
    #endif
}
else  do some fall back stuff

// If you wish to cap the frame rate to a known value, such as 15 fps, set 
// minFrameDuration.
AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
if (conn.supportsVideoMinFrameDuration)
    conn.videoMinFrameDuration = CMTimeMake(1,2);
else
    videoOutput.minFrameDuration = CMTimeMake(1,2);


if ([captureSession canAddInput:videoInput]) 
    [captureSession addInput:videoInput];


if ([captureSession canAddOutput:videoOutput])
    [captureSession addOutput:videoOutput];

if (![captureSession isRunning])
    [captureSession startRunning];

有什么想法吗?我想念什么吗?

Any ideas? Am I missing something? Is this the best way to throttle?

AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
if (conn.supportsVideoMinFrameDuration)
    conn.videoMinFrameDuration = CMTimeMake(1,2);
else
    videoOutput.minFrameDuration = CMTimeMake(1,2);


推荐答案

Mike Ullrich的答案一直有效到ios7。这两个不幸的是,ios7中不推荐使用这些方法。您必须在AVCaptureDevice本身上设置 activeVideo {Min | Max} FrameDuration

Mike Ullrich's answer worked up until ios 7. These two methods are unfortunately deprecated in ios7. You have to set the activeVideo{Min|Max}FrameDuration on the AVCaptureDevice itself. Something like:

int fps                             = 30;  // Change this value
AVCaptureDevice *device             = ...; // Get the active capture device
[device lockForConfiguration:nil];
[device setActiveVideoMinFrameDuration:CMTimeMake(1, fps)];
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, fps)];
[device unlockForConfiguration];

这篇关于我想限制AVCapture框架中的视频捕获帧速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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