Something như thế này ...
(NB: chọn lọc từ một dự án lớn hơn nhiều, vì vậy tôi có thể bao gồm một số phần không cần thiết một cách tình cờ).
Bạn sẽ cần phải lấy CALayer của đồng hồ/hoạt hình của bạn, và đặt nó vào var myClockLayer (sử dụng 1/3 đường xuống bởi công cụ andimation).
Điều này cũng giả định video đến của bạn chỉ có hai bản nhạc - âm thanh và video. Nếu bạn có nhiều hơn, bạn cần đặt id theo dõi trong "asTrackID: 2" cẩn thận hơn.
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){}];
Tôi muốn làm điều tương tự. Bạn đã thực hiện việc này chưa? Bạn có thể giúp tôi rằng làm thế nào tôi có thể cập nhật "myClockLayer" Theo câu trả lời của Adam. – Nikunj
bạn có nhận được gì liên quan đến điều này không ??? –