VideoFlint / Cabbage

A video composition framework build on top of AVFoundation. It's simple to use and easy to extend.
MIT License
1.52k stars 221 forks source link

player background blur effect #66

Open Trushangpatel3993 opened 3 years ago

Trushangpatel3993 commented 3 years ago

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 }

Trushangpatel3993 commented 3 years ago

Anyone can help me with this.

Alexey-Matjuk commented 3 years ago

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