Mp3s & UltraID3Lib

As I started listening to my music this morning, I notice something odd in MediaMonkey. My album art was all kinds of messed up. I am not totally sure how this happened, but my album art was not right. Fortunately, even though I embed the artwork into the ID3v2 tags, I keep a jpeg file in each album’s folder so I always have a copy of the artwork. The good news is that, this file was not messed up in anyway on all the folders I checked. This means I can replace the embedded artwork with the file on my computer. But to do this manually would take hours. Hours that I do not have. The solution? Write a C# program to handle it for me.

Using the UltraID3Lib .Net ID3 Library, I was able to construct a program that handled this task in a nice way. Now, this is not perfect but it did manage to do the job I needed.  Code after the jump…

 

private static void FixAlbumArt(FileInfo MyFile)
{
  //Find the jpeg file in the directory of the Mp3 File
  //We will embed this image into the ID3v2 tag
  FileInfo[] fiAlbumArt = MyFile.Directory.GetFiles("*.jpg");
  if (fiAlbumArt.Length < 1)
  {
    Console.WriteLine("No Album Art Found in {0}", MyFile.Directory.Name);
    return;
  }
  string AlbumArtFile = fiAlbumArt[0].FullName;

  //Create Mp3 Object
  UltraID3 myMp3 = new UltraID3();
  myMp3.Read(MyFile.FullName);
  ID3FrameCollection myArtworkCollection =
    myMp3.ID3v23Tag.Frames.GetFrames(MultipleInstanceFrameTypes.Picture);

  if (myArtworkCollection.Count > 0)
  {//Get Rid of the Bad Embedded Artwork
    #region Remove All Old Artwork
    for (int i = 0; i < myArtworkCollection.Count; i++)
    {
      ID3PictureFrame ra = (ID3PictureFrame)myArtworkCollection[0];
      try
      {
        myMp3.ID3v23Tag.Frames.Remove(FrameTypes.Picture);
      }
      catch { }
    }
    myArtworkCollection.Clear();

     //Save out our changes so that we are working with the
    //most up to date file and tags
    myMp3.ID3v23Tag.WriteFlag = true;
    myMp3.Write();
    myMp3.Read(MyFile.FullName);
    #endregion Remove All Old Artwork
  }
  //Create a PictureFrame object, pointing it at the image on my PC
  ID3PictureFrame AlbumArt =
    new ID3PictureFrame(
    (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(AlbumArtFile),
    PictureTypes.CoverFront, "Attached picture", TextEncodingTypes.ISO88591);
  myMp3.ID3v23Tag.Frames.Add(AlbumArt);
  myMp3.ID3v23Tag.WriteFlag = true;
  myMp3.Write();

  myMp3 = null;
}

Nothing too complicated, and this could probably be split up into multiple functions, but I was in a hurry (this took me about 15 minutes to get to a point that worked for me). I think that I might be able to thread this as well, since I have so many files doing a threadpool of like 10 or so files at once could speed things up a little bit.

[Technorati Tag: ]
[Technorati Tag: ]

Advertisements
This entry was posted in Technology. Bookmark the permalink.

2 Responses to Mp3s & UltraID3Lib

  1. Yuriy Zaletskyy says:

    Versiion which I downloaded from the internet doesn't have public property ID3v23Tag.WriteFlag. So I can not change it. And whey I call myMp3.Write(), I am getting error .

  2. Yuriy Zaletskyy says:

    Versiion which I downloaded from the internet doesn't have public property ID3v23Tag.WriteFlag. So I can not change it. And whey I call myMp3.Write(), I am getting error .

Comments are closed.