How to find the highest resolution AVCaptureDeviceFormat suitable for recording to a file?
Asked Answered
U

1

9

I'm using AVFoundation to record from the device's camera to a movie file, using AVCaptureMovieFileOutput. I want to allow the user to switch between high frame rate and high resolution modes before recording begins, but I can't work out how you're supposed to know which AVCaptureDeviceFormat yields the highest resolution suitable for video recording.

For example, on the iPod touch I'm testing on, it lists the following device formats for the rear camera:

<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>

Selecting the highest frame rate is fairly trivial (and an example is given in the documentation); you simply need to iterate through the formats and their supported frame rate ranges to find the one with the highest frame rate. As far as I can tell, the format with the highest frame rate can always be used to record to a video file.

The problem occurs when I try to find the format with the highest resolution. 1080p is, in this case, the highest that you can record to, but formats are listed all the way up to 2448p; I can switch the camera hardware to this format but it refuses to record to a file with anything higher than 1080p. Of course I could hardcode it to only use 1080p, but we have devices which support recording in 4K now.

My question is, how can I filter this list so that it only shows formats suitable for recording? I've looked through the documentation for AVCaptureDeviceFormat and CMFormatDescription, but can't find anything which indicates whether the given format is recordable. I've seen this question which is similar, but constantly switching between formats until isActive is true seems like a bad way of doing it.

Can I find the correct format to use without having to try them all to see which ones work?

Up answered 14/10, 2015 at 13:35 Comment(0)
V
5

In most cases setting AVCaptureSession.sessionPreset to one of AVCaptureSessionPreset... should be enough. Use canSetSessionPreset(preset:) to check if the preset is supported by the current device.

I wanted to get the highest resolution (width) possible; regardless of frame-rate, etc. Here is my solution:

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
}

Admittedly, getting the "420f" (as opposed to "420v") variant is a bit weird...

Since every device return different formats for AVCaptureDevice.formats I'd assume that any returned formats work on the current device. Some information can be found in this technical note from Apple.

Value answered 24/2, 2016 at 16:33 Comment(1)
It is better to replace 875704422 with '420f'.Gassaway

© 2022 - 2024 — McMap. All rights reserved.