AVCaptureVideoDataOutput 中的无效键 -> ProfileLevel (macOS)

Invalid keys -> ProfileLevel in a AVCaptureVideoDataOutput (macOS)

我正在向 AVCaptureVideoDataOutput 添加密钥。添加密钥 AVVideoProfileLevelKey 时系统抛出我 ->

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]

密钥是 AVVideoSettings.h 的一部分,AVVideoCodecKeyAVVideoWidthKeyAVVideoHeightKey 也是,但是我不清楚在哪里可以找到 [= 支持的密钥11=].

None 的设置适用于 AVCaptureVideoDataOutput,它为您提供原始视频帧。您正在配置它,就好像它正在将帧编码到文件中一样。因此,您的设置字典应该与 AVAssetWriterAVAssetWriterInput 一起使用,或者使用 setOutputSettings 方法在 AVCaptureMovieFileOutput 上设置。

密钥及其构建方式在(键入 AVVideoCodecKey 并跳转到定​​义)中进行了描述。在你的特定情况下 AVVideoProfileLevelKey : AVVideoProfileLevelH264HighAutoLevel 应该放在另一个字典中,这个字典应该设置为 AVVideoCompressionPropertiesKey:

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

希望对大家有所帮助