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

Michael Bradshaw mbradshaw at sorensonmedia.com
Tue Oct 16 23:13:27 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.



More information about the Libav-user mailing list