3
votes

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
Have you found solution? I am also facing same issue.Payal Maniyar

1 Answers

0
votes

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