iphoneavfoundationframe-rateavcapturesessionapple-vision

iPhone capture session: Set custom frame-rate


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)

However, printing my .activeFormat tells me my iPhone only supports 30 fps.

I need 60 fps to match the frame rate of my machine learning model.

Configuration:

I do not have any cameras in this enum that allow me more than 30 fps. I create the videoDevice object therefore with:

let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,                                           
                                          for: .video,
                                          position: .back)

What am I doing wrong?

Thanks


Solution

  • Thanks, this answered my question!:)

    For anyone still wondering below is the code that I used:

        // 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 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()
    
        // Debug only
        // print(videoDevice?.activeFormat)
    

    However, make sure to add some error handling:D

    Thanks, again.