Handling the ID3v2 tag
Leave feedback
ID3v2 is a metadata standard that is primarily used with mp3 files. Although it bears the name ID3, its structure is very different from ID3v1. ID3v2 tags consist of a number of frames, each of which contains a piece of metadata.
NoteFor more information on the ID3v2 standard visit http://id3.org/id3v2.3.0. Please note there are three versions of ID3v2: ID3v2.2, ID3v2.3, ID3v2.4.
The GroupDocs.Metadata API allows reading the ID3v2 tag in an MP3 audio. To get more information about ID3 tags, visit: https://en.wikipedia.org/wiki/ID3
The following steps show how to read the ID3v2 tag in an MP3 file.
- Load an MP3 file
- Extract the root metadata package
- Get the ID3v2 tag by using the Mp3RootPackage.ID3V2property
- If the ID3v2 tag is not null then check for all of its properties
AdvancedUsage.ManagingMetadataForSpecificFormats.
using (Metadata metadata = new Metadata(Constants.MP3WithID3V2))
{
var root = metadata.GetRootPackage<MP3RootPackage>();
if (root.ID3V2 != null)
{
Console.WriteLine(root.ID3V2.Album);
Console.WriteLine(root.ID3V2.Artist);
Console.WriteLine(root.ID3V2.Band);
Console.WriteLine(root.ID3V2.Title);
Console.WriteLine(root.ID3V2.Composers);
Console.WriteLine(root.ID3V2.Copyright);
Console.WriteLine(root.ID3V2.Publisher);
Console.WriteLine(root.ID3V2.OriginalAlbum);
Console.WriteLine(root.ID3V2.MusicalKey);
if (root.ID3V2.AttachedPictures != null)
{
foreach (var attachedPicture in root.ID3V2.AttachedPictures)
{
Console.WriteLine(attachedPicture.AttachedPictureType);
Console.WriteLine(attachedPicture.MimeType);
Console.WriteLine(attachedPicture.Description);
// ...
}
}
// ...
}
}
The GroupDocs.Metadata API supports updating the ID3v2 tag in an MP3 audio file.
The following are the steps to update the ID3v2 tag in an MP3 file.
- Load an MP3 file
- Extract the root metadata package
- Create the ID3v2 tag if it’s missing
- Update ID3v2 fields using the Mp3RootPackage.ID3V2 property
- Save the changes
The following code snippet shows how to update the ID3v2 tag in an MP3 file.
AdvancedUsage.ManagingMetadataForSpecificFormats.
using (Metadata metadata = new Metadata(Constants.MP3WithID3V2))
{
var root = metadata.GetRootPackage<MP3RootPackage>();
if (root.ID3V2 == null)
{
root.ID3V2 = new ID3V2Tag();
}
root.ID3V2.Album = "test album";
root.ID3V2.Artist = "test artist";
root.ID3V2.Band = "test band";
root.ID3V2.TrackNumber = "1";
root.ID3V2.MusicalKey = "C#";
root.ID3V2.Title = "code sample";
root.ID3V2.Date = "2019";
// ...
metadata.Save(Constants.OutputMp3);
}
To remove the ID3v2 tag from an MP3 audio just assign null to the Mp3RootPackage.ID3V2 property. The code sample below shows how to remove the ID3v2 tag from an MP3 file.
AdvancedUsage.ManagingMetadataForSpecificFormats.
using (Metadata metadata = new Metadata(Constants.MP3WithID3V2))
{
var root = metadata.GetRootPackage<MP3RootPackage>();
root.ID3V2 = null;
metadata.Save(Constants.OutputMp3);
}
You may easily run the code above and see the feature in action in our GitHub examples:
The MP3 Metadata Editing app is available to view and edit metadata of MP3 audio files, including title, artist, album, genre, and other ID3 tags.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.