IOS视频编辑功能详解上篇-添加水印
IOS视频编辑功能详解上篇-添加水印
前言
一、视频加水印
1.1、GPUImage方案
/** 使用GPUImage加载水印 @param vedioPath 视频路径 @param img 水印图片 @param coverImg 水印图片二 @param question 字符串水印 @param fileName 生成之后的视频名字 */-(void)saveVedioPath:(NSURL*)vedioPath WithWaterImg:(UIImage*)img WithCoverImage:(UIImage*)coverImg WithQustion:(NSString*)question WithFileName:(NSString*)fileName{[SVProgressHUD showWithStatus:@"生成水印视频到系统相册"];// 滤镜// filter = [[GPUImageDissolveBlendFilter alloc] init];// [(GPUImageDissolveBlendFilter *)filter setMix:0.0f];//也可以使用透明滤镜// filter = [[GPUImageAlphaBlendFilter alloc] init];// //mix即为叠加后的透明度,这里就直接写1.0了// [(GPUImageDissolveBlendFilter *)filter setMix:1.0f];filter = [[GPUImageNormalBlendFilter alloc] init];NSURL *sampleURL = vedioPath;AVAsset *asset = [AVAsset assetWithURL:sampleURL];CGSize size = asset.naturalSize;movieFile = [[GPUImageMovie alloc] initWithAsset:asset];movieFile.playAtActualSpeed = NO;// 文字水印UILabel *label = [[UILabel alloc] init];label.text = question;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 *uielement = [[GPUImageUIElement alloc] initWithView:subView];NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.mp4",fileName]];unlink([pathToMovie UTF8String]);NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720.0, 1280.0)];GPUImageFilter* progressFilter = [[GPUImageFilter alloc] init];[progressFilter addTarget:filter];[movieFile addTarget:progressFilter];[uielement addTarget:filter];movieWriter.shouldPassthroughAudio = YES;// movieFile.playAtActualSpeed = true;if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] > 0){movieFile.audioEncodingTarget = movieWriter;} else {//no audiomovieFile.audioEncodingTarget = nil;}[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];// 显示到界面[filter addTarget:movieWriter];[movieWriter startRecording];[movieFile startProcessing];// dlink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgress)];// [dlink setFrameInterval:15];// [dlink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];// [dlink setPaused:NO];__weak typeof(self) weakSelf = self;//渲染[progressFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {//水印可以移动CGRect frame = coverImageView1.frame;frame.origin.x += 1;frame.origin.y += 1;coverImageView1.frame = frame;//第5秒之后隐藏coverImageView2if (time.value/time.timescale>=5.0) {[coverImageView2 removeFromSuperview];}[uielement update];}];//保存相册[movieWriter setCompletionBlock:^{dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{__strong typeof(self) strongSelf = weakSelf;[strongSelf->filter removeTarget:strongSelf->movieWriter];[strongSelf->movieWriter finishRecording];__block PHObjectPlaceholder *placeholder;if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(pathToMovie)){NSError *error;[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:movieURL];placeholder = [createAssetRequest placeholderForCreatedAsset];} error:&error];if (error) {[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error]];}else{[SVProgressHUD showSuccessWithStatus:@"视频已经保存到相册"];}}});}];}
-(void)useGpuimage{NSURL *videoPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"selfS" ofType:@"MOV"]];[self saveVedioPath:videoPath WithWaterImg:[UIImage imageNamed:@"avatar.png"] WithCoverImage:[UIImage imageNamed:@"demo.png"] WithQustion:@"文字水印:hudongdongBlog" WithFileName:@"waterVideo"];}
NSAssert(status == GL_FRAMEBUFFER_COMPLETE, @"Incomplete filter FBO: %d", status);
if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] > 0){movieFile.audioEncodingTarget = movieWriter;} else {//no audiomovieFile.audioEncodingTarget = nil;}
1.2、AVFoundation方案
///使用AVfoundation添加水印- (void)AVsaveVideoPath:(NSURL*)videoPath WithWaterImg:(UIImage*)img WithCoverImage:(UIImage*)coverImg WithQustion:(NSString*)question WithFileName:(NSString*)fileName{if (!videoPath) {return;}//1 创建AVAsset实例 AVAsset包含了video的所有信息 self.videoUrl输入视频的路径//封面图片NSDictionary *opts = [NSDictionary dictionaryWithObject:@(YES) forKey:AVURLAssetPreferPreciseDurationAndTimingKey];videoAsset = [AVURLAsset URLAssetWithURL:videoPath options:opts]; //初始化视频媒体文件CMTime startTime = CMTimeMakeWithSeconds(0.2, 600);CMTime endTime = CMTimeMakeWithSeconds(videoAsset.duration.value/videoAsset.duration.timescale-0.2, videoAsset.duration.timescale);//声音采集AVURLAsset * audioAsset = [[AVURLAsset alloc] initWithURL:videoPath options:opts];//2 创建AVMutableComposition实例. apple developer 里边的解释 【AVMutableComposition is a mutable subclass of AVComposition you use when you want to create a new composition from existing assets. You can add and remove tracks, and you can add, remove, and scale time ranges.】AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];//3 视频通道 工程文件中的轨道,有音频轨、视频轨等,里面可以插入各种对应的素材AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];//把视频轨道数据加入到可变轨道中 这部分可以做视频裁剪TimeRange[videoTrack insertTimeRange:CMTimeRangeMake(startTime, endTime)ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]atTime:kCMTimeZero error:nil];//音频通道AVMutableCompositionTrack * audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];//音频采集通道AVAssetTrack * audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];[audioTrack insertTimeRange:CMTimeRangeMake(startTime, endTime) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];//3.1 AVMutableVideoCompositionInstruction 视频轨道中的一个视频,可以缩放、旋转等AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoTrack.timeRange.duration);// 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 instructionsmainInstruction.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.renderSize = CGSizeMake(renderWidth, renderHeight);mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];mainCompositionInst.frameDuration = CMTimeMake(1, 25);[self applyVideoEffectsToComposition:mainCompositionInst WithWaterImg:img WithCoverImage:coverImg WithQustion:question size:CGSizeMake(renderWidth, renderHeight)];// 4 - 输出路径NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",fileName]];unlink([myPathDocs UTF8String]);NSURL* videoUrl = [NSURL fileURLWithPath:myPathDocs];dlink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgress)];[dlink setFrameInterval:15];[dlink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];[dlink setPaused:NO];// 5 - 视频文件输出exporter = [[AVAssetExportSession alloc] initWithAsset:mixCompositionpresetName:AVAssetExportPresetHighestQuality];exporter.outputURL=videoUrl;exporter.outputFileType = AVFileTypeQuickTimeMovie;exporter.shouldOptimizeForNetworkUse = YES;exporter.videoComposition = mainCompositionInst;[exporter exportAsynchronouslyWithCompletionHandler:^{dispatch_async(dispatch_get_main_queue(), ^{//这里是输出视频之后的操作,做你想做的[self exportDidFinish:exporter];});}];}- (void)applyVideoEffectsToComposition:(AVMutableVideoComposition *)composition WithWaterImg:(UIImage*)img WithCoverImage:(UIImage*)coverImg WithQustion:(NSString*)question size:(CGSize)size {UIFont *font = [UIFont systemFontOfSize:30.0];CATextLayer *subtitle1Text = [[CATextLayer alloc] init];[subtitle1Text setFontSize:30];[subtitle1Text setString:question];[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 = [question 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 overlayCALayer *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 = [AVVideoCompositionCoreAnimationToolvideoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];}//更新生成进度- (void)updateProgress {[SVProgressHUD showProgress:exporter.progress status:NSLocalizedString(@"生成中...", nil)];if (exporter.progress>=1.0) {[dlink setPaused:true];[dlink invalidate];// [SVProgressHUD dismiss];}}
[videoTrack insertTimeRange:CMTimeRangeMake(startTime, endTime)ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]atTime:kCMTimeZero error:nil];[audioTrack insertTimeRange:CMTimeRangeMake(startTime, endTime) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];
float renderWidth, renderHeight;
参考文章
美颜滤镜篇
IOS使用GPUImage滤镜初级试水
GPUImage录像的一些备忘
GPUImage录制加美颜、柔光
GPUImage详细解析(三)- 实时美颜滤镜
GPUImage详细解析(十)用GPUImage和指令配合合并视频
ios GPUImage简单滤镜 -- 录制视频(保存+聚焦)
GPUImage 实现自定义相机
GPUImage详细解析(二)
GPUImage--美颜滤镜GPUImageBeautifyFilter
使用GPUImage开启的相机进行摄像,保存写入到Path
水印篇
视频编辑功能详解上篇-添加水印
GPUImage详细解析(七)文字水印和动态图像水印
给GPUImage录制的视频添加水印
iOS 视频剪切、旋转,视频添加音频、添加水印,视频导出
视频特效制作:如何给视频添加边框、水印、动画以及3D效果
ios 视频编辑,添加文字、图片(CA动画)水印,合成视频
iOS 视频剪辑 (添加水印,裁剪,合并视频,添加背景音乐)
[Assertion failure in -[GPUImageMovieWriter createDataFBO]](http://blog.csdn.net/think_ma/article/details/43342251)
The current version, the video processing error at the beginning
来源https://blog.csdn.net/walkerwqp/article/details/79163821?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-2&spm=1001.2101.3001.4242