我想動態地將視覺效果更改爲正在運行的視頻。我正在使用GPUImage框架來改變視覺效果。我從Here下載了示例項目。在這個GPUImage中,我選擇了SimpleVideoFileFilter示例。這個例子運行一個過濾器,只是我修改了代碼,目前它支持10個過濾器。我的問題是,視頻文件在GPUImageView中播放,現在我選擇另一個過濾器。突然間,視頻效果也在變化。但那個視頻是從一開始就開始的。我想爲當前正在播放的視頻動態更改過濾器。 我的代碼是:如何將動態視覺效果添加到iOS中運行的視頻?
#pragma mark - Play Video with Effects
- (void)getVideo:(NSURL *)url
{
movieFile = [[GPUImageMovie alloc] initWithURL:url];
movieFile.runBenchmark = YES;
movieFile.playAtActualSpeed = YES;
// filter = [[GPUImagePixellateFilter alloc] init];
[movieFile addTarget:filter];
// Only rotate the video for display, leave orientation the same for recording
filterView = (GPUImageView *)self.view;
[filter addTarget:filterView];
// 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
NSURL *movieURL1 = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL1 size:CGSizeMake(640.0, 480.0)];
[filter 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:^{
[filter removeTarget:movieWriter];
[movieWriter finishRecording];
UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, nil, nil);
}];
}
- (void)event:(UIButton*)sender
{
[filter removeTarget:filterView];
UIButton *selectedBtn = sender;
[movieFile removeTarget:filter];
switch (selectedBtn.tag)
{
case 0:
filter = [[GPUImageBrightnessFilter alloc] init];
break;
case 1:
filter = [[GPUImageGrayscaleFilter alloc] init];
break;
case 2:
filter = [[GPUImageSketchFilter alloc] init];
break;
case 3:
filter = [[GPUImageToonFilter alloc] init];
break;
case 4:
filter = [[GPUImageMonochromeFilter alloc] init];
break;
case 5:
filter = [[GPUImagePixellateFilter alloc] init];
break;
case 6:
filter = [[GPUImageCrosshatchFilter alloc] init];
break;
case 7:
filter = [[GPUImageVignetteFilter alloc] init];
break;
case 8:
filter = [[GPUImageColorInvertFilter alloc] init];
break;
case 9:
filter = [[GPUImageLevelsFilter alloc] init];
[(GPUImageLevelsFilter *)filter setRedMin:1.0 gamma:1.0 max:0.0 minOut:0.5 maxOut:0.5];
break;
default:
break;
}
[self getVideo:movieURL];
}
請幫我解決這個問題。
爲什麼重新運行'[self getVideo:movieURL];'每次更改過濾器設置?當然,這會重新啓動您的視頻。只需更改過濾器屬性或換出過濾器,而無需重新生成電影實例。您可以輕鬆暫停影片,更換濾鏡,然後重新開始播放動畫以動態更改這些效果。 –
Hi @BradLarson,首先感謝您寶貴的GPUImage框架。其實我很擔心這個問題。請幫我解決這個問題。我不知道如何動態更換過濾器。您是否可以編輯上述代碼併發布回答。 – Sabs
@BradLarson如何暫停正在運行的電影? –