Search code examples
swiftmacosavfoundationcore-graphicscore-media

Invalid keys -> ProfileLevel in a AVCaptureVideoDataOutput (macOS)


I'm adding keys to my AVCaptureVideoDataOutput. when adding the key AVVideoProfileLevelKey the system throws me ->

Failed to set (contentViewController) user defined inspected property on (NSWindow): *** +[AVVideoOutputSettings videoOutputSettingsWithVideoSettingsDictionary:] Output settings dictionary contains one or more invalid keys: ProfileLevel

let captureSession = AVCaptureSession()
var videoCaptureOutput = AVCaptureVideoDataOutput()
videoCaptureOutput.videoSettings = [AVVideoCodecKey: AVVideoCodecType.h264,
                                        AVVideoWidthKey : 1280,
                                        AVVideoHeightKey : 720,
                                        AVVideoProfileLevelKey : AVVideoProfileLevelH264HighAutoLevel] as [String : Any]

The key is part of AVVideoSettings.h, so is AVVideoCodecKey, AVVideoWidthKey and AVVideoHeightKey however it's unclear to me where to find what keys are supported by AVCaptureVideoDataOutput.


Solution

  • Keys and how they can be constructed are described in (type AVVideoCodecKey and jump to definition). In your particular case AVVideoProfileLevelKey : AVVideoProfileLevelH264HighAutoLevel should be placed in another dictionary and this dictionary should be set to AVVideoCompressionPropertiesKey:

    let compressionSettings = [AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel]
    videoCaptureOutput.videoSettings = [AVVideoCodecKey: AVVideoCodecType.h264,
                                        AVVideoWidthKey : 1280,
                                        AVVideoHeightKey : 720,
                                        AVVideoProfileLevelKey : compressionSettings]
    

    Hope this can help anyone