[Libav-user] Libavformat - Guranteed seeking before keyframe

James Crisafulli james2048 at gmail.com
Sat Mar 6 21:44:23 EET 2021


On Sat, 6 Mar 2021 at 00:52, ilkercan Kaya <canilkerkaya at gmail.com> wrote:

> Hi Everyone,
>
> Anyone kindly wanting to help me on this matter? I am really stuck. In
> case of my question being vague, I can reword it. Please let me know.
>
> Kind regards.
>
>
> On Wed, Mar 3, 2021, 19:31 ilkercan Kaya <canilkerkaya at gmail.com> wrote:
>
>> In case this got lost, I'm kindly replying to this thread.
>>
>> On Sun, Feb 21, 2021, 04:20 ilkercan Kaya <canilkerkaya at gmail.com> wrote:
>>
>>> Hello Everyone,
>>>
>>> I am trying to seek my audio ogg file with the following:
>>>
>>> int64_t targetPts = av_rescale_q(seekTimeMicro, AV_TIME_BASE_Q, streamTimeBase);
>>>
>>> av_seek_frame(avFormatContext, streamInfoIndex, targetPts, AVSEEK_FLAG_BACKWARD);
>>>
>>>
>>> This works all fine. Most of the time AVSEEK_FLAG_BACKWARD makes sure
>>> that the stream is seeked to a time before targetPts. However, there are
>>> times the opposite happens, seeking a time after targetPts. I can see this
>>> with the first frame I request from avcodec_receive_frame() having pts
>>> (avFrame->pts) greater than my targetPts. I need to ensure that the first
>>> frame I receive is always smaller than my targetPts. (it's an overhead in
>>> my system, long story).
>>>
>>> I read it that here
>>> <https://stackoverflow.com/questions/20734814/ffmpeg-av-seek-frame-with-avseek-flag-any-causes-grey-screen>:
>>> "It appears that, for some reason, av_seek_frame() using
>>> AVSEEK_FLAG_BACKWARD returns the next keyframe when the frame that I am
>>> seeking is the one directly before this keyframe. Otherwise it returns the
>>> previous keyframe (which is what I want).". I suspect that this is the
>>> case. However, this post is too old so it might be outdated.
>>>
>>> Could you help me?
>>> Thank you!
>>>
>>>
>>> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/libav-user
>
> To unsubscribe, visit link above, or email
> libav-user-request at ffmpeg.org with subject "unsubscribe".



Hi Ilkercan,

I wanted to share what I used to seek so that the timestamp will be behind
the one you ask for.
I used this to implement a video player where when you seek, the timestamp
will be behind what you're looking for, then I can loop decode until the
requested PTS precisely.
Caveat: this worked for me for video, but may not work for you for audio.
In my use case I discarded the audio packets.
I used this API which is currently discouraged (not sure when it will be
completed, it has been saying that comment since 2.8 or even before):
https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/avformat.h#L2362.
I couldn't get the same results with av_seek_frame.

The code:

int64_t streamTimestamp = YOUR_STREAM_TIMESTAMP_TO_SEEK_TO;
int flags = 0;
// The idea is that we ask to get the low bound keyframe. This way we
always get the keyframe before our desired one.
// Set min_ts to 0, max to your ts, ts to your ts.
int ret = avformat_seek_file(formatCtx, YOUR_STREAM_INDEX, 0,
streamTimestamp, streamTimestamp, flags);

Hope it is useful.
- James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20210306/368f1451/attachment.html>


More information about the Libav-user mailing list