5

I want to see a video preview on my iPhone's display with 240 fps.
My Code (simplyfied) is like this (following): A session is made, the camera is activated and the actual video preview is shown on the display.

var session: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?



override func viewDidLoad() {
    super.viewDidLoad()
    super.viewWillAppear(true)

    session = AVCaptureSession()

    let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    var error: NSError?
    var input: AVCaptureDeviceInput!
    do {
        input = try AVCaptureDeviceInput(device: backCamera)

    } catch let errorgesehen as NSError {
        error = errorgesehen
        input = nil
        print(error!.localizedDescription)
    }

    configureCameraForHighestFrameRate(device: backCamera!)


    if error == nil && session!.canAddInput(input) {
        session!.addInput(input)
    }

    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
    videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
    videoPreviewLayer!.frame = CGRect(x: 0.0, y: 0.0, width: view.bounds.size.width, height: view.bounds.size.height)
    videoPreviewLayer!.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 1).cgColor
    view.layer.addSublayer(videoPreviewLayer!)


    session?.startRunning()

    UIApplication.shared.isIdleTimerDisabled = true
}

Now I wanted that this video preview is shown in 240fps (60 or 120 would be ok too, but 240 is the best). To solve this problem, I have used this: https://developer.apple.com/documentation/avfoundation/avcapturedevice (I have transformed it to Swift). The function configureCameraForHighestFrameRate has the following code:

func configureCameraForHighestFrameRate(device: AVCaptureDevice) {
    var bestFormat: AVCaptureDeviceFormat? = nil
    var bestFrameRateRange: AVFrameRateRange? = nil
    for formatf in device.formats {
        var format = formatf as! AVCaptureDeviceFormat
        print(format)
        for rangef in format.videoSupportedFrameRateRanges {
            var range = rangef as! AVFrameRateRange
            print(range)
            if (bestFrameRateRange == nil) {
                bestFormat = format
                bestFrameRateRange = range
            } else if range.maxFrameRate > bestFrameRateRange!.maxFrameRate {
                bestFormat = format
                bestFrameRateRange = range
            }
        }
    }

    if (bestFormat == nil) {
        print("Es gibt keine Formate, die Apokalypse ist ausgebrochen.")
        return;
    } else if (bestFrameRateRange == nil) {
        print("Es gibt keine Bilder, die Apokalypse ist ausgebrochen.")
        return;
    }

    let Richtig = bestFormat!
    let fps = bestFrameRateRange!
    do {
        try device.lockForConfiguration()
    }
    catch let error as NSError {
        print(error.description)
    }
    device.activeFormat = Richtig
    device.activeVideoMinFrameDuration = fps.minFrameDuration
    device.activeVideoMaxFrameDuration = fps.minFrameDuration
    device.unlockForConfiguration()
}

This code looks for the AVCaptureDeviceFormat in which the fps number is maximal (=240). The actual format is set to this Format ans the fps number to the maximum (=240).
But I have the problem that the video output still contains only 30 frames per secons (if I turn the iPhone very quickly, I will recognize it).
Additionally, there are several formats in which the maximal fps are 240. This code only choses the first of them, but I ask me whether there is a differnce and which to choose to get the best output. Can anybody say a difference of the several formts or which of them is the format, the "Camera" App uses in the Slow Motion tool? Thank you very much :)

Korne127
  • 168
  • 14
  • just see [Capture with high fps mode](https://stackoverflow.com/questions/58968764/60-fps-video-recording-problem-even-it-is-capable-swift/62258152#62258152) – Frank Jun 09 '20 at 09:07

1 Answers1

2

You should start your AVCaptureSession before setting format configurations to AVCaptureDevice

Eugene Alexeev
  • 1,152
  • 12
  • 32