<div dir="ltr"><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 6 Mar 2021 at 00:52, ilkercan Kaya <<a href="mailto:canilkerkaya@gmail.com" target="_blank">canilkerkaya@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>Hi Everyone,<div dir="auto"><br></div><div dir="auto">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. </div><div dir="auto"><br></div><div dir="auto">Kind regards. </div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 3, 2021, 19:31 ilkercan Kaya <<a href="mailto:canilkerkaya@gmail.com" target="_blank">canilkerkaya@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>In case this got lost, I'm kindly replying to this thread. <br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Feb 21, 2021, 04:20 ilkercan Kaya <<a href="mailto:canilkerkaya@gmail.com" rel="noreferrer" target="_blank">canilkerkaya@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello Everyone,<br><br>I am trying to seek my audio ogg file with the following: <br><br><pre style="background-color:rgb(43,43,43);color:rgb(169,183,198);font-family:Consolas,monospace;font-size:12pt"></pre><div><pre style="background-color:rgb(43,43,43);color:rgb(169,183,198);font-family:Consolas,monospace;font-size:12pt"><span style="color:rgb(185,188,209)">int64_t </span>targetPts = av_rescale_q(seekTimeMicro<span style="color:rgb(204,120,50)">, </span><span style="color:rgb(144,139,37)">AV_TIME_BASE_Q</span><span style="color:rgb(204,120,50)">, </span>streamTimeBase)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span>av_seek_frame(avFormatContext<span style="color:rgb(204,120,50)">, </span>streamInfoIndex<span style="color:rgb(204,120,50)">, </span>targetPts<span style="color:rgb(204,120,50)">, </span><span style="color:rgb(144,139,37)">AVSEEK_FLAG_BACKWARD</span>)<span style="color:rgb(204,120,50)">;</span></pre></div><div><br></div><div>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).<br><br>I read it that <a href="https://stackoverflow.com/questions/20734814/ffmpeg-av-seek-frame-with-avseek-flag-any-causes-grey-screen" rel="noreferrer noreferrer" target="_blank">here</a>: "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.<br><br>Could you help me?<br>Thank you!</div><div><br></div><div><br></div></div>
</blockquote></div></div></div>
</blockquote></div></div></div>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="https://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">https://ffmpeg.org/mailman/listinfo/libav-user</a><br>
<br>
To unsubscribe, visit link above, or email<br>
<a href="mailto:libav-user-request@ffmpeg.org" target="_blank">libav-user-request@ffmpeg.org</a> with subject "unsubscribe".</blockquote><div><br></div><div><br></div>Hi Ilkercan,<div><br></div><div>I wanted to share what I used to seek so that the timestamp will be behind the one you ask for.</div><div>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.</div><div>Caveat: this worked for me for video, but may not work for you for audio. In my use case I discarded the audio packets.</div><div>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): <a href="https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/avformat.h#L2362" target="_blank" class="cf_div_theme_dark clutterFree_existingDuplicate clutterFree_noIcon">https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/avformat.h#L2362</a>. I couldn't get the same results with av_seek_frame.</div><div><br></div><div>The code: </div><div><br></div><div>int64_t streamTimestamp = YOUR_STREAM_TIMESTAMP_TO_SEEK_TO;<br>int flags = 0;<br>    // The idea is that we ask to get the low bound keyframe. This way we always get the keyframe before our desired one.</div><div>// Set min_ts to 0, max to your ts, ts to your ts.<br>int ret = avformat_seek_file(formatCtx, YOUR_STREAM_INDEX, 0, streamTimestamp, streamTimestamp, flags);<br></div><div><br></div><div>Hope it is useful.</div><div>- James</div></div></div>