title: FFMpeg avformat_write_header always returns < 0
body: <p>We've asked a freelancer to build a video encoder with FFMPeg for iOS but there is a bug and the freelancer is no longer available. I very inexperienced in FFMpeg and video encoding and am trying to debug this error.</p>
  
  <p>From what I understand, we're attempting to create an output file and create a header for it however, avformat_write_header is always less than zero. If I comment it out, it does not work</p>
  
  <pre><code>- (BOOL) writeHeaderWithError:(NSError *__autoreleasing *)error {
      AVDictionary *options = NULL;
  
      // Write header for output file
      int writeHeaderValue = avformat_write_header(self.formatContext, &amp;options);
      if (writeHeaderValue &lt; 0) {
          if (error != NULL) {
              *error = [FFUtilities errorForAVError:writeHeaderValue];
          }
          av_dict_free(&amp;options);
          return NO;
      }
      av_dict_free(&amp;options);
      return YES;
  }
  </code></pre>
  
  <p>Below is some relevant code of how we instantiate a FFOutputFile</p>
  
  <pre><code>    - (AVFormatContext*) formatContextForOutputPath:(NSString*)outputPath options:(NSDictionary*)options {
      AVFormatContext *outputFormatContext = NULL;
      NSString *outputFormatString = [options objectForKey:kFFmpegOutputFormatKey];
  
      int openOutputValue = avformat_alloc_output_context2(&amp;outputFormatContext, NULL, [outputFormatString UTF8String], [outputPath UTF8String]);
      if (openOutputValue &lt; 0) {
          avformat_free_context(outputFormatContext);
          return nil;
      }
      return outputFormatContext;
  }
  
  - (void) addOutputStream:(FFOutputStream*)outputStream {
      [self.streams addObject:outputStream];
  }
  
  - (id) initWithPath:(NSString *)path options:(NSDictionary *)options {
      if (self = [super initWithPath:path options:options]) {
          self.formatContext = [self formatContextForOutputPath:path options:options];
          self.streams = [NSMutableArray array];
          self.bitstreamFilters = [NSMutableSet set];
      }
      return self;
  }
  </code></pre>