AVCaptureConnection isVideoMinFrameDurationSupported是假的?

时间:2013-06-13 14:37:07

标签: ios ipad cocoa-touch avcapturesession frame-rate

所以在我的应用程序中,我正在录制视频。我想将录制视频的fps限制为15 fps,因为速度更快,我处理每一帧都有问题。该应用程序的目标是iOS 5.0+,因此我不必担心iOS的旧版本。

我知道要设置最大fps,我可以使用AVCaptureConnection的setVideoMinFrameDuration。我也知道为了让它实际工作,我还必须setVideoMaxFrameDuration。但是,似乎在我的iPad上,AVCaptureConnection的isVideoMinFrameDurationSupported总是返回false,因此我从不设置videoMinFrameDuration,最终不得不回退到设置AVCAptureVideoDataOutput的minFrameDuration(这是一个不推荐的调用,并导致警告等等。谁能解释为什么我无法设置videoMinFrameDuration?

代码:

AVCaptureVideoDataOutput *videoDataOut = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary *settings = [[NSDictionary alloc]
                                        initWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange],
                                        (id)kCVPixelBufferPixelFormatTypeKey, nil];
videoDataOut.videoSettings = settings;

captureQueue = dispatch_queue_create("videoCaptureQueue", NULL);
    [videoDataOut setSampleBufferDelegate:self queue:captureQueue];
    videoDataOut.alwaysDiscardsLateVideoFrames = YES;
    AVCaptureConnection *conn = [videoDataOut connectionWithMediaType:AVMediaTypeVideo];

    // This if block is failing for some reason even though I'm running iOS 5.0+
    if ([conn isVideoMinFrameDurationSupported] && [conn isVideoMaxFrameDurationSupported]){
        [conn setVideoMinFrameDuration:CMTimeMake(1, pParams->fps)];
        [conn setVideoMaxFrameDuration:CMTimeMake(1, pParams->fps)];
    }
    else {
        videoDataOut.minFrameDuration = CMTimeMake(1, pParams->fps);
    }
    [captureSession addOutput:videoDataOut];

dispatch_release(captureQueue);

1 个答案:

答案 0 :(得分:2)

我想出了我的问题。在尝试设置minFrameDuration之前,你必须将输出添加到捕获会话中(或者,就此而言,拉出AVCaptureConnection)。

所以,新代码看起来像这样:

AVCaptureVideoDataOutput *videoDataOut = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary *settings = [[NSDictionary alloc]
                                        initWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange],
                                        (id)kCVPixelBufferPixelFormatTypeKey, nil];
videoDataOut.videoSettings = settings;

captureQueue = dispatch_queue_create("videoCaptureQueue", NULL);
[videoDataOut setSampleBufferDelegate:self queue:captureQueue];
videoDataOut.alwaysDiscardsLateVideoFrames = YES;
[captureSession addOutput:videoDataOut];

AVCaptureConnection *conn = [videoDataOut connectionWithMediaType:AVMediaTypeVideo];
if ([conn isVideoMinFrameDurationSupported] && [conn isVideoMaxFrameDurationSupported]){
        [conn setVideoMinFrameDuration:CMTimeMake(1, pParams->fps)];
        [conn setVideoMaxFrameDuration:CMTimeMake(1, pParams->fps)];
}

dispatch_release(captureQueue);