I managed to solve this problem after many hours of testing. I need to change two things. 1) add the “exact” option when creating the asset.
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES}; AVURLAsset *videoAsset3 = [AVURLAsset URLAssetWithURL:clip3URL options:options];
2) do not use
CMTime duration3 = [videoAsset3 duration];
use
AVAssetTrack *videoAsset3Track = [[videoAsset3 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; CMTime duration3 = videoAsset3Track.timeRange.duration;
I found out about this after setting the background color of AVPlayer to blue, then I noticed the appearance of blue frames, so the problem is related to timing. After I changed the settings above, the various videos aligned when using:
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration3) ofTrack:videoAsset3Track atTime:kCMTimeZero error:&error];
elprl
source share