Hey all!
I'm trying to build a Camera app that records Video and Audio buffers (AVCaptureVideoDataOutput
and AVCaptureAudioDataOutput
) to an mp4/mov file using AVAssetWriter
.
When creating the Recording Session, I noticed that it blocks for around 5-7 seconds before starting the recording, so I dug deeper to find out why.
This is how I create my AVAssetWriter:
let assetWriter = try AVAssetWriter(outputURL: tempURL, fileType: .mov)
let videoWriter = self.createVideoWriter(...)
assetWriter.add(videoWriter)
let audioWriter = self.createAudioWriter(...)
assetWriter.add(audioWriter)
assetWriter.startWriting()
There's two slow parts here in that code:
-
The
createAudioWriter(...)
function takes ages!This is how I create the audio
AVAssetWriterInput
:// audioOutput is my AVCaptureAudioDataOutput, audioInput is the microphone let settings = audioOutput.recommendedAudioSettingsForAssetWriter(writingTo: .mov) let format = audioInput.device.activeFormat.formatDescription let audioWriter = AVAssetWriterInput(mediaType: .audio, outputSettings: settings, sourceFormatHint: format) audioWriter.expectsMediaDataInRealTime = true
The above code takes up to 3000ms on an iPhone 11 Pro!
When I remove the recommended settings and just pass
nil
asoutputSettings
:audioWriter = AVAssetWriterInput(mediaType: .audio, outputSettings: nil) audioWriter.expectsMediaDataInRealTime = true
...It initializes almost instantly - something like 30 to 50ms.
-
Starting the
AVAssetWriter
takes ages!Calling this method:
assetWriter.startWriting()
...takes takes 3000 to 5000ms on my iPhone 11 Pro!
Does anyone have any ideas why this is so slow? Am I doing something wrong?
It feels like passing nil
as the outputSettings
is not a good idea, and recommendedAudioSettingsForAssetWriter
should be the way to go, but 3 seconds initialization time is not acceptable.
Here's the full code: RecordingSession.swift
from react-native-vision-camera. This gets called from here.
I'd appreciate any help, thanks!