- Course
- Binary Files
- Read ID3 v1 tags from MP3 file
Read ID3 v1 tags from MP3 file
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that reads ID3 v1 specification tags from an MP3 music file. You should also check if the ID tag corresponds to the TAG characters of version 1.
ID3 specifications apply to any audiovisual file or container. However, audio containers are generally applied mainly. There are three versions of the specification that are compatible. For example, a file can contain tags simultaneously version 1.1 and version 2.0. In this case, the media player must decide which ones are relevant.
ID3 version 1
This first specification is very simple. It consists of attaching a block of fixed size of 128 bytes at the end of the file in question. This block contains the following tags:
- An identification header with the characters "TAG".
- Title: 30 characters.
- Artist: 30 characters.
- Album: 30 characters.
- Year: 4 characters.
- One comment: 30 characters.
- Genre (musical): a character.
All tags use ASCII characters, except the genre, which is an integer stored in a single byte. The musical genre associated with each byte is predefined in the standard and includes definitions of 80 genres, numbered from 0 to 79. Certain reproduction programs have expanded on their own the defined genres (from number 80).
Input
Output
TAG
Impact Moderato
Nauj Llopir
YouTube Audio Library
1993
:
Solution
using System;
using System.IO;
using System.Text;
public class ReadID3v1Tags
{
public class ID3v1Tag
{
private static byte idTagLenght = 3;
private static byte titleTagLenght = 30;
private static byte artistTagLenght = 30;
private static byte albumTagLenght = 30;
private static byte yearTagLenght = 4;
private static byte commentTagLenght = 30;
private static byte genreTagLenght = 1;
public byte[] Id = new byte[idTagLenght];
public byte[] Title = new byte[titleTagLenght];
public byte[] Artist = new byte[artistTagLenght];
public byte[] Album = new byte[albumTagLenght];
public byte[] Year = new byte[yearTagLenght];
public byte[] Comment = new byte[commentTagLenght];
public byte[] Genre = new byte[genreTagLenght];
public bool IsID3v1Tag()
{
if (Encoding.Default.GetString(Id).Equals("TAG"))
{
return true;
}
return false;
}
public override string ToString()
{
StringBuilder tag = new StringBuilder();
tag.AppendLine(Encoding.Default.GetString(Id));
tag.AppendLine(Encoding.Default.GetString(Title));
tag.AppendLine(Encoding.Default.GetString(Artist));
tag.AppendLine(Encoding.Default.GetString(Album));
tag.AppendLine(Encoding.Default.GetString(Year));
tag.AppendLine(Encoding.Default.GetString(Comment));
tag.AppendLine(Encoding.Default.GetString(Genre));
return tag.ToString();
}
}
public static void Main(string[] args)
{
const int SIZE = 128;
byte[] data = new byte[SIZE];
ID3v1Tag tag = new ID3v1Tag();
// Read data file .mp3
string fileName = "input.mp3";
using (FileStream file = File.OpenRead(fileName))
{
file.Seek(-128, SeekOrigin.End);
file.Read(tag.Id, 0, tag.Id.Length);
file.Read(tag.Title, 0, tag.Title.Length);
file.Read(tag.Artist, 0, tag.Artist.Length);
file.Read(tag.Album, 0, tag.Album.Length);
file.Read(tag.Year, 0, tag.Year.Length);
file.Read(tag.Comment, 0, tag.Comment.Length);
file.Read(tag.Genre, 0, tag.Genre.Length);
}
if (tag.IsID3v1Tag())
{
Console.WriteLine(tag.ToString());
}
}
}