AVCaptureSession视频定向iOS

AVCaptureSession视频定向iOS,ios,avfoundation,avplayer,avcapturesession,avmutablecomposition,Ios,Avfoundation,Avplayer,Avcapturesession,Avmutablecomposition,我正在使用AVCaptureSession捕获视频。问题是我不理解视频定位背后的全部逻辑。我设置视频方向如下: AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; [captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 因此,我录制了视频并向用

我正在使用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


我真的很感激如果有人能告诉我怎么做,或者甚至看到代码中的错误,我真的很感激。提前谢谢

只需尝试以下方法即可解决定位问题:

首先,从代码中删除这两行:

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]];
}
编辑:

  • 最后添加我的代码

我希望它能起作用。

@Mayur我已经检查过了,并且已经尝试过了这方面的一切,但都没有用。似乎无论我设置什么,我的录音都是水平旋转的。我在用AVMutableComposition创作曲目时试图设置方向。你是在纵向模式下拍摄视频吗?@Mayur是的,我是在纵向模式下拍摄的。然后我用AVMutableComposition添加音乐,非常简单的东西,没有旋转或任何东西。但我的视频是水平的。我将它保存到iphone并发送到我的mac电脑,在mac电脑上也是一样的。水平旋转。已经三天了:/请检查我的答案仍然是一样的:/@Lukas做最后一次尝试&尝试在这行[self.captureSession startRunning]之后添加我的代码@卢卡斯:看起来这是个关键问题。我必须在设备上试用。我为此选择了不同的库,无法解决:/