gpt4 book ai didi

ios - AVMutableComposition - 连接的视频 Assets 在第一个 Assets 后停止

转载 作者:行者123 更新时间:2023-11-29 01:47:34 25 4
gpt4 key购买 nike

我正在使用以下代码连接多个 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);
}

生成的合成一直播放到所有原始 Assets 的组合持续时间,音频播放正确,但只有第一个 Assets 的视频播放,然后在其最后一帧暂停。

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

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

最佳答案

您需要使用 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];

关于ios - AVMutableComposition - 连接的视频 Assets 在第一个 Assets 后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com