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

后端 未结 2 801
一生所求
一生所求 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:51

    I think you can use AVCaptureVideoDataOutput to process each frame and use AVAssetWriter to record the processed frame.You can refer to this answer

    https://stackoverflow.com/a/4944594/379941 .

    use AVAssetWriterPixelBufferAdaptor's appendPixelBuffer:withPresentationTime: method to export
    And I strongly suggest using OpenCV to process frame. this is a nice tutorial

    http://aptogo.co.uk/2011/09/opencv-framework-for-ios/.

    OpenCV library is very great.

    0 讨论(0)
  • 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){}];
    
    0 讨论(0)
提交回复
热议问题