含泪把这块代码贴这
GPUImage我就不多说了(原理试着研究)
#import "GPUImage.h"
#import "GPUImageBeautifyFilter.h"// git上的大牛封装的
/**
GPUImage 链式控件
*/
@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;
@property (nonatomic , strong) GPUImageMovieWriter *movieWriter;
@property (nonatomic, strong) GPUImageView *filterView;
@property (nonatomic,strong)GPUImageBeautifyFilter *beautifyFilter;
初始化链式反应
self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionFront];
//输出图像旋转方式
self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
self.videoCamera.horizontallyMirrorFrontFacingCamera = YES;
self.filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
[self.view addSubview:self.filterView];
[self movieWriterSet];
- (void)movieWriterSet
{
//该句可防止允许声音通过的情况下,避免录制第一帧黑屏闪屏(====)
[self.videoCamera addAudioInputsAndOutputs];
// 保存按URL
self.url = [[RAFileManager defaultManager] filePathUrlWithUrl:[self getVideoSaveFilePathString]];
_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:self.url size:CGSizeMake(kScreenWidth, kScreenHeight)];
self.videoCamera.audioEncodingTarget = _movieWriter;
_movieWriter.encodingLiveVideo = YES;
[self.videoCamera startCameraCapture];
// 创建滤镜:磨皮,美白,组合滤镜
GPUImageFilterGroup *groupFilter = [[GPUImageFilterGroup alloc] init];
// 磨皮滤镜
GPUImageBilateralFilter *bilateralFilter = [[GPUImageBilateralFilter alloc] init];
// 值越小,磨皮效果越好
[bilateralFilter setDistanceNormalizationFactor:60];
[groupFilter addTarget:bilateralFilter];
// 美白滤镜 -- 会闪屏 暂弃
// GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
// //美白效果
// brightnessFilter.brightness = 0.8;
// [groupFilter addTarget:brightnessFilter];
// 设置滤镜组链
// [bilateralFilter addTarget:brightnessFilter];
[groupFilter setInitialFilters:@[bilateralFilter]];
// groupFilter.terminalFilter = brightnessFilter;
// 设置GPUImage响应链,从数据源 => 滤镜 => 最终界面效果
[self.videoCamera addTarget:groupFilter];
[groupFilter addTarget:self.filterView];
self.beautifyFilter = [[GPUImageBeautifyFilter alloc] init];
[self.videoCamera addTarget:self.beautifyFilter];
[self.beautifyFilter addTarget:self.filterView];
[self.beautifyFilter addTarget:_movieWriter];
}
因为可能会有录制失败的时候,或者录制不符合要求,需要重新录制
比如我们就要求录制时间大于30s,有点蛋疼的赶脚
//移除之前所有处理链
[self.videoCamera removeAllTargets];
// 重建处理链
[self movieWriterSet];
基本的处理就这么多了,不过我们的需求是要像映客样,录制完成后接着反复播放录制的视频
这里有个方法,不是方法,就是个block
[self.movieWriter setCompletionBlock:^{}];
当我欣喜的写上后,发现播放的视频会黑屏,这就尴尬了
过了很久,我终于发现
我解决不了...
不过我发现我把录制完的视频地址拿出来再播放是没有问题的,明锐如我猜到可能是录制完接着就播放,这里播放时机不对,但我™不知道具体原因啊。。。
// 加了延时,直接跳页面会掉帧,可能没有保存完整 没有找到更好的解决办法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{...});
不说了 工头喊我搬砖呢