这是对先前但仅略微相关的 question 的跟进
我正在使用 GPUImage 库将滤镜应用于我的相机应用程序中的静态照片和视频。几乎一切都运行良好。我未能解决的另一个问题如下:
- 我捕捉一个 GPUImageMovie
- 我将它写入文件系统
- 我从文件系统读取它并应用新过滤器
- 我将它写入文件系统中的不同 URL
保存到该新 URL 的是一段时长正确但没有移动的视频。当我点击播放时,它只是一个静止图像,我认为是视频的第一帧。每当我对从文件系统检索的视频应用任何过滤器时,都会发生这种情况。将过滤器应用于实时录制视频效果很好。我的代码如下。
谁能告诉我如何修改它以保存应用滤镜的整个原始视频?
- (void)applyProcessingToVideoAtIndexPath:(NSIndexPath *)indexPath withFilter:(GPUImageFilter *)selectedFilter
{
NSArray *urls = [self.videoURLsByIndexPaths objectForKey:self.indexPathForDisplayedImage];
NSURL *url = [urls lastObject];
self.editedMovie = [[GPUImageMovie alloc] initWithURL:url];
assert(!!self.editedMovie);
[self.editedMovie addTarget:selectedFilter]; // apply the user-selected filter to the file
NSURL *movieURL = [self generatedMovieURL];
// A different movie writer than the one I was using for live video capture.
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 640.0)];
[selectedFilter addTarget:movieWriter];
movieWriter.shouldPassthroughAudio = YES;
self.editedMovie.audioEncodingTarget = movieWriter;
[self.editedMovie enableSynchronizedEncodingUsingMovieWriter:movieWriter];
[movieWriter startRecording];
[self.editedMovie startProcessing];
__weak GPUImageMovieWriter *weakWriter = movieWriter;
__weak CreateContentViewController *weakSelf = self;
[movieWriter setCompletionBlock:^{
[selectedFilter removeTarget:weakWriter];
[weakWriter finishRecordingWithCompletionHandler:^{
NSArray *urls = [weakSelf.videoURLsByIndexPaths objectForKey:weakSelf.indexPathForDisplayedImage];
urls = [urls arrayByAddingObject:movieURL];
NSMutableDictionary *mutableVideoURLs = [weakSelf.videoURLsByIndexPaths mutableCopy];
[mutableVideoURLs setObject:urls forKey:weakSelf.indexPathForDisplayedImage];
weakSelf.videoURLsByIndexPaths = mutableVideoURLs;
dispatch_sync(dispatch_get_main_queue(), ^{
[self.filmRoll reloadData];
[weakSelf showPlayerLayerForURL:movieURL onTopOfImageView:weakSelf.displayedImageView];
});
}];
}];
}
最佳答案
我正在为 future 的访问者发布这个问题的“答案”。我的代码现在可以正常工作,但事实是我不确定是什么造成了差异。我不希望上面的代码成为任何人的转移注意力。问题可能出在我的代码库的其他地方。但是,我想提一下问题中发布的方法与我的工作代码中的方法之间的区别。
我简化了 Controller 的结构,因此它一次只处理一个媒体项。没有更多的 videoURLsByIndexPath,我只有 self.mediaItem。显然,这使组织更易于管理。我还怀疑在 movie writer 的完成 block 内向该字典添加 movieURL 条目可能与我看到的意外结果有关。
movieWriter 现在是一个强大的属性,而不是 ivar。这不应该影响 AFAIK,因为 ivars 默认为强引用。不过,这是此处发布的内容与我的工作代码之间的差异。
几乎肯定不相关,但我确实在
self.editedMovie.audioEncodingTarget = self.movieWriter
周围放置了一个 if-else 来检查[self.mediaItem tracksWithType: AVMediaTypeAudio]
大于 0。由于不再需要维护视频 URL 字典,我只需调用
self.mediaItem = [AVURLAsset assetWithURL:movieURL];
。我在电影编剧的完成 block 之外执行此操作。
我觉得这不是最有值(value)的答案,但我希望我在这里提到的要点有用。
关于ios - GPUImage过滤视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973697/