【问题标题】:AVFoundation - how to mirror video from webcam - Mac OS XAVFoundation - 如何从网络摄像头镜像视频 - Mac OS X
【发布时间】:2014-05-16 13:45:35
【问题描述】:

我正在尝试在 mac os x 上镜像从网络摄像头收到的视频。我想避免在收到视频缓冲区后进行手动翻转/转换。所以,我想设置AVCaptureSession,以便在AVCaptureVideoDataOutputSampleBufferDelegatecaptureOutput 方法中接收到的视频缓冲区被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更新并支持此方法。

【问题讨论】:

  • isVideoMirrored API 上发现了相同的行为,我使用了预览层,预览层可以正确设置为镜像,但我使用AVCaptureAudioDataOutput 在输出委托中实际录制视频:captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) 样本缓冲区未镜像。

标签: macos avfoundation webcam-capture


【解决方案1】:

您可以在预览层上添加一个变换,以在帧到达预览窗口之前翻转帧的 x 值。

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

然后您可以通过导出会话运行录制的视频并进行相同的转换。这样,视频预览将与最终录制的视频相匹配。有点破解,但它得到了相同的结果。

【讨论】:

  • 谢谢。但问题是,我曾提到不愿使用预览层(在第 1 段中)
  • OSX 不尊重 videoMirrored 属性,可能是一个错误。使用预览层是我发现实现效果的唯一方法。
【解决方案2】:

为什么要破解它很容易。只需设置您的 AVCaptureConnection 的自动调整视频镜像,然后手动设置。

    aPreviewLayer.connection.automaticallyAdjustsVideoMirroring = NO;
    aPreviewLayer.connection.videoMirrored = YES;

【讨论】:

  • 谢谢。但请参阅我对您不情愿预览层的提及(在问题的第 1 段中)。在我对先前答案的评论中再次提到了这一点。
  • 我认为你不能不使用预览层。但是你当然可以通过例如缩放来达到相同的效果。通过 (x,y,z) = (-1,1,1) 缩放缓冲区将实现镜像效果:)
  • I don't think you can without using the preview layer. - 然后要结束这个问题,为了支持它,我们需要一个有效的源文档或 Apple 接受的错误报告。
【解决方案3】:

Swift 5 版本的“Þorvaldur Rúnarsson”答案:

previewLayer.connection?.automaticallyAdjustsVideoMirroring = false
previewLayer.connection?.isVideoMirrored = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多