Handling the ID3v2 tag

Waht is ID3v2?

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.

Reading an ID3v2 tag

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.

  1. Load an MP3 file
  2. Extract the root metadata package
  3. Get the ID3v2 tag by using the Mp3RootPackage.ID3V2property
  4. If the ID3v2 tag is not null then check for all of its properties

AdvancedUsage.ManagingMetadataForSpecificFormats.Audio.MP3.MP3ReadID3V2Tag

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

				// ...
			}
		}

		// ...
	}
}

Updating an ID3v2 tag

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.

  1. Load an MP3 file
  2. Extract the root metadata package
  3. Create the ID3v2 tag if it’s missing
  4. Update ID3v2 fields using the Mp3RootPackage.ID3V2 property
  5. Save the changes

The following code snippet shows how to update the ID3v2 tag in an MP3 file.

AdvancedUsage.ManagingMetadataForSpecificFormats.Audio.MP3.MP3UpdateID3V2Tag

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

Removing an ID3v2 tag

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.Audio.MP3.MP3RemoveID3V2Tag

using (Metadata metadata = new Metadata(Constants.MP3WithID3V2))
{
	var root = metadata.GetRootPackage<MP3RootPackage>();
	root.ID3V2 = null;
	metadata.Save(Constants.OutputMp3);
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.