我使用录制视频(.mp4 文件)AVAssetWriter
with CMSampleBuffer
数据(来自视频、音频输入)。
录制时我想处理帧,我正在转换CMSampleBuffer
to CIImage
并对其进行处理。
但如何更新CMSampleBuffer
用我新处理的图像缓冲区CIImage
?
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
if output == videoOutput {
let imageBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
let ciimage: CIImage = CIImage(cvPixelBuffer: imageBuffer)
... // my code to process CIImage (for example add augmented reality)
// but how to convert it back to CMSampleBuffer?
// because AVAssetWriterInput to encode video/audio in file needs CMSampleBuffer
...
}
...
}
您需要使用 CIContext 将 CIImage 渲染到 CVPixelBuffer 中render(_:to:bounds:colorSpace:) https://developer.apple.com/documentation/coreimage/cicontext/1437835-render方法。
然后您可以使用例如从 CVPixelBuffer 创建 CMSampleBufferCMSampleBufferCreateReadyWithImageBuffer(_:_:_:_:_:) https://developer.apple.com/documentation/coremedia/1489745-cmsamplebuffercreatereadywithima
出于效率原因,您可能需要使用 CVPixelBuffer 池,Apple 的 AVCamPhotoFilter 示例代码中显示了一个示例。特别是,请参阅RosyCIRenderer 类 https://developer.apple.com/library/content/samplecode/AVCamPhotoFilter/Listings/AVCamPhotoFilter_RosyCIRenderer_swift.html.
另请参阅这个答案,它可能对您有帮助将 CIFilter 应用到视频文件并保存 https://stackoverflow.com/questions/39114863/applying-a-cifilter-to-a-video-file-and-saving-it
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)