title: How to save a movie from AVCapture
body: <p>I've been trying to figure out AVCapture the last couple of days and am struggling to save a video. My understanding is that you call <code>[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];</code> and then at a later time you can call <code>[movieFileOutput stopRecording];</code> And it should then call the delegate method <code>-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{</code>. After that I should be able to save the movie with something like <code>UISaveVideoAtPathToSavedPhotosAlbum([outputFileURL path] ,nil,nil,nil);</code> But apparently I'm not doing it correctly. When I start the session and then <code>startRecordingToOutputFile</code> it immediately calls the delegate <code>didFinishRecording</code>. I can't figure out why. Here is a my code:</p>
  
  <pre><code>-(void)viewDidAppear:(BOOL)animated{
          [super viewDidAppear:animated];
          session = [[AVCaptureSession alloc] init];
          [session beginConfiguration];
          session.sessionPreset = AVCaptureSessionPresetMedium;
  
          AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
      captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
      captureVideoPreviewLayer.frame = self.imagePreview.bounds; //UIView *imagePreview
      [self.imagePreview.layer addSublayer:captureVideoPreviewLayer];
  
      AVCaptureDevice *device = [self getCamera];
      NSError *error = nil;
      AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&amp;error];
      if (!input) {
          // Handle the error appropriately.
          NSLog(@"ERROR: trying to open camera: %@", error);
      }
  
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectoryPath = [paths objectAtIndex:0];
  
      movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
  
      NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"];
      NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:@"Test"] stringByAppendingString:@".mp4"];
      NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];
  
      [session addInput:input];
      [session addOutput:movieFileOutput];
      [session commitConfiguration];
      [session startRunning];
      [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
      [NSTimer timerWithTimeInterval:7 target:self selector:@selector(stopRun) userInfo:nil repeats:NO];
      /*
      [self initializeCamera];
       */
  }
   -(void)stopRun{
          [movieFileOutput stopRecording];
      }
  
  -(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
      NSLog(@"capture done url: %@",outputFileURL);
      UISaveVideoAtPathToSavedPhotosAlbum([outputFileURL path] ,nil,nil,nil);
  }
  
  -(AVCaptureDevice*)getCamera{
      NSArray *devices = [AVCaptureDevice devices];
      AVCaptureDevice *frontCamera;
      AVCaptureDevice *backCamera;
      for (AVCaptureDevice *device in devices) {
          NSLog(@"Device name: %@", [device localizedName]);
          if ([device hasMediaType:AVMediaTypeVideo]) {
              if ([device position] == AVCaptureDevicePositionBack) {
                  NSLog(@"Device position : back");
                  backCamera = device;
              }
              else {
                  NSLog(@"Device position : front");
                  frontCamera = device;
              }
          }
      }
      return frontCamera;
  }
  </code></pre>
  
  <p>Sorry that it's so lengthy. I hope a lot of this code can be useful to someone else.</p>