iOS camera recording video with initial zoom results in black frames for the first second

Viewed 397

I have an application that records videos. I can start and stop recording, thus having various segments recording, much like TikTok. Recently, I implemented setting videoZoomFactor to AVCaptureDevice. It works normal while recording. But if I zoom before recording and press on record to start, the output video result's first frame is black. Also, if during the same AVCaptureSession I record with zoom, remove this recording and record again with zoom, the black frames do not appear. From this I conclude that calling startRecording on AVCaptureMovieFileOutput fixes something between first recording and second.

Can someone please advise me on solution? I am thinking of doing a workaround where I will remove first frame in case it was recorded with initial zoom, but I'd like to understand why this happens and have proper solution.

I have a pretty simple setup for camera recording. I was following steps in official guide from documentation. Here is the part of setting zoom factor.

self.sessionQueue.async {
        do {
            try device.lockForConfiguration()
            device.videoZoomFactor = factor
            device.unlockForConfiguration()
        } catch {
            print("\(error.localizedDescription)")
        }
    }
1 Answers

Finally, I figured out what was causing the problem. During the whole capture session setup, I wasn't adding stabilization to the output connection. So after adding file output to the session, I added that part and the video now is shot normally, and doesn't have black frames in the beginning. Here is the piece of code.

        self.captureSession.addOutput(self.videoFileOutput)
        if let connection = self.videoFileOutput.connection(with: AVMediaType.video) {
            if connection.isVideoStabilizationSupported {
                connection.preferredVideoStabilizationMode = .auto
            }
        }
Related