简体   繁体   English

如何在AVMutableComposition中使用多个轨道

[英]How to use multiple tracks in an AVMutableComposition

Please see the code below. 请参见下面的代码。 I'm trying to add a 2nd track so that I have a smaller video overlaid on top of a background video. 我正在尝试添加第二首曲目,以便在背景视频之上叠加一个较小的视频。 However I can only get the background video to display in the final exported file. 但是,我只能将背景视频显示在最终导出的文件中。 What am I doing wrong? 我究竟做错了什么? And how do I control the order of tracks, ie the layering of the composition? 以及如何控制音轨的顺序,即构图的分层?

    AVMutableComposition* composition = [[AVMutableComposition alloc] init];

    NSURL* backgroundURL = [NSURL fileURLWithPath:backgroundOutputPath];        
    AVURLAsset* url0 = [AVURLAsset URLAssetWithURL:backgroundURL options:nil];        

    AVMutableCompositionTrack *backgroundVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack* bgAssetTrack = [[url0 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    [backgroundVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url0 duration])  ofTrack:bgAssetTrack atTime:kCMTimeZero error:&error];

     NSURL* videoURL = [[NSBundle mainBundle] URLForResource: @"star" withExtension:@"mp4"];
     AVURLAsset* url = [AVURLAsset URLAssetWithURL:videoURL options:nil];

     AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

     AVAssetTrack *clipVideoTrack = [[url tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
     [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration])  ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error];



    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];


    NSString *exportPath = [documentsDirectoryPath stringByAppendingPathComponent:@"test_AV.mp4"];

    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    NSURL *exportURL = [NSURL fileURLWithPath:exportPath];

    exportSession.outputURL = exportURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        NSLog (@"Exporting. status is %d",
               exportSession.status);
        switch (exportSession.status) {
            case AVAssetExportSessionStatusFailed:
            case AVAssetExportSessionStatusCompleted: {
                NSLog(@"export done");
                break;
            }
        };
    }];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导出多个轨道:AVMutableComposition和AVAssetExportSession - Exporting Multiple Tracks: AVMutableComposition and AVAssetExportSession 在AVMutableComposition中的多个视频轨道之间切换 - Switching between multiple video tracks in a AVMutableComposition 如何在 iOS 上使用 AVMutableComposition 和 CALayers - How to use AVMutableComposition and CALayers on iOS 如何同时将 AVAssetReader 和 AVAssetWriter 用于多个轨道(音频和视频)? - How to use AVAssetReader and AVAssetWriter for multiple tracks (audio and video) simultaneously? 如何在AVQueuePlayer或AVMutableComposition中播放多个视频而无需任何打算或冻结? - How to play multiple videos in AVQueuePlayer or AVMutableComposition without any gapes or freeze? 如何使用iOS上的AVMutableComposition和CALayers将多个CALayer添加到视频文件中 - How to add multiple CALayer to a video file using AVMutableComposition and CALayers on iOS 如何在 AVMutableComposition 中同时为多个视频层设置动画? - How to animate multiple video layers at the same time in AVMutableComposition? AVMutableComposition - 如何将多个音频记录与 1 个视频记录合并 - AVMutableComposition -How to Merge Multiple Audio Recordings with 1 Video Recording 如何在mpmovieplayer上播放avmutablecomposition? - how to play an avmutablecomposition on mpmovieplayer? 如何将 AVMutableComposition 传递给 AVAssetWriter - How to pass AVMutableComposition to AVAssetWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM