tags:

views:

1428

answers:

5

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);
+3  A: 

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?

Jon Skeet
One example: MPEG Layer 3 Audio, Duration 1:29, Bitrate: 128kbps, Audio rate: 44kHz. The function finds something like 1:12 for duration. I will have no control over the kind of MP3 that will be uploaded to the system, so I need it to work no matter encoding/bitrate/whatever.
marcgg
After testing with taglib I think that the mp3s I have been given are a bit messed up or encoded differently... I'll just work my way around this (see my other comments on Jon's and Thomas' answers)
marcgg
@marcgg: Personally I think you ought to accept Thomas's answer, as its more generally useful.
Jon Skeet
@marcgg: do you want code sample that uses mpg123 and will determine *EXACT* mp3 file duration?
Daniel Mošmondor
+5  A: 

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

Thomas Levesque
It looks like constant bitrate (see my comment on Jon's answer). How could I test this?
marcgg
about your edit: taglib can get the duration? That'd be awesome, I'll check this out right now
marcgg
It only returns 0... I guess that the files I've been given are encoded weirdly, which would explain all this. I think I will just find a way around it
marcgg
TagLib Sharp is giving me result around 70% of the real duration.
J. Pablo Fernández
Almost everyone uses variable bit rate these days for music
Joe Philllips
A: 

The second post in the thread might help you: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c72033c2-c392-4e0e-9993-1f8991acb2fd

Emiswelt
A: 

I would consider using an external application to do this. Consider trying Sox and just run the version of the program that's executed by using soxi (no exe) and try parsing that output. Given your options I think you're better off just trusting someone else who has spent the time to work out all the weirdness in mp3 files unless this functionality is core to what you're doing. Good luck!

Jon
sox sounds like overkill for what I'm trying to do, don't you think?
marcgg
Yeah but if it works every time and you can move on with life isn't that worth it?
Jon
true :) I think I'll just find a way around. All this is to send to a flash player, but I know how to get the duration in the flash, so I can just figure it out once the file is sent. Thanks for the anwser thought!
marcgg
+2  A: 

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.

Daniel Mošmondor