1

我使用AVCaptureSession捕捉视频。问题是我不明白视频方向背后的整个逻辑。我设置视频定位是这样的:AVCaptureSession视频方向iOS

AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; 
[captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 

所以我录制视频和显示预览给用户,但视频的方向始终是横盘整理。我需要它永远是肖像。 我发现了一些类似的问题,但没有找到解决方案。

我使用录制视频代码:

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混用。但是我所有的视频都是水平旋转的。将其与可变因素混合时,我不会做任何旋转。

我真的很感激,如果有人可以给我指示该做什么或者甚至可以看到代码中会出现什么问题,提前致谢!

+0

@Mayur我已经检查过,并尝试了一切从此已经没有帮助。看来,无论我设定我的录音是否仍然水平旋转。我正在尝试使用AVMutableComposition编写曲目时设置方向。 – Lukas

+0

你是否在纵向模式下拍摄视频? – Mayur

+0

@Mayur是的,我正在肖像模式下他们。然后我使用AVMutableComposition来添加音乐,非常简单的东西,没有旋转或任何东西。但我的视频是水平的。我将它保存到iPhone并将其发送到我的Mac,这是一样的Mac。水平旋转。在这个问题上我已经三天了:/ – Lukas

回答

1

简单地尝试这种解决定位问题:

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]]; 
} 

首先从代码中删除此2号线

编辑:

  • 只需在最后添加我的代码即可。

我希望它能工作。

+0

它仍然是一样的:/ – Lukas

+0

@Lukas做最后一次尝试并尝试在此行后面的最后添加我的代码 [self.captureSession startRunning]; – Mayur

+0

仍然相同:/ – Lukas