我正在尝试在 mac os x 上镜像从网络摄像头收到的视频。我想避免在收到视频缓冲区后进行手动翻转/转换。所以,我想设置AVCaptureSession
这样在captureOutput
方法中接收到的视频缓冲区AVCaptureVideoDataOutputSampleBufferDelegate
被 AVFoundation 本身镜像。我不想使用预览层。
在 iMac(10.8.5) 上,要镜像视频,AVCaptureConnection isVideoMirroringSupported
在设置videoMirrored
属性之前已成功测试。captureOutput
但是在委托中接收到的视频缓冲区没有被镜像。
注意:当我遵循这个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 - 在这个 2010 wwdc 苹果演示文稿(3Mb pdf)的幻灯片 73 中,提到了setVideoOrientation
“AVCaptureVideoDataOutput”连接不支持的内容。但在 2013 年,Apple文档更新并支持此方法。