如何在C#中获取MP3的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1214040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 11:06:51  来源:igfitidea点击:

How to get the length of a MP3 in C#

c#.netmp3

提问by marcgg

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.

是的,这是这个问题的完全重复,但是作为答案给出和接受的链接对我不起作用。它返回不正确的值(2 分钟的 mp3 将列为 1'30,3 分钟列为 2'20),没有明显的模式。

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

所以又来了:如何使用 C# 获取 MP3 的长度?

or

或者

What am I doing wrong with the MP3Header class:

我对MP3Header 类做错了什么

MP3Header mp3hdr = new MP3Header();
bool boolIsMP3 = mp3hdr.ReadMP3Information("1.mp3");
if(boolIsMP3)
  Response.Write(mp3hdr.intLength);

采纳答案by Thomas Levesque

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

显然这个类使用文件大小/比特率计算持续时间。这仅适用于恒定比特率,我假设您的 MP3 具有可变比特率...

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

编辑:看看TagLib Sharp,它可以给你持续时间

回答by Jon Skeet

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.

您如何确定“错误”的 MP3 的长度?我经常发现头信息可能是错误的:例如,有一个特定版本的 LAME 有这个问题。

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

如果您在 Windows 资源管理器中调出文件的属性,会显示什么?

回答by Jon

I would consider using an external application to do this. Consider trying Soxand 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!

我会考虑使用外部应用程序来做到这一点。考虑尝试Sox并运行使用 soxi(无 exe)执行的程序版本,然后尝试解析该输出。鉴于您的选择,我认为您最好只相信其他人花时间解决 mp3 文件中的所有奇怪问题,除非此功能是您正在做的工作的核心。祝你好运!

回答by Daniel Mo?mondor

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

我包装了 mp3 解码器库,并将其提供给 .net 开发人员。你可以在这里找到它:

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

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

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

包括将 mp3 文件转换为 PCM 和读取 ID3 标签的示例。

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.

我猜你可以用它来读取 mp3 文件的持续时间。最坏的情况是您读取所有帧并计算持续时间 - VBR 文件。

To accurately determine mp3 duration, you HAVE TOread 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.

为了准确地确定MP3期限,你必须从自己的总结时间阅读所有的帧,并计算时间。很多情况下,人们将各种“元数据”放入 mp3 文件中,因此如果您根据比特率和文件大小进行估计,您会猜错。

回答by Daniel Mo?mondor

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.

根本无法估计 VBR 文件的长度。它内部的每个 mp3 帧都可能具有不同的比特率,因此从读取文件的任何部分,您无法知道文件的任何其他部分的数据密度。确定 VBR mp3 的确切长度的唯一方法是对其进行整体解码,或者(如果您知道如何)一一读取帧的所有标头,并收集其解码的 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.

只有当您使用的 CPU 是您需要节省的宝贵资源时,您才会使用后面的方法。否则,解码整个文件,你就会有持续时间。

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

您可以使用我的 mpg123 端口来完成这项工作: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.

更多:许多 mp3 文件都添加了“东西”,作为 id3 标签,如果您没有浏览所有文件,您可能会错误地在持续时间计算中使用该标签。

回答by Ivan Kochurkin

There is my solution for C# with soxsound processing library.

有我的带有sox声音处理库的C# 解决方案。

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;
}