我想在视频播放时对视频文件应用过滤器(效果).
我正在使用@BradLarson(伟大的)GPUImage框架来做到这一点,这里的问题是框架不支持播放视频时的音频播放.
所以我有两个选择:
1)深入GPUImage代码并改变GPUImageMovie,因此它也将处理音频缓冲区.这需要知道同步音频和视频帧,不幸的是我没有.我看到一些黑客试图用AVAudioPlayer播放音频,但是有很多同步问题.
2)使用CoreImage框架而不是GPUImage.
所以我想看看使用本机iOS CoreImage和CIFilter来完成这个工作的第二个选项.
问题是,我没有找到任何关于CIFilter如何执行此操作的示例,如何对文件的视频应用过滤器?
我必须使用AVAssetReader来读取视频并处理每个帧吗?如果是这样,我回到了我第一个同步音频和音频的问题.视频.
还是有办法直接在视频或预览图层上应用过滤器链?
欣赏任何帮助:)
解决方法
仅使用您正在使用的GPUImage框架…
那是迄今为止最好的视频过滤器框架.通过框架 https://github.com/BradLarson/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;
- GPUImageMovie *movieUrl = [[GPUImageMovie alloc] initWithURL:videoUrl];
- self.cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:videoArea];
- movieUrl.runBenchmark = YES;
- movieUrl.playAtActualSpeed = YES;
- [movieUrl addTarget:self.cropFilter];
- //Setting path for temporary storing the video in document directory
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
- [NSString stringWithFormat:@"CroppedVideo-%d.mov",arc4random() % 1000]];
- NSURL *movieURL = [NSURL fileURLWithPath:myPathDocs];
- AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
- AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
- CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
- movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(videoAssetTrack.naturalSize.width,videoAssetTrack.naturalSize.height)];
- [_cropFilter addTarget:movieWriter];
- movieWriter.shouldPassthroughAudio = YES;
- movieUrl.audioEncodingTarget = movieWriter;
- [movieUrl enableSynchronizedEncodingUsingMovieWriter:movieWriter];
- [self.movieWriter startRecordingInOrientation:videoTransform];
- [self.movieWriter startRecording];
- [movieUrl startProcessing];
- __block BOOL completeRec = NO;
- __unsafe_unretained typeof(self) weakSelf = self;
- [self.movieWriter setCompletionBlock:^{
- [weakSelf.cropFilter removeTarget:weakSelf.movieWriter];
- [weakSelf.movieWriter finishRecording];
- [movieUrl removeTarget:weakSelf.cropFilter];
- if (!completeRec)
- {
- [weakSelf videoCropDoneUrl:movieURL];
- completeRec = YES;
- }
- }];