Search code examples
c#id3taglib-sharp

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.

I've already tried to find libraries which do the job for me. I found TagLib# which is a very cool solution for getting tags/properties of different file formats. (including mp3).

I can use this library to get the title, the artist, the beats per minute and so on.. only the initial key value is missing for my use which isn't featured, unfortunately.

I also tried to find other solutions which support the initial key property but I haven't found one.

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

I'll post a short part of my code which can be used to determine different info about one song in a pattern like this: (["bpm"]"title" - "artist")

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

Thanks for any help or recommendations in advance! :)


Solution

  • Try this:

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

    On Windows 10 you can also use:

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

    See here for documentation on MusicProperties object and here for the valid music properties.