Search code examples
c#mp3id3

Edit mp3 id3 lyrics data c#


I've seen many solutions for editing Artist and Album info, etc, but is there a way to edit the lyrics in mp3 id3 data? There are softwares for this, but I'm asking if there's a way to program this in c#. Let's say I have the lyrics in text files for all of my music files, how do I put them into the mp3 files?

Thanks.


Solution

  • There is no internal built-in solution for this, you will have to use an external library such as these:

    http://sourceforge.net/projects/csid3lib/

    http://id3.codeplex.com/

    The latter looks more complete and feature rich.

    For example, to get the lyrics of a file:

    string[] mp3Files = Directory.GetFiles(_mp3Directory, "*.mp3");
    
                InfoProvider chartLyricsProvider = new ChartLyricsInfoProvider();
                InfoProvider lyrDbLyricsProvider = new LyrDbInfoProvider();
                foreach (string mp3File in mp3Files)
                {
                    Console.Write(Path.GetFileNameWithoutExtension(mp3File));
    
                    Id3Tag tag;
                    using (var mp3 = new Mp3File(mp3File))
                        tag = mp3.GetTag(Id3TagFamily.FileStartTag);
                    if (tag == null)
                        continue;
    
                    if (!tag.Artists.IsAssigned || !tag.Title.IsAssigned)
                    {
                        Console.WriteLine();
                        continue;
                    }
    
                    Console.WriteLine(" ({0} - {1})", tag.Artists.Values[0], tag.Title.Value);
    
                    Id3Tag[] lyricsTags = GetLyrics(tag, chartLyricsProvider, lyrDbLyricsProvider);
                    if (lyricsTags == null || lyricsTags.Length == 0)
                        continue;
    
                    string outputFilename = string.Format("{0} - {1}.txt", tag.Artists.Values[0], tag.Title.Value);
                    string outputFile = Path.Combine(_outputDirectory, outputFilename);
                    using (var lyricsWriter = new StreamWriter(outputFile, false))
                        lyricsWriter.Write(lyricsTags[0].Lyrics[0].Lyrics);
    
                    Console.WriteLine("    {0}", outputFilename);
                }
    

    I am sure that after looking through the docs of both those libraries, you can find how to assign lyrics.