[Ffmpeg-devel] Seeking and keyframes

Rich Felker dalias
Tue Jun 21 07:07:10 CEST 2005

On Mon, Jun 20, 2005 at 12:21:43PM +0200, Michel Bardiaux wrote:
> M?ns Rullg?rd wrote:
> >Steve Willis <steve at stevewillis.net> writes:
> >
> >
> >>Greetings,
> >>
> >>Please pardon my novice question. I'm working on a project where the
> >>user needs to analyze video frame by frame, and possibly cut video at
> >>any arbitrary frame (not necessarily a keyframe.) This is not the same
> >>as a typical video player...video does not have to be "played" in
> >>real-time, just stepped through frame by frame. More importantly, I
> >>need a way to get a frame for a particular time accurate to the frame
> >>(not to the closest keyframe.)
> >>
> >>I've been looking at the documentation for av_seek_frame(), and I see
> >>that it seeks to the first keyframe before the given timestamp by
> >>default. Is there a simple way to change this behavior so it finds the
> >>previous keyframe and then regenerates the forward intra-frame to
> >>exactly match the timestamp?
> >
> >
> >No.  Libavformat doesn't have access to the decoder to perform the
> >decoding of the frames after the keyframe up to the requested frame.
> >
> >
> >>Alternatively, is there a simple way to use ffmpeg to transcode the
> >>video such that every frame is a keyframe?
> >
> >
> >You could use mjpeg.
> >
> >
> >>A final (very beginner) question: why is seeking only to keyframes the
> >>default? Is it very inefficient to actually generate the exact frame
> >>for a given timestamp each time a user seeks?
> >
> >
> >It's somewhat inefficient, but also impossible.
> >
> Would it be possible to have av_seek_frame also return a set of 
> instructions to follow to obtain the specified frame? E.g. for a GOP of 
> structure IBBP in display order, if you specified seek to the 2nd B 
> frame, seek_frame would tell you back "read_frame+decode, 
> read_frame+skip, read_frame+decode, read_frame+decode" (Correct?)

Again, this is not possible without knowing details of the codec.
av_seek_frame takes place at the demuxer level only. Anyway after
seeking to the prior keyframe it's entirely obvious how to get to the
frame you want. You just decode with hurry_up enabled until you get
within some safe range of the desired frame number, then disable
hurry_up and continue up to the frame you want.


More information about the ffmpeg-devel mailing list