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

1.6k views Asked by At

I'm trying to get my head around this. I want to record live video with Swift on OSX, using the AVAssetWriterInput.

I created an instance of AVAssetWriterInput named input, and I want to append the data to the input, and then write it to file.

I set expectsMediaDataInRealTime to true like it said in the docs. When I run the app I get this error:

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

I use captureOutput to get the data from the camera.

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

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

Does the AVAssetWriterInput will handle the encoding of the video? Should I use 2 writers if I want also stream the data?

Thanks

1

There are 1 answers

0
liugw On BEST ANSWER

Make sure call AVAssetWriter.startWriting() before input.append(sampleBuffer)