【发布时间】:2015-02-26 00:13:20
【问题描述】:
我正在编写一段代码,用于从 iOS 设备上的多个图像和多个视频生成幻灯片视频。我可以使用单个视频和多个图像来做到这一点,但我无法弄清楚如何将其增强为多个视频。
这里是the sample video 我能够用一个视频和两个图像生成。
这是准备导出器的主要例程。
// Prepare the temporary location to store generated video
NSURL * urlAsset = [NSURL fileURLWithPath:[StoryMaker tempFilePath:@"mov"]];
// Prepare composition and _exporter
AVMutableComposition *composition = [AVMutableComposition composition];
AVAssetExportSession* exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = urlAsset;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = [self _addVideo:composition time:timeVideo];
这里是 _addVideo:time: 方法,它创建了 videoLayer。
-(AVVideoComposition*) _addVideo:(AVMutableComposition*)composition time:(CMTime)timeVideo {
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = _sizeVideo;
videoComposition.frameDuration = CMTimeMake(1,30); // 30fps
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,timeVideo) ofTrack:_baseVideoTrack atTime:kCMTimeZero error:nil];
// Prepare the parent layer
CALayer *parentLayer = [CALayer layer];
parentLayer.backgroundColor = [UIColor blackColor].CGColor;
parentLayer.frame = CGRectMake(0, 0, _sizeVideo.width, _sizeVideo.height);
// Prepare images parent layer
CALayer *imageParentLayer = [CALayer layer];
imageParentLayer.frame = CGRectMake(0, 0, _sizeVideo.width, _sizeVideo.height);
[parentLayer addSublayer:imageParentLayer];
// Specify the perspecrtive view
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1.0 / imageParentLayer.frame.size.height;
imageParentLayer.sublayerTransform = perspective;
// Animations
_beginTime = 1E-10;
_endTime = CMTimeGetSeconds(timeVideo);
CALayer* videoLayer = [self _addVideoLayer:imageParentLayer];
[self _addAnimations:imageParentLayer time:timeVideo];
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
// Prepare the instruction
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
{
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, timeVideo);
AVAssetTrack *videoTrack = [[composition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
[layerInstruction setTransform:_baseVideoTrack.preferredTransform atTime:kCMTimeZero];
instruction.layerInstructions = @[layerInstruction];
}
videoComposition.instructions = @[instruction];
return videoComposition;
}
_addAnimation:time: 方法添加图像层,并调度包括_videoLayer在内的所有层的动画。
到目前为止一切正常。
但是,我不知道如何将第二个视频添加到此幻灯片中。
AVFoundation 编程指南中的示例使用多个视频合成指令(AVMutableVideoCompositionInstruction)来组合两个视频,但它只是将它们渲染为单个 CALayer 对象,该对象在 videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:inLayer: 方法(AVVideoCompositionCoreAnimationTool)中指定。
我想将两个视频轨道渲染到两个单独的图层(图层 1 和图层 2)中,并分别为它们设置动画,就像我正在处理与图像关联的图层一样。
【问题讨论】:
-
你有没有想过如何做到这一点?
-
不,我没有。我认为这是 AVFoundation 的局限性。
-
你实际上是在哪里将图像添加到图像层?
-
你能分享你的代码吗?
-
我有同样的要求,我被卡住了,因为目前核心动画工具只接受一个视频层,我想在不同的层播放视频。你成功了吗?如果是这样,请让我们都知道,你是如何做到这一点的。谢谢。
标签: ios video core-animation avfoundation avmutablecomposition