• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在 .NET 检索 MP3 的持续时间?

用户头像
it1352
帮助1

问题说明

我已经构建了一个 WPF 应用程序,用户可以在其中将 MP3 文件拖放到列表框上.我需要一种方法来计算播放列表的总持续时间.

I have build a WPF application where users can drag and drop MP3 files onto a listbox. I need a way to calculate the total duration of the playlist.

我应该使用哪些库?或者是否可以仅使用 .NET 框架?

Any libraries I should use? Or is it possible using only the .NET framework?

正确答案

#1

经过大量理论分析,我找到了一种正确且无可争议地计算 mp3 文件时长的方法.

After lots of theorizing, I found a way to correctly and indisputably calculate a duration of an mp3 file.

让我首先重申为什么上述标准方法不起作用:

Let me first re-iterate why standard methods above won't work:

ID3 方法:并非所有文件都有 id3 标签,如果有,则可能没有设置持续时间字段.

ID3 method: not all files have id3 tags, and if they have it, they might not have duration field set in it.

通过读取一帧来估计 * 文件大小:不适用于 VBR 文件.

Estimating by reading one frame * file size: not gonna work for VBR files.

Xing header:不是所有文件都有.

Xing header: not all files have it.

通过 PCM 大小解码和确定它:我有 3 GB 以上的文件,我不会等到它解码.

Decoding and determining it via PCM size: I have 3 GB file, I'm not going to wait until it decodes.

我到处阅读,所有的东西都指向 NAudio.马克,感谢您的努力和干净的来源!但是,NAudio 主要建议的一种方法是使用 Mp3FileReader 读取文件并获取所有帧.问题:Mp3FileReader 在开始时创建一个 TOC,即使对于只有一天的小文件也需要永远:)

I read everywhere and all things lead to NAudio. Mark, THANKS for the good effort and clean source! However, a method that is mostly suggested with NAudio is to read a file using Mp3FileReader and get all frames. Problem: Mp3FileReader creates a TOC at the start and that takes forever, even for small files of only ONE day :)

Mark 建议我删除 TOC 创建,因为源可用,并且在执行此操作时,我发现了更简单的方法.这里是;不言自明:

Mark suggested that I remove TOC creation, since source is available, and while doing it, I found much simpler method. Here it is; is speaks for itself:

    double GetMediaDuration(string MediaFilename)
    {
        double duration = 0.0;
        using (FileStream fs = File.OpenRead(MediaFilename))
        {
            Mp3Frame frame = Mp3Frame.LoadFromStream(fs);
            if (frame != null)
            {
                _sampleFrequency = (uint)frame.SampleRate;
            }
            while (frame != null)
            {
                if (frame.ChannelMode == ChannelMode.Mono)
                {
                    duration  = (double)frame.SampleCount * 2.0 / (double)frame.SampleRate;
                }
                else
                {
                    duration  = (double)frame.SampleCount * 4.0 / (double)frame.SampleRate;
                }
                frame = Mp3Frame.LoadFromStream(fs);
            }
        }
        return duration;
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjgjf
系列文章
更多 icon
同类精品
更多 icon
继续加载