swift - 在没有预览窗口的情况下使用 AVCaptureVideoDataOutputSampleBufferDelegate

标签 swift macos avfoundation avcapturesession cmsamplebuffer

我正在开发一个基于 Swift 的 macOS 应用程序,我需要在其中捕获视频输入,但不将其显示在屏幕上......而不是显示视频,我想将缓冲数据发送到其他地方进行处理,并且最终将其显示在 SceneKit 场景中的对象上。

我有一个 CameraInput 类,它有一个 prepareCamera 方法:

    fileprivate func prepareCamera() {
        self.videoSession = AVCaptureSession()
        self.videoSession.sessionPreset = AVCaptureSession.Preset.photo

        if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
            for device in devices {
                if device.hasMediaType(AVMediaType.video) {
                    cameraDevice = device

                    if cameraDevice != nil  {
                        do {
                            let input = try AVCaptureDeviceInput(device: cameraDevice)


                            if videoSession.canAddInput(input) {
                                videoSession.addInput(input)
                            }


                           } catch {
                            print(error.localizedDescription)
                        }
                    }
                }
            }

            let videoOutput = AVCaptureVideoDataOutput()
            videoOutput.setSampleBufferDelegate(self as AVCaptureVideoDataOutputSampleBufferDelegate, queue: DispatchQueue(label: "sample buffer delegate", attributes: []))
            if videoSession.canAddOutput(videoOutput) {
                videoSession.addOutput(videoOutput)
            }
        }
    }

还有一个启动AVCaptureSession session 的startSession 方法:

fileprivate func startSession() {
    if let videoSession = videoSession {
        if !videoSession.isRunning {
            self.videoInputRunning = true
            videoSession.startRunning()
        }
    }
}

我还实现了 AVCaptureVideoDataOutputSampleBufferDelegate,我打算在其中捕获 CMSampleBuffer 供以后使用:

extension CameraInput: AVCaptureVideoDataOutputSampleBufferDelegate {

    internal func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
        print(Date())
    }
}

但是,永远不会调用委托(delegate)。在这种情况下,我必须显示视频输出以便调用它吗?

最佳答案

您的任何问题都与您是否显示捕获视频的预览无关。

如果您使用的是 Swift 4(看起来您是),您要实现的委托(delegate)方法签名不是 captureOutput(_:didOutputSampleBuffer:from:),它是this :

optional func captureOutput(_ output: AVCaptureOutput, 
              didOutput sampleBuffer: CMSampleBuffer, 
                     from connection: AVCaptureConnection)

不相关的提示:

  • 命名空间常量意味着您可以根据需要更简短;例如videoSession.sessionPreset = .photo

  • AVCaptureDevice.devices() 已弃用。不用调用它并自己循环遍历设备,只需 ask AVCaptureDevice for exactly the kind of device you want :

    let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, 
                                           for: .video, position: .back)
    
  • 如果您的类已经声明符合 AVCaptureVideoDataOutputSampleBufferDelegatevideoOutput.setSampleBufferDelegate(self as AVCaptureVideoDataOutputSampleBufferDelegate协议(protocol)。


最后,如果您只是想将来自摄像机的实时视频映射到 SceneKit 场景的某个部分,请注意,在 iOS 11 中,您可以将 AVCaptureDevice 分配给 SCNMaterialPropertycontents 直接 - 无需自己抓取、处理和移动像素缓冲区。

关于swift - 在没有预览窗口的情况下使用 AVCaptureVideoDataOutputSampleBufferDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663309/

相关文章:

swift - 在 Xcode UI 测试中编辑记录的元素

macos - TideSDK Mac 上的 CSS3 transform 属性

objective-c - 设置 CALayer 渐变背景

ios - 在没有 CADisplayLink 的情况下在 openGLES 中渲染 YUV 帧

ios - 多个 SKPhysicsBody 来自带有 alpha 的纹理

ios - 如何在点击时制作 scrollView 'bounce'

ios - 如何知道哪个单元格打开了 UiTableViewController?

windows - 编写在 Windows 和 Mac 上运行的脚本

iphone - 将相机视频从iOS设备流式传输到另一个iOS设备

ios - AVAssetReaderOutput copyNextSampleBuffer 在设备上编码带音频的视频时挂起