这可能是因为第一个视频的结束时间与另一个视频的开始时间不匹配。可以在第一个AVAssetTrack中遍历视频轨道,查找最后一帧的展示时间,然后将其与总持续时间比较。另外,在第一个视频的AVMutableCompositionTrack上设置结束时间作为最后一帧的展示时间可能会有所帮助。
示例代码:
let composition = AVMutableComposition() let firstAsset: AVAsset = //第一个视频 let secondAsset: AVAsset = //第二个视频
//添加第一个视频到AVMutableComposition中 let firstTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) try? firstTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: firstAsset.duration), of: firstAsset.tracks(withMediaType: .video)[0], at: CMTime.zero)
//添加第二个视频到AVMutableComposition中 let secondTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) try? secondTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: secondAsset.duration), of: secondAsset.tracks(withMediaType: .video)[0], at: firstAsset.duration)
//查找第一个视频的最后一帧的展示时间 let firstTrackDuration = firstTrack?.timeRange.duration let firstTrackLastFrameTime = CMTimeAdd(firstTrackDuration?.start ?? CMTime.zero, firstTrackDuration ?? CMTime.zero)
//将第一个视频的AVMutableCompositionTrack的结束时间设置为最后一帧的展示时间 try? firstTrack?.insertEmptyTimeRange(CMTimeRangeMake(start: firstTrackLastFrameTime, duration: CMTime.zero))