Is there a way to encode a video so that forward and backward playback is very smooth? Is it possible in h264?

My understanding of the basic structure of the video encoding is that if the video is made of frames

f1  f2  f3  f4  f5  f6  f7  f8  f9  f10 ...

that are encoded as

i1  d2  d3  d4  d5  i6  d7  d8  d9  d10 ...


i(k) == f(k), d(k) == f(k) - f(k-1)

ie i is an iframe and d is a frame difference, so that the video size is much smaller that the size of all the images (frames) because deltas are smaller than entire frames. Then to get f8, I've to stream and decode from the closest previous iframe i6 and apply the deltas.

So the decoding/streaming cost per frame going forward is at most the cost of an iframe. But going backward, it can be much more. For instance if on f6 I want to go to the previous frame f5, I need to decode everything from the previous iframe f1. Is there a way to encode things so that this doesn't happen, for instance by additionally storying the convenient (and somehow redundant) info f6 - f5 ?

I'm familiar with ffmpeg and ideally would like to use it.

My use case is the following:

I'm streaming a video to a mobile client. The video is always paused and the user seeks back and forth through a slider. The backward seeking sometimes froze, and I believe it's when it's going through a keyframe and has to go back a lot and redecode to reach the previous frame. Forward seeking is fine.

1 Answer 1


It seems that you want to use a key frame for every frame, so that there is no forward compression. If each frame is stored entirely (though compressed), then the cost of going backwards and forward would be the same (except the cost of the additional storage space). M-JPEG does this naturally for example.

With H264, you can set the minimum and maximum distance between key frames (iframes) when using ffmpeg:

--keyint <integer> (x264)
-g <integer> (FFmpeg)
Keyframe interval, also known as GOP length. This 
determines the maximum distance between I-frames. 
Very high GOP lengths will result in slightly more 
efficient compression, but will make seeking in the 
video somewhat more difficult. Recommended default: 250 

--min-keyint <integer> (x264)
-keyint_min <integer> (FFmpeg)
Minimum GOP length, the minimum distance between I-frames. 
Recommended default: 25 

If you set both of these to 1, then this should force an iframe per frame, and no predictive or delta frames.

  • This should work, but an intra-coded stream may not be necessary. See here.
    – Gyan
    Oct 28, 2016 at 4:02
  • @Mulvya Yeah, that option uses 2-3 frames per keyframe rather than 1 suggested above. It would be interesting to see the difference with +rtphint on and off - I guess that depends somewhat on the OPs use case.
    – Paul
    Oct 28, 2016 at 4:44
  • Thanks. I'll give mjpeg a try. I'll edit the OP for my use case
    – Guig
    Oct 28, 2016 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.