【问题标题】:Get "initial Key" value from .mp3 file从 .mp3 文件中获取“初始密钥”值
【发布时间】:2019-07-04 20:05:36
【问题描述】:

我找不到从 mp3 文件中读取“初始键”属性以在我的应用程序中使用歌曲信息的方法。

我已经尝试找到可以为我完成这项工作的库。我发现TagLib# 是一个非常酷的解决方案,用于获取不同文件格式的标签/属性。 (包括mp3)。

我可以使用这个库来获取标题、艺术家、每分钟的节拍数等等。不幸的是,我使用的初始键值丢失了,它没有被推荐。

我也尝试找到其他支持初始密钥属性的解决方案,但我没有找到。

我已经找到了一个似乎可以解决相同问题的来源,并使用 TagLib# 解决了它,但我不知道他是如何解决这个问题的。 使用 Ctrl + F 并搜索“Initial”以找到代码块。 可以找到链接here

我将发布我的代码的一小部分,它可用于以如下模式确定有关一首歌曲的不同信息:(["bpm"]"title" - "artist")

    var file = TagLib.File.Create(filePath);
    return $"[{file.Tag.BeatsPerMinute}]{file.Tag.Title} - {file.Tag.FirstPerformer}";

提前感谢您的任何帮助或建议! :)

【问题讨论】:

  • 您要提取哪一代和版本的 MP3 ID 标签?
  • 我真的不知道“初始键”值存储在哪个版本中。但在查看解决方案后,我认为使用的是版本 2。

标签: c# id3 taglib-sharp


【解决方案1】:

试试这个:

public static void Main(string[] args)
{
    var path = …
    var file = TagLib.File.Create (path);
    var id3tag = (TagLib.Id3v2.Tag)file.GetTag (TagTypes.Id3v2);
    var key = ReadInitialKey (id3tag);
    Console.WriteLine ("Key = " + key);
}

static string ReadInitialKey(TagLib.Id3v2.Tag id3tag)
{
    var frame = id3tag.GetFrames<TextInformationFrame>().Where (f => f.FrameId == "TKEY").FirstOrDefault();
    return frame.Text.FirstOrDefault() ;
}

在 Windows 10 上,您还可以使用:

async Task<string> ReadInitialKey(string path)
    {
        StorageFile file = await StorageFile.GetFileFromPathAsync(path);
        Windows.Storage.FileProperties.MusicProperties musicProperties = await file.Properties.GetMusicPropertiesAsync();
        var props = await musicProperties.RetrievePropertiesAsync(null);
        var inkp = props["System.Music.InitialKey"];
        return (string)inkp;
    }

有关 MusicProperties 对象的文档请参阅 here,有关有效音乐属性的文档请参阅 here

【讨论】:

  • 非常感谢,这立即生效! :) 现在我很好奇您是如何找到该解决方案的。或者你以前遇到过这个问题吗?
  • 前段时间我在做一些媒体的事情,所以我记得看到这些框架。 id3.org/id3v2.4.0-frames 具有名称和含义。因此,通过在 VS 中进行一些实验,我找到了它。至于 W10 的东西,我最近才意识到这些类,它们可以在控制台和普通的 .net 桌面应用程序中使用。顺便说一句,您的链接文档也有类似的解决方案。
【解决方案2】:

您可以使用 Shell 读取所有 MP3 属性。

在 Windows 10、VS 2015 上测试 =>

// Add Reference Shell32.DLL
string sFolder = "e:\\";
string sFile= "01. IMANY - Don't Be so Shy (Filatov & Karas Remix).mp3";
List<string> arrProperties = new List<string>();
Shell objShell = new Shell();
Folder objFolder;
objFolder = objShell.NameSpace(sFolder);
int nMaxProperties = 332;
for (int i = 0; i < nMaxProperties; i++)
{
    string sHeader = objFolder.GetDetailsOf(null, i);
    arrProperties.Add(sHeader);
}
FolderItem objFolderItem = objFolder.ParseName(sFile);
if (objFolderItem != null)
{
    for (int i = 0; i < arrProperties.Count; i++)
    {
        Console.WriteLine((i + ('\t' + (arrProperties[i] + (": " + objFolder.GetDetailsOf(objFolderItem, i))))));
    }
}

【讨论】:

    【解决方案3】:

    只是借用nuget: mono TaglibSharp的代码:

    var tfile = TagLib.File.Create(@"..");
    string initialKey = null;
    
    if (tfile.GetTag(TagTypes.Id3v2) is TagLib.Id3v2.Tag id3v2)
    {
        /*
        // test: add custom Initial Key tag 
        var frame = TextInformationFrame.Get(id3v2, "TKEY", true);
        frame.Text = new[] {"qMMM"};
        frame.TextEncoding = StringType.UTF8;
        tfile.Save();
        */
    
        var frame = TextInformationFrame.Get(id3v2, "TKEY", false);         
        initialKey = frame?.ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 2012-01-31
      • 1970-01-01
      • 2021-10-19
      • 2022-01-07
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多