ios,如何降低摄像机视频流的FPS

时间:2014-07-01 16:11:54

标签: ios video

我需要降低来自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次。

你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用ACCaptureDevice的activeVideoMinFrameDuration和activeVideoMaxFrameDuration来更新会话的帧。

像:

[captureDevice setActiveVideoMaxFrameDuration:CMTimeMake(2, 1)];
[captureDevice setActiveVideoMinFrameDuration:CMTimeMake(2, 1)];

您还可以参考:https://developer.apple.com/documentation/avfoundation/avcapturedevice/1389290-activevideominframeduration