ActiveFormat tells my that my iPhone 12 Pro Max only supports 30 frames per second

Hi,

I've set up a captureSession and am now trying to set the framerate to 60. I am using an iPhone 12 Pro Max.

I am trying to set the frame rate with: videoDevice?.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: 60), but printing my .activeFormat tells me my iPhone only supports 30 fps.

What am I doing wrong?

Thanks
Answered by konsti_q in 669342022
Update and Answer: The activeFormat is just the currently active format. Printing .formats can provide you all possible formats. If you want to set your frames per second to 60, do so by:

Code Block swift
// Instantiate the video device: wide angle camera, back position
        let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
                                                  for: .video,
                                                  position: .back)
        
        
        // Set the frame rate to 60, as expected by the machine learning model
        try! videoDevice?.lockForConfiguration()
        
        videoDevice?.activeFormat = (videoDevice?.formats[30])!
        videoDevice?.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: 60)
        videoDevice?.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 60)
        
        videoDevice?.unlockForConfiguration()
        
        // print(videoDevice?.activeFormat)

Thanks to Rhythmic Fistman for helping me on this question. https://stackoverflow.com/users/22147/rhythmic-fistman
Accepted Answer
Update and Answer: The activeFormat is just the currently active format. Printing .formats can provide you all possible formats. If you want to set your frames per second to 60, do so by:

Code Block swift
// Instantiate the video device: wide angle camera, back position
        let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
                                                  for: .video,
                                                  position: .back)
        
        
        // Set the frame rate to 60, as expected by the machine learning model
        try! videoDevice?.lockForConfiguration()
        
        videoDevice?.activeFormat = (videoDevice?.formats[30])!
        videoDevice?.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: 60)
        videoDevice?.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 60)
        
        videoDevice?.unlockForConfiguration()
        
        // print(videoDevice?.activeFormat)

Thanks to Rhythmic Fistman for helping me on this question. https://stackoverflow.com/users/22147/rhythmic-fistman

For others encountering this issue, the more generic way to solve this problem is to choose the format that satisfies your desired conditions.

For example, if you wanted to choose a format in 720p and compatible with 60 fps, you could do the following:

    let frameDuration = CMTimeMake(value: 1, timescale: 60)

    // Get format that is 720p resolution and is compatible with 60 fps.
    let format = device.formats.first {
      let dimensions = $0.formatDescription.dimensions
      let hasCompatibleFrameRateRange = $0.videoSupportedFrameRateRanges.contains {
        $0.minFrameRate <= 60 && 60 <= $0.maxFrameRate
      }
      return dimensions.width == 1280 && dimensions.height == 720 && hasCompatibleFrameRateRange
    }

    if let format = format {
      device.activeFormat = format
      device.activeVideoMinFrameDuration = frameDuration
      device.activeVideoMaxFrameDuration = frameDuration
    }
ActiveFormat tells my that my iPhone 12 Pro Max only supports 30 frames per second
 
 
Q