0

次のコードを使用して複数を連結しています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);
}

結果として得られるコンポジションは、すべての元のアセットの合計デュレーションまで再生され、オーディオは正しく再生されますが、最初のアセットのビデオのみが再生され、最終フレームで一時停止します。

私が間違ったことについて何か考えはありますか?

元のアセットの順序は関係ありません。最初のビデオとすべてのオーディオが再生されます。

4

2 に答える 2

1

AVVideoCompositionInstructions のインスタンスで AVVideoComposition を作成する必要があります。このサンプル コードを確認してください。

興味のあるコードは、次の性質のものです。

AVMutableVideoCompositionLayerInstruction *videoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableVideoTrack];
[self.layerInstructions addObject:videoCompositionLayerInstruction];
        AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
        passThroughInstruction.timeRange = trackTimeRange;
        passThroughInstruction.layerInstructions = @[videoCompositionLayerInstruction];
[self.compositionInstructions addObject:passThroughInstruction];
于 2015-07-31T04:34:54.553 に答える
0

わかりました、それは私の間違いでした。ループのaddMutableTrackWithMediaType: に入れます。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);
}
于 2015-07-31T09:56:26.927 に答える