Reputation: 1179
I have a video with a solid green background, that I am trying to make transparent with GPUImageChromaKeyFilter.
When I have clear colour for the player view, the result is that the video is not really transparent, but tints the background:
When I change the background to white, the green background is gone, but obviously the view is not transparent:
What am I doing wrong?
My code is:
let movie = GPUImageMovie(URL: url)
let filter = GPUImageChromaKeyFilter()
filter.setColorToReplaceRed(0, green: 1, blue: 0)
movie.addTarget(filter)
let playerView = GPUImageView()
playerView.backgroundColor = UIColor.clearColor()
playerView.setBackgroundColorRed(0, green: 0, blue: 0, alpha: 0)
let trinity = (movieFile, filter, playerView)
filter.addTarget(playerView)
movie.startProcessing()
I am storing the movie, filter and view to avoid ARC releasing them when scope is exited and I add the view to the correct superview and position it with auto layout.
The video I am using is an h264 video - https://dl.dropboxusercontent.com/u/1400954/Coin.mp4
Upvotes: 2
Views: 1563
Reputation: 1179
I got this working. The solution is to use GPUImageChromaKeyBlendFilter. It blends two sources by replacing the pixels of the colour specified in setColorToReplaceRed in the first source with the pixels in the second source.
So I made a GPUImagePicture with a transparent UIImage and added the filter as a target to it. You can use a transparent PNG or build an UIImage and fill it with transparent in code (empty UIImage won't work!)
Now everything works beautifully!
The code is
let filter = GPUImageChromaKeyBlendFilter()
filter.setColorToReplaceRed(0, green: 1, blue: 0)
let movieFile = GPUImageMovie(URL: url)
movieFile.addTarget(filter)
let backgroundImage = UIImage(named: "MTransparent")
let sourcePicture = GPUImagePicture(image: backgroundImage, smoothlyScaleOutput: true)
sourcePicture.addTarget(filter)
sourcePicture.processImage()
let playerView = GPUImageView()
playerView.backgroundColor = UIColor.clearColor()
filter.addTarget(playerView)
movieFile.startProcessing()
Upvotes: 3