Как использовать метод removeTimeRange для AVMutableComposition?

Я создал несколько видео и хочу удалить последние 0,5 секунды всех треков в композиции. Я считаю, что removeTimeRange можно использовать в ситуации. Документ для этих методов читается как

Removes a specified timeRange from all tracks of the composition

Но не в состоянии выяснить диапазон, который должен быть предоставлен для достижения этого. Мой код композиции следующий:

AVAsset *asset0 = [self currentAsset:0];
AVAsset *asset1 = [self currentAsset:1];
AVAsset *asset2 = [self currentAsset:2];
AVAsset *asset3 = [self currentAsset:3];
AVAsset *asset4 = [self currentAsset:4];

NSArray *assets = @[asset0, asset1, asset2, asset3, asset4];

AVMutableComposition *mutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                                   preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                   preferredTrackID:kCMPersistentTrackID_Invalid];

NSMutableArray *instructions = [NSMutableArray new];
CGSize size = CGSizeZero;

CMTime time = kCMTimeZero;
for (AVAsset *asset in assets)
{
    AVAssetTrack *assetTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
    AVAssetTrack *audioAssetTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;

    NSError *error;
    [videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetTrack.timeRange.duration)
                                   ofTrack:assetTrack
                                    atTime:time
                                     error:&error];
    if (error) {
        NSLog(@"Error - %@", error.debugDescription);
    }

    [audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetTrack.timeRange.duration)
                                   ofTrack:audioAssetTrack
                                    atTime:time
                                     error:&error];
    if (error) {
        NSLog(@"Error - %@", error.debugDescription);
    }

    AVMutableVideoCompositionInstruction *videoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    videoCompositionInstruction.timeRange = CMTimeRangeMake(time, assetTrack.timeRange.duration);
    videoCompositionInstruction.layerInstructions = @[[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack]];
    [instructions addObject:videoCompositionInstruction];

    time = CMTimeAdd(time, assetTrack.timeRange.duration);

    if (CGSizeEqualToSize(size, CGSizeZero)) {
        size = assetTrack.naturalSize;;
    }
}

AVMutableVideoComposition *mutableVideoComposition = [AVMutableVideoComposition videoComposition];
mutableVideoComposition.instructions = instructions;

mutableVideoComposition.frameDuration = CMTimeMake(1, 30);
mutableVideoComposition.renderSize = size;

pi = [AVPlayerItem playerItemWithAsset:mutableComposition];
pi.videoComposition = mutableVideoComposition;

player = [AVPlayer playerWithPlayerItem:[[CameraEngine engine] pi]];

player.volume = 0.75;
playerLayer = [AVPlayerLayer playerLayerWithPlayer: player];

playerLayer.frame = self.bounds;
[self.layer addSublayer: playerLayer];
[playerLayer setNeedsDisplay];
[player play];

Я хочу удалить 0,5 секунды видео со всех 5 дорожек после композиции, потому что между ними появляются пустые кадры при смене дорожки.

Все треки в порядке (нет черной рамки в конце). Я попытался удалить кадры непосредственно из AVMutableCompositionTrack, но снова после композиции появляются пустые кадры.

Итак, я хочу знать, как создать этот временной диапазон?


person blancos    schedule 02.04.2015    source источник
comment
mattgemmell.com/what-have-you-tried   -  person Lord Zsolt    schedule 02.04.2015
comment
@LordZsolt: ценю ваше предложение. Отредактировал вопрос с необходимой информацией.   -  person blancos    schedule 02.04.2015
comment
О, вы действительно обновились на основе моего отзыва? Это редкость :) +1   -  person Lord Zsolt    schedule 02.04.2015