我想在Mac OS X上鏡像從網絡攝像頭接收到的視頻。我想避免在收到視頻緩衝區後做手動翻轉/變換。所以,我想要設置AVCaptureSession
,使得在captureOutput
方法AVCaptureVideoDataOutputSampleBufferDelegate
中接收到的視頻緩衝區被AVFoundation本身鏡像。我不想使用預覽圖層。AVFoundation - 如何從網絡攝像頭鏡像視頻 - Mac OS X
在設置videoMirrored
屬性之前,在iMac(10.8.5)上,爲了鏡像視頻,已成功測試了AVCaptureConnection isVideoMirroringSupported
。但是,在captureOutput
代表中收到的視頻緩衝區不是鏡像的。
注意:iOS上的視頻鏡像成功,當我按照this SO回答。但它在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中,提到setVideoOrientation
在'AVCaptureVideoDataOutput`連接上不受支持。但在2013年,Apple docs已更新並支持此方法。
謝謝。但有問題,我曾提到過不願意使用預覽圖層(在第1段中) – Kiran
OSX不尊重videoMirrored屬性,可能是一個錯誤。使用預覽圖層是我發現達到效果的唯一方式。 – tansk1