ID3v1 位於 MP3 檔案末尾固定 128 位元組:
| 欄位 | 位移 | 長度 |
|---|---|---|
| TAG 標識 | 0 | 3 bytes ("TAG") |
| 標題 | 3 | 30 bytes |
| 歌手 | 33 | 30 bytes |
| 專輯 | 63 | 30 bytes |
| 年份 | 93 | 4 bytes |
| 備註 | 97 | 28 bytes |
| 流派 | 125 | 1 byte |
def get_id3v1(filename):
with open(filename, 'rb') as f:
f.seek(-128, 2) # 從檔尾倒數 128 bytes
tag = f.read(3)
if tag != b'TAG':
return None
title = f.read(30).rstrip(b'\x00').decode('latin-1', errors='ignore')
artist = f.read(30).rstrip(b'\x00').decode('latin-1', errors='ignore')
album = f.read(30).rstrip(b'\x00').decode('latin-1', errors='ignore')
year = f.read(4).rstrip(b'\x00').decode('latin-1', errors='ignore')
comment = f.read(28).rstrip(b'\x00').decode('latin-1', errors='ignore')
# genre byte 略
return {'title': title, 'artist': artist, 'album': album, 'year': year, 'comment': comment}
info = get_id3v1('song.mp3')
if info:
print(f"Title: {info['title']}")
print(f"Artist: {info['artist']}")
else:
print("無 ID3v1 標籤")
mutagen 或 eyed3 同時支援 v1/v2