【问题标题】:How can I add a watermark in a captured video on iOS [duplicate]如何在iOS上捕获的视频中添加水印[重复]
【发布时间】:2013-04-02 15:50:17
【问题描述】:

我想知道是否有人可以告诉我如何实现这一目标。如果正在考虑几个解决方案:

  1. 从捕获的视频中创建单个图像,然后将它们合并为每个图像,然后创建一个新的 AVAsset...您不觉得有点复杂吗?

  2. 合并 2 个视频,一个是透明的(一个带有水印的),另一个是相机拍摄的资产。

【问题讨论】:

    标签: iphone ios objective-c ipad avasset


    【解决方案1】:

    此代码在视频上添加文本或字符串,保存视频后,您将在任何播放器上播放。 此代码的最大优点是提供带声音的视频。一切尽在一个代码中(即文本和图像)。

    #import <AVFoundation/AVFoundation.h>
    
    -(void)MixVideoWithText
    {
        AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil];
        AVMutableComposition* mixComposition = [AVMutableComposition composition];
    
        AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        AVAssetTrack *clipAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        //If you need audio as well add the Asset Track for audio here
    
        [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
        [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
    
        [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];
    
        CGSize sizeOfVideo=[videoAsset naturalSize];
    
        //TextLayer defines the text they want to add in Video
        //Text of watermark 
        CATextLayer *textOfvideo=[[CATextLayer alloc] init];
        textOfvideo.string=[NSString stringWithFormat:@"%@",text];//text is shows the text that you want add in video.
        [textOfvideo setFont:(__bridge CFTypeRef)([UIFont fontWithName:[NSString stringWithFormat:@"%@",fontUsed] size:13])];//fontUsed is the name of font
        [textOfvideo setFrame:CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height/6)];
        [textOfvideo setAlignmentMode:kCAAlignmentCenter];
        [textOfvideo setForegroundColor:[selectedColour CGColor]];
    
        //Image of watermark 
        UIImage *myImage=[UIImage imageNamed:@"one.png"];
        CALayer layerCa = [CALayer layer];
        layerCa.contents = (id)myImage.CGImage;
        layerCa.frame = CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height); 
        layerCa.opacity = 1.0; 
    
        CALayer *optionalLayer=[CALayer layer];
        [optionalL addSublayer:textOfvideo];
        optionalL.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
        [optionalL setMasksToBounds:YES];
    
        CALayer *parentLayer=[CALayer layer];
        CALayer *videoLayer=[CALayer layer];
        parentLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
        videoLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
        [parentLayer addSublayer:videoLayer];
        [parentLayer addSublayer:optionalLayer];
        [parentLayer addSublayer:layerCa];
    
        AVMutableVideoComposition *videoComposition=[AVMutableVideoComposition videoComposition] ;
        videoComposition.frameDuration=CMTimeMake(1, 30);
        videoComposition.renderSize=sizeOfVideo;
        videoComposition.animationTool=[AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
    
        AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
        instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);
        AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
        instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
        videoComposition.instructions = [NSArray arrayWithObject: instruction];
    
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
        NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/utput_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];
    
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
        exportSession.videoComposition=videoComposition;
    
        exportSession.outputURL = [NSURL fileURLWithPath:destinationPath];
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch (exportSession.status)
        {
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export OK");
                if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(destinationPath)) {
                    UISaveVideoAtPathToSavedPhotosAlbum(destinationPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
                }
                break;
            case AVAssetExportSessionStatusFailed:
                NSLog (@"AVAssetExportSessionStatusFailed: %@", exportSession.error);
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export Cancelled");
                break;
        }
      }];
    }
    

    显示保存视频后会出现的错误。

    -(void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo 
    {
        if(error)
            NSLog(@"Finished saving video with error: %@", error); 
    }
    

    【讨论】:

    • 注意上面的行“videoComposition.frameDuration=CMTimeMake(1, 10);”应该是“videoComposition.frameDuration=CMTimeMake(1, 30);”默认帧率为 30。看起来也更好。
    • 你能建议我如何为 CATextLayer 设置合适的框架吗?看我的问题stackoverflow.com/questions/31780060/…
    • 这会将视频保存在错误的方向。有什么解决办法吗?
    • [layerInstruction setTransform: ] 中使用CGAffineTransform。它会给你准确的方向
    • 对视网膜水印图像的像素化有什么建议吗?玩 contentsScale 和 renderScale 并没有帮助。感谢您的帮助!
    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2017-03-24
    • 1970-01-01
    • 2018-01-28
    相关资源
    最近更新 更多