【发布时间】:2014-07-01 16:11:54
【问题描述】:
我需要降低来自 iPhone 摄像头的视频流的帧速率。
这是我使用的代码:
[...]
_captureSession = [[AVCaptureSession alloc] init];
if( [_captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) {
[_captureSession setSessionPreset:AVCaptureSessionPreset640x480];
} else {
NSLog(@"Could not initialize 640x480 video stream");
exit(EXIT_FAILURE);
}
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
if ([videoCaptureDevice lockForConfiguration:&error]) {
AVCaptureDeviceFormat *format = [videoCaptureDevice activeFormat];
NSArray *supportedFPS = format.videoSupportedFrameRateRanges;
CMTime maxFrameDuration;
float maxSeconds = FLT_MIN;
for(AVFrameRateRange *fpsRange in supportedFPS) {
float currentSeconds = CMTimeGetSeconds(fpsRange.maxFrameDuration);
if(currentSeconds > maxSeconds) maxFrameDuration =fpsRange.maxFrameDuration;
NSLog(@"Supported range. Max frame rate: %f (min frame duration %f) -
Min frame rate: %f (max frame duration %f)",
fpsRange.maxFrameRate,
CMTimeGetSeconds(fpsRange.minFrameDuration),
fpsRange.minFrameRate,
CMTimeGetSeconds(fpsRange.maxFrameDuration));
} // for
NSLog(@"Setting min/max duration for frames to %f", CMTimeGetSeconds(maxFrameDuration));
[videoCaptureDevice setActiveVideoMaxFrameDuration:maxFrameDuration];
[videoCaptureDevice setActiveVideoMinFrameDuration:maxFrameDuration];
[...]
} // lockForConf
这个想法是
- 获取当前格式,设置视频流大小后;
- 获取具有可用帧速率范围的数组
- 找到帧持续时间最长的那个
- 将捕获设备的最小/最大帧持续时间设置为最大值。
在 for 循环中打印的唯一日志消息是:
Supported range. Max frame rate: 30.000000 (min frame duration 0.033333) -
Min frame rate: 2.000000 (max frame duration 0.500000)
尽管似乎可以将 FPS 设置为每秒 2 帧,但相机仍然每秒输出大量帧。我不知道如何获取视频流的 FPS,但相机绝对不是每秒只服务 2 个。
你有什么建议吗?
【问题讨论】:
-
您的问题解决了吗?
-
我无法解决。