iOS AVFoundation - Show a time display over a video and export

后端 未结 2 802
一生所求
一生所求 2021-01-31 23:37

I want to show a display overlay over a video and export that video including this display. I had a look into the AVFoundation Framework, AVCompositions, AVAssets etc. but I sti

2条回答
  •  生来不讨喜
    2021-01-31 23:53

    Something like this...

    (NB: culled from a much larger project, so I may have included some unnecessary pieces by accident).

    You'll need to grab the CALayer of your clock / animation, and set it to the var myClockLayer (used 1/3 of the way down by the andimation tool).

    This also assumes your incoming video has just two tracks - audio and video. If you have more, you'll need to set the track id in "asTrackID:2" more carefully.

    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){}];
    

提交回复
热议问题