【问题标题】:CIImage back to CMSampleBufferCIImage 返回 CMSampleBuffer
【发布时间】:2018-03-02 20:39:12
【问题描述】:

我使用AVAssetWriterCMSampleBuffer 数据(来自视频、音频输入)录制视频(.mp4 文件)。

在录制时我想处理帧,我将CMSampleBuffer 转换为CIImage 并进行处理。

但是如何使用来自CIImage 的新处理图像缓冲区更新CMSampleBuffer

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
       ...
    } 
    ...
}

【问题讨论】:

    标签: ios swift core-image video-recording


    【解决方案1】:

    您需要使用 CIContext 的 render(_:to:bounds:colorSpace:) 方法将您的 CIImage 渲染到 CVPixelBuffer 中。

    然后您可以使用例如从 CVPixelBuffer 创建一个 CMSampleBuffer CMSampleBufferCreateReadyWithImageBuffer(_:_:_:_:_:)

    出于效率原因,您可能需要使用 CVPixelBuffer 池,Apple 的 AVCamPhotoFilter 示例代码中显示了一个示例。特别是,请参阅RosyCIRenderer class

    另请参阅此答案,这可能会对您有所帮助Applying a CIFilter to a Video File and Saving it

    【讨论】:

    • 是的,我已经这样做了,而且速度很慢 (render(_:to:bounds:colorSpace:)) - stackoverflow.com/questions/49066195/…
    • 我需要在录制时处理帧
    • 您是否正在初始化 CIContext 以使用 OpenGL 或金属进行基于 GPU 的渲染?即 init(eaglContext:) 或 init(mtlDevice:)
    • 我这里没有 CIContext stackoverflow.com/questions/49066195/…
    • CIContext(options: nil),没有选项,反正我不想加载CPU或GPU,还有别的,它只是做了一些不必要的转换,消耗资源,我不认为这样任务我需要使用 GPU,我在 Android 上使用 FFmpeg Recorder/OpenCV 完成了类似的任务(所有处理在 CPU 上),将文本添加到帧
    猜你喜欢
    • 2013-02-25
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多