【问题标题】:How can I get the BPM property of an MP3 file in a Windows Forms App如何在 Windows 窗体应用程序中获取 MP3 文件的 BPM 属性
【发布时间】:2018-06-22 18:15:35
【问题描述】:

我正在尝试从 MP3 文件中获取 BPM 属性:

根据这个问题,我可以看到如何在 Windows 应用商店应用程序中执行此操作:

How to read Beats-per-minute tag of mp3 file in windows store apps C#?

但看不到如何在 Windows 窗体应用程序中使用 Windows.Storage。 (如果我理解正确,那是因为 Windows.Storage 是特定于 UWP 的。)

如何在表单应用程序中阅读此内容?如果没有原生库,很高兴使用(希望是免费的)库。

【问题讨论】:

  • BPM 并不总是记录在元数据中。有时需要计算。您是否尝试 1) 从音频中计算 BPM。 2) 读取所有文件类型的通用元数据。 3) 读取 MP3 文件上的 ID3v2 标签。 4) 读取与 Windows Explorer 相同的元数据。
  • 4 - 我不担心计算 bpm,因为曲调已经在商业软件(RekordBox 或 Serato DJ)中进行了分析。我只需要能够抓住价值
  • 我猜你正在寻找这个stackoverflow.com/questions/37869388/…

标签: c# mp3 file-properties


【解决方案1】:

有一个版本 TagLib 已移植到可移植类库 (PCL) 版本,Windows 窗体可以引用该版本并用于提取该信息。

我引用了 PCL 版本 TagLib#.Portable,它可以通过 Nuget 在 TagLib.Portable 获得

打开文件并读取所需信息很简单。

class Example {

    public void GetFile(string path) {
        var fileInfo = new FileInfo(path);
        Stream stream = fileInfo.Open(FileMode.Open);
        var abstraction = new TagLib.StreamFileAbstraction(fileInfo.Name, stream, stream);
        var file = TagLib.File.Create(abstraction);//used to extrack track metadata

        var tag = file.Tag;

        var beatsPerMinute = tag.BeatsPerMinute; //<--

        //get other metadata about file

        var title = tag.Title;
        var album = tag.Album;
        var genre = tag.JoinedGenres;
        var artists = tag.JoinedPerformers;
        var year = (int)tag.Year;
        var tagTypes = file.TagTypes;

        var properties = file.Properties;
        var pictures = tag.Pictures; //Album art
        var length = properties.Duration.TotalMilliseconds;
        var bitrate = properties.AudioBitrate;
        var samplerate = properties.AudioSampleRate;
    }
}

【讨论】:

    【解决方案2】:

    您可以为此使用 Windows 的 Scriptable Shell Objects。 item 对象有一个ShellFolderItem.ExtendedProperty 方法

    您所关注的属性是一个名为System.Music.BeatsPerMinute的官方Windows属性

    所以,这里是你如何使用它(你不需要引用任何东西,这要感谢 COM 对象的酷 dynamic C# 语法):

    static void Main(string[] args)
    {
        string path = @"C:\path\kilroy_was_here.mp3";
    
        // instantiate the Application object
        dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    
        // get the folder and the child
        var folder = shell.NameSpace(Path.GetDirectoryName(path));
        var item = folder.ParseName(Path.GetFileName(path));
    
        // get the item's property by it's canonical name. doc says it's a string
        string bpm = item.ExtendedProperty("System.Music.BeatsPerMinute");
        Console.WriteLine(bpm);
    }
    

    【讨论】:

    • 你,先生,是个传奇
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2016-06-15
    • 2022-11-30
    • 1970-01-01
    • 2018-11-04
    • 2019-08-25
    • 1970-01-01
    相关资源
    最近更新 更多