Cabbage
Cabbage copied to clipboard
player background blur effect
Currently, I am trying with this function but not getting proper resolution
open func newRenderedPixelBufferForRequest(request: AVAsynchronousVideoCompositionRequest) -> CVPixelBuffer? {
guard let outputPixels = renderContext?.newPixelBuffer() else { return nil }
guard let instruction = request.videoCompositionInstruction as? VideoCompositionInstruction else {
return nil
}
var image = CIImage(cvPixelBuffer: outputPixels)
// Background
let backgroundImage = CIImage(color: instruction.backgroundColor).cropped(to: image.extent)
image = backgroundImage.composited(over: image)
if let destinationImage = instruction.apply(request: request) {
image = destinationImage.composited(over: image)
}
VideoCompositor.ciContext.render(image, to: outputPixels)
return outputPixels }
Anyone can help me with this.
In my case I added blurred background of a first frame like this:
let timeline = Timeline()
...
let blurredFirstFrame: CIImage = ...
timeline.passingThroughVideoCompositionProvider = BackgroundComposition(backgroundImage: blurredFirstFrame)
import AVFoundation
import VFCabbage
final class BackgroundComposition: VideoCompositionProvider {
let backgroundImage: CIImage
init(backgroundImage: CIImage) {
self.backgroundImage = backgroundImage
}
func applyEffect(
to sourceImage: CIImage,
at time: CMTime,
renderSize: CGSize
) -> CIImage {
sourceImage.composited(over: backgroundImage)
}
}