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

时间:2016-12-23 05:28:13

标签: ios objective-c swift uilabel gpuimage

如何向此过滤器管道添加动态文本?我知道我需要一个混合滤镜和一个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

1 个答案:

答案 0 :(得分: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);
}];