Skip to content

Instantly share code, notes, and snippets.

@abinhho
Created March 29, 2022 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abinhho/68cbfb9eae821d252f9104fce2bd6c08 to your computer and use it in GitHub Desktop.
Save abinhho/68cbfb9eae821d252f9104fce2bd6c08 to your computer and use it in GitHub Desktop.
func setMaxFps(device: AVCaptureDevice) {
var minFPS = 0.0
var maxFPS = 0.0
var maxWidth:Int32 = 0
var selectedFormat:AVCaptureDevice.Format? = nil
for format in device.formats {
for range in format.videoSupportedFrameRateRanges {
let desc = format.formatDescription
let dimentions = CMVideoFormatDescriptionGetDimensions(desc)
if(minFPS <= range.minFrameRate && maxFPS <= range.maxFrameRate && maxWidth <= dimentions.width){
minFPS = range.minFrameRate
maxFPS = range.maxFrameRate
maxWidth = dimentions.width
selectedFormat = format
}
}
}
do {
try device.lockForConfiguration()
device.activeFormat = selectedFormat!
device.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: Int32(minFPS))
// Set lowest frame rate to reduce memory
device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: Int32(minFPS))
device.unlockForConfiguration()
} catch {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment