我是新来AVCaptureSession并希望更好地了解如何使用它。所以我设法捕获视频流作为分离CIImages并将其转换为UIImages。现在,我希望能够获得第二捕捉每秒帧数的数量,最好是能设置。
任何想法如何做到这一点?
你可以使用AVCaptureConnection
的videoMinFrameDuration
访问设定值。
见AVCaptureConnection documentation
考虑output
是AVCaptureVideoDataOutput
对象。
AVCaptureConnection *conn = [output connectionWithMediaType:AVMediaTypeVideo];
if (conn.isVideoMinFrameDurationSupported)
conn.videoMinFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
if (conn.isVideoMaxFrameDurationSupported)
conn.videoMaxFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
更多信息,请参阅我的答案在此SO question
AVCaptureConnection's videoMinFrameDuration
已被弃用。
您可以使用AVCaptureDevice
性能来检测所支持的视频帧速率范围和可分配的最小和最大帧速率使用性能。
device.activeFormat.videoSupportedFrameRateRanges
返回由设备支持的所有视频帧速率范围。
device.activeVideoMinFrameDuration
和device.activeVideoMaxFrameDuration
可以用于指定帧持续时间。
要设置捕获会话帧速率,您必须将其设置使用device.activeVideoMinFrameDuration和device.activeVideoMaxFrameDuration(如有必要)的设备上。
在斯威夫特4,你可以这样做:
extension AVCaptureDevice {
func set(frameRate: Double) {
guard let range = activeFormat.videoSupportedFrameRateRanges.first,
range.minFrameRate...range.maxFrameRate ~= frameRate
else {
print("Requested FPS is not supported by the device's activeFormat !")
return
}
do { try lockForConfiguration()
activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate))
activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate))
unlockForConfiguration()
} catch {
print("LockForConfiguration failed with error: \(error.localizedDescription)")
}
}
}
并调用它
device.set(frameRate: 60)
像这样做
if let frameSupportRange = currentCamera.activeFormat.videoSupportedFrameRateRanges.first {
captureSession.beginConfiguration()
// currentCamera.activeVideoMinFrameDuration = CMTimeMake(1, Int32(frameSupportRange.maxFrameRate))
currentCamera.activeVideoMinFrameDuration = CMTimeMake(1, YOUR_FPS_RATE)
captureSession.commitConfiguration()
}