NOP*_*NOP 5 xcode objective-c++ avfoundation ios avcapturesession
我似乎在将相机设置为以 240 FPS 录制时遇到问题,但由于某种原因,输出文件仅为 30 FPS。
这是我设置相机的代码(首先实例化):
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];
}
Run Code Online (Sandbox Code Playgroud)
最后,在 ViewController 中设置 AVCameraFileOutput:
// 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];
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
编辑:这是 ffprobe 在结果文件上的输出。
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 应该是什么:
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>
有两种基本上不兼容的方式来配置捕获会话:会话预设和设备格式。预设就是预设,格式就是格式,两者永远不会相遇。
当您setActiveFormat使用捕获设备时,该(以及任何进一步的自定义,如最小/最大帧持续时间)会覆盖来自先前设置的任何会话预设的任何设置。在这种情况下,会话预设更改为AVCaptureSessionPresetInputPriority以指示会话的设置不再控制一切。
如果在设备上设置活动格式(并进一步自定义设备设置)后调用setSessionPreset捕获会话,则新预设会覆盖/撤消设备格式设置。这就是你似乎在做的事情。由于您已经设置并配置了设备格式,因此您根本不需要使用会话预设 — 只需省略setSessionPreset配置函数末尾的调用即可。
有关更多详细信息:关于会话预设与设备格式的最佳概述是来自 WWDC13 的此视频,当时引入了设备格式 API。(尽管该视频早于许多功能,例如高 FPS/慢动作录制,需要通过activeFormat而不是进行配置sessionPreset。)
(哇,我可以在这么短的时间内用同一个链接回答两个问题,它仍然在我的剪贴板上。)
此外,要知道,你的循环查找所需的格式可能不会总是得到你想要的,因为它选择的第一个中AVCaptureDevice.formats符合您需要的FPS和宽度阵列。例如,根据您是否/如何处理样本缓冲区,您可能关心是否获得420f或420v格式,并且根据您对输出文件的处理方式,您可能关心是否支持广色域设备(例如iPhone 7 和 iPad Pro 9.7 英寸)以 sRGB 或 P3 格式拍摄。查看完整的相机功能列表,确保您在多个设备上都能获得所需的功能。
我最终回答了我自己的问题。
除了使用预设之外,我发现为了设置相机配置,我需要将相机添加到捕获会话中,对其进行配置,然后立即启动捕获会话。然而,我在配置相机之前将其添加到捕获会话中,这似乎不会导致配置被提交。
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |