Skip to content

Instantly share code, notes, and snippets.

@Limon-O-O
Created October 31, 2016 07:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Limon-O-O/1fe698e2e0d002bcd4adcc8d245276d2 to your computer and use it in GitHub Desktop.
Save Limon-O-O/1fe698e2e0d002bcd4adcc8d245276d2 to your computer and use it in GitHub Desktop.
Control video frame rate with AVFoundation
extension AVCaptureDevice {
/// http://stackoverflow.com/questions/21612191/set-a-custom-avframeraterange-for-an-avcapturesession#27566730
func configureDesiredFrameRate(_ desiredFrameRate: Int) {
var isFPSSupported = false
do {
if let videoSupportedFrameRateRanges = activeFormat.videoSupportedFrameRateRanges as? [AVFrameRateRange] {
for range in videoSupportedFrameRateRanges {
if (range.maxFrameRate >= Double(desiredFrameRate) && range.minFrameRate <= Double(desiredFrameRate)) {
isFPSSupported = true
break
}
}
}
if isFPSSupported {
try lockForConfiguration()
activeVideoMaxFrameDuration = CMTimeMake(1, Int32(desiredFrameRate))
activeVideoMinFrameDuration = CMTimeMake(1, Int32(desiredFrameRate))
unlockForConfiguration()
}
} catch {
print("lockForConfiguration error: \(error.localizedDescription)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment