我正在使用 AVFoundation 在我的电影中添加水印。这适用于互联网和 Apple 上流传的代码。但我不想完整显示水印,我想在同一部电影中显示不同的水印。
我有一个 AVAsset:
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"MOV"];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
avasset_camera = [AVAsset assetWithURL:url];
一个 AVMutableComposition:
AVMutableComposition *mix = [AVMutableComposition composition];
UIImage 转换为 CALayer,然后添加到另一个层以与动画工具合并:
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:
AVMutableVideoComposition* videoComp = [[AVMutableVideoComposition videoComposition] retain];
videoComp.renderSize = videoSize;
videoComp.frameDuration = CMTimeMake(1, 30);
AVVideoCompositionCoreAnimationTool *animationVideoTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
videoComp.animationTool = animationVideoTool;
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 导出它
这将产生一个带有完整视频水印的视频。我想要实现的是来自相机的视频,前 5 秒带有水印。它会消失一段时间,然后显示另一个图像(也是水印)。
我被困住了......我已经在 AVFoundation 上观看了 WWDC 视频数万亿次,但它缺乏深入的视野。
当我更改指令的 timeRange 时,它不会导出,因为持续时间(范围)必须与 AVAssetTrack 的持续时间(范围)相同。我一直在尝试插入多个指令,但到目前为止没有成功。