简单介绍
2020/04 已重构 支持cocoapod
这篇文章是自己做短视频功能这几个月的一个总结,以下文字或代码有任何不妥的地方希望各位积极指出错误,并给出建议。
从短视频拍摄基本功能来说,我想应该有以下,美颜,水印,断点拍摄,编辑功能,编辑功能包含,滤镜添加,背景音乐添加,贴图贴纸添加,高质量压缩(视频降低码率)。针对这些功能,我会逐一介绍其实现方法。
使用到的第三方库:TZImagePickerController,GPUImage,SDAVAssetExportSession
demo效果图
文章末尾会附上demo地址
预览画面支持美颜切换,前后相机切换
录制中
暂停录制,支持断点回删
滤镜选择共8重
选择音乐
贴纸选择,可随意调整贴纸位置
预览发布页面,按各自实际app需求调整
录制功能实现
录制功能是基于GPUImageVideoCamera实现的:
相机初始化
videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
给相机添加滤镜
filter = [[LFGPUImageEmptyFilter alloc] init]; //这个滤镜是一个空滤镜
filteredVideoView = [[GPUImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //创建预览画面
[videoCamera addTarget:filter];
[filter addTarget:filteredVideoView];
开起预览
[videoCamera startCameraCapture]; //启动相机
切换至美颜滤镜
[videoCamera removeAllTargets]; //移除其它滤镜
filter = [[GPUImageBeautifyFilter alloc] init];//美颜滤镜
[videoCamera addTarget:filter];
[filter addTarget:filteredVideoView];
视频录制以及断点续拍
断点续拍的实质就是把多个视频合并成同一视频
开始录制
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720.0, 1280.0)];
movieWriter.isNeedBreakAudioWhiter = YES;
movieWriter.encodingLiveVideo = YES;
movieWriter.shouldPassthroughAudio = YES;
[filter addTarget:movieWriter];
videoCamera.audioEncodingTarget = movieWriter;
[movieWriter startRecording];
暂停录制
暂停录制就是把视频先保存起来,并将视频地址保存在数组中,这样就能实现断点续拍。
[movieWriter finishRecording];
[filter removeTarget:movieWriter];
[urlArray addObject:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@",pathToMovie]]];
拍摄完成后的合成操作以及水印添加
创建音视频合成体,以及视频通道容器,音频通道容器
[[AVMutableComposition alloc] init];
[mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
[mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
用AVAsset取出素材,并且分别取出音频轨道 与视频轨道按时间措添加至 上面创建的音视频容器中
AVAsset* asset = [AVURLAsset URLAssetWithURL:videosPathArray[i] options:options];
//获取AVAsset中的音频 或者视频
AVAssetTrack *assetAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] firstObject];
//向通道内加入音频或者视频
BOOL ba = [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:assetAudioTrack
atTime:totalDuration
error:&erroraudio];
//向通道内加入视频步骤与上面音频的添加差不多
视频水印的添加,创建layer ,并将layer 添加AVMutableVideoComposition 这个对象中
视频导出,创建AVAssetExportSession 对象 ,并为其设置videoComposition属性,以此添加水印
//视频导出工具
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPreset1280x720];
exporter.videoComposition = videoComp;
导出方法 - (void)exportAsynchronouslyWithCompletionHandler:(void (^)(void))handler;
导出成功后进入下一个视频编辑页面
视频滤镜处理
创建视频预览,使用GPUImageMovie工具 并结合avPlayer
mainPlayer = [[AVPlayer alloc] init];
playerItem = [[AVPlayerItem alloc] initWithURL:videoURL];
[mainPlayer replaceCurrentItemWithPlayerItem:playerItem];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:mainPlayer];
movieFile = [[GPUImageMovie alloc] initWithPlayerItem:playerItem];
movieFile.runBenchmark = YES; movieFile.playAtActualSpeed = YES;
//新建一个空白滤镜添加至 movieFile
filter = [[LFGPUImageEmptyFilter alloc] init];
_filtClassName = @"LFGPUImageEmptyFilter";
[movieFile addTarget:filter];
//视频预览容器view
_filterView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_filterView];
[filter addTarget:_filterView];
开始预览方法:- (void)startProcessing;
//avplay开始播放
[mainPlayer play];
[movieFile startProcessing];
切换滤镜效果
[movieFile removeAllTargets];
//新建一个新的滤镜文件
filter = [[NSClassFromString(_filtClassName) alloc] init];
//将滤镜添加至 movieFile上
[movieFile addTarget:filter];
//重新添加预览容器view
[filter addTarget:_filterView];
生成带滤镜处理后的视频
//1.新建用于导出的MovieFile
endMovieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
endMovieFile.runBenchmark = YES;
endMovieFile.playAtActualSpeed = NO;
//2.给MovieFile添加滤镜
endFilter = [[NSClassFromString(_filtClassName) alloc] init];
[endMovieFile addTarget:endFilter];
//3.新建GPUImageMovieWriter写入类
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720.0, 1280.0)];
[endFilter addTarget:movieWriter];
movieWriter.shouldPassthroughAudio = YES;
endMovieFile.audioEncodingTarget = movieWriter;
[endMovieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];
//4.开始写入
[movieWriter startRecording];
[endMovieFile startProcessing];
[movieWriter setCompletionBlock:^{
//.完成后的逻辑处理
}];
音乐合成处理/贴图合成处理
此处方法原理 与第一页视频合成,水印处理基本相同。前者就是将音频素材 添加到音频轨道中, 后者还是视频layer上加layer;
视频的压缩处理
NSDictionary* options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES};
AVAsset* asset = [AVURLAsset URLAssetWithURL:inputVideoUrl options:options];
NSArray* keys = @[@"tracks",@"duration",@"commonMetadata"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
SDAVAssetExportSession *compressionEncoder = [SDAVAssetExportSession.alloc initWithAsset:asset]; // provide inputVideo Url Here
compressionEncoder.outputFileType = AVFileTypeMPEG4;//文件输出格式
compressionEncoder.outputURL = outputVideoUrl; //文件输出地址
//视频参数配置
compressionEncoder.videoSettings = @
{
AVVideoCodecKey: AVVideoCodecH264,//编码方式
AVVideoWidthKey: @720, //视频宽
AVVideoHeightKey: @1280, //视频高
AVVideoCompressionPropertiesKey: @
{
//2000*1000 建议800*1000-5000*1000
// AVVideoAverageBitRateKey: @2500000, // Give your bitrate here for lower size give low values
AVVideoAverageBitRateKey: _bit,//比特lv
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
AVVideoAverageNonDroppableFrameRateKey: _frameRate,//fps
},
};
//音频参数配置
compressionEncoder.audioSettings = @
{
AVFormatIDKey: @(kAudioFormatMPEG4AAC),//编码方式
AVNumberOfChannelsKey: @2,//通道,双通道
AVSampleRateKey: @44100,//菜样率
AVEncoderBitRateKey: @128000,//编码比特lv
};
[compressionEncoder exportAsynchronouslyWithCompletionHandler:^
{
//处理成功
}];
}];