欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

视频水印

程序员文章站 2022-03-27 20:29:38
...

#pragma mark =====下面是合成视频的方法===========

- (void)vision:(PBJVision *)vision capturedVideo:(NSDictionary *)videoDict error:(NSError *)error

{

    _recording = NO; //这句可能是控制完成存储之后就不能再录的。。。

    

    if (error) {

        NSLog(@"encounted an error in video capture (%@)", error);

        return;

    }

    _currentVideo = videoDict;

    NSString *videoPath = [_currentVideo  objectForKey:PBJVisionVideoPathKey];

 

    

    NSString* videoName = @"export.mov"; //这里换成wmv格式的就不行了。

    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];

    

    [self loadVideoByPath:videoPath andSavePath:exportPath];

    

    

    

}

 

#pragma mark ==== 合成的========

 

- (void) loadVideoByPath:(NSString*) v_strVideoPath andSavePath:(NSString*) v_strSavePath {

    

     NSLog(@"\nv_strVideoPath = %@ \nv_strSavePath = %@\n ",v_strVideoPath,v_strSavePath);

     AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]];

     CMTime assetTime = [avAsset duration];

     Float64 duration = CMTimeGetSeconds(assetTime);

     NSLog(@"视频时长 %f\n",duration);

    

     AVMutableComposition *avMutableComposition = [AVMutableCompositioncomposition];

    

     AVMutableCompositionTrack *avMutableCompositionTrack = [avMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];

    

     AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    

     NSError *error = nil;

     // 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长

     [avMutableCompositionTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0.1f, 30), CMTimeMakeWithSeconds(duration, 30))

                      ofTrack:avAssetTrack

                       atTime:kCMTimeZero

                       error:&error];

    

     AVMutableVideoComposition *avMutableVideoComposition = [AVMutableVideoCompositionvideoComposition];

    

     avMutableVideoComposition.renderSize = CGSizeMake(320.0f, 480.0f);

     avMutableVideoComposition.frameDuration = CMTimeMake(1, 30);

    

//     CALayer *animatedTitleLayer = [self buildAnimatedTitleLayerForSize:CGSizeMake(320, 88)];

    

    UIImage *waterMarkImage = [UIImage imageNamed:@"logo.png"];

    CALayer *waterMarkLayer = [CALayer layer];

    waterMarkLayer.frame = CGRectMake(0, 60, 320, 222);

    waterMarkLayer.contents = (id)waterMarkImage.CGImage;

    

    

     CALayer *parentLayer = [CALayer layer];

     CALayer *videoLayer = [CALayer layer];

     parentLayer.frame = CGRectMake(0, 0, 320, 480);

     videoLayer.frame = CGRectMake(0, 0, 320, 480);

     [parentLayer addSublayer:videoLayer];

     [parentLayer addSublayer:waterMarkLayer];

     avMutableVideoComposition.animationTool = [AVVideoCompositionCoreAnimationToolvideoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

    

     AVMutableVideoCompositionInstruction *avMutableVideoCompositionInstruction = [AVMutableVideoCompositionInstructionvideoCompositionInstruction];

    

     [avMutableVideoCompositionInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];

    

     AVMutableVideoCompositionLayerInstruction *avMutableVideoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstructionvideoCompositionLayerInstructionWithAssetTrack:avAssetTrack];

     [avMutableVideoCompositionLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];

    

     avMutableVideoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositionLayerInstruction];

    

    

     avMutableVideoComposition.instructions = [NSArray arrayWithObject:avMutableVideoCompositionInstruction];

    

    

     NSFileManager *fm = [[NSFileManageralloc] init];

     if ([fm fileExistsAtPath:v_strSavePath]) {

          NSLog(@"video is have. then delete that");

          if ([fm removeItemAtPath:v_strSavePath error:&error]) {

               NSLog(@"delete is ok");

              }else {

                   NSLog(@"delete is no error = %@",error.description);

                  }

         }

    

    

     AVAssetExportSession *avAssetExportSession = [[AVAssetExportSessionalloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x480];

     [avAssetExportSession setVideoComposition:avMutableVideoComposition];

     [avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];

     avAssetExportSession.outputFileType = @"com.apple.quicktime-movie";

//     [avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie];//这句话要是要的话,会出错的。。。

     [avAssetExportSession setShouldOptimizeForNetworkUse:YES];

     [avAssetExportSession exportAsynchronouslyWithCompletionHandler:^(void){

 

 

 

 

 

 

 

 

//其实只要进入这个方法合成就已经成功了。。。。不需要再用下面的switch 了。。。这个问题我找了三天。。。终于发现了。。。。。。。把下面的switch语句删除就行了。,直接把成功以后的代码写到本方法就ok了。。。。。。。。。。。。。。。。。。。

          switch (avAssetExportSession.status) {

                   caseAVAssetExportSessionStatusFailed:

                    NSLog(@"exporting failed %@",[avAssetExportSession error]);

                    break;

                   caseAVAssetExportSessionStatusCompleted:

                    NSLog(@"exporting completed");

                  //下面是按照上面的要求合成视频的过程。

                       // 下面是把视频存到本地相册里面,存储完后弹出对话框。

                  NSLog(@"该视频的大小为:%lf M",[self fileSizeAtPath:v_strSavePath]);

                       [_assetLibrary writeVideoAtPathToSavedPhotosAlbum:avAssetExportSession.outputURL completionBlock:^(NSURL *assetURL, NSError *error1) {

                           UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"好的!" message: @"整合并保存成功!"

                                                                          delegate:nil

                                                                 cancelButtonTitle:@"OK"

                                                                 otherButtonTitles:nil];

                           [alert show];

                           

                       }];

                    break;

                   caseAVAssetExportSessionStatusCancelled:

                  

            

                  NSLog(@"export cancelled");

                 

                    break;

              }

         }];

     if (avAssetExportSession.status != AVAssetExportSessionStatusCompleted){

          NSLog(@"Retry export");

         }

}

 

