我像这样通过 GPUImage 创建视频效果
self.overlayerView = [[GPUImageView alloc] init];
self.overlayerView.frame = self.view.frame;
dispatch_queue_t queue = dispatch_queue_create("queue", NULL);
dispatch_async(queue, ^{
NSURL *sourceURL = [[NSBundle mainBundle] URLForResource:@"212121" withExtension:@"mp4"];
GPUImageMovie *sourceMovie = [[GPUImageMovie alloc] initWithURL:sourceURL];
sourceMovie.playAtActualSpeed = YES;
sourceMovie.shouldRepeat = YES;
sourceMovie.shouldIgnoreUpdatesToThisTarget = YES;
NSURL *maskURL = [[NSBundle mainBundle] URLForResource:@"rose" withExtension:@"mp4"];
GPUImageMovie *maskMovie = [[GPUImageMovie alloc] initWithURL:maskURL];
maskMovie.playAtActualSpeed = YES;
maskMovie.shouldRepeat = YES;
NSURL *alphaURL = [[NSBundle mainBundle] URLForResource:@"rose_alpha" withExtension:@"mp4"];
GPUImageMovie *alphaMovie = [[GPUImageMovie alloc] initWithURL:alphaURL];
alphaMovie.playAtActualSpeed = YES;
alphaMovie.shouldRepeat = YES;
NSURL *topURL = [[NSBundle mainBundle] URLForResource:@"screen" withExtension:@"mp4"];
GPUImageMovie *topMovie = [[GPUImageMovie alloc] initWithURL:topURL];
topMovie.playAtActualSpeed = YES;
topMovie.shouldRepeat = YES;
filter0 = [[GPUImageThreeInputFilter alloc] initWithFragmentShaderFromString:@"precision highp float;uniform sampler2D inputImageTexture;uniform sampler2D inputImageTexture2;uniform sampler2D inputImageTexture3;varying vec2 textureCoordinate;void main(){vec4 video=texture2D(inputImageTexture,textureCoordinate);vec4 mv=texture2D(inputImageTexture2, textureCoordinate);vec4 alpha = texture2D(inputImageTexture3, textureCoordinate);gl_FragColor = video * (1.0 - alpha.r) + mv;}"];
filter1 = [[GPUImageTwoInputFilter alloc] initWithFragmentShaderFromString:@"\nprecision highp float;\nuniform sampler2D inputImageTexture; //video\nuniform sampler2D inputImageTexture2; //screen\nvarying vec2 textureCoordinate;\nvoid main()\n{\nvec4 video = texture2D(inputImageTexture, textureCoordinate);\nvec4 screen = texture2D(inputImageTexture2, textureCoordinate);\nmediump vec4 whiteColor = vec4(1.0);\ngl_FragColor = whiteColor - ((whiteColor - screen) * (whiteColor - video));\n}"];
[sourceMovie addTarget:filter0];
[maskMovie addTarget:filter0];
[alphaMovie addTarget:filter0];
[filter0 addTarget:filter1];
[topMovie addTarget:filter1];
[sourceMovie startProcessing];
[alphaMovie startProcessing];
[maskMovie startProcessing];
[topMovie startProcessing];
[filter0 forceProcessingAtSize:CGSizeMake(480,480)];
[filter1 forceProcessingAtSize:CGSizeMake(480,480)];
dispatch_async(dispatch_get_main_queue(), ^{
[filter1 addTarget:self.overlayerView];
});
});
视频有黑色背景是因为 alphaMovie 和 maskMovie 没有同时播放?
无黑底效果视频
问题:
1:如何去除黑色背景?
2:为什么我的效果视频是黑色背景?
最佳答案
GPUImage 框架不包含对此类 alpha channel 功能的支持。有绿屏功能,因此如果您在绿屏上预先制作视频,则可以将视频从绿屏背景中分离出来。但是,您在这里描述的是一个 alpha channel 视频和第二个视频,并且它无法正常工作,因为您同时从两个不同的视频源中提取并且它们不会保持同步。请注意,即使使用绿屏功能,精确边缘也存在问题,如 this blog post 中所述。 (包括源代码)。基本问题是滤镜斜坡可以以奇怪的方式处理接近绿色但不完全是绿色的边缘。您可能需要考虑的另一种方法是,在尝试使用 iOS 视频逻辑播放视频之前,将 N 帧预合成为 1 个视频。
关于ios - GPUImageMovie 不支持 Alpha channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34989942/