title: How to control key-frame generation of ffmpeg?
body: <p>I'm making a segmenter that intervene ffmpeg's write_frame function and write output data to separate files. Each segmented file contains segment of about 3 seconds video. </p>
  
  <p>The code does following:</p>
  
  <pre><code>1 - Get transcoded packet  
  2 - Check if it contains key frame data, if yes goto 3. 
  3 - Check the duration of current segment, if it exceed 3 seconds, goto 4 
  4 - Close file, and create new segment, write packet to segment file, goto-1
  </code></pre>
  
  <p>General speaking, every segment contains at least 3 seconds video data, and it starts with a key frame.</p>
  
  <p>The problem is that the output video's duration are very different, some contain 3 seconds, some contain 5 or 6.</p>
  
  <p>I suspect that the problem due to how ffmpeg generate key frames during transcoding. If the "distance" between two adjacent keyframes are 6s, i got 6 seconds segment. </p>
  
  <p>Here is my questions:</p>
  
  <ol>
  <li><p>is that true that ffmpeg generate keyframes at irregular intervals (and interval time can be up to few second (eg. 6)?</p></li>
  <li><p>How can we control the ffmpeg key frame generation? (i guess there should be a ffmpeg command's argument for this, -force_key_frames maybe, but I'm not sure)</p></li>
  </ol>