正文
给视频添加水印的原理是把视频的每一帧都重新渲染,这个功能我们可以用GPUImage和AVFoundation两个框架来做。下面,我就来详细阐述这两种不同的方案。
AVFoundation
使用AVFoundation为视频添加水印步骤如下:
1.拿到视频和音频资源
2.创建AVMutableComposition对象
3.往AVMutableComposition对象添加视频资源,同时设置视频资源的时间段和插入点
4.往AVMutableComposition对象添加音频资源,同时设置音频资源的时间段和插入点
5.往AVMutableComposition对象添加要追加的音频资源,同时设置音频资源的时间段,插入点和混合模式
6.导出视频。导出视频依然使用的是AVAssetExportSession
/**
<#Description#>
@param videoPathURL 视频URL
@param img 水印图片
@param coverImg 水印图片
@param text 文字
*/
- (void)saveAVideoPath:(NSURL *)videoPathURL withWaterImg:(UIImage *)img coverImg:(UIImage *)coverImg text:(NSString *)text{
if (!videoPathURL) return;
//1 创建AVAsset实例 AVAsset包含了video的所有信息 self.videoUrl输入视频的路径
NSDictionary *opts = [NSDictionary dictionaryWithObject:@(YES) forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoPathURL options:opts]; //初始化视频媒体文件
CMTime startTime = kCMTimeZero;
CMTime endTime = CMTimeMakeWithSeconds(videoAsset.duration.value/videoAsset.duration.timescale, videoAsset.duration.timescale);
//2 创建AVMutableComposition实例
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
//3 视频通道 工程文件中的轨道,有音频轨、视频轨等,里面可以插入各种对应的素材
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
//把视频轨道数据加入到可变轨道中 这部分可以做视频裁剪TimeRange
[videoTrack insertTimeRange:CMTimeRangeMake(startTime, endTime)
ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
//说明有音频
if ([[videoAsset tracksWithMediaType:AVMediaTypeAudio] count]>0) {
//声音采集
AVURLAsset * audioAsset = [[AVURLAsset alloc] initWithURL:videoPathURL options:opts];
//音频通道
AVMutableCompositionTrack * audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
//音频采集通道
[audioTrack insertTimeRange:CMTimeRangeMake(startTime, endTime) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject] atTime:kCMTimeZero error:nil];
}
//3.1 AVMutableVideoCompositionInstruction 视频轨道中的一个视频,可以缩放、旋转等
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(startTime, endTime);
// 3.2 AVMutableVideoCompositionLayerInstruction 一个视频轨道,包含了这个轨道上的所有视频素材
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// UIImageOrientation videoAssetOrientation_ = UIImageOrientationUp;
BOOL isVideoAssetPortrait_ = NO;
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0) {
// videoAssetOrientation_ = UIImageOrientationRight;
isVideoAssetPortrait_ = YES;
}
if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0) {
// videoAssetOrientation_ = UIImageOrientationLeft;
isVideoAssetPortrait_ = YES;
}
// if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0) {
// videoAssetOrientation_ = UIImageOrientationUp;
// }
// if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) {
// videoAssetOrientation_ = UIImageOrientationDown;
// }
[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];
// [videolayerInstruction setOpacity:0.0 atTime:endTime];
// 3.3 - Add instructions
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];
//AVMutableVideoComposition:管理所有视频轨道,可以决定最终视频的尺寸,裁剪需要在这里进行
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
CGSize naturalSize;
if(isVideoAssetPortrait_){
naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);
} else {
naturalSize = videoAssetTrack.naturalSize;
}
float renderWidth, renderHeight;
renderWidth = naturalSize.width;
renderHeight = naturalSize.height;
mainCompositionInst.renderSize = CGSizeMake(renderWidth, renderHeight);
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1,30);
//生成水印
[self creatWaterWithComposition:mainCompositionInst waterImg:[UIImage imageNamed:@"avatar"] coverImg:[UIImage imageNamed:@"demo"] text:@"avtest" size:CGSizeMake(renderWidth, renderHeight)];
NSString *pathToMovie =[self getVideoSaveFilePathString:@".MOV" addPathArray:NO];
unlink([pathToMovie UTF8String]);
NSURL *compressionFileURL = [NSURL fileURLWithPath:pathToMovie];
AVAssetExportSession *exporter =[[AVAssetExportSession alloc]initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
exporter.outputURL=compressionFileURL;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = mainCompositionInst;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
switch (exporter.status) {
case AVAssetExportSessionStatusFailed:{
_hud.hidden =YES;
NSLog(@"Export Status: Fail :%@",exporter.error.localizedDescription);
break;
}
case AVAssetExportSessionStatusCancelled:{
NSLog(@"Export Status: Cancell");
break;
}
case AVAssetExportSessionStatusCompleted: {
_hud.hidden =YES;
[self savePhotosAlbum:compressionFileURL];
break;
}
case AVAssetExportSessionStatusUnknown: {
NSLog(@"Export Status: Unknown");
break;
}
case AVAssetExportSessionStatusExporting : {
NSLog(@"Export Status: Exporting");
break;
}
case AVAssetExportSessionStatusWaiting: {
NSLog(@"Export Status: Wating");
break;
}
};
});
}];
}
注:必须判断是否有音频[[videoAsset tracksWithMediaType:AVMediaTypeAudio] count]>0
,否则的话会出现问题。框架定义了一个名为AVURLAssetPreferPreciseDurationAndTimingKey
的选项,选项带有@YES值可以确保当资源的属性使用AVAsynchronousKeyValueLoading
协议载入时可以计算出准确的时长和时间信息。虽然使用这个option时还会载入过程增添一些额外开销,不过这可以保证资源正处于合适的编辑状态。生成水印的代码如下:
- (void)creatWaterWithComposition:(AVMutableVideoComposition *)composition waterImg:(UIImage *)img coverImg:(UIImage *)coverImg text:(NSString *)text size:(CGSize )size{
UIFont *font = [UIFont systemFontOfSize:30.0];
CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
[subtitle1Text setFontSize:30];
[subtitle1Text setString:text];
[subtitle1Text setAlignmentMode:kCAAlignmentCenter];
[subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]];
subtitle1Text.masksToBounds = YES;
subtitle1Text.cornerRadius = 23.0f;
[subtitle1Text setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor];
CGSize textSize = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
[subtitle1Text setFrame:CGRectMake(50, 100, textSize.width+20, textSize.height+10)];
//水印
CALayer *imgLayer = [CALayer layer];
imgLayer.contents = (id)img.CGImage;
// imgLayer.bounds = CGRectMake(0, 0, size.width, size.height);
imgLayer.bounds = CGRectMake(0, 0, 210, 50);
imgLayer.position = CGPointMake(size.width/2.0, size.height/2.0);
//第二个水印
CALayer *coverImgLayer = [CALayer layer];
coverImgLayer.contents = (id)coverImg.CGImage;
// [coverImgLayer setContentsGravity:@"resizeAspect"];
coverImgLayer.bounds = CGRectMake(50, 200,210, 50);
coverImgLayer.position = CGPointMake(size.width/4.0, size.height/4.0);
// 2 - The usual overlay
CALayer *overlayLayer = [CALayer layer];
[overlayLayer addSublayer:subtitle1Text];
[overlayLayer addSublayer:imgLayer];
overlayLayer.frame = CGRectMake(0, 0, size.width, size.height);
[overlayLayer setMasksToBounds:YES];
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, size.width, size.height);
videoLayer.frame = CGRectMake(0, 0, size.width, size.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:overlayLayer];
[parentLayer addSublayer:coverImgLayer];
//设置封面
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"opacity"];
anima.fromValue = [NSNumber numberWithFloat:1.0f];
anima.toValue = [NSNumber numberWithFloat:0.0f];
anima.repeatCount = 0;
anima.duration = 5.0f; //5s之后消失
[anima setRemovedOnCompletion:NO];
[anima setFillMode:kCAFillModeForwards];
anima.beginTime = AVCoreAnimationBeginTimeAtZero;
[coverImgLayer addAnimation:anima forKey:@"opacityAniamtion"];
composition.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
}
其中使用AVFoundation + CoreAnimation的合成方式为视频水印添加动态效果我以后还会讨论,今天就先到这里。
GPUImage
GPUImage使用GPUImageUIElement和GPUImageMovieWriter重新进行渲染,通过叠加滤镜来重新生成视频。滤镜可以采用GPUImageDissolveBlendFilter。这种方案相当于把视频重新录制一边,边录制边重新添加水印,所以兼容性比较好,几乎只要支持的视频格式全都可以处理,但是注意没有声音视频的情况,如果原视频没有声音,在创建新视频采集声音的时候,会出现错误Assertion failure in -[GPUImageMovieWriter createDataFBO]
,所以也要先判断是否有音频[[videoAsset tracksWithMediaType:AVMediaTypeAudio] count]>0
。总体来说GPUImage只是简单的提供了加载滤镜的变相方案,原理其实就是加了一个滤镜,这个滤镜的纹理是水印的那个图片,然后混合罢了,并没有提供更深层的编辑功能。不多说,代码如下:
/**
使用GPUImage加载水印
@param videoPathURL 视频路径
@param img 水印图片
@param coverImg 水印图片二
@param text 字符串水印
*/
- (void)saveVideoPath:(NSURL*)videoPathURL WithWaterImg:(UIImage*)img WithCoverImage:(UIImage*)coverImg WithText:(NSString*)text
{
if (!videoPathURL) return;
GPUImageOutput <GPUImageInput> *filter = [[GPUImageNormalBlendFilter alloc] init];
//滤镜
// filter =[[GPUImageDissolveBlendFilter alloc]init];
// [(GPUImageDissolveBlendFilter *)_filter setMix:0.0f];
//也可以使用透明滤镜
// filter =[[GPUImageAlphaBlendFilter alloc]init];
// [(GPUImageDissolveBlendFilter *)_filter setMix:1.0f];
AVAsset *asset = [AVAsset assetWithURL:videoPathURL];
CGSize size = asset.naturalSize;
_movieFile = [[GPUImageMovie alloc] initWithAsset:asset];
_movieFile.playAtActualSpeed = NO;
// 文字水印
UILabel *label = [[UILabel alloc] init];
label.text = text;
label.font = [UIFont systemFontOfSize:30];
label.textColor = [UIColor whiteColor];
[label setTextAlignment:NSTextAlignmentCenter];
[label sizeToFit];
label.layer.masksToBounds = YES;
label.layer.cornerRadius = 18.0f;
[label setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[label setFrame:CGRectMake(50, 100, label.frame.size.width+20, label.frame.size.height)];
//图片水印
UIImage *coverImage1 = [img copy];
UIImageView *coverImageView1 = [[UIImageView alloc] initWithImage:coverImage1];
[coverImageView1 setFrame:CGRectMake(0, 100, 210, 50)];
//第二个图片水印
UIImage *coverImage2 = [coverImg copy];
UIImageView *coverImageView2 = [[UIImageView alloc] initWithImage:coverImage2];
[coverImageView2 setFrame:CGRectMake(270, 100, 210, 50)];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
subView.backgroundColor = [UIColor clearColor];
[subView addSubview:coverImageView1];
[subView addSubview:coverImageView2];
[subView addSubview:label];
GPUImageUIElement *element = [[GPUImageUIElement alloc] initWithView:subView];
NSString *pathToMovie =[self getVideoSaveFilePathString:@".MOV" addPathArray:NO];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
self.movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720.0, 1280.0)];
GPUImageFilter* progressFilter = [[GPUImageFilter alloc] init];
[progressFilter addTarget:filter];
[_movieFile addTarget:progressFilter];
[element addTarget:filter];
self.movieWriter.shouldPassthroughAudio = YES;
if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] > 0){
_movieFile.audioEncodingTarget = self.movieWriter;
} else {
//no audio
_movieFile.audioEncodingTarget = nil;
}
// movieFile.playAtActualSpeed = true;
[_movieFile enableSynchronizedEncodingUsingMovieWriter:self.movieWriter];
// 显示到界面
[filter addTarget:self.movieWriter];
[self.movieWriter startRecording];
[_movieFile startProcessing];
WeakSelf(self);
//渲染
[progressFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {
//水印可以移动
CGRect frame = coverImageView1.frame;
frame.origin.x += 1;
frame.origin.y += 1;
coverImageView1.frame = frame;
//第5秒之后隐藏coverImageView2
if (time.value/time.timescale>=5.0) {
[coverImageView2 removeFromSuperview];
}
[element update];
}];
[self.movieWriter setCompletionBlock:^{
StrongSelf(self);
dispatch_async(dispatch_get_main_queue(), ^{
[self.hud hideAnimated:YES];
[filter removeTarget:self.movieWriter];
[self.movieWriter finishRecording];
//保存相册
[self savePhotosAlbum:movieURL];
});
}];
}
代码中在progressFilter setFrameProcessingCompletionBlock的回调中,设置了各个元素的显示、移动,这样的话就可以更加自由的设置水印的显示与否,比如水印在刚开始显示,五秒之后消失等功能,这个相当于每一帧都在渲染,所以如果是采用什么动画效果的话可以直接设置frame即可。
有什么问题欢迎大家留言讨论。