tags:

views:

12256

answers:

7

What's a quick and easy way to view and edit ID3 tags (artist, album, etc.) using C#?

+10  A: 

TagLib Sharp has support for reading ID3 tags.

tj9991
I started using TagLib sharp based on the recommendation here and it worked great for my application (which was read-only of MP3 info).
greg7gkb
+1. Just what I needed.
David Stratton
+13  A: 

TagLib Sharp is pretty popular.

As a side note, if you wanted to take a quick and dirty peek at doing it yourself.. here is a C# snippet I found to read an mp3's tag info.

class MusicID3Tag

{

    public byte[] TAGID = new byte[3];      //  3
    public byte[] Title = new byte[30];     //  30
    public byte[] Artist = new byte[30];    //  30 
    public byte[] Album = new byte[30];     //  30 
    public byte[] Year = new byte[4];       //  4 
    public byte[] Comment = new byte[30];   //  30 
    public byte[] Genre = new byte[1];      //  1

}

string filePath = @"C:\Documents and Settings\All Users\Documents\My Music\Sample Music\041105.mp3";

        using (FileStream fs = File.OpenRead(filePath))
        {
            if (fs.Length >= 128)
            {
                MusicID3Tag tag = new MusicID3Tag();
                fs.Seek(-128, SeekOrigin.End);
                fs.Read(tag.TAGID, 0, tag.TAGID.Length);
                fs.Read(tag.Title, 0, tag.Title.Length);
                fs.Read(tag.Artist, 0, tag.Artist.Length);
                fs.Read(tag.Album, 0, tag.Album.Length);
                fs.Read(tag.Year, 0, tag.Year.Length);
                fs.Read(tag.Comment, 0, tag.Comment.Length);
                fs.Read(tag.Genre, 0, tag.Genre.Length);
                string theTAGID = Encoding.Default.GetString(tag.TAGID);

                if (theTAGID.Equals("TAG"))
                {
                    string Title = Encoding.Default.GetString(tag.Title);
                    string Artist = Encoding.Default.GetString(tag.Artist);
                    string Album = Encoding.Default.GetString(tag.Album);
                    string Year = Encoding.Default.GetString(tag.Year);
                    string Comment = Encoding.Default.GetString(tag.Comment);
                    string Genre = Encoding.Default.GetString(tag.Genre);

                    Console.WriteLine(Title);
                    Console.WriteLine(Artist);
                    Console.WriteLine(Album);
                    Console.WriteLine(Year);
                    Console.WriteLine(Comment);
                    Console.WriteLine(Genre);
                    Console.WriteLine();
                }
            }
        }
Simucal
That is only for ID3v1. v2 has another structure, it's in the beginning of the file (as opposed to v1 which was in the end) and has variable length (v1 is always 128 bytes).
jishi
+1. Just what I needed.
David Stratton
+4  A: 

UltraID3Lib...

//using HundredMilesSoftware.UltraID3Lib;
UltraID3 u = new UltraID3();
u.Read(@"C:\mp3\song.mp3");
//view
Console.WriteLine(u.Artist);
//edit
u.Artist = "New Artist";
u.Write();
Matt
+33  A: 

Thirding TagLib Sharp.

TagLib.File f = TagLib.File.Create(path);
f.Tag.Album = "New Album Title";
f.Save();
Luke
+1. Just what I needed.
David Stratton
Can anybody tell me how to set Artist property? There are a lot of related properties (FirstArtist, Artist, JointedArtists, FirstPerformer) and almost all of them are read-only or deprecated...
Laserson
+1  A: 

I tried to create an app to update the tags on MP3 files...but if is use 'TagLib Sharp' and the code in the accepted answer all the tags just are set to nothing.

So I tried 'UltraID3Lib' but if I set the genre to "Podcast" it errors. I think perhaps "Podcast" is not part to the standard tags. And it also errors with "Index was out of range..." on .read on some podcast...in particular the Stackoverflow mp3.

Eventually I used http://home.wanadoo.nl/squell/id3.html and a batch file

A: 

I wrapped mp3 decoder library and made it available for .net developers. You can find it here:

http://sourceforge.net/projects/mpg123net/

Included are the samples to convert mp3 file to PCM, and read ID3 tags.

Daniel Mošmondor
+1  A: 

Does anybody know where TagLib Sharp went? Clicking on the links to it, above, gets an error page.

--UPDATE I found the new location, here: http://www.novell.com/products/linuxpackages/opensuse11.1/taglib-sharp.html

Anyone know how to access the files listed here?

(taglib-sharp.com looks like it was taken over by porn.)

hewins
I was searching for the new location. Thanks!
Peerke