Failed to write video with AVAssetWriter

299 Views Asked by At

I am using AVAssetWriter to save screen recoding to mp4 (Withd H.264 video and aac audio encoding). Everything works perfectly as expected but some users complained about the 0KB video size issue, that is no data is being written to the output file. By trying over and over again I was able to reproduce this issue. It happened after a few successful writes (sometimes after 2 sometimes after 5 and on some machines it never happens). I compared the successful flow and failure looked at the logs for it. The only thing which I found out was this

CMIO_Unit_Converter_Audio.cpp:588:RebuildAudioConverter AudioConverterSetProperty() failed (1886547824)

Where

kAudioFormatUnsupportedPropertyError = 1886547824

Which leads me to check the audio format in case of failure but the Audio format for AVAssetWriter was perfectly fine. I am setting up audio writer input as AVAssetWriter Inputs

[<AVAssetWriterInput: 0x600002047950, mediaType = vide, outputSettings = {
    AVVideoCodecKey = avc1;
    AVVideoHeightKey = 840;
    AVVideoWidthKey = 1360;
}>, <AVAssetWriterInput: 0x60000205e740, mediaType = soun, outputSettings = {
    AVFormatIDKey = 1633772320;
    AVNumberOfChannelsKey = 2;
    AVSampleRateKey = 44100;
}>]

My code to create AVAssetWriterInput for video and audio is as follow Audio AVAssetWriterInput

settings = [
    AVFormatIDKey : kAudioFormatMPEG4AAC,
    AVNumberOfChannelsKey : ch,
    AVSampleRateKey : rate,
]
audioInput = AVAssetWriterInput(mediaType: .audio, outputSettings: settings)

Video AVAssetWriterInput

var settings: [String : Any] = [
    AVVideoWidthKey  : cx,
    AVVideoHeightKey : cy,
    AVVideoCodecKey  : AVVideoCodecType.h264,
]
videoInput = AVAssetWriterInput(mediaType: .video, outputSettings: settings)

I noticed error in the log appears after AVCaptureSession is being started and before AVCaptureVideoDataOutputSampleBufferDelegate's captureOutput. I logged AVCaptureSession inputs and output as well.

AVCaptureSession Inputs

[<AVCaptureScreenInput: 0x6000022e18e0>, <AVCaptureDeviceInput: 0x6000022e0a00 [Built-in Microphone]>]

AVCaptureSession Outputs

[<AVCaptureAudioDataOutput: 0x6000022e1f40>, <AVCaptureVideoDataOutput: 0x60000228b7a0>]

But again I don't see any problem here. I have been trying to figure this out for a good few days but failed to do so. I don't have any idea what triggers it and even when everything is properly set, it fails to write any output. This is only happening with video+audio if I write video only it works properly.

0

There are 0 best solutions below