# Python program to read ID3v1.1 tags from an MP3 file
def read_id3_tags(mp3_file):
try:
with open(mp3_file, "rb") as file:
# Move the pointer to the last 128 bytes (ID3v1.1 tag is always at the end)
file.seek(-128, 2) # Seek 128 bytes from the end of the file
# Read the last 128 bytes
tag = file.read(128)
# Check for the "TAG" header indicating an ID3v1.1 tag
if tag[0:3].decode("ascii") == "TAG":
# Extract the ID3v1.1 information
title = tag[3:33].decode("ascii", errors="ignore").strip()
artist = tag[33:63].decode("ascii", errors="ignore").strip()
album = tag[63:93].decode("ascii", errors="ignore").strip()
year = tag[93:97].decode("ascii", errors="ignore").strip()
comment = tag[97:127].decode("ascii", errors="ignore").strip()
genre = tag[127] # Genre is a single byte (integer)
# List of standard genre numbers (ID3v1.1 standard list)
genres = [
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska", "Death Metal",
"Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
"Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip",
"Gospel", "Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
"Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial",
"Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta Rap",
"Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave",
"Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
"Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk",
"Swing", "Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
"Progressive Rock", "Psychedelic Rock", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
"Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "Acapella", "Euro-House", "Dance Hall"
]
genre_name = genres[genre] if genre < len(genres) else "Unknown"
# Print the extracted ID3v1.1 tag information
print(f"Title: {title}")
print(f"Artist: {artist}")
print(f"Album: {album}")
print(f"Year: {year}")
print(f"Comment: {comment}")
print(f"Genre: {genre_name}")
else:
print("No ID3v1.1 tag found in this file.")
except FileNotFoundError:
print(f"Error: The file '{mp3_file}' does not exist.")
except Exception as e:
print(f"Error: {str(e)}")
# Example usage
read_id3_tags("example.mp3")
Output
Title: Sample Song
Artist: Sample Artist
Album: Sample Album
Year: 2024
Comment: Sample Comment
Genre: Rock