gpt4 book ai didi

ios - 如何找到适合录制到文件的最高分辨率 AVCaptureDeviceFormat?

转载 作者:可可西里 更新时间:2023-11-01 03:32:52 24 4
gpt4 key购买 nike

我正在使用 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 录制的设备。

我的问题是,如何过滤此列表,使其只显示适合录制的格式?我查看了 AVCaptureDeviceFormatCMFormatDescription 的文档,但找不到任何指示给定格式是否可记录的内容。我看过this question这很相似,但在 isActive 为真之前不断地在格式之间切换似乎是一种糟糕的做法。

我能否找到要使用的正确格式,而不必尝试所有格式以查看哪些有效?

最佳答案

在大多数情况下,将 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
}

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

由于每个设备为 AVCaptureDevice.formats 返回不同的格式,我假设任何返回的格式都适用于当前设备。一些信息可以在这个 technical note 中找到来自 Apple。

关于ios - 如何找到适合录制到文件的最高分辨率 AVCaptureDeviceFormat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com