Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

ios - Video with GPUImageChromaKeyFilter has tint when played in transparent GPUImageView?

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:

Tint on clear

When I change the background to white, the green background is gone, but obviously the view is not transparent:

No tint on white

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!

Working!

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...