[Libav-user] Building an index for arbitrary-frame-accurate VBR seeking use byte offset

stelefx at gmail.com stelefx at gmail.com
Fri Dec 17 19:57:28 EET 2021


I have various videos that are VBR and don't have an accurate duration 
or frame rate.

I wrote some code to index the files by reading through the whole thing 
by packet and recording the pts, byte offset, and duration for each 
frame, and recording which ones are I-frames. This also allows getting 
an accurate duration for the files that didn't have one too.

Now I'm attempting to use the index to jump to any arbitrary frame, by 
first seeking to the byte offset of the I-frame before the desired 
frame, and then decoding packets until the desired number of 
non-I-frames are processed.

I've tried this both using the byte offset and with the pts of that 
reference I-frame, but in many cases I'm getting repeating "loops" in 
the video.

This is probably my misunderstanding of what the fields in the AVFrame 
are. While building the index, I'm using a loop of av_read_frame() and 
avcodec_decode_video2(). When I get a picture, and if key_frame is set, 
I record the frame's pkt_pos, pts, and pkt_dts values in a dictionary 
keyed by the frame number.

When I seek to an arbitrary frame, I look up the first entry in the 
dictionary with the frame # I'm looking for, and then to:

av_seek_frame(context, video_stream_index, pkt_pos_for_nearest_iframe, 
AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_BYTES);

Then if the frame I'm looking for is not an I-frame, I repeat the 
av_frame_read/avcodec_decode_video2 loop until I get to that frame number.

Does anyone have any experience with this kind of thing who could tell 
me what I'm doing wrong?




More information about the Libav-user mailing list