从 .mp3 文件中获取 "initial Key" 值

Get "initial Key" value from .mp3 file

I can't find a way to read the "initial key" property from an mp3 file to use the song information in my application.

我已经尝试寻找可以为我完成这项工作的图书馆。我发现 TagLib# 这是获取 tags/properties 不同文件格式的非常酷的解决方案。 (包括 mp3)。

我可以使用这个库来获取标题、艺术家、每分钟的节拍等。不幸的是,我只缺少初始键值,这不是特色。

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

I already found a source which seems to address the same issue and solves it with using TagLib#, but I can't figure out how he solved that problem. Use Ctrl + F and search for "Initial" to find the code block. You can find the link here

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

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

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

您可以使用 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))))));
    }
}

只是从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();
}

试试这个:

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

有关有效的音乐属性,请参阅 here for documentation on MusicProperties object and here