9

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);
hippietrail
  • 15,848
  • 18
  • 99
  • 158
marcgg
  • 65,020
  • 52
  • 178
  • 231
  • Kind of duplicated question; I tried to answer that here: http://stackoverflow.com/questions/383164/how-to-retrieve-duration-of-mp3-in-net/13269914#13269914 – Daniel Mošmondor Nov 09 '12 at 16:50

7 Answers7

13

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
  • 286,951
  • 70
  • 623
  • 758
4

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 Jul 31 '09 at 18:38
  • 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 Jul 31 '09 at 18:53
  • 4
    @marcgg: Personally I think you ought to accept Thomas's answer, as its more generally useful. – Jon Skeet Jul 31 '09 at 22:31
  • @marcgg: do you want code sample that uses mpg123 and will determine *EXACT* mp3 file duration? – Daniel Mošmondor Sep 07 '10 at 08:02
3

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
  • 19,718
  • 12
  • 58
  • 99
0

Length of the VBR file CAN'T be estimated at all. Every mp3 frame inside of it could have different bitrate, so from reading any part of the file you can't know what density of the data is at any other part of the file. Only way of determining EXACT length of VBR mp3 is to DECODE it in whole, OR (if you know how) read all the headers of the frames one by one, and collect their decoded DURATION.

You will use later method only if the CPU that you use is a precious resource that you need to save. Otherwise, decode the whole file and you'll have the duration.

You can use my port of mpg123 to do the job: http://sourceforge.net/projects/mpg123net/

More: many mp3 files have "stuff" added to it, as a id3 tags, and if you don't go through all the file you could mistakenly use that tag in duration calculation.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
0

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

sth
  • 222,467
  • 53
  • 283
  • 367
Emiswelt
  • 3,909
  • 1
  • 38
  • 56
0

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
  • 2,085
  • 2
  • 20
  • 28
  • sox sounds like overkill for what I'm trying to do, don't you think? – marcgg Jul 31 '09 at 18:44
  • Yeah but if it works every time and you can move on with life isn't that worth it? – Jon Jul 31 '09 at 18:50
  • 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 Jul 31 '09 at 18:51
0

There is my solution for C# with sox sound processing library.

public static double GetAudioDuration(string soxPath, string audioPath)
{
    double duration = 0;
    var startInfo = new ProcessStartInfo(soxPath,
        string.Format("\"{0}\" -n stat", audioPath));
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardOutput = true;
    var process = Process.Start(startInfo);
    process.WaitForExit();

    string str;
    using (var outputThread = process.StandardError)
        str = outputThread.ReadToEnd();

    if (string.IsNullOrEmpty(str))
        using (var outputThread = process.StandardOutput)
            str = outputThread.ReadToEnd();

    try
    {
        string[] lines = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        string lengthLine = lines.First(line => line.Contains("Length (seconds)"));
        duration = double.Parse(lengthLine.Split(':')[1]);
    }
    catch (Exception ex)
    {
    }

    return duration;
}
Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80