如何设置 AVAssetWriterInput 的预期帧率

Eug*_*eev 5 video frame-rate ios avassetwriter avassetwriterinput

我有一个应用程序,它以不同的方式对视频进行编码并将其保存到照片库 - 它可以剪切特定的时间范围,添加图片、文本等。一切都运行良好,直到我尝试对视频进行 120+ fps 的编码。问题是视频似乎是慢动作,而我根本不追求这个目标。

在这里,我发现了有关AVAssetWritterInput名为 的属性AVVideoExpectedSourceFrameRateKey,但问题是当我尝试将此参数应用于我的 时AVAssetWritterInput,我收到此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetWriterInput initWithMediaType:outputSettings:sourceFormatHint:] Output settings dictionary contains one or more invalid keys: ExpectedFrameRate'

这是我的AVAssetWriterInput初始化,一点也不花哨:

let input = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: [AVVideoCodecKey: AVVideoCodecJPEG,
                                                                                         AVVideoHeightKey: correctedContentSize.height,
                                                                                         AVVideoWidthKey: correctedContentSize.width,
                                                                                         AVVideoExpectedSourceFrameRateKey: 60])
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。谢谢。

小智 0

问题来自于您将密钥放置在字典中以将设置加载到outputSettings 中的方式。键“AVVideoExpectedSourceFrameRateKey”实际上应该位于嵌套字典中,其键是“AVVideoCompressionPropertiesKey”。所以你有一个字典的字典作为输出设置。它应该看起来像这样:

let outputSettings:[String: Any] = [
            AVVideoCodecKey: AVVideoCodecJPEG,
            AVVideoHeightKey: correctedContentSize.height,
            AVVideoWidthKey: correctedContentSize.width
            AVVideoCompressionPropertiesKey: 
               [AVVideoExpectedSourceFrameRateKey: 60]                                         
        ]
Run Code Online (Sandbox Code Playgroud)

如果您想在播放此视频时使用它来调整 Feed,可以在此处找到有关此过程的更多信息:

AVAssetWriter AVVideoExpectedSourceFrameRateKey(帧速率)被忽略