我已经设置了一个 captureSession,现在正在尝试将帧率设置为 60。我使用的是 iPhone 12 Pro Max。
我试图设置帧速率:
videoDevice?.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: 60)
但是,打印我的.activeFormat 告诉我我的 iPhone 只支持 30 fps。
我需要 60 fps 来匹配我的机器学习模型的帧速率。
配置:
建造宽 anglecamera,
视频,
背部位置,
景观正确方向。
我在这个枚举中没有任何相机允许我超过 30 fps。因此,我创建了 videoDevice 对象:
let videoDevice = CaptureDevice.default(.builtInWideAngleCamera,
for: .video,
position: .back)
我做错了什么?
谢谢

videoDevice.activeFormat
只是当前格式。videoDevice.formats
包含所有可能的格式。
矿山报告许多能够 60fps 的格式,例如
<CaptureDeviceFormat: 0x28337d7c0 'vide'/'420f' 1280x 720, { 1- 60 fps}, HRSI:2112x1188, fov:70.291, binned, supports vis, max zoom:24.00 (upscales @1.50), AF System:1, ISO:33.0-3168.0, SS:0.000015-1.000000, supports wide color, supports multicam>
...
所以选择最适合你的格式,然后让你的activeFormat
并设置所需的帧持续时间如下:
try! videoDevice.lockForConfiguration()
videoDevice.activeFormat = my60FPSFormat
videoDevice.activeVideoMinFrameDuration = CMTime(value: 1, timescale: 60)
videoDevice.activeVideoMaxFrameDuration = CMTime(value: 1, timescale: 60)
videoDevice.unlockForConfiguration()

谢谢,这回答了我的问题!:)
对于任何人仍然想知道下面是我使用的代码:
// Instantiate the video device: wide angle camera, back position
let videoDevice = CaptureDevice.default(.builtInWideAngleCamera,
for: .video,
position: .back)
// Set the frame rate to 60, as expected by the model
try! videoDevice?.lockForConfiguration()
videoDevice?.activeFormat = (videoDevice?.formats[30])!
videoDevice?.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: 60)
videoDevice?.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 60)
videoDevice?.unlockForConfiguration()
// Debug only
// print(videoDevice?.activeFormat)
但是,请确保添加一些错误处理:D
再次感谢。

虽然其他答案提供了解决方案的大纲,但当我需要给定帧速率的最佳分辨率时,对我有用的是,如果你想进一步限制某些帧分辨率,你可以添加一个过滤器方法来确保尺寸的阈值。
extension CaptureDevice {
func set(frameRate: Double) {
do { try lockForConfiguration()
activeFormat = formats.sorted(by: { f1, f2 in
f1.formatDescription.dimensions.height > f2.formatDescription.dimensions.height && f1.formatDescription.dimensions.width > f2.formatDescription.dimensions.width
}).first(where: { format in
format.videoSupportedFrameRateRanges.contains { range in
range.maxFrameRate == frameRate
}
}) ?? activeFormat
guard let range = activeFormat.videoSupportedFrameRateRanges.first,
range.minFrameRate...range.maxFrameRate ~= frameRate
else {
print("Requested FPS is not supported by the device's activeFormat !")
return
}
activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate))
activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate))
unlockForConfiguration()
} catch {
print("LockForConfiguration failed with error: \(error.localizedDescription)")
}
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(42条)