Cabbage icon indicating copy to clipboard operation
Cabbage copied to clipboard

player background blur effect

Open Trushangpatel3993 opened this issue 4 years ago • 2 comments

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 avatar Mar 04 '21 04:03 Trushangpatel3993

Anyone can help me with this.

Trushangpatel3993 avatar Mar 07 '21 04:03 Trushangpatel3993

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

Alexey-Matjuk avatar Apr 22 '21 11:04 Alexey-Matjuk