// Video settings input import AVFoundation func createAudioInput(sampleBuffer: CMSampleBuffer) -> AVAssetWriterInput { let audioSettings = [ AVFormatIDKey: kAudioFormatMPEG4AAC, AVSampleRateKey: 48000, AVEncoderBitRatePerChannelKey: 64000, AVNumberOfChannelsKey: 2 ] as [String : Any] let audioInput = AVAssetWriterInput(mediaType: .audio, outputSettings: audioSettings, sourceFormatHint: sampleBuffer.formatDescription) audioInput.expectsMediaDataInRealTime = false return audioInput } func createVideoInput(videoBuffers: CVPixelBuffer) -> AVAssetWriterInput { let videoSettings = [ AVVideoCodecKey: AVVideoCodecType.h264, AVVideoWidthKey: CVPixelBufferGetWidth(videoBuffers), AVVideoHeightKey: CVPixelBufferGetHeight(videoBuffers) ] as [String: Any] let videoInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoSettings) videoInput.expectsMediaDataInRealTime = false return videoInput } func createPixelBufferAdaptor(videoInput: AVAssetWriterInput) -> AVAssetWriterInputPixelBufferAdaptor { let attributes = [ kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32ARGB, kCVPixelBufferWidthKey: videoInput.outputSettings?[AVVideoWidthKey] as Any, kCVPixelBufferHeightKey: videoInput.outputSettings?[AVVideoHeightKey] as Any ] as [String: Any] return AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput, sourcePixelBufferAttributes: attributes) }