Sign in to follow this  
SiS-Shadowman

Calculating length of an mp3 file

Recommended Posts

I'm trying to calculate the playing length of an mp3 file, but I don't know how to do that. I've found descriptions for the mp3 header, but nothing that indicates how I can calculate the playing length. I've tested several files, and in every one, the frames do have a different bitrate but also a different sampling rate. I've tried to add up the frame size for every frame, but some frames seem to have a forbidden bitrate and/or sampling rate. I don't know if my method is correct, so I'm asking you. Do you know a method to calculate the length? I know that I can just load the file with an audio library and let it calculate the length, but it takes to long, as the libraries decompress the frames as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiS-Shadowman

Do you know a method to calculate the length?


MP3 is an arbitrary data stream. In that stream, there may be MP3 headers. Scan through entire stream, searching for headers. Parse them to obtain their bitrate. Sum it up.

In other words, you have no choice but to parse entire file. There is no MP3 file format as such.

Share this post


Link to post
Share on other sites
Quote:
Original post by george135
Quote:

In other words, you have no choice but to parse entire file.


No.
Can be done with MM win32 apis.


Yes, existing API can be used. But that API parses the stream as well. My point was that there is no "length" field anywhere in file, and file is determined by number and type of headers.

The MP3 file structure indicates where bit-rate field is stored.

Bit-rate is number of bits per second. From that, and data chunk length, you can calculate how many seconds that particular chunk runs. Then you sum these chunk running times.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this