2015-10-14 112 views
6

我使用AVFoundation從設備的相機錄製到電影文件,使用AVCaptureMovieFileOutput。我想讓用戶在開始錄製之前在高幀率和高分辨率模式之間切換,但我無法弄清楚您應該如何知道哪個AVCaptureDeviceFormat可以產生適合視頻錄製的最高分辨率。如何找到適合錄製到文件的最高分辨率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是真實的,似乎是一種不好的方式。

我可以找到正確的格式來使用,而無需全部嘗試查看哪些可用?

回答

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中找到。