Silence-GitHub / BBMetalImage

A high performance Swift library for GPU-accelerated image/video processing based on Metal.
MIT License
989 stars 126 forks source link

Question: How to replace a colour with a different or transparent colour? #100

Open teejayhh opened 2 years ago

teejayhh commented 2 years ago

I am playing around with this awesome library and I am wondering if it is possible the remove solid colour background from a video and replace it with another solid colour. Sorta like a green screen effect. I am also wondering if it is possible to replace that colour with a transparent (colour/image). I am pretty new to Image processing but the chromaKey filter seemed to do what I wanted it to do but I cant get this to work

        let filePath = file("bla.mp4")
        print(filePath.absoluteString)

        let outputUrl = URL(fileURLWithPath: filePath.absoluteString)
        removeFile("bla.mp4")
        videoWriter = BBMetalVideoWriter(url: outputUrl, frameSize: BBMetalIntSize(width: 1920, height: 1080))

        videoSource = BBMetalVideoSource(url: sourceURL)
        videoSource.audioConsumer = videoWriter

        let col = BBMetalColor(red: 93, green: 192, blue: 246)
        let filter = BBMetalChromaKeyFilter(thresholdSensitivity: 0.1, smoothing: 0.1, colorToReplace: col)

        videoSource
            .add(consumer: filter)
            .add(consumer: videoWriter)

        videoWriter.start()

        videoSource.start { [unowned self] _ in
            videoWriter.finish {
                print("Finish")
            }
        } 

The result doesnt seem to have an alpha channel so the background appears to be black. (I have tried this with a video which has an alpha channel and that works)

Is something like this possible ?

Cheers Thomas