AVFileCaptureOutput:不以 240 fps 录制
AVFileCaptureOutput: Not recording at 240 fps
我将相机设置为以 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];
}
最后,在 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];
我哪里错了?
编辑:这是 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
在您的捕获设备上时,那(以及 min/max 帧持续时间等任何进一步的自定义)将覆盖来自先前设置的任何会话预设的任何设置。在这种情况下,会话预设更改为 AVCaptureSessionPresetInputPriority
,表示会话设置不再控制一切。
如果在设备上设置活动格式(并进一步自定义设备设置)后,您在捕获会话中调用 setSessionPreset
,新预设 overrides/undoes 设备格式设置。这就是你似乎正在做的事情。由于您已经设置并配置了设备格式,因此根本不需要使用会话预设 — 只需在配置函数末尾省略 setSessionPreset
调用即可。
有关更多详细信息:我见过的关于会话预设与设备格式的最佳概述是 this video from WWDC13, when the device format API was introduced. (Even though that video predates a lot of the features,例如 high-FPS/slow-mo 录制,需要通过 activeFormat
而不是 sessionPreset
.)
(哇,我可以在如此短的时间内用相同的 link 回答两个问题,以至于它仍然在我的剪贴板上。)
此外,请注意,用于查找所需格式的循环可能并不总能找到您想要的格式,因为它在 AVCaptureDevice.formats
数组中选择了 first匹配您想要的 fps 和宽度。例如,根据 whether/how 你正在处理样本缓冲区,你可能会关心你得到的是 420f
还是 420v
格式,并且取决于你对输出文件的处理方式可能会关心支持广色域的设备(如 iPhone 7 和 iPad Pro 9.7 英寸)是否以 sRGB 或 P3 格式捕获。查看 camera features 的完整列表,确保您在多种设备上获得所需内容。
我最后回答了我自己的问题。
除了使用预设之外,我发现为了设置相机配置,我需要将相机添加到捕获会话,配置它,然后立即启动捕获会话。然而,我在配置相机之前将其添加到捕获会话中,这似乎不会导致配置被提交。
我将相机设置为以 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];
}
最后,在 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];
我哪里错了?
编辑:这是 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
在您的捕获设备上时,那(以及 min/max 帧持续时间等任何进一步的自定义)将覆盖来自先前设置的任何会话预设的任何设置。在这种情况下,会话预设更改为 AVCaptureSessionPresetInputPriority
,表示会话设置不再控制一切。
如果在设备上设置活动格式(并进一步自定义设备设置)后,您在捕获会话中调用 setSessionPreset
,新预设 overrides/undoes 设备格式设置。这就是你似乎正在做的事情。由于您已经设置并配置了设备格式,因此根本不需要使用会话预设 — 只需在配置函数末尾省略 setSessionPreset
调用即可。
有关更多详细信息:我见过的关于会话预设与设备格式的最佳概述是 this video from WWDC13, when the device format API was introduced. (Even though that video predates a lot of the features,例如 high-FPS/slow-mo 录制,需要通过 activeFormat
而不是 sessionPreset
.)
(哇,我可以在如此短的时间内用相同的 link 回答两个问题,以至于它仍然在我的剪贴板上。)
此外,请注意,用于查找所需格式的循环可能并不总能找到您想要的格式,因为它在 AVCaptureDevice.formats
数组中选择了 first匹配您想要的 fps 和宽度。例如,根据 whether/how 你正在处理样本缓冲区,你可能会关心你得到的是 420f
还是 420v
格式,并且取决于你对输出文件的处理方式可能会关心支持广色域的设备(如 iPhone 7 和 iPad Pro 9.7 英寸)是否以 sRGB 或 P3 格式捕获。查看 camera features 的完整列表,确保您在多种设备上获得所需内容。
我最后回答了我自己的问题。
除了使用预设之外,我发现为了设置相机配置,我需要将相机添加到捕获会话,配置它,然后立即启动捕获会话。然而,我在配置相机之前将其添加到捕获会话中,这似乎不会导致配置被提交。