AVFileCaptureOutput:不以240 fps录制 [英] AVFileCaptureOutput: Not recording at 240 fps
问题描述
我似乎在将相机设置为以240 FPS录制时遇到问题,但是由于某种原因,输出文件仅为30 FPS.
I seem to be having a problem where I am setting the camera to record at 240 FPS, but for some reason the output file is only 30 FPS.
这是我用于设置相机的代码(首先实例化):
Here is my code for setting up the camera (this is instantiated first):
class HFRCamera {
public:
HFRCamera();
AVCaptureDeviceInput *camera;
AVCaptureDeviceInput *microphone;
AVCaptureDevice *videoCam;
AVCaptureDevice *audioInput;
AVCaptureSession *capSession;
void start();
void config();
void stop();
};
HFRCamera::HFRCamera() {
// Set up capture session and add video camera and microphone
this->capSession = [[AVCaptureSession alloc] init];
this->videoCam = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
this->config();
}
void HFRCamera::start() {
[this->capSession startRunning];
}
void HFRCamera::stop() {
[this->capSession stopRunning];
}
void HFRCamera::config() {
const CGFloat desiredFPS = 240;
AVCaptureDeviceFormat *selectedFormat = nil;
AVFrameRateRange *frameRateRange = nil;
int32_t maxWidth = 0;
for (AVCaptureDeviceFormat *format in [this->videoCam formats]) {
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
CMFormatDescriptionRef desc = format.formatDescription;
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(desc);
int32_t width = dimensions.width;
if (range.minFrameRate <= desiredFPS && desiredFPS <= range.maxFrameRate && width >= maxWidth) {
selectedFormat = format;
frameRateRange = range;
maxWidth = width;
}
}
}
if ([videoCam lockForConfiguration:nil]) {
std::cout << "HERE\n";
NSLog(@"selected format:%@", selectedFormat);
this->videoCam.activeFormat = selectedFormat;
this->videoCam.activeVideoMinFrameDuration = CMTimeMake(1, (int32_t)desiredFPS);
this->videoCam.activeVideoMaxFrameDuration = CMTimeMake(1, (int32_t)desiredFPS);
[this->videoCam unlockForConfiguration];
NSLog(@"%s AVCaptureDevice: %@", __PRETTY_FUNCTION__, selectedFormat);
}
if (this->videoCam) {
NSError *err;
this->camera = [AVCaptureDeviceInput deviceInputWithDevice:this->videoCam error:&err];
if (!err) {
if ([this->capSession canAddInput:this->camera])
[this->capSession addInput:this->camera];
else
NSLog(@"Could not add video input.");
} else
NSLog(@"Could not create video input");
} else {
NSLog(@"Could not create video capture device.");
}
this->audioInput = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *err = nil;
this->microphone = [AVCaptureDeviceInput deviceInputWithDevice:this->audioInput error:&err];
if (this->microphone)
[this->capSession addInput:this->microphone];
// Configure camera
[this->capSession setSessionPreset:AVCaptureSessionPresetHigh];
}
最后,在ViewController中设置AVCameraFileOutput:
And finally, setting up the AVCameraFileOutput in ViewController:
// Configure the movie file output
self.movieFile = [[AVCaptureMovieFileOutput alloc] init];
self.movieFile.minFreeDiskSpaceLimit = 1024 * 1024;
CMTime maxDuration = CMTimeMakeWithSeconds(60*60, 240); // 1 hour at 240 fps should be more than enough
self.movieFile.maxRecordedDuration = maxDuration;
if ([self.camera.capSession canAddOutput:self.movieFile])
[self.camera.capSession addOutput:self.movieFile];
我要去哪里错了?
这是ffprobe在结果文件上的输出.
This is the output from ffprobe on the resulting file.
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 15166 kb/s, 30 fps, 30 tbr, 600 tbn, 1200 tbc (default)
但是,这就是activeFormat的本意:
But yet, this is what the activeFormat supposedly is:
AVCaptureDevice: <AVCaptureDeviceFormat: 0x17401f780 'vide'/'420f' 1280x 720, { 6-240 fps}, fov:59.680, binned, supports vis, max zoom:65.50 (upscales @1.45), AF System:1, ISO:22.0-704.0, SS:0.000002-0.166667, supports wide color>
推荐答案
我最终回答了自己的问题.
I ended up answering my own question.
除了使用预设外,我发现为了设置摄像机配置,我需要将摄像机添加到捕获会话中,对其进行配置,然后立即启动捕获会话.而我是在配置摄像机之前将摄像机添加到捕获会话的,这似乎并没有导致配置被提交.
In addition to using a preset, I discovered that in order to set the camera configuration, I need to add the camera to the capture session, configure it and then start the capture session immediately. Whereas, I was adding the camera to the capture session before I had configured it, which doesn't seem to cause the configuration to be committed.
这篇关于AVFileCaptureOutput:不以240 fps录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!