Calculating audio track duration in seconds #267
Replies: 7 comments
-
Yup!
We're getting better with this over time for formats that make it hard. For MP3, Symphonia will look for a Xing/Info tag and use that to populate For OGG, if the source is seekable we should provide you an accurate Of course, counting samples will always be accurate!
You don't actually need to decode. You can accumulate the value of |
Beta Was this translation helpful? Give feedback.
-
Thanks for your answer!
What is the precision of this estimate? I absolutely need to have a precision of +/- 1 second for my current use case.
Does this method work with formats like MP3? If each sample can have a different bitrate, getting the total number of samples won't help to compute the total duration? Also I don't use symphonia for playback, I only use it to extract metadata from audio files, so I suppose I don't need to substract trim_start and trim_end? |
Beta Was this translation helpful? Give feedback.
-
It should be a sample accurate length in the vast majority of cases. However, since it is just a piece of metadata up to the encoder to specify correctly, it could also be completely wrong!
The method will work for all formats. However, for VBR MP3 it becomes a bit less accurate than counting samples. For VBR, Symphonia averages multiple MPEG audio frames to get the average bitrate, then uses that for the estimate.
MP3 encoders add padding samples that need to be trimmed off by a decoder. So it'll be more accurate by about +/- 50ms if you subtract the trim amounts. If you count samples, the decoder will do the trimming for you so you don't need to do anything. However, if you just use the raw timing information from the |
Beta Was this translation helpful? Give feedback.
-
I don't really get what you mean, concretely what would the code look like if I want to count samples to measure precise duration? And how far from reality would be the duration computed by the packets only (using |
Beta Was this translation helpful? Give feedback.
-
The solutions I proposed in the previous message: decoding and counting samples vs. just summing These two methods are the most accurate. Both require minimal modifications of the standard decode loop from the getting started example. However, there are two things to note:
Since your desired accuracy is +/-1 second, I recommend using You should also enable gapless playback, see here. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your answer. I don't get why I should enable gapless playback - concretely how is it useful for summing durations? |
Beta Was this translation helpful? Give feedback.
-
Strictly speaking, the padding ("gaps") added by the decoder is a consequence of the manner in which most lossy codecs work. Since these samples are undesirable, we usually enable gapless mode to trim them off. More concretely, enabling gapless mode more faithfully decodes the original length of the audio. I'm going to move this to discussion since this is question about digital audio in general. |
Beta Was this translation helpful? Give feedback.
-
Hi there!
I'm currently trying to compute the duration of a given
Track
. From what I understand, the following works:Is that right?
In case these values aren't available (e.g. formats with variable sample width such as MPEG or Vorbis), I understand the entire audio track needs to be read in order to determine the duration from the sum of all samples, right?
How do I achieve this? I have the loop that can get me a set of
AudioBufferRef
, but I don't know how to get an actual duration from it.How can I compute it? Thanks in advance for your help :)
Beta Was this translation helpful? Give feedback.
All reactions