【发布时间】:2014-05-16 13:45:35
【问题描述】:
我正在尝试在 mac os x 上镜像从网络摄像头收到的视频。我想避免在收到视频缓冲区后进行手动翻转/转换。所以,我想设置AVCaptureSession,以便在AVCaptureVideoDataOutputSampleBufferDelegate 的captureOutput 方法中接收到的视频缓冲区被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 苹果演示文稿(3Mb pdf)的幻灯片 73 中,提到“AVCaptureVideoDataOutput”连接不支持setVideoOrientation。但2013年苹果docs更新并支持此方法。
【问题讨论】:
-
在
isVideoMirroredAPI 上发现了相同的行为,我使用了预览层,预览层可以正确设置为镜像,但我使用AVCaptureAudioDataOutput在输出委托中实际录制视频:captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)样本缓冲区未镜像。
标签: macos avfoundation webcam-capture