快捷搜索:

我们可以在播放时实时的给视频添加滤镜

有了GPUImage,我们可以十分方便的给视频添加滤镜。例如,我们可以在播放时实时的给视频添加滤镜,以观察效果。代码如下:

 //播放视频 AVPlayerItem *item =[[AVPlayerItem alloc]initWithURL:_videoURL]; AVPlayer *player =[[AVPlayer alloc]initWithPlayerItem:item]; AVPlayerLayer *playerLayer =[AVPlayerLayer playerLayerWithPlayer:player]; playerLayer.frame =self.view.bounds; [self.view.layer addSublayer:playerLayer]; movieFile =[[GPUImageMovie alloc]initWithPlayerItem:item]; movieFile.runBenchmark =YES; movieFile.playAtActualSpeed =YES; filter =[[GPUImageSketchFilter alloc] init]; [movieFile addTarget:filter]; filterView =[[GPUImageView alloc]initWithFrame:self.view.bounds]; [self.view addSubview:filterView]; [filter addTarget:filterView]; [player play]; [movieFile startProcessing];

而把添加滤镜后的视频保存到本地的话 ,则需要GPUImage框架中的GPUImageMovieWriter这个类写到本地即可,还是贴代码:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [movieFile removeAllTargets];// //新的滤镜// filter =[[GPUImageSepiaFilter alloc]init];//// [movieFile addTarget:filter];// [filter addTarget:filterView]; //movieWriter 写到本地 endMovieFile =[[GPUImageMovie alloc]initWithURL:_videoURL]; endMovieFile.runBenchmark =YES; endMovieFile.playAtActualSpeed =YES; endFilter =[[GPUImageSepiaFilter alloc]init]; [endMovieFile addTarget:endFilter]; NSString *pathToMovie =[self getVideoSaveFilePathString:@".MOV" addPathArray:NO]; unlink([pathToMovie UTF8String]); NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie]; movieWriter =[[GPUImageMovieWriter alloc]initWithMovieURL:movieURL size:CGSizeMake(720, 1280)]; [endFilter addTarget:movieWriter]; movieWriter.shouldPassthroughAudio =YES; endMovieFile.audioEncodingTarget =movieWriter; [endMovieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter]; [movieWriter startRecording]; [endMovieFile startProcessing]; [movieWriter setCompletionBlock:^{ WeakSelf; dispatch_async(dispatch_get_main_queue(), ^{ [self.hud hideAnimated:YES]; [endFilter removeTarget:movieWriter]; [movieWriter finishRecording]; //保存相册 [self savePhotosAlbum:movieURL]; }); }]; });

注:3秒后保存到本地相当于按钮事件,示例而已。movieWriter相当于把视频重写一遍,完成回调后要移除target,还要结束录制。

有什么问题欢迎留言讨论。

本文由澳门新葡萄京8455官网发布于澳门新葡萄京8455官网,转载请注明出处:我们可以在播放时实时的给视频添加滤镜

您可能还会对下面的文章感兴趣: