Adding a blank video clip at the end of an AVMutableComposition fails

I'm struggling with a problem that drives me crazy.



I have an `AVMutableComposition` and am adding videos and animation-layers to it.



At the end of the whole composition, I want to show a JPEG for 5 seconds.



I tried using `insertEmptyTimeRange`, but apparently, this fails when used at the end of a composition.



So I created a 5 second video that just shows a black screen and add this at the end of the composition.



The final goal is an AVExportSession that saves the whole video to the device.



This works up to a certain amount of videos I stitch together, but after a certain amount, I get a weird error message saying:



Export failed: Error Domain=AVFoundationErrorDomain Code=-11821 "Cannot Decode" UserInfo={NSLocalizedFailureReason=The media data could not be decoded. It may be damaged., NSLocalizedDescription=Cannot Decode, NSUnderlyingError=0x1c46507d0 {Error Domain=NSOSStatusErrorDomain Code=-16977 "(null)"}}



When I just PLAY the video, using an AVPlayer, everything works fine.



Someone in another thread suggested it might have something to do with the used time-ranges. But they're all fine!



Here's the NSLog of the times that are stored in an array as well as a totalPlayTime I accumulate while stitching the videos together:



composition start **************************
    2018-07-10 22:12:03.536511+0200 StoryCatcher[6884:5992609] Part-Zero-Time 0.000000
    2018-07-10 22:12:03.536595+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 5.000000
    2018-07-10 22:12:03.536736+0200 StoryCatcher[6884:5992609] insert-point 5.000000
    2018-07-10 22:12:03.561522+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:03.561652+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:03.565835+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:03.571170+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 316.396667
    2018-07-10 22:12:03.692364+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:03.692482+0200 StoryCatcher[6884:5992609] Part-Zero-Time 316.396667
    2018-07-10 22:12:03.692524+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 321.396667
    2018-07-10 22:12:03.692618+0200 StoryCatcher[6884:5992609] insert-point 321.396667
    2018-07-10 22:12:03.704607+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:03.704722+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:03.707101+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:03.709913+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 632.793333
    2018-07-10 22:12:03.718035+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:03.718146+0200 StoryCatcher[6884:5992609] Part-Zero-Time 632.793333
    2018-07-10 22:12:03.718188+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 637.793333
    2018-07-10 22:12:03.718229+0200 StoryCatcher[6884:5992609] insert-point 637.793333
    2018-07-10 22:12:03.729603+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:03.729669+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:03.731642+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:03.734172+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 949.190000
    2018-07-10 22:12:03.749377+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:03.749457+0200 StoryCatcher[6884:5992609] Part-Zero-Time 949.190000
    2018-07-10 22:12:03.749492+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 954.190000
    2018-07-10 22:12:03.749530+0200 StoryCatcher[6884:5992609] insert-point 954.190000
    2018-07-10 22:12:03.759300+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:03.759373+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:03.761563+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:03.764029+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 1265.586667
    2018-07-10 22:12:04.161698+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:04.161809+0200 StoryCatcher[6884:5992609] Part-Zero-Time 1265.586667
    2018-07-10 22:12:04.161849+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 1270.586667
    2018-07-10 22:12:04.162000+0200 StoryCatcher[6884:5992609] insert-point 1270.586667
    2018-07-10 22:12:04.172305+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:04.172389+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:04.174729+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:04.177622+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 1581.983333
    2018-07-10 22:12:04.185607+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:04.185733+0200 StoryCatcher[6884:5992609] Part-Zero-Time 1581.983333
    2018-07-10 22:12:04.185822+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 1586.983333
    2018-07-10 22:12:04.185866+0200 StoryCatcher[6884:5992609] insert-point 1586.983333
    2018-07-10 22:12:04.198139+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:04.198248+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:04.200642+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:04.203660+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 1898.380000
    2018-07-10 22:12:04.211604+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:04.211698+0200 StoryCatcher[6884:5992609] Part-Zero-Time 1898.380000
    2018-07-10 22:12:04.211734+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 1903.380000
    2018-07-10 22:12:04.211803+0200 StoryCatcher[6884:5992609] insert-point 1903.380000
    2018-07-10 22:12:04.221522+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:04.221591+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:04.223646+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:04.226646+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 2214.776667
    2018-07-10 22:12:04.234286+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:04.234377+0200 StoryCatcher[6884:5992609] Part-Zero-Time 2214.776667
    2018-07-10 22:12:04.234412+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 2219.776667
    2018-07-10 22:12:04.234478+0200 StoryCatcher[6884:5992609] insert-point 2219.776667
    2018-07-10 22:12:04.244362+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:04.244448+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:04.246527+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:04.249212+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 2531.173333
    2018-07-10 22:12:04.257031+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:04.257120+0200 StoryCatcher[6884:5992609] Part-Zero-Time 2531.173333
    2018-07-10 22:12:04.257155+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 2536.173333
    2018-07-10 22:12:04.257226+0200 StoryCatcher[6884:5992609] insert-point 2536.173333
    2018-07-10 22:12:04.267056+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:04.267170+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:04.269269+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:04.271888+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 2847.570000
    2018-07-10 22:12:04.279912+0200 StoryCatcher[6884:5992609]
   
    composition start **************************
    2018-07-10 22:12:04.280009+0200 StoryCatcher[6884:5992609] Part-Zero-Time 2847.570000
    2018-07-10 22:12:04.280046+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 2852.570000
    2018-07-10 22:12:04.280622+0200 StoryCatcher[6884:5992609] insert-point 2852.570000
    2018-07-10 22:12:04.290581+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
    2018-07-10 22:12:04.290669+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
    2018-07-10 22:12:04.293119+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
    2018-07-10 22:12:04.295703+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 3163.966667
    2018-07-10 22:12:04.304021+0200 StoryCatcher[6884:5992609] Transition times (
        "CMTime: {0/600 = 0.000}",
        "CMTime: {3000/600 = 5.000}",
        "CMTime: {189838/600 = 316.397, rounded}",
        "CMTime: {189838/600 = 316.397}",
        "CMTime: {192838/600 = 321.397, rounded}",
        "CMTime: {379676/600 = 632.793, rounded}",
        "CMTime: {379676/600 = 632.793}",
        "CMTime: {382676/600 = 637.793, rounded}",
        "CMTime: {569514/600 = 949.190, rounded}",
        "CMTime: {569514/600 = 949.190}",
        "CMTime: {572514/600 = 954.190, rounded}",
        "CMTime: {759352/600 = 1265.587, rounded}",
        "CMTime: {759352/600 = 1265.587}",
        "CMTime: {762352/600 = 1270.587, rounded}",
        "CMTime: {949190/600 = 1581.983, rounded}",
        "CMTime: {949190/600 = 1581.983}",
        "CMTime: {952190/600 = 1586.983, rounded}",
        "CMTime: {1139028/600 = 1898.380, rounded}",
        "CMTime: {1139028/600 = 1898.380}",
        "CMTime: {1142028/600 = 1903.380, rounded}",
        "CMTime: {1328866/600 = 2214.777, rounded}",
        "CMTime: {1328866/600 = 2214.777}",
        "CMTime: {1331866/600 = 2219.777, rounded}",
        "CMTime: {1518704/600 = 2531.173, rounded}",
        "CMTime: {1518704/600 = 2531.173}",
        "CMTime: {1521704/600 = 2536.173, rounded}",
        "CMTime: {1708542/600 = 2847.570, rounded}",
        "CMTime: {1708542/600 = 2847.570}",
        "CMTime: {1711542/600 = 2852.570, rounded}",
        "CMTime: {1898380/600 = 3163.967, rounded}"
    )
    2018-07-10 22:12:04.304990+0200 StoryCatcher[6884:5992609] Total Play time 3163.966667
    2018-07-10 22:12:04.305050+0200 StoryCatcher[6884:5992609] Credits start 3163.966667
    2018-07-10 22:12:04.311304+0200 StoryCatcher[6884:5992609] Credits duration: 5.0
    2018-07-10 22:12:04.314987+0200 StoryCatcher[6884:5992609] Total Play time 3168.966667


