Created
October 31, 2016 07:48
-
-
Save Limon-O-O/1fe698e2e0d002bcd4adcc8d245276d2 to your computer and use it in GitHub Desktop.
Control video frame rate with AVFoundation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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