title: Create MPEG-2 Transfer Stream with AVFoundation
body: <p>I'm attempting to create a properly formatted video file for use in Apple's HTTP Live Streaming. Here is the code that creates the file:</p>
  
  <pre><code>// Init the device inputs
  AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil];
  AVCaptureDeviceInput *newAudioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self audioDevice] error:nil];
  
  // Create session
  AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
  newCaptureSession.sessionPreset = AVCaptureSessionPresetMedium;
  self.session = newCaptureSession;
  
  // Add inputs and output to the capture session
  if ([newCaptureSession canAddInput:newVideoInput]) {
      [newCaptureSession addInput:newVideoInput];
  }
  if ([newCaptureSession canAddInput:newAudioInput]) {
      [newCaptureSession addInput:newAudioInput];
  }
  
  // Setup the output
  AVCaptureMovieFileOutput *aMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
  if ([self.session canAddOutput:aMovieFileOutput])
      [self.session addOutput:aMovieFileOutput];
  
  aMovieFileOutput.maxRecordedDuration = CMTimeMakeWithSeconds(10.f, 1);
  
  // Begin recording
  AVCaptureConnection *videoConnection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  if ([videoConnection isVideoOrientationSupported])
      [videoConnection setVideoOrientation:[self calculateVideoOrientation]];
  
  [aMovieFileOutput startRecordingToOutputFileURL:[self nextOutputURL] recordingDelegate:self];
  </code></pre>
  
  <p>The <code>[self nextOutputURL]</code> returns a valid <code>NSURL</code>, the file is successfully saved to disk and I can open and view the file in VLC and QuickTime. The video format as viewed in VLC is 'avc1' which I gather is <a href="http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Overview" rel="nofollow">the same as H.264</a>. The video format as viewed in QuickTime is H.264. The extension on the file is, of course, .ts. </p>
  
  <p>It appears that I'm doing everything correctly, yet when I try and validate my HTTP Live stream with <code>mediastreamvalidator</code>, I get the following error:</p>
  
  <pre><code>http://imac.local.:2020/fileSequence000.ts:
  ERROR: (-12971) failed to parse segment as either an MPEG-2 TS or an ES
  </code></pre>
  
  <p>Anyone know what I may be doing wrong?</p>