This is the code I use to add the blank Video and the credit's layer:



CMTime creditsDuration = CMTimeMakeWithSeconds(5, 600);
    CMTime creditsFadeOutStart = CMTimeMakeWithSeconds(4.5, 600);
    CMTime creditsStart = totalPlayTime;
  
    CMTimeRange creditsRange = CMTimeRangeMake(creditsStart, creditsDuration);
  
    // Insert empty video
  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_1080p" ofType:@"mov"];
    NSURL *trackUrl = [NSURL fileURLWithPath:path];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:trackUrl options: nil];
    AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *audio = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
  
    totalPlayTime = CMTimeAdd(totalPlayTime, creditsDuration);
  
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,creditsDuration) ofTrack:track atTime:creditsStart error:nil]; // Add video
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,creditsDuration) ofTrack:audio atTime:creditsStart error:nil]; // Add audio
  
    AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
  
    [passThroughLayer setOpacity:0.0 atTime:creditsStart];
  
    NSMutableArray *layerInstructions = [[NSMutableArray alloc] init];
  
    [layerInstructions addObject:passThroughLayer];
  
    AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    passThroughInstruction.timeRange = creditsRange;
  
    passThroughInstruction.layerInstructions = [NSArray arrayWithArray:layerInstructions];
    [videoCompositionInstructions addObject:passThroughInstruction];
  
    UIImage *logo = [UIImage imageNamed:@"endCredit.png"];
  
    float logoSize = 1.0;
  
    if(self.isPreview == NO){
    logoSize *= sizeMultiplier;
    }else{
    logoSize *= sizeMultiplier;
    }
  
    CGImageRef bdImg = CGImageRetain([logo CGImage]);
    CALayer *logoLayer = [CALayer layer];
    logoLayer.contents = (__bridge_transfer id)bdImg;
    logoLayer.bounds = CGRectMake(0, 0, 480 * logoSize, 320 * logoSize);
    logoLayer.anchorPoint = CGPointMake(0.5, 0.5);
  
    logoLayer.position = CGPointMake(0, 0);
  
    [self.animationLayer addSublayer:logoLayer];
  
    float fadeEnd = CMTimeGetSeconds(CMTimeAdd(creditsStart, CMTimeMakeWithSeconds(4, 600)));
    float fadeStart = CMTimeGetSeconds(creditsStart);
  
    [logoLayer addAnimation:[VideoComposerHelpers fadeInOut:fadeStart end:fadeEnd duration:0.5] forKey:nil];


HELP!!! 😉



So again, it works up to about 8 or 9 videos without a problem, after that, I get the mentioned error message. So it seems like some kind of error accumulates over time. But I can't find any problem in the calculations.