2

所以在我的应用程序中,我正在录制视频。我想将录制视频的 fps 限制为 15 fps,因为速度更快,而且我在处理每一帧时遇到问题。该应用程序针对 iOS 5.0+,因此我不必担心旧版本的 iOS。

我知道要设置最大 fps,我可以使用 AVCaptureConnection 的 setVideoMinFrameDuration。我也知道要让它真正起作用,我还必须设置VideoMaxFrameDuration。但是,在我的 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);
4

1 回答 1

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);
于 2013-06-17T17:46:01.080 回答