【问题标题】:AVMutableComposition - concatenated video assets stops after first assetAVMutableComposition - 连接的视频资产在第一个资产后停止
【发布时间】:2015-07-30 15:51:31
【问题描述】:

我正在使用以下代码连接多个AVURLAssets

AVMutableComposition * movie = [AVMutableComposition composition];

CMTime offset = kCMTimeZero;

for (AVURLAsset * asset in assets) {
    AVMutableCompositionTrack *compositionVideoTrack = [movie addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *compositionAudioTrack = [movie addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
    AVAssetTrack *assetAudioTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;

    CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
    NSError * error = nil;

    if (![compositionVideoTrack insertTimeRange: timeRange ofTrack: assetVideoTrack atTime: offset error: &error]) {
        NSLog(@"Error adding video track - %@", error);
    }
    if (![compositionAudioTrack insertTimeRange: timeRange ofTrack: assetAudioTrack atTime: offset error: &error]) {
        NSLog(@"Error adding audio track - %@", error);
    }

    offset = CMTimeAdd(offset, asset.duration);
}

合成的合成播放到所有原始资产的总时长,并且音频正确播放,但仅播放第一个资产的视频,然后在其最后一帧暂停。

对我做错了什么有什么想法吗?

原始资产的顺序无关紧要 - 第一个视频和所有音频播放。

【问题讨论】:

    标签: ios objective-c avfoundation avmutablecomposition avurlasset


    【解决方案1】:

    您需要使用 AVVideoCompositionInstructions 的实例创建一个 AVVideoComposition。看看这个sample code

    您会感兴趣的代码是这样的:

    AVMutableVideoCompositionLayerInstruction *videoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableVideoTrack];
    [self.layerInstructions addObject:videoCompositionLayerInstruction];
            AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
            passThroughInstruction.timeRange = trackTimeRange;
            passThroughInstruction.layerInstructions = @[videoCompositionLayerInstruction];
    [self.compositionInstructions addObject:passThroughInstruction];
    

    【讨论】:

    • 我不确定您从哪里得到这个想法,但这是错误的答案(请参阅我自己的解决方案)。不过感谢您的尝试。
    • 您的解决方案确实有效。我正在考虑一种更具可扩展性的解决方案,您希望在其中使用多个合成轨道而不是插入同一个合成轨道。使用 AVMutableVideoCompositionLayerInstructions、AVMutableVideoCompositionInstruction 和 AVVideoComposition 可以让您执行更多编辑操作,例如过渡、叠加等。很高兴您找到了适合您的东西。
    • Johnathan,你到底是如何在这里执行循环的?我使用了代码 vut 视频没有循环播放,只播放一次..
    【解决方案2】:

    好吧,这是我的错误。我会把addMutableTrackWithMediaType: inside 放在for 循环中。傻我。固定如下,它就像一个魅力!

    我会留在这里,以防其他人有同样的问题。

    AVMutableComposition * movie = [AVMutableComposition composition];
    AVMutableCompositionTrack *compositionVideoTrack = [movie addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *compositionAudioTrack = [movie addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    
    CMTime offset = kCMTimeZero;
    
    for (AVURLAsset * asset in assets) {
    
        AVAssetTrack *assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
        AVAssetTrack *assetAudioTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
    
        CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
        NSError * error = nil;
    
        if (![compositionVideoTrack insertTimeRange: timeRange ofTrack: assetVideoTrack atTime: offset error: &error]) {
            NSLog(@"Error adding video track - %@", error);
        }
        if (![compositionAudioTrack insertTimeRange: timeRange ofTrack: assetAudioTrack atTime: offset error: &error]) {
            NSLog(@"Error adding audio track - %@", error);
        }
    
        offset = CMTimeAdd(offset, asset.duration);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多