示例:使用 GPUImageMovie 为视频添加滤镜,并保存文件(简单使用)
//
// ViewController.m
// GPUImageTest2
//
// Created by 黄健 on 16/7/2.
// Copyright © 2016年 黄健. All rights reserved.
//
#import "ViewController.h"
#import "GPUImage.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet GPUImageView *gpuImageView;
@property (nonatomic,strong) GPUImageMovie *movie;
@property (nonatomic,strong) GPUImageMovieWriter *writer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 视频路径(同上一篇 GPUImageMovieWriter 文章)
NSURL *sampleURL = [[NSBundle mainBundle]URLForResource:@"demo" withExtension:@"mp4" subdirectory:nil];
// 初始化 movie
_movie = [[GPUImageMovie alloc] initWithURL:sampleURL];
_movie.shouldRepeat = NO;
_movie.playAtActualSpeed = YES;
// 设置加滤镜视频保存路径
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mp4"];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
// 初始化
_writer = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480, 640)];
_writer.encodingLiveVideo = NO;
_writer.shouldPassthroughAudio = NO;
/**
如果你设置了 _movie.audioEncodingTarget = _writer;
会报如下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetWriterInput appendSampleBuffer:] Cannot append sample buffer: Input buffer must be in an uncompressed format when outputSettings is not nil'
暂时没去深究,以后再解决!
*/
// 添加滤镜
GPUImageToonFilter *filter = [[GPUImageToonFilter alloc] init];
[_movie addTarget:filter];
[filter addTarget:_writer];
[_movie enableSynchronizedEncodingUsingMovieWriter:_writer];
[_writer startRecording];
[_movie startProcessing];
__weak typeof(self) weakSelf = self;
[_writer setCompletionBlock:^{
NSLog(@"OK");
[filter removeTarget:weakSelf.writer];
[weakSelf.writer finishRecording];
}];
}
@end
》》测试
运行结果就是:加滤镜处理后的新视频大小3M(原视频674KB)。
》》问题
处理中CPU->140%,处理完CPU->0%
处理中内存->122MB,处理完内存->73MB
参考文章
使用GPUImage实现视频滤镜