ios – 在视频文件上应用过滤器

前端之家收集整理的这篇文章主要介绍了ios – 在视频文件上应用过滤器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在视频播放时对视频文件应用过滤器(效果).

我正在使用@BradLarson(伟大的)GPUImage框架来做到这一点,这里的问题是框架不支持播放视频时的音频播放.

所以我有两个选择:

1)深入GPUImage代码并改变GPUImageMovie,因此它也将处理音频缓冲区.这需要知道同步音频和视频帧,不幸的是我没有.我看到一些黑客试图用AVAudioPlayer播放音频,但是有很多同步问题.

2)使用CoreImage框架而不是GPUImage.

所以我想看看使用本机iOS CoreImage和CIFilter来完成这个工作的第二个选项.

问题是,我没有找到任何关于CIFilter如何执行此操作的示例,如何对文件的视频应用过滤器?

我必须使用AVAssetReader来读取视频并处理每个帧吗?如果是这样,我回到了我第一个同步音频和音频的问题.视频.
还是有办法直接在视频或预览图层上应用过滤器链?

欣赏任何帮助:)

解决方法

仅使用您正在使用的GPUImage框架…
那是迄今为止最好的视频过滤器框架.通过框架 https://github.com/BradLarson/GPUImage的文档滚动页面,您将找到可用的过滤器的详细信息…

这个过滤器被应用在视频上并写入你必须使用GPUImageMovieWriter类的视频…它自动处理音频..

你不必维护它…使用GPUImageMovieWriter的shouldPassThroughAudio属性,它将自己管理音频.

使用本教程帮助http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework

这里是我使用GPUImage框架裁剪视频的代码,编辑后存储不被删除.

NSURL * videoUrl = [selectedAsset defaultRepresentation] .url;

  1. GPUImageMovie *movieUrl = [[GPUImageMovie alloc] initWithURL:videoUrl];
  2.  
  3. self.cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:videoArea];
  4. movieUrl.runBenchmark = YES;
  5. movieUrl.playAtActualSpeed = YES;
  6. [movieUrl addTarget:self.cropFilter];
  7.  
  8. //Setting path for temporary storing the video in document directory
  9. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  10. NSString *documentsDirectory = [paths objectAtIndex:0];
  11. NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
  12. [NSString stringWithFormat:@"CroppedVideo-%d.mov",arc4random() % 1000]];
  13. NSURL *movieURL = [NSURL fileURLWithPath:myPathDocs];
  14.  
  15. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
  16. AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
  17. CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
  18.  
  19. movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(videoAssetTrack.naturalSize.width,videoAssetTrack.naturalSize.height)];
  20.  
  21. [_cropFilter addTarget:movieWriter];
  22. movieWriter.shouldPassthroughAudio = YES;
  23.  
  24. movieUrl.audioEncodingTarget = movieWriter;
  25.  
  26. [movieUrl enableSynchronizedEncodingUsingMovieWriter:movieWriter];
  27.  
  28. [self.movieWriter startRecordingInOrientation:videoTransform];
  29. [self.movieWriter startRecording];
  30.  
  31. [movieUrl startProcessing];
  32. __block BOOL completeRec = NO;
  33. __unsafe_unretained typeof(self) weakSelf = self;
  34. [self.movieWriter setCompletionBlock:^{
  35.  
  36. [weakSelf.cropFilter removeTarget:weakSelf.movieWriter];
  37. [weakSelf.movieWriter finishRecording];
  38. [movieUrl removeTarget:weakSelf.cropFilter];
  39. if (!completeRec)
  40. {
  41. [weakSelf videoCropDoneUrl:movieURL];
  42. completeRec = YES;
  43. }
  44. }];

猜你在找的iOS相关文章