美文网首页
GPUImage视频编辑

GPUImage视频编辑

作者: 夜空丶 | 来源:发表于2017-06-12 18:27 被阅读0次

在上一篇文章:GPUImage的使用(图片渲染篇) 中已经介绍了如何接入GPUImage,并且实现了简单的图片渲染,在这片文章中我来介绍一下如何给已保存的视频进行编辑。

给视频渲染,最基本的需要声明三个变量:GPUImageMovie(视频载体),GPUImageOutput(滤镜),GPUImageMovieWriter(写入视频文件类)。

@interface VideoEditViewController : UIViewController
{
    //预览用
    GPUImageMovie *movieFile;
    //保存用
    GPUImageMovie *saveMovie;
    //预览用的滤镜
    GPUImageOutput<GPUImageInput> *filter;
    //保存用的滤镜
    GPUImageOutput<GPUImageInput> *savefilter;
    GPUImageMovieWriter *saveWrite;
    
}

因为GPUImageMovieWriter保存编辑视频的时候是从预览到的那一帧开始写入的,所以我把预览跟保存用的GPUImageMovie和GPUImageOutput分成两个对象来操作,也就是说,预览的时候用一个GPUImageMovie对象,保存的时候用另外一个新的GPUImageMovie对象

先看预览的代码:

//初始化GPUImageMovie
    movieFile = [[GPUImageMovie alloc] initWithURL:self.pathURL];
    //设置预览重复播放
    movieFile.shouldRepeat = YES;
    movieFile.runBenchmark = YES;
    //设置渲染速度与视屏播放速度一致
    movieFile.playAtActualSpeed = YES;
    
    //初始化磨皮滤镜
    filter = [[GPUImageBeautifyFilter alloc] init];
    //原图
    filter = [[GPUImageBrightnessFilter alloc] init];
    [(GPUImageBrightnessFilter *)filter setBrightness:0];
    savefilter = [[GPUImageBrightnessFilter alloc] init];
    [(GPUImageBrightnessFilter *)savefilter setBrightness:0];
    //把滤镜添加到GPUImageMovie中
    [movieFile addTarget:filter];
    
    // Only rotate the video for display, leave orientation the same for recording
    //创建预览的GPUImageView并显示
    filterView = [[GPUImageView alloc] init];
    filterView.frame = CGRectMake(0, 64, ScreenWidth, ScreenWidth);
//    filterView.transform = CGAffineTransformMakeRotation(90 * M_PI/180.0);
    
    [self.view addSubview:filterView];
    [self.view sendSubviewToBack:filterView];
    //把滤镜内容显示到预览中
    [filter addTarget:filterView];
    //开始预览
    [movieFile startProcessing];
 //重新创建GPUImageMovie用于保存
    saveMovie = [[GPUImageMovie alloc] initWithURL:self.pathURL];
    saveMovie.shouldRepeat = YES;
    saveMovie.runBenchmark = YES;
    saveMovie.playAtActualSpeed = YES;
    
    [saveMovie addTarget:savefilter];
    
    //保存路径
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.mp4"];
    unlink([path UTF8String]);
    saveURL = [NSURL fileURLWithPath:path];
    //清空路径里的文件
    [[NSFileManager defaultManager] removeItemAtURL:saveURL error:nil];
    
    saveWrite = [[GPUImageMovieWriter alloc] initWithMovieURL:saveURL size:CGSizeMake(ScreenWidth, ScreenWidth) fileType:AVFileTypeMPEG4 outputSettings:nil];
    [savefilter addTarget:saveWrite];
    
    saveWrite.shouldPassthroughAudio = NO;
    saveWrite.encodingLiveVideo = YES;
    
//    saveMovie.audioEncodingTarget = saveWrite;
    [saveMovie enableSynchronizedEncodingUsingMovieWriter:saveWrite];
    [saveWrite startRecording];
    [saveMovie startProcessing];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES].label.text = @"正在导出。。。";
    __weak VideoEditViewController *weakSelf = self;
    __weak GPUImageOutput<GPUImageInput> *weakSaveFilter = savefilter;
    __weak GPUImageMovieWriter *weakmovieWriter = saveWrite;
    __weak GPUImageMovie *weakSaveMovie = saveMovie;
    __weak NSURL *weakSaveURL = saveURL;
    [saveWrite setCompletionBlock:^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
                [weakSaveFilter removeTarget:weakmovieWriter];
                [weakmovieWriter finishRecording];
                [weakSaveMovie endProcessing];
                [weakSaveMovie cancelProcessing];
                [weakSaveMovie removeTarget:weakSaveFilter];
                [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
                NSLog(@"保存成功");
        });
     
    }];

注意:

1:路径问题

NSURL *saveURL = [NSURL fileURLWithPath:savePath];

创建URL要用fileURLWithPath不能用URLWithString,否则会报错!

2:保存时报couldn't write a frame

[saveWrite startRecording];

方法一定要在

[saveMovie startProcessing];

方法后面,不然编辑有时候会编辑失败

相关文章

网友评论

      本文标题:GPUImage视频编辑

      本文链接:https://www.haomeiwen.com/subject/brtrqxtx.html