我正在使用AVFoundation使用AVCaptureMovieFileOutput
从设备的相机录制到电影文件。我希望允许用户在录制开始之前在高帧率和高分辨率模式之间切换,但我无法弄清楚你应该知道哪个AVCaptureDeviceFormat
产生适合视频录制的最高分辨率。 / p>
例如,在我正在测试的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录制的设备。
我的问题是,如何过滤此列表,使其仅显示适合录制的格式?我查看了AVCaptureDeviceFormat
和CMFormatDescription
的文档,但找不到任何指示给定格式是否可记录的内容。我已经看到this question类似,但在格式之间不断切换,直到isActive
为真,这似乎是一种糟糕的做法。
我是否可以找到正确的格式而无需全部查看哪些格式有效?
答案 0 :(得分:1)
在大多数情况下,将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的technical note中找到。