Reputation: 8397
I need to quickly seek thru H.264
encoded video stream in MP4
container. I am using libav to decode frames, so I stumbled upon avformat_seek_file()
method.
My problem is, assuming H.264
stream begins with keyframe, when I seek to timestamp 0 (regardless of time_base), I should be at the beggining of the stream. But Im not. I usually get few seconds into video. Also, if I seek to, for example 10 seconds, I usually get around 12 or so. Is it possible for keyframes to be so "rare"? It seems that AVSEEK_FLAG_ANY
has no impact on seek result. Tested on multiple FullHD H.264 MP4
videos.
Code:
unsigned long seekTo = 0;
//Doesen´t actually matter for 0 since it will be also 0
seekTo = av_rescale_q(seekTo, AVRational{1, AV_TIME_BASE}, pFormatCtx->streams[videoStream]->time_base);
int result = avformat_seek_file(pFormatCtx, videoStream, INT_FAST64_MIN, seekTo, seekTo, AVSEEK_FLAG_ANY);
avcodec_flush_buffers(pCodecCtx);
Upvotes: 4
Views: 3839
Reputation: 7906
My problem is, assuming H.264 stream begins with keyframe, when I seek to timestamp 0 (regardless of time_base), I should be at the beggining of the stream
Note that some files can have their first keyframe at a negative DTS, e.g. you need to seek to timestamp -1 or something like this.
You can set the flag inside AVFMT_SEEK_TO_PTS into AVInputFormat::flags
before opening the AVFormatContext
to use PTS which will be 0-based.
Upvotes: 0
Reputation: 595
Try using av_seek_frame instead. Read here for some gotchas about using that and seeking around.
Upvotes: 0