将动态文本GPUImage添加到过滤视频中

3
我该如何向这个过滤器管道添加动态文本?我知道我需要一个混合过滤器和一个UIElement,但不确定要将其添加到哪个目标/位置。有人能帮忙吗?
videoCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPresetMedium cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationLandscapeRight;

cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0, 0, 1, 1)];
sourcePicture = [[GPUImagePicture alloc] initWithImage:[NPFilterBuilder getTextureOverlay] smoothlyScaleOutput:NO];
[sourcePicture processImage];

customFilter = [NPFilterBuilder getFilter];
[videoCamera addTarget:cropFilter];
[cropFilter addTarget:customFilter atTextureLocation:0];
[sourcePicture addTarget:customFilter atTextureLocation:1];

[customFilter addTarget:mViewCameraPreview];

[videoCamera startCameraCapture];

我知道我需要做这样的事情:

blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
blendFilter.mix = 1.0;
uiElementInput = [[GPUImageUIElement alloc]initWithView:[self buildTimeLabel]];

[uiElementInput addTarget:blendFilter];

__unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;
[filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
    [weakUIElementInput update];
}];

我需要在筛选器链中的某个位置添加blendFilter,但是我无法确定应该在哪里添加。我制作了一张图表,展示了我认为目前链的样子:

enter image description here

动态文本是什么意思?你的意思是想将文本作为滤镜覆盖在图像上吗? - Alistra
1个回答

3
也许这会对你有所帮助:
NSURL *sampleURL = [[NSBundle mainBundle] URLForResource:@"Missile_Preview" withExtension:@"m4v"];

movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
movieFile.runBenchmark = YES;
movieFile.playAtActualSpeed = NO;

filter = [[GPUImageBrightnessFilter alloc] init];
[(GPUImageBrightnessFilter*)filter setBrightness:0.0];

GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
blendFilter.mix = 1.0;

UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-20)];
contentView.backgroundColor = [UIColor clearColor];

UIImageView *ivTemp = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 147, 59)];
ivTemp.image = [UIImage imageNamed:@"logo.png"];
[contentView addSubview:ivTemp];

UILabel *lblDemo = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
lblDemo.text = @"Paracha";
lblDemo.font = [UIFont systemFontOfSize:30];
lblDemo.textColor = [UIColor redColor];
lblDemo.tag = 1;
lblDemo.hidden = YES;
lblDemo.backgroundColor = [UIColor clearColor];
[contentView addSubview:lblDemo];

uiElementInput = [[GPUImageUIElement alloc] initWithView:contentView];
[filter addTarget:blendFilter];
[uiElementInput addTarget:blendFilter];

[movieFile addTarget:filter];

// Only rotate the video for display, leave orientation the same for recording
GPUImageView *filterView = (GPUImageView *)vwVideo;
[filter addTarget:filterView];
[blendFilter addTarget:filterView];

[filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
    if (frameTime.value/frameTime.timescale == 2) {
        [contentView viewWithTag:1].hidden = NO;
    }
    [uiElementInput update];
}];

// In addition to displaying to the screen, write out a processed version of the movie to disk
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie

movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:[NSURL fileURLWithPath:pathToMovie] size:CGSizeMake(640.0, 480.0)];
[filter addTarget:movieWriter];
[blendFilter addTarget:movieWriter];

// Configure this for video from the movie file, where we want to preserve all video frames and audio samples
movieWriter.shouldPassthroughAudio = YES;
movieFile.audioEncodingTarget = movieWriter;
[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

[movieWriter startRecording];
[movieFile startProcessing];

[movieWriter setCompletionBlock:^{
    UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, nil, nil);
}];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接