Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the length of a MP3 in C#

Yes this is an exact duplicate of this question, but the link given and accepted as answer is not working for me. It is returning incorrect values (a 2 minutes mp3 will be listed as 1'30, 3 minutes as 2'20) with no obvious pattern.

So here it is again: how can I get the length of a MP3 using C# ?

or

What am I doing wrong with the MP3Header class:

MP3Header mp3hdr = new MP3Header();
bool boolIsMP3 = mp3hdr.ReadMP3Information("1.mp3");
if(boolIsMP3)
  Response.Write(mp3hdr.intLength);
like image 740
marcgg Avatar asked Jul 31 '09 18:07

marcgg


3 Answers

I wrapped mp3 decoder library and made it available for .net developers. You can find it here:

http://sourceforge.net/projects/mpg123net/

Included are the samples to convert mp3 file to PCM, and read ID3 tags.

I guess that you can use it to read mp3 file duration. Worst case will be that you read all the frames and compute the duration - VBR file.

To accurately determine mp3 duration, you HAVE TO read all the frames and calculate duration from their summed duration. There are lots of cases when people put various 'metadata' inside mp3 files, so if you estimate from bitrate and file size, you'll guess wrong.

like image 42
Daniel Mošmondor Avatar answered Nov 11 '22 15:11

Daniel Mošmondor


Apparently this class computes the duration using fileSize / bitRate. This can only work for constant bitrate, and I assume your MP3 has variable bitRate...

EDIT : have a look at TagLib Sharp, it can give you the duration

like image 145
Thomas Levesque Avatar answered Nov 11 '22 13:11

Thomas Levesque


How have you ascertained the lengths of the MP3s which are "wrong"? I've often found that the header information can be wrong: there was a particular version of LAME which had this problem, for example.

If you bring the file's properties up in Windows Explorer, what does that show?

like image 42
Jon Skeet Avatar answered Nov 11 '22 14:11

Jon Skeet