因此,在我的应用中,我正在录制视频。我想将录制视频的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);

最佳答案

我想出了问题。您必须在尝试设置minFrameDuration之前将输出添加到捕获 session 中(或者就此而言,拉出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);

关于ios - AVCaptureConnection isVideoMinFrameDurationSupported是否为假?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17089860/

10-09 10:09