Hey! I just solved this problem and am very happy to share my working solution.
The trick here is that two videos can have different fps. And we need to update the frameDuration AVMutableVideoComposition parameter of our AVPlayerItem
in general, the code looks like this
enum Const {
static let perciseValue = 1000000
}
struct SemgentModel {
let framerate: Float
let timeRange: CMTimeRange
}
var mySegments: [SemgentModel] = ..add segments
var player: AVPlayer?
func updateFramerateOfComposition(newRate: Float) {
let mutableComposition = player?.playerItem?.videoComposition?.mutableCopy() as? AVMutableVideoComposition
mutableComposition?.frameDuration = CMTime(value: CMTimeValue(1 * Const.perciseValue), timescale: CMTimeScale(newRate * Float(Const.perciseValue)))
player?.playerItem?.videoComposition = mutableComposition
}
func setTime(time: CMTime) {
if let segment = mySegments.first(where: { $0.timeRange.contains(time) }) {
updateFramerateOfComposition(newRate: segment.framerate)
}
player?.seek( to: seekTargetTime, toleranceBefore: .zero, toleranceAfter: .zero) { ... }
}