How to draw text into CIImage?

2019-08-17 04:23发布

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

0条回答
登录 后发表回答