title: MPEG-2 video encoding with ffmpeg API: bigger filesize than ffmpeg.exe
body: <p>I am trying to encode a video from raw YUV to MPEG-2 using the ffmpeg API. </p>
  
  <p>My problem is that the <em>API</em>-generated file is approx. 1.7 times bigger than the equivalent files generated by <em>ffmpeg</em> itself.</p>
  
  <p>I use the quantization parameter (via <code>qmin</code> and <code>qmax</code>) instead of the bitrate. </p>
  
  <p>The <em>API</em>-version is basically:</p>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>//...
  pCodecCtx-&gt;pix_fmt = PIX_FMT_YUV420P;
  pCodecCtx-&gt;qmin = 3;
  pCodecCtx-&gt;qmax = 3;
  pCodecCtx-&gt;time_base.num = 1;
  pCodecCtx-&gt;time_base.den = 30;
  avcodec_open(pCodecCtx, avcodec_find_encoder(CODEC_ID_MPEG2VIDEO));
  //...
  while(/*...*/) {
    avcodec_encode_video(pCodecCtx, pOutbuf, outbufSize, pPicture);
    //..
  }
  //...
  </code></pre>
  
  <p>For <em>ffmpeg</em> itself, I use the command: </p>
  
  <pre><code>ffmpeg -s 352x288 -r 30 -i foreman_352x288.yuv -f mpeg2video -vcodec mpeg2video -r 30 -pix_fmt yuv420p -qmin 3 -qmax 3 foreman.m2v
  </code></pre>
  
  <p>Why does the <em>API</em>-generate file achieve a bitrate of 5212 kb/s and the file generated by  <em>ffmpeg</em> for the same <code>qp</code> a bitrate of 3047 kb/s?? </p>
  
  <p>(Even more puzzling is that the smaller <em>ffmpeg</em> version has a slightly higher PSNR, 40.49 dB vs. 40.02 dB). </p>
  
  <p>Are there any other relevant parameters that I missed? Does the <em>ffmpeg</em> actually respect the quantization parameter?</p>