我正在使用AVCaptureSession捕获视频。问题是我不了解视频定位背后的整个逻辑。我设置了这样的视频方向:
AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
[captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
所以我录制视频并向用户显示预览,但视频方向始终是侧面的。我需要它永远是肖像。 我在SO上发现了一些类似的问题,但没有找到解决方案。
我用来录制视频的代码:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.captureSession = session;
AVCaptureDevice *VideoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (VideoDevice)
{
NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error];
self.videoInputDevice = input;
if (!error)
{
if ([self.captureSession canAddInput:self.videoInputDevice])
[self.captureSession addInput:self.videoInputDevice];
}
}
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput)
[self.captureSession addInput:audioInput];
[self.previewLayer removeFromSuperlayer];
self.previewLayer = nil;
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
self.movieFileOutput = movieFileOutput;
Float64 TotalSeconds = kDefaultRecordingTime;
int32_t preferredTimeScale = 30;
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
self.movieFileOutput.maxRecordedDuration = maxDuration;
self.movieFileOutput.movieFragmentInterval = kCMTimeInvalid;
self.movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;
if ([self.captureSession canAddOutput:self.movieFileOutput])
[self.captureSession addOutput:self.movieFileOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetMedium];
if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720])
[self.captureSession setSessionPreset:AVCaptureSessionPreset1280x720];
CGRect layerRect = [self.recordingView bounds];
[self.previewLayer setBounds:layerRect];
[self.previewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
CGRectGetMidY(layerRect))];
[self.recordingView.layer addSublayer:self.previewLayer];
[self.recordingView bringSubviewToFront:self.recordButton];
[self.recordingView bringSubviewToFront:self.frontCameraButton];
AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
[captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
[self.captureSession commitConfiguration];
[self.captureSession startRunning];
然后我录制另一个视频并使用AVMutableComposition混合它。但是我的所有视频都是水平旋转的。将它与avmutablecomposition混合时,我不进行任何旋转。
我真的很感激,如果有人可以给我指示该做什么,或者甚至可以看到代码中有什么问题,我会非常感激。提前谢谢!
答案 0 :(得分:1)
只需尝试此操作即可解决方向问题:
首先从代码中删除这2行:
AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
[captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
现在在同一个地方添加此代码:
AVCaptureConnection *videoConnection = nil;
for ( AVCaptureConnection *connection in [movieFileOutput connections] )
{
NSLog(@"%@", connection);
for ( AVCaptureInputPort *port in [connection inputPorts] )
{
NSLog(@"%@", port);
if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
}
}
}
if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
[videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]];
}
修改:
我希望它能奏效。