problem: FFMPEG seeking with av_seek_frame using byte positions

Nick Verlinden picture Nick Verlinden · Jun 13, 2010 · Viewed 9.5k times · Source

I am trying to get the av_seek_frame() function to go to a byte position I specify. I am implementing a frame accurate seeking mechanism for my application, and the way I see it, I will scan the entire video file, and store byte positions for each keyframe in a struct. I found out where to get the current byte position: AVPacket.pos. I now test this position with av_seek_frame like this:

av_seek_frame( pFormatCtx, videoStream, 110285594, AVSEEK_FLAG_BYTE);

However, this does not seem to do the right thing, when I call av_read_frame, it just starts with frame 23. If I do not seek, it starts at frame 1.

Answer

Nick Verlinden picture Nick Verlinden · Jun 17, 2010

For those who are interested, I found the solution. After hours of googling and some simplistic form of reverse engineering, I found how to get and set the byte location of the open video.

To get the file position: AVFormatContext.pb.pos

for example:

int64_t byteposition = pFormatCtx->pb->pos;

To set the file position: url_seek(AVFormatContext.pb, Position, SEEK_SET);

for example:

url_seek(pFormatCtx->pb, 27909056, SEEK_SET);

Don't forget to flush the buffers if you change the location while playing. If you do this before you do av_read_frame for the first time, flushing is not necessary.

Kind Regards, Nick Verlinden