使用GPUImage过滤视频 [英] Filtering video with GPUImage

查看:173
本文介绍了使用GPUImage过滤视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用 GPUImage 并尝试过滤视频.实时视频过滤效果很好.当我尝试从文件系统将视频读取到内存中并使用 sunsetlakes软件教程页面和SimpleVideoFileFilter演示中.

I'm using GPUImage in my application and trying to filter video. Live video filtering is working well. The trouble comes up when I try to read a video into memory from the filesystem and apply filters using the code posted on the sunsetlakessoftware tutorial page and in the SimpleVideoFileFilter demo.

编辑:我意识到我的原始帖子可能没有提出足够具体的问题.我要问的是:如何准确地将视频从磁盘读取到内存中,应用GPUImageFilter,然后使用过滤后的版本覆盖原始视频?

EDIT: I realized that my original post may not have been posing a specific enough question. What I am asking is: How exactly can I read a video from disk into memory, apply a GPUImageFilter, and then overwrite the original with the filtered version?

应用程序崩溃,出现以下错误:

The application is crashing with the following error:

-[AVAssetWriter startWriting] Cannot call method when status is 2

状态2为AVAssetWriterStatusCompleted.我已经看到其他所有三个AVAssetWriterStatus都发生相同的故障.

status 2 is AVAssetWriterStatusCompleted. I've seen the same failure occur with all three other AVAssetWriterStatuses.

我已经在下面发布了相关代码.

I have posted the relevant code below.

GPUImageFilter *selectedFilter = [self.allFilters objectAtIndex:indexPath.item];

// get the file url I stored when the video was initially captured
NSURL *url = [self.videoURLsByIndexPath objectForKey:self.indexPathForDisplayedImage];

GPUImageMovie *movieFile = [[GPUImageMovie alloc] initWithURL:url];
movieFile.runBenchmark = YES;
movieFile.playAtActualSpeed = NO;
[movieFile addTarget:selectedFilter]; // apply the user-selected filter to the file

unlink([url.absoluteString UTF8String]); // delete the file that was at that file URL so it's writeable

// A different movie writer than the one I was using for live video capture.
GPUImageMovieWriter *editingMovieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:url size:CGSizeMake(640.0, 640.0)];

[selectedFilter addTarget:editingMovieWriter];

editingMovieWriter.shouldPassthroughAudio = YES;
movieFile.audioEncodingTarget = editingMovieWriter;
[movieFile enableSynchronizedEncodingUsingMovieWriter:editingMovieWriter];

[editingMovieWriter startRecording];
[movieFile startProcessing]; // Commenting out this line prevents crash

// weak variables to prevent retain cycle
__weak GPUImageMovieWriter *weakWriter = editingMovieWriter;
__weak id weakSelf = self;
[editingMovieWriter setCompletionBlock:^{
    [selectedFilter removeTarget:weakWriter];
    [weakWriter finishRecording];
    [weakSelf savePhotosToLibrary]; // use ALAssetsLibrary to write to camera roll
}];

也许我的问题与editingMovieWriter的范围有关.也许事实是,我正在使用尝试写入的相同URL初始化GPUImageMovie实例.我已经阅读了GPUImage github问题页面上的几篇文章,关于SO,自述文件和上面链接的教程的几篇相关文章.

Perhaps my issue is with the scope of the editingMovieWriter. Or perhaps the fact that I am initializing a GPUImageMovie instance with the same URL that I am attempting to write to. I have read several posts on the issues page of the GPUImage github, several related posts on SO, the readme, and the tutorial linked above.

任何对此问题的见解将不胜感激.谢谢.

Any insight into this issue would be greatly appreciated. Thanks.

推荐答案

在这里可能至少有一件事.在上面的代码中,您并没有获得对movieFile源对象的强大引用.

There's at least one thing here that might be behind this. In the code above, you're not hanging on to a strong reference to your movieFile source object.

如果这是一个启用了ARC的项目,则在您完成设置方法的那一刻,该对象将被释放(如果不是,则将泄漏该对象).这将停止电影播放,重新分配电影本身,并导致黑帧沿过滤器管道发送(其他潜在的不稳定性).

If this is an ARC-enabled project, that object will be deallocated the instant that you complete your setup method (if it's not, you'll be leaking that object). That will stop movie playback, deallocate the movie itself, and lead to black frames being sent down the filter pipeline (other potential instabilities).

由于所有影片处理都是异步的,因此您需要使movieFile成为一个高度引用的实例变量,以确保它挂在此设置方法之上.

You need to make movieFile a strongly-referenced instance variable to make sure it hangs on past this setup method, since all movie processing is asynchronous.

这篇关于使用GPUImage过滤视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