- (CALayer *)buildAnimatedTitleLayerForSize:(CGSize)videoSize

{

     // 视频的显示大小

     CGSize dataLayerSize = CGSizeMake(320.0f, 88.0f);

     // Create a layer for the overall title animation.

     CALayer *animatedTitleLayer = [CALayer layer];

     // 由于旋转过了,所以高与宽互相转换了,原来 480*640 由于旋转 成了640*480

     // 他们的起点还是左下角

     animatedTitleLayer.frame = CGRectMake(0.0f, 0, dataLayerSize.width, dataLayerSize.height);

     animatedTitleLayer.backgroundColor = [UIColor clearColor].CGColor;

     // 水印

     UIImage *waterMarkImage = [UIImage imageNamed:@"logo.png"];

     CALayer *waterMarkLayer = [CALayer layer];

     waterMarkLayer.contents = (id)waterMarkImage.CGImage ;

     waterMarkLayer.frame = CGRectMake(0, 0, 320, 88);

     waterMarkLayer.opacity = 0.6f;

     // 文字

     CATextLayer *textLayer = [CATextLayer layer];

     textLayer.string = @"Add your context";

     textLayer.font = (__bridge CFTypeRef)(@"Helvetica");

     textLayer.fontSize = 20.0f;

     textLayer.shadowOpacity = 0.6f ;

     textLayer.backgroundColor = [UIColorclearColor].CGColor ;

     textLayer.foregroundColor = [UIColorredColor].CGColor ;

     textLayer.frame = CGRectMake(15.0f, dataLayerSize.height-20.0f-15.0f, dataLayerSize.width, 20.0f);

     [animatedTitleLayer addSublayer:waterMarkLayer];

     [animatedTitleLayer addSublayer:textLayer];

     return animatedTitleLayer;

}

//单个文件的大小

//- (long long) fileSizeAtPath:(NSString*) filePath{

//    NSFileManager* manager = [NSFileManager defaultManager];

//    if ([manager fileExistsAtPath:filePath]){

//        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

//    }

//    return 0;

//}

//遍历文件夹获得文件夹大小,返回多少M

- (float ) folderSizeAtPath:(NSString*) folderPath{

    NSFileManager* manager = [NSFileManagerdefaultManager];

    if (![manager fileExistsAtPath:folderPath]) return 0;

    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

    NSString* fileName;

    long long folderSize = 0;

    while ((fileName = [childFilesEnumerator nextObject]) != nil){

        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

        folderSize += [self fileSizeAtPath:fileAbsolutePath];

    }

    return folderSize/(1024.0*1024.0);

 

}