title: Video Recording with AVCaptureSession Doesn't Save Any Video
body: <p>I am attempting to record video and save it to file using AVCaptureSession. I am using a singleton to handle my capture session, as there are several VCs in my app where I'm displaying a video preview. </p>
  
  <p>The video preview displays correctly, but when I attempt to record video everything appears to work and I get no errors however when I look at the app container, no video files have been saved.</p>
  
  <p>Here are the relevant functions from my Capture Session code</p>
  
  <pre><code>-(void)recordVideoSetup{
      // Code adapted from
      // www.ios-developer.net/iphone-ipad-programmer/development/camera/record-video-with-avcapturesession-2
  
      NSLog(@"Adding Movie File Output");
  
      MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
  
      Float64 TotalSeconds = 600; // 10 minutes max
      int32_t preferredTimeScale = 30; //fps
      CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale); // Set Max Duration
  
      MovieFileOutput.maxRecordedDuration = maxDuration;
      MovieFileOutput.minFreeDiskSpaceLimit = 1024*1024*10; // 10 MB
  
      if ([captureSession canAddOutput:MovieFileOutput]) {
          NSLog(@"Added output to capture session");
          [captureSession addOutput:MovieFileOutput];
      }
  
  
      AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];
  
      // SET LANDSCAPE ORIENTATION
      if ([CaptureConnection isVideoOrientationSupported]) {
          AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeRight;
          [CaptureConnection setVideoOrientation:orientation];
      }
  
  
      NSLog(@"Setting image quality");
      [captureSession setSessionPreset:AVCaptureSessionPresetMedium];
      if ([captureSession canSetSessionPreset:AVCaptureSessionPreset640x480])
          [captureSession setSessionPreset:AVCaptureSessionPreset640x480];
  
  }
  
  -(NSURL*)startRecordingVideo:(NSString *)surveyID {
      DDLogCInfo(@"Video Recording Started");
  
      // Get URL to record to
  
      NSString *documentsDirPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
      documentsDirPath = [documentsDirPath stringByAppendingPathComponent:@"video"];
      NSURL *documentsDirUrl = [NSURL fileURLWithPath:documentsDirPath isDirectory:YES];
      NSString *filename = [NSString stringWithFormat:@"%@.mp4", surveyID];
      NSURL *url = [NSURL URLWithString:filename relativeToURL:documentsDirUrl];
  
      DDLogCInfo(@"Path to video: %@", url.path);
  
      NSFileManager *filemanager = [NSFileManager defaultManager];
      if ([filemanager fileExistsAtPath:[documentsDirPath stringByAppendingString:filename]]) {
          NSError *error;
          if ([filemanager removeItemAtURL:url error:&amp;error] == NO) {
              DDLogCError(@"Could not record video");
              return nil;
          }
      }
  
      [MovieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
  
      return url;
  }
  
  -(void)stopRecordingVideo {
      DDLogCInfo(@"Video Recording Ended");
      [MovieFileOutput stopRecording];
  
  }
  
  
  -(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
  
      // Check if Recording was successfull
  
      BOOL recordedSuccessfully = YES;
  
      if ([error code] != noErr) {
          NSLog(@"A Problem occurred with recording");
  
          // A Problem Occurred
          id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
          if (value) {
              recordedSuccessfully = [value boolValue];
          }
      }
  
      if (!recordedSuccessfully) {
          DDLogCInfo(@"!!! - VIDEO RECORDING ERROR");
      }
  
  }
  </code></pre>
  
  <p>EDIT: Some additional info
  This is the log output I get:</p>
  
  <pre><code>SafeCurveSpeed[9060:3691230] Adding Movie File Output
  SafeCurveSpeed[9060:3691230] Added output to capture session
  SafeCurveSpeed[9060:3691230] Setting image quality
  SafeCurveSpeed[9060:807] Video Recording Started
  SafeCurveSpeed[9060:807] Path to video: "/var/mobile/Containers/Data/Application/44A52ADB-0E36-4303-9DE3-99E9CCCAF3FC/Documents/video/survey-video-10.mp4"
  SafeCurveSpeed[9060:807] Video Recording Ended
  </code></pre>
  
  <p>EDIT 2: 
  I've added a method to observe "AVCaptureSessionRuntimeErrorNotification" Notifications, but none fired during 'recording'. I've also added a log call to the top of the <code>captureOutput didFinishRecordingToOutputFileAtURL:</code> method, and found that it did not reach that method at all after recording had finished. Still unsure of the cause though.</p>