iOS 菜鸟之旅音视频

iOS-单个视频进行滤镜处理

2019-11-07  本文已影响0人  涛涛灬灬

视频预览层
self.preview = [[GPUImageView alloc] initWithFrame:self.view.bounds];
self.preview.layer.contentsScale = 2.0;
[self.preview setBackgroundColorRed:0.0 green:0.0 blue:0.0 alpha:1.0];
// self.preview.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
[self.view addSubview:self.preview];

GPUImageViewMove预览播放的视频没有声音 如果需要播放声音 使用AVPlayer进行处理

manger处理

//开始进行滤镜渲染

// 设置输出路径
NSString * pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
// - 如果文件已存在,AVAssetWriter不允许直接写进新的帧,所以会删掉老的视频文件
unlink([pathToMovie UTF8String]);
self.movieURL = [NSURL fileURLWithPath:pathToMovie];

// 输出 后面的size可改 ~ 现在来说480*640有点太差劲了
AVAsset *asset = [AVAsset assetWithURL:url];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
//    CGAffineTransform t = videoTrack.preferredTransform;//这里的矩阵有旋转角度,转换一下即可

NSLog(@"=====hello  width:%f===height:%f",videoTrack.naturalSize.width,videoTrack.naturalSize.height);//宽高

_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:self.movieURL size:videoTrack.naturalSize];

[self.filter addTarget:_movieWriter];

_movieWriter.shouldPassthroughAudio = YES;
_movieFile.audioEncodingTarget = _movieWriter;
[_movieFile enableSynchronizedEncodingUsingMovieWriter:_movieWriter];

// 完成之后的回调 - 为啥100%了之后还会待一会才调用这个completeBlock
__weak typeof(self) weakself = self;
[_movieWriter setCompletionBlock:^{
    __strong typeof (weakself) strongSelf = weakself;
    [strongSelf.filter removeTarget:strongSelf.movieWriter];
    [strongSelf.movieWriter finishRecording];
    if (callBack) {
        callBack(strongSelf.movieURL);
    }

// // 异步写入相册
// dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// dispatch_async(concurrentQueue, ^{
// [strongSelf saveVideo:strongSelf.movieURL];
// });

}];
[self.movieWriter startRecording];
[_movieFile startProcessing];

}

//videoPath为视频下载到本地之后的本地路径

}

}

上一篇下一篇

猜你喜欢

热点阅读