我想降低AVCaptureSession的帧速率,因为我的视频处理目前无法应对30fps。我已经找到了类似的问题,但到目前为止,提出的答案对我来说并不奏效。顺便说一句,我正在使用内置的macBook相机。
我试过了:
if ([_camera lockForConfiguration:nil])
{
_camera.activeVideoMinFrameDuration = CMTimeMake(1, 20);
_camera.activeVideoMaxFrameDuration = CMTimeMake(1, 20);
[_camera unlockForConfiguration];
}
但是这不会编译并在第三行引发Thread1:signal SIGABRT
错误。我怀疑这是因为该设备可能不支持20 fps。
论坛的另一个尝试是:
- (void)attemptToConfigureFPS
{
NSError *error;
if (![_camera lockForConfiguration:&error]) {
NSLog(@"Could not lock device %@ for configuration: %@", self, error);
return;
}
AVCaptureDeviceFormat *format = _camera.activeFormat;
double epsilon = 0.00000001;
int desiredFrameRate = 10;
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
NSLog(@"Pre Minimum frame rate: %f Max = %f", range.minFrameRate, range.maxFrameRate);
if (range.minFrameRate <= (desiredFrameRate + epsilon) &&
range.maxFrameRate >= (desiredFrameRate - epsilon)) {
NSLog(@"Setting Frame Rate.");
_camera.activeVideoMaxFrameDuration = (CMTime){
.value = 1,
.timescale = desiredFrameRate,
.flags = kCMTimeFlags_Valid,
.epoch = 0,
};
_camera.activeVideoMinFrameDuration = (CMTime){
.value = 1,
.timescale = desiredFrameRate,
.flags = kCMTimeFlags_Valid,
.epoch = 0,
};
self.activeVideoMinFrameDuration = self.activeVideoMaxFrameDuration;
NSLog(@"Post Minimum frame rate: %f Max = %f", range.minFrameRate, range.maxFrameRate);
break;
}
}
[_camera unlockForConfiguration];
// Audit the changes
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
NSLog(@"Post Minimum frame rate: %f Max = %f", range.minFrameRate, range.maxFrameRate);
}
}
这次代码编译,但是调试器的检查显示,
if (range.minFrameRate <= (desiredFrameRate + epsilon) &&
range.maxFrameRate >= (desiredFrameRate - epsilon))
评估为NO
,因此永远不会执行应调整帧速率的其余代码。这对我来说似乎很奇怪,非常感谢你的帮助。非常感谢你提前!