title: How to insert a key frame(Iframe) to a h.264 video stream in ffmpeg C++ api?
body: <p>I have a real time video stream, and want to cut some video clips from it by accurate timestamp(pts).</p>
  
  <p>When I receiver an avpacket, I decode it, and do something and cache the avpacket. I don't want to re-encode all avpackets, it cost cpu resource.</p>
  
  <p>There are many <a href="https://en.wikipedia.org/wiki/Group_of_pictures" rel="nofollow">gop structure</a> in H.264 stream, usually we should cut the video begin at the key frame, and end at the key frame. Otherwise the front some frames in the video clip would display error. </p>
  
  <p>Now I use <a href="https://ffmpeg.org/doxygen/2.8/group__lavf__encoding.html#gaa85cc1774f18f306cd20a40fc50d0b36" rel="nofollow">av_write_frame</a> to make avpacket to video. But sometimes the length of gop is very long, such as it could be 250, 8.3s(30 frame per second). It means the distance between two I-frame could be 250 frames. The video clip is short, I don't want to add too many unused frames. </p>
  
  <p>How should I do? I think i should insert a i-frame at the start position of video clip. Could I change a p-frame to i-frame?</p>
  
  <p>Thanks your reading!</p>