How to watermark your video with different images and different CMTimes using AVFoundation

谁都会走 提交于 2019-11-29 04:10:51

问题


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.

I've an AVAsset:

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

avasset_camera = [AVAsset assetWithURL:url];

An AVMutableComposition:

AVMutableComposition *mix = [AVMutableComposition composition];

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];

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;

The instruction for the VideoComposition:

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

And the instruction for the layer:

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

AVMutableVideoCompositionLayerInstruction *layerInstruction =  [AVMutableVideoCompositionLayerInstruction  videoCompositionLayerInstructionWithAssetTrack:videoTrack];

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

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

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).

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

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.


回答1:


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).



来源:https://stackoverflow.com/questions/16166433/how-to-watermark-your-video-with-different-images-and-different-cmtimes-using-av

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!