我想在视频上显示一个显示叠加层并导出包含此显示的视频.我看了AVFoundation框架,AVCompositions,AVAssets等,但我仍然没有想到实现这一目标.有一个名为AVSynchronizedLayer的类可以让你动画与视频同步的动画,但是我不想动画,我想要将时间显示叠加到视频的每一帧中.任何建议?
问候
解决方法
这样的东西……
(注意:从一个更大的项目中淘汰,所以我可能偶然包含了一些不必要的部分).
您需要获取时钟/动画的CALayer,并将其设置为var myClockLayer(使用andimation工具使用1/3).
这也假设您的传入视频只有两个轨道 – 音频和视频.如果你有更多,你需要更仔细地设置“asTrackID:2”中的轨道ID.
AVURLAsset* url = [AVURLAsset URLAssetWithURL:incomingVideo options:nil]; AVMutableComposition *videoComposition = [AVMutableComposition composition]; AVMutableCompositionTrack *compositionVideoTrack = [videoComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; AVAssetTrack *clipVideoTrack = [[url tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,[url duration]) ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error]; AVMutableVideoComposition* videoComposition = [[AVMutableVideoComposition videoComposition]retain]; videoComposition.renderSize = CGSizeMake(320,240); videoComposition.frameDuration = CMTimeMake(1,30); videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:myClockLayer asTrackID:2]; AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; instruction.timeRange = CMTimeRangeMake(kCMTimeZero,CMTimeMakeWithSeconds(60,30) ); AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack]; instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; videoComposition.instructions = [NSArray arrayWithObject: instruction]; exporter = [[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality] ; exporter.videoComposition = videoComposition; exporter.outputURL=url3; exporter.outputFileType=AVFileTypeQuickTimeMovie; [exporter exportAsynchronouslyWithCompletionHandler:^(void){}];