2016-10-03 60 views
6

我已经做了一个捕获设备视频输入的代码,到目前为止它工作正常。以下是我已经设置如何在运行会话期间更改AVCaptureMovieFileOutput视频方向?

// add preview layer 
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session]; 
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
[self.videoView.layer addSublayer:_previewLayer]; 

// add movie output 
_movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 
[_session addOutput:_movieFileOutput]; 
AVCaptureConnection *movieFileOutputConnection = [_movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; 
movieFileOutputConnection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation]; 

// start session 
[_session startRunning]; 

其中:

- (AVCaptureVideoOrientation) videoOrientationFromCurrentDeviceOrientation { 
    switch ([[UIApplication sharedApplication] statusBarOrientation]) { 
     case UIInterfaceOrientationPortrait: { 
      return AVCaptureVideoOrientationPortrait; 
     } 
     case UIInterfaceOrientationLandscapeLeft: { 
      return AVCaptureVideoOrientationLandscapeLeft; 
     } 
     case UIInterfaceOrientationLandscapeRight: { 
      return AVCaptureVideoOrientationLandscapeRight; 
     } 
     case UIInterfaceOrientationPortraitUpsideDown: { 
      return AVCaptureVideoOrientationPortraitUpsideDown; 
     } 
     case UIInterfaceOrientationUnknown: { 
      return 0; 
     } 
    } 
} 

现在,当界面取向的变化我想我的输出也发生变化,所以我有这样的:

- (void) updatePreviewLayer { 
    _previewLayer.frame = CGRectMake(0, 0, self.videoView.frame.size.width, self.videoView.frame.size.height); 
    _previewLayer.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation]; 
    [_session beginConfiguration]; 
    AVCaptureConnection *movieFileOutpurConnection = [_movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; 
    movieFileOutpurConnection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation]; 
    [_session commitConfiguration]; 
} 

但唉它不工作。似乎有一次,我首先在电影输出上设置了视频方向,它保持不变,稍后不能更改。因此,如果我以横向模式开始拍摄,然后转换为纵向模式,则横向模式的视频将会正常,但纵向模式将会旋转。如果我以纵向模式启动,则相同,但横向将旋转。

有什么办法可以做到这一点吗?

回答

1

尝试添加此您启动会话之前:

[_movieFileOutput setRecordsVideoOrientationAndMirroringChanges:YES asMetadataTrackForConnection:movieFileOutputConnection]; 

此方法的头文件文档使它听起来很像你要找的东西:

控制是否电影文件输出将创建一个定时元数据轨道,用于记录 反映在记录 期间对给定连接的videoOrientation和videoMirrored属性所做的更改的样本。

这里还有更多有趣的信息,我都读过。

但是,这种方法实际上并没有旋转你的帧,它使用定时元数据来指示玩家在播放时进行,所以有可能并非所有玩家都会支持这个功能。如果这是一个大忌,那么你就可以有利于低层AVCaptureVideoDataOutput + AVAssetWriter组合,你的videoOrientation变化实际上旋转框架放弃AVCaptureMovieFileOutput,导致文件,这些文件在任何播放器正常播放:

如果AVCaptureVideoDataOutput实例的连接的videoOrientation或videoMirrored属性设置为 非默认值,输出通过物理旋转和/或翻转 样本缓冲区来应用所需的镜像和方向。

p.s.我不认为你需要beginConfiguration/commitConfiguration对,如果你只是改变一个属性,因为这是为了将多个修改分批为一个原子更新。

1

您是否尝试在更改配置之前暂停会话?