如何更改AVCaptureVideoDataOutput的视频方向

时间:2010-09-29 16:02:08

标签: iphone objective-c


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

以下是一些代码:

 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信息。

2 个答案:

答案 0 :(得分:10)

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

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

试一试,对我有用。

答案 1 :(得分:6)

从这里开始:http://developer.apple.com/library/ios/#qa/qa1744/_index.html#//apple_ref/doc/uid/DTS40011134

  

目前,电影文件的捕获输出   (AVCaptureMovieFileOutput)和静止图像(AVCaptureStillImageOutput)   支持设置方向,但数据输出进行处理   视频帧(AVCaptureVideoDataOutput)没有。