AVFoundation - How to Mirror Webcam Video - Mac OS X

I am trying to track video received from a webcam on mac os x. I would like to avoid executing a manual flip / tranform after receiving the video buffer. So, I want to configure AVCaptureSession so that the video buffer received in the captureOutput method AVCaptureVideoDataOutputSampleBufferDelegate itself. I do not want to use the preview layer.

In iMac (10.8.5), to mirror video, AVCaptureConnection isVideoMirroringSupported successfully tested before setting the videoMirrored property. But the video buffer received in the captureOutput captureOutput is not mirrored.

Note. Video mirroring on iOS was successful when I followed this up . But this does not help on mac os x.

The code used is below. A validation error was left for this message.

  //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 ........ 

Less important 1 - although C ++, I also tried to learn about the implementation of OpenCV VideoCapture for a way to mirror video. But OpenCV does not mirror video from the Mac (uses flip). On the left is libVlc / V4L.

Less important 2 - In slide 73 of this 2010 presentation of wwdc apple (3Mb pdf), there is a mention that setVideoOrientation not supported in the connection "AVCaptureVideoDataOutput". But in 2013, the docs update is updated and supports this method.

+7
avfoundation macos webcam-capture
source share
2 answers

You can add a transformation at the preview level to flip the value of x frames before they get into the preview window.

 [[self previewLayer] setTransform:CATransform3DMakeScale(-1, 1, 1)]; 

Then you can start the recorded video through the export session and perform the same conversion. Thus, the preview of the video will correspond to the final recorded video. A bit of a hack, but it gets the same results.

+5
source share

Why crack it when it's very easy. Just set autoAdjustVideoMirroring of your AVCaptureConnection, then set it manually.

  aPreviewLayer.connection.automaticallyAdjustsVideoMirroring = NO; aPreviewLayer.connection.videoMirrored = YES; 
+2
source share

All Articles