How does the h.264 encoder determine where to put i-frames?
This is a complex topic, with the exact algorithm unique to each encoder. 

Below is a pseudocode explanation from a x264 developer. B-frames aren't accounted for, but basic logic should be similar.

encode current frame as (a really fast approximation of) a P-frame and an I-frame.

if ((distance from previous keyframe) &gt; keyint) then
    set IDR-frame
else if (1 - (bit size of P-frame) / (bit size of I-frame) &lt; (scenecut / 100) * (distance from previous keyframe) / keyint) then
    if ((distance from previous keyframe) &gt;= minkeyint) then
        set IDR-frame
    else
        set I-frame
else
    set P-frame

encode frame for real.


scenecut is the scene change threshold value. 0 means current frame is identical to previous frame, and 100 means it is completely different.

keyint is the maximum permitted distance between two keyframes; minkeyint is the minimum.

IDR (instantaneous decoder refresh) frames are keyframes such that no future frame requires to refer to a frame earlier than the IDR-frame for decoding. Not necessarily true for plain I-frames.