[Ffmpeg-devel] av_seek_frame() units

Steve Willis steve
Wed Jul 6 07:38:27 CEST 2005

Hi Ian,

I think what Rich is saying is that there is no formula for accurately 
mapping a time to a frame number for all formats, because some formats 
do not have a constant frame rate. I ran into this issue myself using 
the Java Media Framework (JMF), which I'm much more familiar with. It is 
better to refer to timestamps rather than frames, because there is 
always a frame to fetch for a given timestamp. If you try to fetch a 
frame by sequence number, you have to count from the beginning to be 
sure its correct for all formats, and that gets messy if you need to seek.

With this in mind, I am looking for the "best practice" way to do just 
that...fetch a frame for a given time. This is proving difficult for me, 
because the library can only seek to the nearest keyframe. I need the 
exact frame for a given time...not just the nearest key frame. I'm 
trying out the suggestions I've been given on this list, and I'll post 
my progress if that helps you. It could be another week, though. I'm a 
Java programmer, and I'm struggling to understand both the 
avformat/avcodec library and also C/C++ at large. Oh yeah, I'm also 
using Windows! So far, I'm successfully fetching one frame at a time in 
sequence as an RGB image (which is what I need), so I just have to 
figure out an efficient way of random-access into the stream.

By the way, if you are certain that all the formats you will use will be 
constant frame rate, the timestamp method can still help you:

frame_number = frame_rate * time

...where frame_number and time are in the same units, such as seconds or ms.

Best regards,


Ian Gowen wrote:
> While providing lots of useful info, this still doesn't address my original 
> question: given the frame number, what is the formula to find the correct 
> timestamp for a particular stream? (i.e. in AVStream.time_base units, not 
> --Ian
> On 7/5/05, Michael Niedermayer <michaelni at gmx.at> wrote:
>>On Tuesday 05 July 2005 12:36, Andy Parkins wrote:
>>>On Tuesday 2005 July 05 10:14, Michael Niedermayer wrote:
>>>>>av_seek_frame( fmtCtx, -1, TARGET_PTS, AVSEEK_FLAG_BACKWARD );
>>>>>CodecCtx->hurry_up = 1;
>>>>>do {
>>>>>    av_read_frame( fmtCtx, &Packet );
>>>>>    // should really be checking that this is a video packet
>>>>>    MyPts = Packet.pts / Packet.duration *
>>>>>  AV_TIME_BASE / av_q2d( Stream->r_frame_rate);
>>>>this is completely wrong, the division by Packet.duration makes no sens
>>>>and its 1/Stream.time_base not Stream->r_frame_rate which should be used
>>>How's this?
>>>   MyPts = av_rescale( Packet.pts,
>>>        AV_TIME_BASE * (int64_t) Stream->time_base.num,
>>>        Stream->time_base.den );
>>looks good
>>ffmpeg-devel mailing list
>>ffmpeg-devel at mplayerhq.hu
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at mplayerhq.hu
> http://mplayerhq.hu/mailman/listinfo/ffmpeg-devel

More information about the ffmpeg-devel mailing list