Grzegorz Aperliński
Grzegorz Aperliński

Reputation: 918

How to save encoded CMSampleBuffer samples to mp4 file on iOS

I'm using the VideoToolbox framework to retrieve data from AVCaptureSession and encode it to h264 and acc.

I'm at a point where I:

  1. obtain data using the delegate method func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
  2. convert video CMSampleBuffer to CVImageBuffer and encode it to h264 using VTCompressionSession which passes the encoded frame in VTCompressionOutputCallback
  3. encode audio CMSampleBuffer to acc and pass it as UnsafeMutableAudioBufferListPointer.

I'm now blocked at a point where I want to correctly convert these streams to bytes of Data to write to a url through FileHandle. How should I go about this?

I need to have access to encoded streams before the actual file is finalized so I can't use (I think) AVAssetWriter to save unencoded streams to an mp4 file because from what I can tell I need to pass unencoded samples to AVAssetWriter for it to work properly. Or am I mistaken?

Upvotes: 4

Views: 1938

Answers (1)

Grzegorz Aperliński
Grzegorz Aperliński

Reputation: 918

Turns out you can save encoded CMSampleBuffer using AVAssetWriter but you need to pass nil as outputSettings in AVAssetWriterInput to prevent re-encoding data:

let input AVAssetWriterInput(mediaType: mediaType, outputSettings: nil, sourceFormatHint: sourceFormatHint)

Upvotes: 1

Related Questions