title: Compositing 2 videos on top of each other with alpha
body: <p>AVFoundation allows you to "compose" 2 assets (2 videos) as 2 "tracks", just like in Final Cut Pro, for example. </p>
  
  <p>The theory says I can have 2 videos on top of each other, with alpha, and see both. </p>
  
  <p>Either I'm doing something wrong, or there's a bug somewhere, because the following test code, although a bit messy, clearly states I should see 2 videos, and I only see one, as seen here: <a href="http://lockerz.com/s/172403384" rel="noreferrer">http://lockerz.com/s/172403384</a> -- the "blue" square is IMG_1388.m4v</p>
  
  <p>For whatever reason, IMG_1383.MOV is never shown.</p>
  
  <pre><code>NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil];
  AVMutableComposition *composition = [AVMutableComposition composition];
  CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(4, 1));
  AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
  
  // Track B
  NSURL *urlVideo2 = [NSURL URLWithString:@"file://localhost/Users/me/Movies/Temp/IMG_1388.m4v"];
  AVAsset *video2 = [AVURLAsset URLAssetWithURL:urlVideo2 options:options];
  AVMutableCompositionTrack *videoTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:0];
  NSArray *videoAssetTracks2 = [video2 tracksWithMediaType:AVMediaTypeVideo];
  AVAssetTrack *videoAssetTrack2 = ([videoAssetTracks2 count] &gt; 0 ? [videoAssetTracks2 objectAtIndex:0] : nil);
  [videoTrack2 insertTimeRange:timeRange ofTrack:videoAssetTrack2 atTime:kCMTimeZero error:&amp;error];
  
  AVMutableVideoCompositionLayerInstruction *to = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack2];
  [to setOpacity:.5 atTime:kCMTimeZero];
  [to setTransform:CGAffineTransformScale(videoAssetTrack2.preferredTransform, .5, .5) atTime:kCMTimeZero];
  
  // Track A
  NSURL *urlVideo = [NSURL URLWithString:@"file://localhost/Users/me/Movies/Temp/IMG_1383.MOV"];
  AVURLAsset *video = [AVURLAsset URLAssetWithURL:urlVideo options:options];
  AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:1];
  NSArray *videoAssetTracks = [video tracksWithMediaType:AVMediaTypeVideo];
  AVAssetTrack *videoAssetTrack = ([videoAssetTracks count] &gt; 0 ? [videoAssetTracks objectAtIndex:0] : nil);
  [videoTrack insertTimeRange:timeRange ofTrack:videoAssetTrack atTime:kCMTimeZero error:nil];
  
  AVMutableVideoCompositionLayerInstruction *from = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack];
  [from setOpacity:.5 atTime:kCMTimeZero];
  
  // Video Compostion
  AVMutableVideoCompositionInstruction *transition = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
  transition.backgroundColor = [[UIColor clearColor] CGColor];
  transition.timeRange = timeRange;
  transition.layerInstructions = [NSArray arrayWithObjects:to, from, nil];
  videoComposition.instructions = [NSArray arrayWithObjects:transition,  nil];
  videoComposition.frameDuration = CMTimeMake(1, 30);
  videoComposition.renderSize = CGSizeMake(480, 360);
  
  
  // Export
  NSURL *outputURL = [NSURL URLWithString:@"file://localhost/Users/me/Movies/Temp/export.MOV"];
  AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:[[composition copy] autorelease] presetName:AVAssetExportPresetHighestQuality];
  [exportSession setOutputFileType:@"com.apple.quicktime-movie"];
  exportSession.outputURL = outputURL;
  exportSession.videoComposition = videoComposition;
  [exportSession exportAsynchronouslyWithCompletionHandler:nil];
  
  // Player
  AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
  playerItem.videoComposition = videoComposition;
  AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
  AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
  </code></pre>
  
  <p>Are you seeing something wrong?</p>
  
  <p>The "goal" of this code is to "record" the camera input (video 1) and the opengl output (video 2). I also tried to "compose" them "directly" with Buffers and all that, but I was as well unsuccessful :( Turns out AVFoundation is way less trivial than I thought.</p>