Video with GPUImageChromaKeyFilter has tint when played in transparent GPUImageView?

依然范特西╮ 提交于 2019-11-27 06:29:29

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/33244347/video-with-gpuimagechromakeyfilter-has-tint-when-played-in-transparent-gpuimagev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!