如何使用 Swift 3 中的 sampleBuffer、captureOutput 将视频保存到文件?

How to save a video to file using sampleBuffer, captureOutput in Swift 3?

我正在努力解决这个问题。我想在 OSX 上使用 Swift 录制实时视频,使用 AVAssetWriterInput.

我创建了一个名为 inputAVAssetWriterInput 实例,我想将数据附加到输入,然后将其写入文件。

我将 expectsMediaDataInRealTime 设置为 true,就像文档中所说的那样。当我 运行 应用程序时,出现此错误:

Cannot append sample buffer: Cannot call method when status is 0

我使用 captureOutput 从相机获取数据。

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
   // defined on load
   // var input:AVAssetWriterInput

   // Here I get an error
   input.append(sampleBuffer)
}

AVAssetWriterInput 是否会处理视频的编码?如果我还想流式传输数据,是否应该使用 2 个写入器?

谢谢

确保在 input.append(sampleBuffer)

之前调用 AVAssetWriter.startWriting()