iOS对视频帧数的设置
这段时间一直在研究视频获取方面的知识,查阅一些资料,也没看到一些比较整合的文章,所以把我最近研究的东西整合下,希望对有需要的人提供一些帮助。对于设置帧数方法:官网上已经给出
AVCaptureDeviceFormat *bestFormat = nil;
AVFrameRateRange *bestFrameRateRange = nil;
for ( AVCaptureDeviceFormat *format in [devicee formats] ) {
for ( AVFrameRateRange *range in format.videoSupportedFrameRateRanges ) {
if ( range.maxFrameRate > bestFrameRateRange.maxFrameRate ) {
bestFormat = format;
bestFrameRateRange = range;
}
}
}
if ( bestFormat) {
if ( [devicee lockForConfiguration:NULL] == YES ) {
devicee.activeFormat = bestFormat;
devicee.activeVideoMinFrameDuration = CMTimeMake(1,fps_);
devicee.activeVideoMaxFrameDuration = CMTimeMake(1,fps_);
[devicee unlockForConfiguration];
}
}
这里需要注意的是不同的format所支持的FPS范围是不同的。我把所有的format支持范围打印了下(以下皆以iPhone SE样机作为参数提取):
前置:

由此可见,前置摄像头所能获取的FPS最高是30
后置:

对于不同的CaptureSessionPreset,可设置的FPS范围是不同的,720的最高可以设置到240FPS,1080的最高可设置到120FPS(不同的机型可能有差异)设置CaptureSessionPreset方法是:[session setSessionPreset:AVCaptureSessionPreset1280x720];
如果设置不了相应的FPS,应该是你手机摄像头没有设置这么高的分辨率,可以去设置---->照片与相机---->录制视频下面看看,是不是最高支持30fps,修改下再试试。
当然也有特例,在回调方法captureOutput:didOutputSampleBuffer:fromConnection:中,如果对CMSampleBufferRef进行繁重的业务操作,就会造成丢包现象,帧数会自动下调;这是官方的说法:If multiple sample buffers reference such pools of memory for too long,inputs will no longer be able to copy new samples into memory and those samples will be dropped.
如果有异议或者疑问,欢迎留言,共同研究。