I'm adding three clips to an AVMutableComposition
like this...
let asset = AVURLAsset(url: url, options: [ AVURLAssetPreferPreciseDurationAndTimingKey : true ])
let track = composition.addMutableTrack(withMediaType: .video,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
try track?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asset.duration),
of: asset.tracks(withMediaType: .video)[0],
at: composition.duration)
} catch {
print("Failed to load track")
}
So, that's the full duration of each asset, added at the current end of the composition. I've tried different orders and there's always one flash of the background in between two clips.
I've tried setting at:
to be the sum of the previously added clip's duration but it doesn't change the final result.
I'm also setting the tracks to zero opacity when they've finished so that they don't cover up subsequent tracks.
instruction.setOpacity(0.0, at: composition.duration)
Possibly, the opacity is switching on too early. But this instruction also uses the asset's duration - it's the same data.
I've looked in the debugger and when there's a gap between two clips, the second clip start value is exactly the same as the previous clips' duration (where the first clip starts at 0), and the opacity is also switched on at the same time value. So it looks like I'm at least feeding the composition correct data.
How do I remove the gap?