AVCaptureVideoDataOutput

AVCaptureVideoDataOutput

本文介绍了如何更改AVCaptureVideoDataOutput的视频方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是问题所在。我正在使用AVCaptureVideoDataOutput从相机获取视频帧并使用AVAssetWriter从它们制作视频。它工作正常,但我得到的视频是颠倒的,因为我的应用程序的设备的默认方向是横向左侧,而不是正确的右侧,如AVCaptureVideoDataOutput中默认的那样。我试图在AVCaptureConnection类中更改方向,但isVideoOrientationSupported总是为false,是否可以修复它?


Here's the problem. I am using AVCaptureVideoDataOutput to get video frames from camera and make video from them with AVAssetWriter. Its working OK, but the video that I get is upside down because default orientation of device for my app is landscape left, not landscape right as its stated by default in AVCaptureVideoDataOutput. Im trying to change orientation in AVCaptureConnection class, but isVideoOrientationSupported is always false, is it somehow possible to fix it?

以下是一些代码:

 AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
            deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
            error:nil];
 /*We setupt the output*/
 AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
 captureOutput.alwaysDiscardsLateVideoFrames = YES;
 captureOutput.minFrameDuration = CMTimeMake(1.0, 24.0); //Uncomment it to specify a minimum duration for each video frame
 [captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

 // Set the video output to store frame in BGRA (It is supposed to be faster)
 NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
 NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];



 NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
 [captureOutput setVideoSettings:videoSettings];


 /*And we create a capture session*/
 self.captureSession = [[AVCaptureSession alloc] init];
 self.captureSession.sessionPreset = AVCaptureSessionPresetLow;
 /*We add input and output*/
 if ([self.captureSession canAddInput:captureInput])
 {
  [self.captureSession addInput:captureInput];
 }
 if ([self.captureSession canAddOutput:captureOutput])
 {
  [self.captureSession addOutput:captureOutput];
 }

 /*We add the preview layer*/
 self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession];

 if ([self.prevLayer isOrientationSupported])
 {
  [self.prevLayer setOrientation:AVCaptureVideoOrientationLandscapeLeft];
 }

 self.prevLayer.frame = self.view.bounds;

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

 AVCaptureConnection *videoConnection = NULL;

 [self.captureSession beginConfiguration];

 for ( AVCaptureConnection *connection in [captureOutput connections] )
 {
  for ( AVCaptureInputPort *port in [connection inputPorts] )
  {
   if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
   {
    videoConnection = connection;

   }
  }
 }
    if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
     {
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
     }

 [self.captureSession commitConfiguration];

 [self.captureSession startRunning];

Upd:认为在导出视频时,AVAssetExportSession会丢失preferredTransform信息。

Upd: figured that when exporting video, the AVAssetExportSession loses preferredTransform info.

推荐答案

我遇到了同样的问题,从WWDC围绕AVCamDemo。我不知道为什么(还)但是如果你在创建所有输入/输出/连接后立即查询你的videoConnection,那么isVideoOrientationSupported和supportsVideoOrientation都返回NO。

I ran into the same problem and poked around the AVCamDemo from WWDC. I don't know why (yet) but if you query your videoConnection right after you create all the inputs/outputs/connections then both isVideoOrientationSupported and supportsVideoOrientation return NO.

但是,如果您稍后查询supportsVideoOrientation或isVideoOrientationSupported (例如,在设置GUI之后),则返回YES。例如,我在用户点击记录按钮之后立即查询它[[self movieFileOutput] startRecordingToOutputFileURL ...]

However, if you query supportsVideoOrientation or isVideoOrientationSupported at some later point (after the GUI is setup for instance) then it will return YES. For instance I query it right after the user clicks the record button just before I call [[self movieFileOutput] startRecordingToOutputFileURL...]

试一试,对我有用。

这篇关于如何更改AVCaptureVideoDataOutput的视频方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 20:17