AVCapture在iOS 7中捕获并获得60 fps的帧缓冲区

前端之家收集整理的这篇文章主要介绍了AVCapture在iOS 7中捕获并获得60 fps的帧缓冲区前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个应用程序,需要尽可能多的fps捕获帧缓冲区.我已经弄清楚如何强制 iphone以60 fps捕获
  1. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

方法被称为只有15次,这意味着iPhone降级捕获输出到15 fps.

有没有人面临这样的问题?是否有可能增加捕获帧速率?

更新我的代码

  1. camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  2. if([camera isTorchModeSupported:AVCaptureTorchModeOn]) {
  3. [camera lockForConfiguration:nil];
  4. camera.torchMode=AVCaptureTorchModeOn;
  5. [camera unlockForConfiguration];
  6. }
  7. [self configureCameraForHighestFrameRate:camera];
  8.  
  9. // Create a AVCaptureInput with the camera device
  10. NSError *error=nil;
  11. AVCaptureInput* cameraInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
  12. if (cameraInput == nil) {
  13. NSLog(@"Error to create camera capture:%@",error);
  14. }
  15.  
  16. // Set the output
  17. AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
  18.  
  19. // create a queue to run the capture on
  20. dispatch_queue_t captureQueue=dispatch_queue_create("captureQueue",NULL);
  21.  
  22. // setup our delegate
  23. [videoOutput setSampleBufferDelegate:self queue:captureQueue];
  24.  
  25. // configure the pixel format
  26. videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA],(id)kCVPixelBufferPixelFormatTypeKey,nil];
  27.  
  28. // Add the input and output
  29. [captureSession addInput:cameraInput];
  30. [captureSession addOutput:videoOutput];

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

解决方法

我在iPhone 5上获得了60 fps的采样率,在iPhone 5上采用了120 fps,无论是在捕获输出中进行实时运动检测还是使用AVAssetWriter将帧保存到视频中.

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

  1. AVsession = [[AVCaptureSession alloc] init];
  2.  
  3. AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  4. AVCaptureDeviceInput *capInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
  5. if (capInput) [AVsession addInput:capInput];
  6.  
  7. for(AVCaptureDeviceFormat *vFormat in [videoDevice formats] )
  8. {
  9. CMFormatDescriptionRef description= vFormat.formatDescription;
  10. float maxrate=((AVFrameRateRange*)[vFormat.videoSupportedFrameRateRanges objectAtIndex:0]).maxFrameRate;
  11.  
  12. if(maxrate>59 && CMFormatDescriptionGetMediaSubType(description)==kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
  13. {
  14. if ( YES == [videoDevice lockForConfiguration:NULL] )
  15. {
  16. videoDevice.activeFormat = vFormat;
  17. [videoDevice setActiveVideoMinFrameDuration:CMTimeMake(10,600)];
  18. [videoDevice setActiveVideoMaxFrameDuration:CMTimeMake(10,600)];
  19. [videoDevice unlockForConfiguration];
  20. NSLog(@"formats %@ %@ %@",vFormat.mediaType,vFormat.formatDescription,vFormat.videoSupportedFrameRateRanges);
  21. }
  22. }
  23. }
  24.  
  25. prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: AVsession];
  26. prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  27. [self.view.layer addSublayer: prevLayer];
  28.  
  29. AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
  30. dispatch_queue_t videoQueue = dispatch_queue_create("videoQueue",NULL);
  31. [videoOut setSampleBufferDelegate:self queue:videoQueue];
  32.  
  33. videoOut.videoSettings = @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
  34. videoOut.alwaysDiscardsLateVideoFrames=YES;
  35.  
  36. if (videoOut)
  37. {
  38. [AVsession addOutput:videoOut];
  39. videoConnection = [videoOut connectionWithMediaType:AVMediaTypeVideo];
  40. }

如果您想使用AVAssetWriter写入一个文件,另外两个评论.不要使用pixelAdaptor,只需要添加样品

  1. [videoWriterInput appendSampleBuffer:sampleBuffer]

其次,当设置assetwriter使用

  1. [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
  2. outputSettings:videoSettings
  3. sourceFormatHint:formatDescription];

sourceFormatHint在写入速度方面有所不同.

猜你在找的iOS相关文章