title: How to record video with overlay view
body: <p>Hi I am trying to record video with overlay.</p>
  
  <p>I have written:</p>
  
  <pre><code>-(void)addOvelayViewToVideo:(NSURL *)videoURL
  </code></pre>
  
  <p>to add overlay view on recorded video but it is not working.</p>
  
  <p>I written the code to record video in <code>viewDidLoad</code> using <code>AVCaptureSession</code>.</p>
  
  <pre><code>//In ViewDidLoad
  //CONFIGURE DISPLAY OUTPUT
  self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
  [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  self.previewLayer.frame = self.view.frame;
  [self.view.layer addSublayer:self.previewLayer];
  
  
  -(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
  {
      if(error.code != noErr)
      {
          id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
          if(value)
          {
              isSuccess = [value boolValue];
          }
      }
  
      if(isSuccess)
      {
          ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
          if([assetsLibrary videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
          {
              [self addOverviewToVideo:outputFileURL];
          }
          else{
              NSLog(@"could not saved to photos album.");
          }
      }
  
  }
  
  
  -(void)addOvelayViewToVideo:(NSURL *)videoURL
  {
      AVAsset *asset = [AVAsset assetWithURL:videoURL];
      AVMutableComposition *composition = [[AVMutableComposition alloc] init];
  
      AVMutableCompositionTrack *compositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
      [compositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
      AVMutableVideoCompositionInstruction *compositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
      compositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
      AVMutableVideoCompositionLayerInstruction *videoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionTrack];
      AVAssetTrack *assetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
  
      UIImageOrientation videoAssetOrientation_  = UIImageOrientationUp;
      BOOL isVideoAssetPortrait_  = NO;
      CGAffineTransform videoTransform = assetTrack.preferredTransform;
      if (videoTransform.a == 0 &amp;&amp; videoTransform.b == 1.0 &amp;&amp; videoTransform.c == -1.0 &amp;&amp; videoTransform.d == 0) {
          videoAssetOrientation_ = UIImageOrientationRight;
          isVideoAssetPortrait_ = YES;
      }
      if (videoTransform.a == 0 &amp;&amp; videoTransform.b == -1.0 &amp;&amp; videoTransform.c == 1.0 &amp;&amp; videoTransform.d == 0) {
          videoAssetOrientation_ =  UIImageOrientationLeft;
          isVideoAssetPortrait_ = YES;
      }
      if (videoTransform.a == 1.0 &amp;&amp; videoTransform.b == 0 &amp;&amp; videoTransform.c == 0 &amp;&amp; videoTransform.d == 1.0) {
          videoAssetOrientation_ =  UIImageOrientationUp;
      }
      if (videoTransform.a == -1.0 &amp;&amp; videoTransform.b == 0 &amp;&amp; videoTransform.c == 0 &amp;&amp; videoTransform.d == -1.0) {
          videoAssetOrientation_ = UIImageOrientationDown;
      }
      [videoLayerInstruction setTransform:assetTrack.preferredTransform atTime:kCMTimeZero];
      [videoLayerInstruction setOpacity:0.0 atTime:asset.duration];
  
  
  
      compositionInstruction.layerInstructions = [NSArray arrayWithObject:videoLayerInstruction];
      AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
  
      CGSize naturalSize = CGSizeMake(assetTrack.naturalSize.height, assetTrack.naturalSize.width);
      float renderWidth, renderHeight;
      renderWidth = naturalSize.width;
      renderHeight = naturalSize.height;
      videoComposition.renderSize = CGSizeMake(renderWidth, renderHeight);
      videoComposition.instructions = [NSArray arrayWithObject:compositionInstruction];
      videoComposition.frameDuration = CMTimeMake(1, 30);
  
  
      CALayer *overlayLayer = [CALayer layer];
      UIImage *overlayImage = [UIImage imageNamed:@"sampleHUD"];
      [overlayLayer setContents:(id)[overlayImage CGImage]];
      overlayLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height);
      [overlayLayer setMasksToBounds:YES];
  
      CALayer *parentLayer = [CALayer layer];
      CALayer *videoLayer = [CALayer layer];
      parentLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height);
      videoLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height);
      [parentLayer addSublayer:videoLayer];
      [parentLayer addSublayer:overlayLayer];
  
      videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
      NSLog(@"renderSize:%f,%f", videoComposition.renderSize.width, videoComposition.renderSize.height);
  
      AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
      exportSession.outputURL = videoURL;
      exportSession.outputFileType = AVFileTypeQuickTimeMovie;
      exportSession.shouldOptimizeForNetworkUse = YES;
      exportSession.videoComposition = videoComposition;
      [exportSession exportAsynchronouslyWithCompletionHandler:^{
          dispatch_async(dispatch_get_main_queue(), ^{
              //save the video in photos album
          });
      }];
  
  }
  </code></pre>
  
  <p>I am still unable to figure out what is going wrong here. Need guidance on this.</p>
  
  <p>Can I add overlay while recording video?</p>
  
  <p>Any help will be appreciated.</p>