Is there a standard performant way to edit/draw on a CVImageBuffer/CVPixelBuffer in swift?
All the video editing demos I've found online overlay the drawing (rectangles or text) on the screen and don't directly edit the CVPixelBuffer.
UPDATE I tried using a CGContext but the saved video doesn't show the context drawing
private var adapter: AVAssetWriterInputPixelBufferAdaptor?
extension TrainViewController: CameraFeedManagerDelegate {
func didOutput(sampleBuffer: CMSampleBuffer) {
let time = CMTime(seconds: timestamp - _time, preferredTimescale: CMTimeScale(600))
let pixelBuffer: CVPixelBuffer? = CMSampleBufferGetImageBuffer(sampleBuffer)
guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: colorSpace,
bitmapInfo: alphaInfo.rawValue)
else {
return nil
}
context.setFillColor(red: 1, green: 0, blue: 0, alpha: 1.0)
context.fillEllipse(in: CGRect(x: 0, y: 0, width: width, height: height))
context.flush()
adapter?.append(pixelBuffer, withPresentationTime: time)
}
}