title: exportAsynchronouslyWithCompletionHandler fails with multiple video files (Code=-11820)
body: <p>I'm recording small video clips (around one second or so, with both the front and the rear camera, with possible different orientations). And then try to merge them using AVAssetExportSession. I basically make a composition and a videoComposition with the proper transforms and audio &amp; video tracks. </p>
  
  <p>The problem is that on iOS 5 it fails if you have more than 4 video clips and on iOS 6 the limit seems to be 16 clips. </p>
  
  <p>This to me seems really puzzling. Is AVAssetExportSession doing something weird or does it have some undocumented limitation on the number of clips that can be passed to it? Here are some excerpts from my code:</p>
  
  <pre><code>-(void)exportVideo
  {
      AVMutableComposition *composition = video.composition;
      AVMutableVideoComposition *videoComposition = video.videoComposition;
      NSString * presetName = AVAssetExportPresetMediumQuality;
  
      AVAssetExportSession *_assetExport = [[AVAssetExportSession alloc] initWithAsset:composition presetName:presetName];
      self.exportSession = _assetExport;
  
      videoComposition.renderSize = CGSizeMake(640, 480);
      _assetExport.videoComposition = videoComposition;
  
      NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent: @"export.mov"];
      NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
  
      // Delete the currently exported files if it exists
      if([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
          [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
  
      _assetExport.outputFileType = AVFileTypeQuickTimeMovie;
      _assetExport.outputURL = exportUrl;
      _assetExport.shouldOptimizeForNetworkUse = YES;
  
      [_assetExport exportAsynchronouslyWithCompletionHandler:^{
          switch (_assetExport.status)
          {
              case AVAssetExportSessionStatusCompleted:
                  NSLog(@"Completed exporting!");
                  break;
              case AVAssetExportSessionStatusFailed:
                  NSLog(@"Failed:%@", _assetExport.error.description);
                  break;
              case AVAssetExportSessionStatusCancelled:
                  NSLog(@"Canceled:%@", _assetExport.error);
                  break;
              default:
                  break;
          }
      }];
  }
  </code></pre>
  
  <p>And here's how the compositions are made:</p>
  
  <pre><code>-(void)setVideoAndExport
  {
      video = nil;
      video = [[VideoComposition alloc] initVideoTracks];
  
      CMTime localTimeline = kCMTimeZero;
  
      // Create the composition of all videofiles
      for (NSURL *url in outputFileUrlArray) {
          AVAsset *asset = [[AVURLAsset alloc]initWithURL:url options:nil];
          [video setVideo:url at:localTimeline];
          localTimeline = CMTimeAdd(localTimeline, asset.duration); // Increment the timeline
      }
      [self exportVideo];
  }
  </code></pre>
  
  <p>And here's the meat of the VideoComposition class:</p>
  
  <pre><code>-(id)initVideoTracks
  {
      if((self = [super init]))
      {
          composition = [[AVMutableComposition alloc] init];
          addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
          mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
          instructions = [[NSMutableArray alloc] init];
          videoComposition = [AVMutableVideoComposition videoComposition];
      }
      return self;
  }
  
  
  -(void)setVideo:(NSURL*) url at:(CMTime)to
  {
      asset = [[AVURLAsset alloc]initWithURL:url options:nil];
  
      AVAssetTrack *assetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
  
      AVMutableCompositionTrack *compositionTrackVideo = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
      [compositionTrackVideo insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack: assetTrack atTime:to error:nil];
  
      AVMutableCompositionTrack *compositionTrackAudio = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
      [compositionTrackAudio insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:to error:nil];
  
      mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(to, asset.duration));
  
      AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionTrackVideo];
  
      [layerInstruction setTransform: assetTrack.preferredTransform atTime: kCMTimeZero];
      [layerInstruction setOpacity:0.0 atTime:CMTimeAdd(to, asset.duration)];
      [instructions addObject:layerInstruction];
  
      mainInstruction.layerInstructions = instructions;
      videoComposition.instructions = [NSArray arrayWithObject:mainInstruction];
      videoComposition.frameDuration = CMTimeMake(1, 30);
  }
  </code></pre>