title: Header missing in mpg, in spite of using avformat_write_header
body: <p>I am encoding a live rendered video to mpg and/or mp4 (depends on the later usage of the video) using the ffmpeg C API. When encoding to mp4, everything is well. But when encoding to mpg, the resulting video cannot be played by any player. A quick call to ffprobe on it reveals that the header is missing. But this seems pretty much impossible, as I am explicitly writing it.</p>
  
  <p>This is how I write the header, before any frame is encoded:</p>
  
  <pre><code>// ptr-&gt;oc is the AVFormatContext
  int error = avformat_write_header(ptr-&gt;oc, NULL);
  if (error &lt; 0)
  {
      s_logFile &lt;&lt; "Could not write header. Error: " &lt;&lt; error &lt;&lt; endl;
      fprintf(stderr, "Could not write header. Error: '%i'\n", error);
      return 1;
  }
  </code></pre>
  
  <p>There never is any error when writing the header.</p>
  
  <p>For encoding, I am following <a href="http://ffmpeg.org/doxygen/trunk/muxing_8c-source.html" rel="nofollow">the official muxing.c example</a>, so I do set the CODEC_FLAG_GLOBAL_HEADER flag. I use CODEC_ID_MPEG2VIDEO (for video) and CODEC_ID_MP2 (for audio).</p>
  
  <p>The result mpg does work when I "encode" it in an additional step with an external ffmpeg executable like this: <strong>"ffmpeg -i ownEncoded.mpg -sameq -y working.mpg"</strong>.
  So it seems all the data is there, only the header is missing for some reason...</p>
  
  <p>Here is the only thing ffmpeg is reporting before/when writing the header:</p>
  
  <pre><code>mpeg -------------------
  lvl: 24
  msg: VBV buffer size not set, muxing may fail
  </code></pre>
  
  <p>Could that be the problem?</p>
  
  <p>I wonder what could be wrong here as I encode mp4 with the exact same function, except setting some special values like qmin, qmax, me_method, etc. when encoding to mp4. Do I probably have to set any special values so that ffmpeg really does write the header correctly?</p>