Meg*_*anX 6 iphone video avfoundation ios
我已经制作了一个捕获设备视频输入的代码,到目前为止它工作正常.这是我设定的
// 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];
Run Code Online (Sandbox Code Playgroud)
哪里:
- (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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当界面方向改变时,我希望我的输出也改变,所以我有这个:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
但唉,它不起作用.似乎有一次我首先在电影输出上设置视频方向,它保持不变,以后不能更改.因此,如果我开始以横向模式拍摄,然后更改为肖像,视频将适用于横向模式,但纵向模式将会旋转.如果我以纵向模式开始,则会旋转景观.
有没有办法做到这一点?
尝试在开始会话之前添加此内容:
[_movieFileOutput setRecordsVideoOrientationAndMirroringChanges:YES asMetadataTrackForConnection:movieFileOutputConnection];
Run Code Online (Sandbox Code Playgroud)
此方法的头文件文档使其听起来非常像您正在寻找的内容:
控制影片文件输出是否创建定时元数据轨道,该轨道记录反映录制期间对给定连接的 videoOrientation 和 videoMirrored 属性所做更改的示例。
那里有更多有趣的信息,我会全部阅读。
但是,此方法实际上并不旋转帧,它使用定时元数据来指示播放器在播放时执行此操作,因此可能并非所有播放器都支持此功能。如果这是一个破坏因素,那么您可以放弃AVCaptureMovieFileOutput
,转而选择较低级别 AVCaptureVideoDataOutput
+AVAssetWriter
组合,您的videoOrientation
更改实际上会旋转帧,从而生成可以在任何播放器中正确播放的文件:
If an AVCaptureVideoDataOutput instance's connection's videoOrientation or videoMirrored properties are set to non-default values, the output applies the desired mirroring and orientation by physically rotating and or flipping sample buffers as they pass through it.
p.s. I don't think you need the beginConfiguration
/commitConfiguration
pair if you're only changing one property as that's for batching multiple modifications into one atomic update.
归档时间: |
|
查看次数: |
1270 次 |
最近记录: |