8

我正在使用 AVFoundation 从设备的相机录制到电影文件,使用AVCaptureMovieFileOutput. 我想让用户在录制开始之前在高帧率和高分辨率模式之间切换,但我无法弄清楚你应该如何知道哪种AVCaptureDeviceFormat产生适合视频录制的最高分辨率。

例如,在我正在测试的 iPod touch 上,它列出了后置摄像头的以下设备格式:

<AVCaptureDeviceFormat: 0x13108e260 'vide'/'420v'  192x 144, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @17.00), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310911a0 'vide'/'420f'  192x 144, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @17.00), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310afa60 'vide'/'420v'  352x 288, { 2- 30 fps}, HRSI:2992x2448, fov:49.745, max zoom:153.00 (upscales @8.50), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310af9b0 'vide'/'420f'  352x 288, { 2- 30 fps}, HRSI:2992x2448, fov:49.745, max zoom:153.00 (upscales @8.50), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x131092ad0 'vide'/'420v'  480x 360, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @6.80), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x13108f1c0 'vide'/'420f'  480x 360, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @6.80), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310af300 'vide'/'420v'  640x 480, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @5.10), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x131021990 'vide'/'420f'  640x 480, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @5.10), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310ac0f0 'vide'/'420v'  960x 540, { 2- 30 fps}, HRSI:3264x1836, fov:54.267, supports vis, max zoom:104.38 (upscales @3.09), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310ac180 'vide'/'420f'  960x 540, { 2- 30 fps}, HRSI:3264x1836, fov:54.267, supports vis, max zoom:104.38 (upscales @3.09), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x131091550 'vide'/'420v' 1280x 720, { 2- 30 fps}, HRSI:3264x1836, fov:54.267, supports vis, max zoom:95.62 (upscales @2.32), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310ab800 'vide'/'420f' 1280x 720, { 2- 30 fps}, HRSI:3264x1836, fov:54.267, supports vis, max zoom:95.62 (upscales @2.32), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x13100a350 'vide'/'420v' 1280x 720, { 3-120 fps}, fov:54.267, binned, supports vis, max zoom:52.12 (upscales @1.16), AF System:1, ISO:24.0-768.0, SS:0.000025-0.333333>
<AVCaptureDeviceFormat: 0x131090270 'vide'/'420f' 1280x 720, { 3-120 fps}, fov:54.267, binned, supports vis, max zoom:52.12 (upscales @1.16), AF System:1, ISO:24.0-768.0, SS:0.000025-0.333333>
<AVCaptureDeviceFormat: 0x131021510 'vide'/'420v' 1920x1080, { 2- 30 fps}, HRSI:3264x1836, fov:54.267, supports vis, max zoom:95.62 (upscales @1.55), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x1310a2130 'vide'/'420f' 1920x1080, { 2- 30 fps}, HRSI:3264x1836, fov:54.267, supports vis, max zoom:95.62 (upscales @1.55), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x12fe17b90 'vide'/'420v' 2592x1936, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @1.26), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x13108e010 'vide'/'420f' 2592x1936, { 2- 30 fps}, HRSI:3264x2448, fov:54.267, max zoom:153.00 (upscales @1.26), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x12fe17be0 'vide'/'420v' 3264x2448, { 2- 30 fps}, fov:54.267, max zoom:153.00 (upscales @1.00), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>
<AVCaptureDeviceFormat: 0x12fe17c40 'vide'/'420f' 3264x2448, { 2- 30 fps}, fov:54.267, max zoom:153.00 (upscales @1.00), AF System:1, ISO:24.0-768.0, SS:0.000024-0.500000>

选择最高帧率是相当简单的(文档中给出了一个示例);您只需遍历格式及其支持的帧速率范围即可找到具有最高帧速率的格式。据我所知,最高帧率的格式总是可以用来录制视频文件。

当我尝试找到最高分辨率的格式时,就会出现问题。在这种情况下,1080p 是您可以录制到的最高分辨率,但格式一直列出到 2448p;我可以将相机硬件切换到这种格式,但它拒绝记录到任何高于 1080p 的文件。当然,我可以将其硬编码为仅使用 1080p,但我们现在有支持 4K 录制的设备。

我的问题是,如何过滤此列表,使其仅显示适合录制的格式?我查看了AVCaptureDeviceFormatand的文档CMFormatDescription,但找不到任何表明给定格式是否可录制的内容。我见过这个类似的问题,但是在格式之间不断切换直到isActive为真似乎是一种不好的做法。

我是否可以找到正确的格式来使用,而不必全部尝试以查看哪些有效?

4

1 回答 1

4

在大多数情况下,设置AVCaptureSession.sessionPreset为其中之一AVCaptureSessionPreset...就足够了。用于canSetSessionPreset(preset:)检查当前设备是否支持预设。

我想获得尽可能高的分辨率(宽度);无论帧速率等如何。这是我的解决方案:

captureSession.sessionPreset = AVCaptureSessionPresetInputPriority // Required for the "activeFormat" of the device to be used
let highresFormat = (device.formats as! [AVCaptureDeviceFormat])
    .filter { CMFormatDescriptionGetMediaSubType($0.formatDescription) == 875704422 } // Full range 420f
    .maxElement { a, b in CMVideoFormatDescriptionGetDimensions(a.formatDescription).width < CMVideoFormatDescriptionGetDimensions(b.formatDescription).width }
if let format = highresFormat {
    device.activeFormat = format
}

诚然,获得“420 f ”(相对于“420 v ”)变体有点奇怪......

由于每个设备返回不同的格式,AVCaptureDevice.formats我假设任何返回的格式都适用于当前设备。可以在Apple的此技术说明中找到一些信息。

于 2016-02-24T16:33:03.700 回答