gpt4 book ai didi

ios - 对视频文件应用滤镜

转载 作者:IT王子 更新时间:2023-10-29 08:16:32 26 4
gpt4 key购买 nike

我想在视频播放时对视频文件应用滤镜(效果)。

我目前正在使用@BradLarson 的(很棒的)GPUImage 框架来执行此操作,这里的问题是该框架不支持在播放视频时播放音频。

所以我有两个选择:

1) 深入研究 GPUImage 代码并更改 GPUImageMovie,以便它也能处理音频缓冲区。这需要同步音频和视频帧的知识,不幸的是我没有。我看到一些 hack 试图用 AVAudioPlayer 播放音频但是有很多同步问题。

2) 使用CoreImage框架代替GPUImage

所以我想看看使用 native iOS CoreImageCIFilter 来完成这项工作的第二个选项。

问题是,我找不到任何关于如何使用 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;

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;
}
}];

关于ios - 对视频文件应用滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18505053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com