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)
}
CIHieghtFieldFromMask
andCIShadedMaterial
to do something close to what I'm talking about.... – NutmegCIBlendWithMask
will be fast then would be great, but problem how to convert CIImage back to CMSampleBuffer? – Bronk