如何使用AVFoundation为您的视频添加不同图像和不同CMTime的水印 [英] How to watermark your video with different images and different CMTimes using AVFoundation

查看:116
本文介绍了如何使用AVFoundation为您的视频添加不同图像和不同CMTime的水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVFoundation在我的电影中添加水印。这适用于互联网和Apple上的代码。但我不想在完整的时间内显示水印,我想在同一部电影中显示不同的水印。

I'm using AVFoundation to put a watermark in my movies. This works well with the code that's been going around on the internet and Apple. But I don't want to show the watermark the complete time and I want to show different watermarks in the same movie.

我有一个AVAsset:

I've an AVAsset:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"MOV"];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

avasset_camera = [AVAsset assetWithURL:url];

AVMutableComposition:

An AVMutableComposition:

AVMutableComposition *mix = [AVMutableComposition composition];

UIImage转换为CALayer而不是添加到另一层以与animationTool合并:

The UIImage converted to a CALayer and than added to another layer to incorporate with the animationTool:

UIImage *myImage = [UIImage imageNamed:@"watermark.png"];
CALayer *aLayer = [CALayer layer];
aLayer.contents = (id)myImage.CGImage;
aLayer.frame = CGRectMake(0, 0, 568, 320);     
aLayer.opacity = 1.0;

CGSize videoSize = [avasset_camera naturalSize];
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:aLayer];

而不是AVMutableVideoComposition:

And than a AVMutableVideoComposition:

AVMutableVideoComposition* videoComp = [[AVMutableVideoComposition videoComposition]  retain];
videoComp.renderSize = videoSize;
videoComp.frameDuration = CMTimeMake(1, 30);
AVVideoCompositionCoreAnimationTool *animationVideoTool =    [AVVideoCompositionCoreAnimationTool   videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer   inLayer:parentLayer];

videoComp.animationTool = animationVideoTool;

VideoComposition的说明:

The instruction for the VideoComposition:

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction  videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, avasset_camera.duration);

该图层的说明:

AVAssetTrack *videoTrack = [[mix tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableVideoCompositionLayerInstruction *layerInstruction =  [AVMutableVideoCompositionLayerInstruction  videoCompositionLayerInstructionWithAssetTrack:videoTrack];

instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComp.instructions = [NSArray arrayWithObject: instruction];

然后使用具有VideoComposition属性的AVAssetExportSession导出它

And than export it with an AVAssetExportSession with the property of the VideoComposition

这将产生带有完整视频水印的视频。我想要实现的是来自摄像头的视频,前5秒是水印。它比一段时间消失了,而且还显示了另一张图像(也是水印)。

This will result in a video with the watermark for the complete video. What I want to achieve is a video from camera with the first 5 seconds the watermark. It than disappears for some time and than another image is shown(also a watermark).

我被卡住了......我在AVFoundation上观看了WWDC视频数万亿次,但缺乏深入的视线。

I'm stuck... I've watched the WWDC vid on AVFoundation for trillions of times but it lacks in-depth sight.

当我更改指令的timeRange时,它不会导出,因为持续时间(范围)必须相同在AVAssetTrack的那个。我一直试图插入多个指令但到目前为止没有成功。

When I change the timeRange of the instruction it doesn't export because the duration (range) has to be the same at that of the AVAssetTrack. I've been trying to insert multiple instructions but so far with no success.

推荐答案

AVMutableVideoCompositionLayerInstruction有一个方法[setOpacityRampFromStartOpacity:toEndOpacity: timeRange:]您可以为不同的段设置(每个layerInstruction不重叠)。不是最好的解决方案是创建两个视频轨道 - 一个原始视频,另一个在需要时具有水印和渐变不透明度(在某些片段上仅显示原始视频,在其他片段上显示水印)。

AVMutableVideoCompositionLayerInstruction has a method [setOpacityRampFromStartOpacity: toEndOpacity: timeRange:] which you can set for different segments (non overlaping for each layerInstruction). Not the best solution would be to create two video tracks - one original video and another with watermark and ramp opacity when needed (showing only original on some segments and with watermark on others).

这篇关于如何使用AVFoundation为您的视频添加不同图像和不同CMTime的水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