iOS AVFoundation – 通过video和导出显示时间显示
我想要在video上显示叠加显示,并导出包含此显示的video。 我看了一下AVFoundation框架,AVCompositions,AVAssets等,但我还没有想法实现这一点。 有一个名为AVSynchronizedLayer的类,它可以让你为animation同步animation,但是我不想animation,我只是想把时间显示叠加到video的每一帧中。 有什么build议?
问候
像这样的东西…
(注意:从一个更大的项目中剔除,所以我可能会偶然地包含一些不必要的部分)。
你需要抓住你的时钟/animation的CALayer,并将其设置为var myClockLayer(由animation工具使用1/3)。
这也假定您的传入video只有两个音轨 – audio和video。 如果你有更多,你需要更仔细地设置“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){}];
我想你可以使用AVCaptureVideoDataOutput来处理每一帧,并使用AVAssetWriterlogging处理后的帧。你可以参考这个答案
使用AVAssetWriterPixelBufferAdaptor的appendPixelBuffer:withPresentationTime:方法导出
我强烈build议使用OpenCV来处理帧。 这是一个很好的教程
OpenCV库非常棒。