ios - AVCapture 在 iOS 7 中以 60 fps 捕获和获取帧缓冲区

标签 ios iphone camera avcapturesession frame-rate

我正在开发一个应用程序,它需要以尽可能高的 fps 捕获帧缓冲区。我已经想出如何强制 iphone 以 60 fps 的速度捕获,但是

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

方法每秒仅调用 15 次,这意味着 iPhone 将捕获输出降级为 15 fps。

有人遇到过这样的问题吗?是否有可能提高捕获帧率?

更新我的代码:

camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([camera isTorchModeSupported:AVCaptureTorchModeOn]) {
   [camera lockForConfiguration:nil];
   camera.torchMode=AVCaptureTorchModeOn;
   [camera unlockForConfiguration];
}
[self configureCameraForHighestFrameRate:camera];

// Create a AVCaptureInput with the camera device
NSError *error=nil;
AVCaptureInput* cameraInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
if (cameraInput == nil) {
   NSLog(@"Error to create camera capture:%@",error);
}

// Set the output
AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];

// create a queue to run the capture on
dispatch_queue_t captureQueue=dispatch_queue_create("captureQueue", NULL);

// setup our delegate
[videoOutput setSampleBufferDelegate:self queue:captureQueue];

// configure the pixel format
videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                             nil];

// Add the input and output
[captureSession addInput:cameraInput];
[captureSession addOutput:videoOutput];

我在这里采用了configureCameraForHighestFrameRate 方法https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html

最佳答案

我在 iPhone 5 上以 60 fps 和 iPhone 5s 上的 120 fps 获取样本,无论是在 captureOutput 中进行实时运动检测还是使用 AVAssetWriter 将帧保存到视频时。

您必须将 AVCaptureSession 设置为支持 60 fps 的格式:

AVsession = [[AVCaptureSession alloc] init];

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *capInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (capInput) [AVsession addInput:capInput];

for(AVCaptureDeviceFormat *vFormat in [videoDevice formats] ) 
{
    CMFormatDescriptionRef description= vFormat.formatDescription;
    float maxrate=((AVFrameRateRange*)[vFormat.videoSupportedFrameRateRanges objectAtIndex:0]).maxFrameRate;

    if(maxrate>59 && CMFormatDescriptionGetMediaSubType(description)==kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
    {
        if ( YES == [videoDevice lockForConfiguration:NULL] ) 
        {
           videoDevice.activeFormat = vFormat;
           [videoDevice setActiveVideoMinFrameDuration:CMTimeMake(10,600)];
           [videoDevice setActiveVideoMaxFrameDuration:CMTimeMake(10,600)];
           [videoDevice unlockForConfiguration];
           NSLog(@"formats  %@ %@ %@",vFormat.mediaType,vFormat.formatDescription,vFormat.videoSupportedFrameRateRanges);
        }
     }
}

prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: AVsession];
prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer: prevLayer];

AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
dispatch_queue_t videoQueue = dispatch_queue_create("videoQueue", NULL);
[videoOut setSampleBufferDelegate:self queue:videoQueue];

videoOut.videoSettings = @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
videoOut.alwaysDiscardsLateVideoFrames=YES;

if (videoOut)
{
    [AVsession addOutput:videoOut];
    videoConnection = [videoOut connectionWithMediaType:AVMediaTypeVideo];
}

如果您想使用 AVAssetWriter 写入文件,还有另外两条评论。不要使用 pixelAdaptor,只需使用

广告示例
[videoWriterInput appendSampleBuffer:sampleBuffer]

其次在设置assetwriter时使用

[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                   outputSettings:videoSettings 
                                 sourceFormatHint:formatDescription];

sourceFormatHint 会影响写入速度。

关于ios - AVCapture 在 iOS 7 中以 60 fps 捕获和获取帧缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20330174/

相关文章:

Mobile Safari IOS 6 拍照

c++ - 将帧速率提高到 30 fps 并从 AVT GigE 相机获取 bayerrg8

Android ICS 模拟器相机

ios - 不兼容的指针类型将 User* 发送到 UserProfile* 类型的参数

iphone - 我可以安全使用哪些 iPhone 系统字体以及推荐哪些字体用于何种显示目的?

iphone - 缺少基础 SDK - 永久解决方案?

iphone - 以编程方式更改 UITextField 键盘类型

ios - 带有 AVPlayer iOS 的 Chrome-cast

ios - 对成员 'downloadTask(with:completionHandler:)' 的模糊引用

ios - 根据屏幕大小更改节点定位 Swift/Sprite Kit