I am trying to record 60 fps video by modifying AVCAM application which can be found at:
https://github.com/Lax/Learn-iOS-Swift-by-Examples/tree/master/AVCam/Swift/AVCam
Hence, it is normally getting 2-30 fps with my phone (iPhone X) and I tried to change the format it captures video.
'''
do {
// Choose the back dual camera if available, otherwise default to a wide angle camera.
if let dualCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) {
defaultVideoDevice = dualCameraDevice
do{
if let formats = defaultVideoDevice?.formats {
for format in formats{
let formatDesc = format.formatDescription
print(format.videoSupportedFrameRateRanges)
let frameRate = format.videoSupportedFrameRateRanges.first
print(format.formatDescription)
if let frameRate = frameRate, frameRate.maxFrameRate == 60.0 {
try defaultVideoDevice?.lockForConfiguration()
print(frameRate.maxFrameRate) //here prints 60.0
defaultVideoDevice?.activeVideoMaxFrameDuration = CMTimeMake(1,60)
defaultVideoDevice?.activeVideoMinFrameDuration = CMTimeMake(1,60)
defaultVideoDevice?.unlockForConfiguration()
}
}
}
}
'''
Here at the line of 'defaultVideoDevice?.activeVideoMaxFrameDuration = CMTimeMake(1,60)' I am getting this error:
2019-11-21 09:23:50.225376+0300 AVCam[1250:667986] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[AVCaptureDevice setActiveVideoMaxFrameDuration:] Unsupported frame duration - use -activeFormat.videoSupportedFrameRateRanges to discover valid ranges'
Thanks in advance.
You should set a valid format to AVCaptureDevice
.
You can do this
// get device what you like
let device = xxxxx
// list all default formats for this device
for format in device.formats {
var founded = false
// check what you want and pick a perfect format.
let formatDesc = format.formatDescription
// mediaType / SubType
let mediaType = format.mediaSubType
// if your target is above(equal) iOS 13. use formatDesc.mediaSubType
let mediaSubType = CMFormatDescriptionGetMediaSubType(formatDesc)
// dimensions
// if your target is above(equal) iOS 13. use formatDesc.dimensions
let dimensions = CMVideoFormatDescriptionGetDimensions(formatDesc)
// fps
let ranges = format.videoSupportedFrameRateRanges.first
for supportedFPSRange in ranges {
if supportedFPSRange.maxFrameRate == 60 {
founded = true
}
}
// support Multi cam
let isMultiCamSupported = format.isMultiCamSupported
....
if founded {
// Set activeFormat for device! Your capture device is up and loaded.
do {
try device.lockForConfiguration()
device.activeFormat = format
device.unlockForConfiguration()
} catch {
// catch some locking error
}
// Don't forget break the loop.:p
break
}
}
Or you can use filter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.