Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS AVCaptureSession - How to get/set the number of frames per second recorded?

I'm new to AVCaptureSession and wish to better understand how to work with it. So I managed capturing the video stream as separated CIImages and convert them to UIImages. Now I wish to be able to get the number of Frames Per Second captured and preferably to be able to set it.

Any idea how to do that?

like image 982
Ohad Regev Avatar asked Dec 26 '12 10:12

Ohad Regev


4 Answers

AVCaptureConnection's videoMinFrameDuration is deprecated.

You can use AVCaptureDevice properties to detect supported video frame rate ranges and can assign minimum and maximum frame rates using properties.

device.activeFormat.videoSupportedFrameRateRanges return all video frame rates ranges supported by device.

device.activeVideoMinFrameDuration and device.activeVideoMaxFrameDuration can be used for specifying frame durations.

like image 61
MouseCrasher Avatar answered Oct 04 '22 21:10

MouseCrasher


You could use AVCaptureConnection's videoMinFrameDuration accessor to set the value.

See the AVCaptureConnection documentation

Consider output be AVCaptureVideoDataOutput object.

AVCaptureConnection *conn = [output connectionWithMediaType:AVMediaTypeVideo];

if (conn.isVideoMinFrameDurationSupported)
    conn.videoMinFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
if (conn.isVideoMaxFrameDurationSupported)
    conn.videoMaxFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);

More info, see my answer in this SO question

like image 28
Ilanchezhian Avatar answered Oct 04 '22 23:10

Ilanchezhian


To set the capture session frame rate, you have to set it on the device using device.activeVideoMinFrameDuration and device.activeVideoMaxFrameDuration (if necessary).

In Swift 4 you can do something like this:

extension AVCaptureDevice {
    func set(frameRate: Double) {
    guard let range = activeFormat.videoSupportedFrameRateRanges.first,
        range.minFrameRate...range.maxFrameRate ~= frameRate
        else {
            print("Requested FPS is not supported by the device's activeFormat !")
            return
    }

    do { try lockForConfiguration()
        activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate))
        activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate))
        unlockForConfiguration()
    } catch {
        print("LockForConfiguration failed with error: \(error.localizedDescription)")
    }
  }
}

And call it

device.set(frameRate: 60)
like image 34
Macistador Avatar answered Oct 04 '22 23:10

Macistador


Do it like this

if let frameSupportRange = currentCamera.activeFormat.videoSupportedFrameRateRanges.first {
    captureSession.beginConfiguration()
    // currentCamera.activeVideoMinFrameDuration = CMTimeMake(1, Int32(frameSupportRange.maxFrameRate))
    currentCamera.activeVideoMinFrameDuration = CMTimeMake(1, YOUR_FPS_RATE)
    captureSession.commitConfiguration()
}
like image 37
Changnam Hong Avatar answered Oct 04 '22 21:10

Changnam Hong