AVFileCaptureOutput:不以240 fps录制

时间:2016-12-12 20:25:19

标签: ios xcode avfoundation objective-c++ avcapturesession

我似乎遇到了一个问题,我设置相机以240 FPS记录,但由于某种原因输出文件只有30 FPS。

这是我设置相机的代码(首先实例化):

/var/scratch/wdps1615/spark-2.0.2-bin-without-hadoop/pip install requests

最后,在ViewController中设置AVCameraFileOutput:

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];

}

我哪里错了?

编辑:这是ffprobe在生成的文件中的输出。

// 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];

但是,这就是activeFormat所谓的:

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)

2 个答案:

答案 0 :(得分:4)

有两种很大程度上不兼容的配置捕获会话的方法:会话预设和设备格式。预设是预设,格式是格式,而且twain不会满足。

当您在捕获设备上setActiveFormat时,该(以及任何进一步的自定义设置,如最小/最大帧持续时间)将覆盖来自先前设置的任何会话预设的任何设置。在这种情况下,会话预设更改为AVCaptureSessionPresetInputPriority,表示会话的设置不再控制所有内容。

如果在设备上设置活动格式(以及进一步自定义设备设置)后,在捕获会话上调用setSessionPreset,新预设将覆盖/撤消设备格式设置。这就是你似乎在做的事情。由于您已经设置并配置了设备格式,因此根本不需要使用会话预设 - 只需在配置功能结束时省略setSessionPreset调用。

有关详细信息:在引入设备格式API时,我在会话预设与设备格式上看到的最佳概述是this video from WWDC13。 (即使该视频早于features很多,如高FPS /慢速录音,需要通过activeFormat而不是sessionPreset进行配置。)

(哇,我可以在很短的时间内用相同的链接回答两个问题,它仍然在我的剪贴板上。)

另外,请注意,用于查找所需格式的循环可能并不总能获得所需的格式,因为它在AVCaptureDevice.formats数组中选择与您所需的fps匹配的 first 和宽度。例如,根据您是否/如何处理样本缓冲区,您可能会关心是否获得420f420v格式,并且取决于您对输出文件的处理方式,您可能会关心关于是否可以在sRGB或P3中捕获具有宽色功能的设备(如iPhone 7和iPad Pro 9.7英寸)。查看camera features的完整列表,确保您在多台设备上获得所需内容。

答案 1 :(得分:0)

我最后回答了自己的问题。

除了使用预设外,我发现为了设置摄像头配置,我需要将摄像头添加到捕获会话,配置它,然后立即启动捕获会话。然而,我在配置它之前将相机添加到捕获会话中,这似乎不会导致配置被提交。

相关的iOS文档:https://developer.apple.com/reference/avfoundation/avcapturedevice/1387810-lockforconfiguration?language=objc

相关问题