我正在尝试镜像从mac os x上的网络摄像头收到的视频。我希望在收到视频缓冲区后避免进行手动翻转/转换。因此,我想设置AVCaptureSession
,以便在captureOutput
AVCaptureVideoDataOutputSampleBufferDelegate
方法中收到的视频缓冲区由AVFoundation本身镜像。我不想使用预览图层。
在iMac(10.8.5)上,为了镜像视频,在设置AVCaptureConnection isVideoMirroringSupported
属性之前已成功测试videoMirrored
。但是captureOutput
委托中收到的视频缓冲区没有镜像。
注意:当我按下this SO回答时,iOS上的视频镜像是成功的。但它对mac os x没有帮助。
使用的代码如下。这篇文章遗漏了错误检查。
//create session
_session = [[AVCaptureSession alloc] init];
//get capture device
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//create sesion input
NSError * error;
_sessionInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
//create session output
_sessionOutput = [[AVCaptureVideoDataOutput alloc] init];
[_sessionOutput setAlwaysDiscardsLateVideoFrames:YES];
[[_sessionOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:YES];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[_sessionOutput setVideoSettings:videoSettings];
//serial queue to process video frames
dispatch_queue_t videoOutputQueue = dispatch_queue_create("deviceeraQueue", DISPATCH_QUEUE_SERIAL);
[_sessionOutput setSampleBufferDelegate:self queue:videoOutputQueue];
//begin session configuration
[_session beginConfiguration ];
//input and output for session
if( [_session canAddInput:_sessionInput]) {
[_session addInput:_sessionInput];
}
if( [_session canAddOutput:_sessionOutput]) {
[_session addOutput:_sessionOutput];
}
//set video mirroring
AVCaptureConnection* avConnection = [_sessionOutput connectionWithMediaType:AVMediaTypeVideo];
if( [avConnection isVideoMirroringSupported]) {
avConnection.videoMirrored = YES;
NSLog(@"Video mirroring Support: YES"); // this line is printed
} else {
NSLog(@"Video mirroring Support: NO");
}
//set session preset
[_session setSessionPreset:AVCaptureSessionPreset640x480];
[ _session commitConfiguration ];
...........
...........
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
.........
//sampleBuffer is not mirrored video
........
不太重要1 - 虽然是C ++,但我也试图研究OpenCV的VideoCapture实现,以便镜像视频。但是,OpenCV不会反映来自Mac的视频(使用翻转)。左边是libVlc / V4L。
不太重要2 - 在this 2010 wwdc apple presentation(3Mb pdf)的幻灯片73中,提到“AVCaptureVideoDataOutput”连接不支持setVideoOrientation
。但在2013年,Apple docs已更新并支持此方法。
答案 0 :(得分:6)
您可以在预览图层上添加变换,以在帧到达预览窗口之前翻转帧的x值。
[[self previewLayer] setTransform:CATransform3DMakeScale(-1, 1, 1)];
然后,您可以通过导出会话运行录制的视频并执行相同的转换。这样,视频预览将与最终录制的视频相匹配。一点点黑客,但它得到了相同的结果。
答案 1 :(得分:3)
为什么在非常容易的时候破解它。只需设置AVCaptureConnection的automaticAdjustVideoMirroring,然后手动设置。
aPreviewLayer.connection.automaticallyAdjustsVideoMirroring = NO;
aPreviewLayer.connection.videoMirrored = YES;
答案 2 :(得分:1)
Swift 5版本的“ RorvaldurRúnarsson”答案:
previewLayer.connection?.automaticallyAdjustsVideoMirroring = false
previewLayer.connection?.isVideoMirrored = true