如何更改AVCaptureVideoDataOutput的视频方向

Ste*_*eve 12 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]; 
Run Code Online (Sandbox Code Playgroud)

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

Shi*_*zam 10

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

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

试一试,适合我.


Or *_*bel 6

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

目前,电影文件(AVCaptureMovieFileOutput)和静止图像(AVCaptureStillImageOutput)的捕获输出支持设置方向,但用于处理视频帧的数据输出(AVCaptureVideoDataOutput)则不支持.