Help - Search - Members - Calendar
Full Version: How to calculate duration from MP3 file
Hydrogenaudio Forums > Lossy Audio Compression > MP3 > MP3 - Tech
imanta
I have a number of files and want to programatically find the duration of the mp3. I know there is a calculation using file size and bit rate, but I am not sure of the format.

My file is:

22050Hz Mono
37614 bytes

Any help is appreciated greatly!! Thanks very much.
pdq
Knowing that it is 22050 Hz mono doesn't help much. What you need to know, assuming that the file is CBR, is the bitrate of the file. If the file is ABR then the calculation becomes approximate, and if it is VBR then your only choice is to scan through the entire file. Unless the file has a header that tells you how long the file is, which many encoders will add.
spockep
QUOTE(imanta @ Feb 7 2007, 21:17) *

I have a number of files and want to programatically find the duration of the mp3. I know there is a calculation using file size and bit rate, but I am not sure of the format.

My file is:

22050Hz Mono
37614 bytes

Any help is appreciated greatly!! Thanks very much.



Without knowing the bitrate you can't do it.
imanta
Okay so it looks like I got it. Thanks for the help.
Tom Condon
QUOTE(pdq @ Feb 7 2007, 18:21) *

Knowing that it is 22050 Hz mono doesn't help much. What you need to know, assuming that the file is CBR, is the bitrate of the file. If the file is ABR then the calculation becomes approximate, and if it is VBR then your only choice is to scan through the entire file. Unless the file has a header that tells you how long the file is, which many encoders will add.


So what is the proper algorithm to get duration whether or not it is CBR or VBR? Do you count the size of the frame header in the calculation?
Pesmontis
QUOTE
.. the proper algorithm to get duration whether or not it is CBR or VBR..

That would be like this (please correct me if I'm wrong):

total-number-of-frames x samples-per-frame / sample-frequency

with samples-per-frame for MPEG-1 =
384 samples (Layer I);
1152 samples (Layer II);
1152 samples (Layer III);

with samples-per-frame for MPEG-2 =
192 samples (Layer I);
1152 samples (Layer II);
576 samples (Layer III)

This only holds for a clean MPEG audio stream, which is very probably not the practical case.

QUOTE
.. count the size of the frame header in the calculation..

As you can see header-size's got nothing to do with this calculation.
smack
Pesmontis, your millisecond values are wrong.

QUOTE
.. the proper algorithm to get duration whether or not it is CBR or VBR..
It's as simple as that: count all frames in the file.
Assuming (this can easily be verified while counting) that all frames in the file have the same properties for version (MPEG-1 or MPEG-2) and sampling frequency (e.g. 44.1 kHz) the calculation is the one given by Pesmontis:

duration = total-number-of-frames * samples-per-frame / sample-frequency

samples-per-frame has a value of 1152 (for MPEG-1 Layer III) or 576 (for MPEG-2 Layer III).
Pesmontis
I got these frame-durations from a post in this topic..

Oh I see, I didn't check units, sorry.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.