1. 程式人生 > >05 GUPImage混合模式濾鏡的使用

05 GUPImage混合模式濾鏡的使用

  前幾天一直在研究GPUImage,忽然發現混合模式的濾鏡沒有使用方法,各種搜尋後發現也只是對GPUImage的簡單使用,甚至在GPUImage的GitHub上的Demo裡面都沒有找到這一部分的內容的介紹,花費了很大力氣後終於琢磨出來一點用法,可以對兩個視訊新增濾鏡,效果是講兩個視訊整合為一個視訊,同時顯示,中間會有漸變或者高亮等濾鏡效果

 下面是程式碼

</pre><pre name="code" class="objc">- (IBAction)blendFilter:(id)sender {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    // 要新增濾鏡的視訊1
    GPUImageMovie *movieFile = [[GPUImageMovie alloc] initWithURL:[NSURL fileURLWithPath:[sandBoxPath stringByAppendingPathComponent:@"movie"]]];
    
    movieFile.runBenchmark = YES;
    movieFile.playAtActualSpeed = NO;
    
    // 要新增濾鏡的視訊2
    GPUImageMovie *movieFile2 = [[GPUImageMovie alloc] initWithURL:[[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mov"]];
  
    movieFile2.runBenchmark = YES;
    movieFile2.playAtActualSpeed = NO;
//    GPUImageScreenBlendFilter *filter = [[GPUImageScreenBlendFilter alloc] init];
    GPUImageDivideBlendFilter *filter = [[GPUImageDivideBlendFilter alloc] init];
    [movieFile addTarget:filter];
    [movieFile2 addTarget:filter];

    
    // 新增濾鏡後的視訊儲存的位置
    NSString *pathToMovie = [sandBoxPath stringByAppendingPathComponent:@"last.mov"];
    unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
    
    NSLog(@"%@",pathToMovie);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
    
    GPUImageMovieWriter *movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.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 = self.movieWriter;
    [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];
    
    [movieWriter startRecording];
    [movieFile startProcessing];
    [movieFile2 startProcessing];
    
    // 結束後的回撥
    [movieWriter setCompletionBlock:^{
        [filter removeTarget:movieWriter];
        [movieFile endProcessing];
        [movieFile2 endProcessing];
        [movieWriter finishRecording];
        NSLog(@"ok");
    }];
    
}