Inconsistent FPS (20 FPS Issue) While Recording Video Using AVCaptureSession.

Hi,

I am recording video using my app. And setting up fps also using below code. But sometime video is being recorded using 20 FPS. Can someone please let me know what I am doing wrong?

private func eightBitVariantOfFormat() -> AVCaptureDevice.Format? {
        let activeFormat = self.videoDeviceInput.device.activeFormat
        let fpsToBeSupported: Int = 60
        debugPrint("fpsToBeSupported - \(fpsToBeSupported)" as AnyObject)
        
        let allSupportedFormats = self.videoDeviceInput.device.formats
        debugPrint("all formats - \(allSupportedFormats)" as AnyObject)
        
        let activeDimensions = CMVideoFormatDescriptionGetDimensions(activeFormat.formatDescription)
        debugPrint("activeDimensions - \(activeDimensions)" as AnyObject)
        
        let filterBasedOnDimensions = allSupportedFormats.filter({ (CMVideoFormatDescriptionGetDimensions($0.formatDescription).width == activeDimensions.width) && (CMVideoFormatDescriptionGetDimensions($0.formatDescription).height == activeDimensions.height) })
        
        if filterBasedOnDimensions.isEmpty {
            // Dimension not found. Required format not found to handle.
            debugPrint("Dimension not found" as AnyObject)
            return activeFormat
        }
        
        debugPrint("filterBasedOnDimensions - \(filterBasedOnDimensions)" as AnyObject)
        
        let filterBasedOnMaxFrameRate = filterBasedOnDimensions.compactMap({ format in
            let videoSupportedFrameRateRanges = format.videoSupportedFrameRateRanges
            if !videoSupportedFrameRateRanges.isEmpty {
                let contains = videoSupportedFrameRateRanges.contains(where: { Int($0.maxFrameRate) >= fpsToBeSupported })
                if contains {
                    return format
                } else {
                    return nil
                }
            } else {
                return nil
            }
        })
        
        debugPrint("allFormatsToBeSupported - \(filterBasedOnMaxFrameRate)" as AnyObject)
        
        guard !filterBasedOnMaxFrameRate.isEmpty else {
            debugPrint("Taking default active format as nothing found when filtered using desired FPS" as AnyObject)
            return activeFormat
        }
        
        var formatToBeUsed: AVCaptureDevice.Format!
        if let four_two_zero_v = filterBasedOnMaxFrameRate.first(where: { CMFormatDescriptionGetMediaSubType($0.formatDescription) == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange}) {
            // 'vide'/'420v'
            formatToBeUsed = four_two_zero_v
        } else {
            // Take the first one from above array.
            formatToBeUsed = filterBasedOnMaxFrameRate.first
        }
        
        do {
            try self.videoDeviceInput.device.lockForConfiguration()
            self.videoDeviceInput.device.activeFormat = formatToBeUsed
            self.videoDeviceInput.device.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: Int32(fpsToBeSupported))
            self.videoDeviceInput.device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: Int32(fpsToBeSupported))
            if videoDeviceInput.device.isFocusModeSupported(.continuousAutoFocus) {
                self.videoDeviceInput.device.focusMode = AVCaptureDevice.FocusMode.continuousAutoFocus
            }
            self.videoDeviceInput.device.unlockForConfiguration()
        } catch let error {
            debugPrint("\(error)" as AnyObject)
        }
        return formatToBeUsed
    }

Hello @Anmol_Maheshwari_DView,

A common cause of recording at a lower frame rate than the capture frame rate specified is that the app is doing too much processing to be able to record at the capture frame rate.

Can you provide more details about your capture pipeline?

-- Greg

Inconsistent FPS (20 FPS Issue) While Recording Video Using AVCaptureSession.
 
 
Q