2011-01-20 72 views
8

我想在視頻上顯示疊加顯示並導出包含此顯示的視頻。我查看了AVFoundation Framework,AVCompositions,AVAssets等,但我仍然沒有想法實現這一點。有一個名爲AVSynchronizedLayer的類,它可以讓動畫與視頻同步動畫,但我不想動畫,我只想將時間顯示疊加到視頻的每一幀中。有什麼建議?iOS AVFoundation - 通過視頻顯示時間顯示並導出

問候

+0

我想要做同樣的事情。你做到了嗎?你能幫我,我怎麼能更新「myClockLayer」根據亞當的答案。 – Nikunj 2013-02-25 06:38:54

+0

你有什麼關於這個? – 2013-11-01 06:33:38

回答

5

事情是這樣的......

(注:從一個更大的項目剔除,所以我可能已包括事故發生一些不必要的作品)。

您需要抓住時鐘/動畫的CALayer,並將其設置爲var myClockLayer(由動畫工具使用的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){}];