TinyTag Audio Metadata Reader
tinytag is a small, fast, and feature-rich Python library for reading audio file metadata from various formats including MP3, OGG, FLAC, M4A, WAV, and WMA. It supports ID3v1, ID3v2, OGG Vorbis, OGG Opus, M4A/MP4, and more. The current version is 2.2.1, and it maintains an active release cadence with regular updates.
Warnings
- breaking In version 2.0.0, the `disc`, `disc_total`, `track`, and `track_total` fields changed from `str` to `int`. Code expecting string values for these fields will fail.
- breaking Version 2.0.0 changed the behavior of the `as_dict()` method. Values for fields are now returned as lists, even if they contain only a single item.
- deprecated The `ignore_errors` parameter for `TinyTag.get()` was deprecated in version 2.0.0. Using this parameter is no longer recommended.
- gotcha Prior to version 2.2.1, malformed ID3v2 Synchronized Lyrics (SYLT) strings could cause an infinite loop during tag parsing, leading to application hangs or crashes.
- gotcha Before version 2.1.1, the library incorrectly removed the 'b' character from ID3 string data, leading to corrupted metadata values for certain fields.
Install
-
pip install tinytag
Imports
- TinyTag
from tinytag import TinyTag
Quickstart
import os
from tinytag import TinyTag
# Create a dummy audio file for demonstration
dummy_audio_file = 'example.mp3'
with open(dummy_audio_file, 'wb') as f:
f.write(b'RIFF\x00\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x44\xAC\x00\x00\x88\x58\x01\x00\x02\x00\x10\x00data\x00\x00\x00\x00')
try:
# In a real scenario, replace 'example.mp3' with your actual audio file path
tag = TinyTag.get(dummy_audio_file)
print(f"File: {dummy_audio_file}")
print(f"Artist: {tag.artist}")
print(f"Title: {tag.title}")
print(f"Duration: {tag.duration} seconds")
print(f"Audio offset: {tag.audio_offset}")
print(f"Filesize: {tag.filesize} bytes")
print(f"Bitrate: {tag.bitrate} kBit/s")
print(f"Genre: {tag.genre}")
print(f"Year: {tag.year}")
except Exception as e:
print(f"Error reading tag: {e}")
finally:
# Clean up the dummy file
if os.path.exists(dummy_audio_file):
os.remove(dummy_audio_file)