【问题标题】:How to use multiple tracks in an AVMutableComposition如何在 AVMutableComposition 中使用多个轨道
【发布时间】:2014-01-31 18:07:23
【问题描述】:

请看下面的代码。我正在尝试添加第二个轨道,以便在背景视频之上覆盖一个较小的视频。但是我只能让背景视频显示在最终导出的文件中。我究竟做错了什么?以及如何控制曲目的顺序,即作曲的分层?

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

【问题讨论】:

  • 嘿!我和我的团队正在为同样的问题而疯狂。你解决了吗?

标签: ios avfoundation avassetexportsession avmutablecomposition avasset


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多