[Libav-user] How do I seek to the very beginning of a file?

Don Moir donmoir at comcast.net
Wed Oct 17 01:10:23 CEST 2012


> I'm trying to reliably seek to the very start of a file. Seeing as
> avformat_seek_file is still in "beta" and isn't finished, I'd rather
> not rely on it, so I'm using av_seek_frame (plus I've been advised to
> stay away from avformat_seek_file until it's properly implemented).
> However, I can't figure out how to get av_seek_frame to reliably seek
> to the beginning of the file (that is, I want all streams to be at
> their beginning).
>
> I've tried av_seek_frame with AVSEEK_FLAG_BYTE with a "timestamp"/byte
> offset of 0, but for some reason that doesn't seem to work.
> av_seek_frame returns 0, but all subsequent calls to av_read_frame
> return < 0. However, if I change the seeking flags to just
> AVSEEK_FLAG_BACKWARD then subsequent calls to av_read_frame return 0
> (and av_seek_frame still returns 0). However, I don't like seeking
> with AVSEEK_FLAG_BACKWARD because 1) av_seek_frame only seeks by one
> stream (that is, it only makes sure one stream is at the requested
> position, and depending on how packets/frames are ordered in the file,
> other streams can potentially be past the requested time (I really
> need all of them to be at the beginning)), and 2) seeking by
> timestamps seems silly when I know I just want all the streams to
> start from the very beginning (mostly because timestamp seeking can
> use PTS or DTS and it seems like an unnecessary mess when all I want
> is the start, regardless of PTS/DTS values).
>
> Is there any way to do this with the C API? The only workaround I can
> see is closing and reopening the file, which feels like a really ugly
> hack. Plus that's a lot slower compared to using protocols/formats
> that support seeking, particularly over networked videos.
>
> Thanks,
>
> Michael

I use avformat_seek_file and so does ffplay etc. I always use a valid stream index and not -1. I don't leave it up to ffmpeg to 
determine what stream to seek on.

I always take the first_dts for the stream into account. The first_dts will be the zero mark for the stream. first_dts can be zero, 
less than zero, or greater than zero.

I use AVSEEK_FLAG_BACKWARD (regardless if seeking to zero or not).

With above logic, I can't remember the last time I had a problem seeking to zero (first_dts).

Should I have to do the above ?  probably not...

Also not to say there isn't a few problem files here and there but these just need to be reported so it can be fixed.

Do you have a particular file that is a problem and can be posted ?

Don 



More information about the Libav-user mailing list