Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duration of an MP3/wav audio file

Tags:

audio

delphi

mp3

How do you get the duration (in minutes and seconds) of an MP3/wav audio file in Delphi ?

like image 407
Attilah Avatar asked Jun 30 '09 18:06

Attilah


People also ask

How long is a WAV file?

The WAV format is by definition, the highest quality 16-bit audio format. It is also the largest at about 10 MB per minute of CD quality audio. The quality can be sacrificed for file size by adjusting the sampling rate, data width (i.e. 8-bits), and number of channels (up to 2 for stereo).

How long are MP3 files?

As far as I know, there is no limit. There is nothing in a pure MPEG stream indicating length, which is what I would imagine where the limit would come into play. MPEG frames are lined up one after the other, with nothing linking them (except for the bit reservoir, if used).

How big is a 3 minute WAV file?

While a three-minute song would average 30MB in WAV or AIFF format, that same song converted to MP3 would take up a tenth of the space—only around 3MB.


2 Answers

You can calculate the duration by dividing the size of the file by the bit rate. You can get the bit rate from one of the frame headers. Of course, this won't work for variable rate MP3s, where you can have a different rate for each frame.

Using the Header Layout (it's just four bytes):

  1. Open the MP3 in a stream

  2. Find the beginning of the first frame header by reading until you find the sync header, which has 11 consecutive bits set to 1. This used to be 12, but it was tweaked to allow for MPEG version 2.5.

  3. Determine the MPEG version ID. For the purposes of finding the bit rate, V2.5 is the same as V2

  4. Determine the layer description

  5. Read the bit rate index

  6. Using the MPEG version, layer description and bit rate index, determine the actual bit rate from the bit rate index table in the linked header reference

  7. Divide the file size in kilobits ((8 * size in bytes) / 1000) by the bit rate to get the duration in seconds

I couldn't find a Delphi sample, but here is a C# implementation that uses this technique for reference purposes. See the getLengthInSeconds method.

like image 82
Bruce McGee Avatar answered Oct 04 '22 06:10

Bruce McGee


Mp3 are divided into frames like this

You will need to count the number of frames

like image 38
Eric Avatar answered Oct 04 '22 04:10

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!