gpt4 book ai didi

ios - AVCaptureSession 视频方向 iOS

转载 作者:行者123 更新时间:2023-11-29 11:56:12 25 4
gpt4 key购买 nike

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

编辑:

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

我希望它能奏效。

关于ios - AVCaptureSession 视频方向 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com