Apple 提供的编码器基本上有 2 种,h264 或 avc1 和 jpeg。
let settings:[String:AnyObject] = [
AVVideoCodecKey: AVVideoCodecJPEG,
AVVideoWidthKey: 720,
AVVideoHeightKey: 1280,
AVVideoQualityKey: 0.5
]
//Line below is where the compilation error happened
VideoInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: settings)
VideoInput.expectsMediaDataInRealTime = true
assetWriter.addInput(VideoInput)
现在,如果我从字典中删除 AVVideoQualityKey:0.5,它会完美运行。用钥匙?不。编译错误,下面引用了错误消息。
输出设置字典包含一个或多个无效键:质量'
当我在字典中没有 AVVideoQualityKey 时,最终的电影文件是用 mjpeg 编码器编码的。它太大了,这就是为什么我需要将 jpeg 质量键设置为 0.5。
我想了另一种方法来处理samplebuffer,将它们转换为cvpixelbuffer并自己编码,然后将它们转换回来并用assetwriter编写。但它似乎不是很有效,因为我确定我是否可以使用 AVVideoQualityKey,编码是硬件加速的。