Discussion:
[Libav-user] How to precisely seek in an audio file
Matthieu Regnauld
2018-10-26 13:28:03 UTC
Permalink
Hello,

I try to understand how I can seek in an audio file, at a very precise
position.

For example, I want to set the current position in my file to the frame
#1234567 (in a file encoded at 44100 Hz), which is equivalent to seek at
27994.717 milliseconds.

To achieve that, I tried this:

av_seek_frame(formatContext, audio_stream_index, 1234567,
AVSEEK_FLAG_FRAME);

But for some reason, the positioning is not totally accurate in an OGG file
(there is a delay of about 1/4 second), and not accurate at all in an MP3
file (I stay close to the beginning of the file...).

Here is my code:
https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a

Do I use the function the right way, or even the right function?

Thanks for your help.
Anton Shekhovtsov
2018-10-26 15:04:12 UTC
Permalink
Post by Matthieu Regnauld
Hello,
I try to understand how I can seek in an audio file, at a very precise
position.
For example, I want to set the current position in my file to the frame
#1234567 (in a file encoded at 44100 Hz), which is equivalent to seek at
27994.717 milliseconds.
av_seek_frame(formatContext, audio_stream_index, 1234567,
AVSEEK_FLAG_FRAME);
But for some reason, the positioning is not totally accurate in an OGG
file (there is a delay of about 1/4 second), and not accurate at all in an
MP3 file (I stay close to the beginning of the file...).
https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a
Do I use the function the right way, or even the right function?
Thanks for your help.
_______________________________________________
Libav-user mailing list
http://ffmpeg.org/mailman/listinfo/libav-user
Did you read description of av_seek_frame? The parameter is in time_base
units.
Audio frame is codec internal property and generally meaningless, I think
you take it wrong.
If you want very precise relative to video stream frame, this is quite
complex. You need to consider relative stream offsets. Also there may be
audio decoder delay.
Hope this helps.
Matthieu Regnauld
2018-10-26 15:16:32 UTC
Permalink
Hello,

Thank you for your help.

That said, I'm only dealing with audio file (ogg and mp3), no video at all.
And my goal is to set the current position to, let say, 27994.717
milliseconds (well, as close as possible, if possible with an error margin
below 1 millisecond), and start audio extraction from this very position
(as I do in my code example
<https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a>).

So what is the right way to achieve that? Is there any tutorial or example
somewhere?

Thanks.
Anton Shekhovtsov
2018-10-26 15:51:22 UTC
Permalink
Post by Matthieu Regnauld
Hello,
Thank you for your help.
That said, I'm only dealing with audio file (ogg and mp3), no video at all.
And my goal is to set the current position to, let say, 27994.717
milliseconds (well, as close as possible, if possible with an error margin
below 1 millisecond), and start audio extraction from this very position
(as I do in my code example
<https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a>).
So what is the right way to achieve that? Is there any tutorial or example
somewhere?
Thanks.
_______________________________________________
Libav-user mailing list
http://ffmpeg.org/mailman/listinfo/libav-user
There is a lot of useful info in headers, check it.
I think in your case this is what you need to use:
-do not specify stream index
-do not specify any flags
-position is in AV_TIME_BASE units which is ns if I remember correctly.
Matthieu Regnauld
2018-10-26 16:23:54 UTC
Permalink
Thank you very much.
Now it works the same way for both mp3 and ogg (and actually, position is
in microseconds, for your information).

But I still have a slight delay between the requested position and the
actual position (which is the same if I try with the same song in mp3 or
ogg), even if I ask to position to the very beginning of the file, like
this:

av_seek_frame(formatContext, -1, 0, 0);

I wonder if, maybe, the actual position is the closest keyframe (but is
there any keyframe in ogg or mp3?).
I read the avformat header, but I don't find that much information,
especially since I'm quite new to ffmpeg...

Is there any way that I can fix this annoying delay?

Thank you.

Loading...