gpt4 book ai didi

iphone - 如何更改 AVCaptureVideoDataOutput 的视频方向

转载 作者:太空狗 更新时间:2023-10-30 03:21:53 24 4
gpt4 key购买 nike


这就是问题所在。我正在使用 AVCaptureVideoDataOutput 从相机获取视频帧并使用 AVAssetWriter 从中制作视频。它工作正常,但我得到的视频是颠倒的,因为我的应用程序的设备默认方向是横向左侧,而不是 AVCaptureVideoDataOutput 中默认规定的横向右侧。我试图在 AVCaptureConnection 类中更改方向,但 isVideoOrientationSupported 始终为 false,是否可以通过某种方式修复它?

这是一些代码:

 AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput 
deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
error:nil];
/*We setupt the output*/
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
captureOutput.minFrameDuration = CMTimeMake(1.0, 24.0); //Uncomment it to specify a minimum duration for each video frame
[captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

// Set the video output to store frame in BGRA (It is supposed to be faster)
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];



NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];


/*And we create a capture session*/
self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetLow;
/*We add input and output*/
if ([self.captureSession canAddInput:captureInput])
{
[self.captureSession addInput:captureInput];
}
if ([self.captureSession canAddOutput:captureOutput])
{
[self.captureSession addOutput:captureOutput];
}

/*We add the preview layer*/
self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession];

if ([self.prevLayer isOrientationSupported])
{
[self.prevLayer setOrientation:AVCaptureVideoOrientationLandscapeLeft];
}

self.prevLayer.frame = self.view.bounds;

self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer: self.prevLayer];

AVCaptureConnection *videoConnection = NULL;

[self.captureSession beginConfiguration];

for ( AVCaptureConnection *connection in [captureOutput connections] )
{
for ( AVCaptureInputPort *port in [connection inputPorts] )
{
if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;

}
}
}
if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
}

[self.captureSession commitConfiguration];

[self.captureSession startRunning];

Upd:发现在导出视频时,AVAssetExportSession 丢失了 preferredTransform 信息。

最佳答案

我遇到了同样的问题,并在 WWDC 的 AVCamDemo 周围闲逛。我不知道为什么(还),但是如果您在创建所有输入/输出/连接后立即查询您的 videoConnection,那么 isVideoOrientationSupported 和 supportsVideoOrientation 都会返回 NO。

但是,如果您稍后查询 supportsVideoOrientation 或 isVideoOrientationSupported(例如在设置 GUI 之后),它将返回 YES。例如,在我调用 [[self movieFileOutput] startRecordingToOutputFileURL...]

之前,我在用户单击录制按钮后立即查询它

试一试,对我有用。

关于iphone - 如何更改 AVCaptureVideoDataOutput 的视频方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3823461/

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