title: How to encode with ffmpeg if number of images smaller than GOP size
body: <p>The code bellow creates mp4 video file from jpeg images. When number of images is greater than GOP size the encoding success and after 10th image gotPacket that returned by avcodec_encode_video2 is > 0.</p>
  
  <p>BUT when number of images is smaller than GOP size gotPacket is always = 0 and therefore no images written to the file.</p>
  
  <p>My guess is that avcodec_encode_video2 allocates buffer with length equals to GOP size and until it won't be full gotPacket will be 0.</p>
  
  <p>My question is how to force avcodec_encode_video2 to encode even if it's buffer is not full?</p>
  
  <pre><code>...
  _outStream-&gt;codec.gop_size = 10;
  _outStream-&gt;codec.keyint_min = 10;
  ...
  
  AVFrame* frame = getFrame();
  
  
  AVPacket packet;
  av_init_packet(&amp;packet);
  packet.data = NULL;
  packet.size = 0;
  
  int gotPacket = 0;
  if (avcodec_encode_video2(_outStream-&gt;codec, &amp;packet, frame, &amp;gotPacket) &lt; 0)
      throw std::runtime_error("failed to encode frame");
  
  if (gotPacket)
  {
      ...
      if (av_write_frame(_outContainer.get(), &amp;packet) &lt; 0)
          throw std::runtime_error("failed to write frame");      
      av_free_packet(&amp;packet);
  }
  </code></pre>