How to draw text into CIImage?

牧云@^-^@ 提交于 2020-01-06 06:52:13

问题


How to draw into CIImage (or maybe into CVPixelBuffer, but I guess it easier to add text to CIImage)? not to UIImage

I record video (.mp4 file) using AVAssetWriter and CMSampleBuffer (from video, audio inputs). While recording I want to add text on the video frames, I'm already converting CMSampleBuffer to CIImage, now I need somehow add text to CIImage and convert back to CVPixelBuffer. I didn't really find any simple examples in Swift how to add (draw) text to CIImage or add anything else (like lines, rects, not just text).

/// encode video buffer
func appendVideoSampleBuffer(_ sampleBuffer: CMSampleBuffer) {

    // before encoding video frames into a file I would like process it
    // 1. convert CMSampleBuffer to ciImage

    let cvPixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
    let ciimage : CIImage = CIImage(cvPixelBuffer: cvPixelBuffer)

    // 2. do something with CIImage


    // 3. convert CIImage back to CMSampleBuffer

    let sampleBufferWithText == ...


    appendSampleBuffer(sampleBufferWithText, ofMediaType: AVMediaType.video.rawValue)
}

/// encode audio buffer
func appendAudioSampleBuffer(_ sampleBuffer: CMSampleBuffer) {
    appendSampleBuffer(sampleBuffer, ofMediaType: AVMediaType.audio.rawValue)
}

来源:https://stackoverflow.com/questions/49048860/how-to-draw-text-into-ciimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!