【问题标题】:How to change video orientation for AVCaptureVideoDataOutput如何更改 AVCaptureVideoDataOutput 的视频方向
【发布时间】:2010-09-29 16:02:08
【问题描述】:


这就是问题所在。我正在使用 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]; 

更新:发现在导出视频时,AVAssetExportSession 丢失了preferredTransform 信息。

【问题讨论】:

  • 史蒂夫,你搞定了吗?我想知道你是如何使用 AVAssetWriter 的。我希望能够从 iphone 流式传输视频。
  • 希望对您有所帮助。 pastebin.com/RVEqWnyN
  • 当你使用你得到的URL时:“Pastebin”可能已经过期或被删除了!
  • @Steve:hi,现在我也有同样的问题。你有什么解决办法吗???如何使 AVCaptureConnection 支持视频方向????请帮助我
  • 请在 Swift 中查看我的Answer。它非常适合我。

标签: iphone objective-c


【解决方案1】:

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

但是,如果您稍后查询supportsVideoOrientation 或isVideoOrientationSupported(例如在设置GUI 之后),那么它将返回YES。例如,在我调用 [[self movieFileOutput] startRecordingToOutputFileURL...]

之前,我在用户单击录制按钮后立即查询它

试一试,对我有用。

【讨论】:

  • 哦,哇,这很棘手!但它有效。我认为苹果应该解决这个问题。 isVideoOrientationSupported 属性应该能够设置在与创建 AVCaptureOutput 相同的运行循环中。在稍后的某个时间点这样做只会不必要地使代码更加复杂。即使背后有原因,至少文件应该讲到这一点。在我找到这篇文章之前,我必须手动转换我的坐标来读取像素!
  • 其实在后面的runloop中不需要再做。您可以在 addOutput: 到 AVCaptureSession 之后立即执行此操作。
  • 这是一个救命的答案。我真的不明白为什么文档中没有提到这些条件。
  • 这对我来说是一个救命的答案
  • 大家好,我在使用该演示时也遇到了同样的问题,我做了很多研发工作但仍然遇到同样的问题,请帮我解决这个问题。跨度>
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多