使用AVAssetWriter导出ScreenCaptureKit采集的CVPixelBuffer画质不佳
屏幕采集导出MP4画质差问题
我在macOS上使用ScreenCaptureKit采集屏幕,功能运行正常。但通过AVAssetWriter将采集到的CVPixelBuffer导出为MP4文件时,视频画质很差。将采集帧的CVPixelBuffer转换为NSImage并保存为PNG时画质无损失,说明采集的帧未经过压缩。我尝试过多种AVAssetWriter设置组合,导出视频的画质仍不理想,以下是我的配置代码:
// Create a new AVAssetWriter to write the captured recording // to a new file. videoFileWriter = try? AVAssetWriter(url: prepareVideoOutputURL(), fileType: AVFileType.mp4) let outputSettings: [String : Any] = [ AVVideoCodecKey : AVVideoCodecType.hevc , AVVideoHeightKey: frame.size.height - CGFloat(Self.topCropHeightWithoutScalingInPixels)/scaleFactor, AVVideoWidthKey: frame.size.width, AVVideoCompressionPropertiesKey: [ AVVideoAllowFrameReorderingKey: false, AVVideoExpectedSourceFrameRateKey: numberOfFramePerSecond, AVVideoMaxKeyFrameIntervalKey: numberOfFramePerSecond, AVVideoMaxKeyFrameIntervalDurationKey: 1 ], // HD Color space https://developer.apple.com/library/archive/technotes/tn2227/_index.html // https://developer.apple.com/documentation/avfoundation/media_reading_and_writing/tagging_media_with_video_color_information AVVideoColorPropertiesKey: [ AVVideoColorPrimariesKey: AVVideoColorPrimaries_ITU_R_709_2, AVVideoTransferFunctionKey: AVVideoTransferFunction_ITU_R_709_2, AVVideoYCbCrMatrixKey: AVVideoYCbCrMatrix_ITU_R_709_2 ] ] videoWriterInput = AVAssetWriterInput( mediaType: AVMediaType.video, outputSettings: outputSettings, sourceFormatHint: nil ) if videoFileWriter.canAdd(videoWriterInput) { videoFileWriter.add(videoWriterInput) } videoWriterInputAdapter = AVAssetWriterInputPixelBufferAdaptor( assetWriterInput: videoWriterInput, sourcePixelBufferAttributes: nil )
画质良好的PNG帧截图:

画质较差的视频帧截图:

内容的提问来源于stack exchange,提问作者Peacemoon
相关产品推荐
相关产品推荐

