【发布时间】:2017-12-05 21:56:20
【问题描述】:
我正在开发一个基于 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 会话的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())
}
}
但是,委托永远不会被调用。这是我必须显示视频输出以便调用它的情况吗?
【问题讨论】:
-
你在什么类下写这几行代码?你的输入设备是什么?
-
@ElTomato 输入设备只是我的 mac 的 iSIGht 相机。该类是一个名为 CameraInput 的自定义类,它继承自 NSObject。
-
您没有使用 previewLayer。它是干什么用的?据我所知,你确实需要它。
-
@ElTomato 啊,是的;不小心留在了那里 - 我更新了上面的代码。
标签: swift macos avfoundation avcapturesession cmsamplebuffer