Overview
TheMetadataHandler class provides a comprehensive interface for managing audio metadata across multiple formats. It supports reading and writing tags, cover art, lyrics, and audio file properties.
Supported Audio Formats
- MP3 (ID3v2 tags)
- FLAC (Vorbis Comments)
- OGG Vorbis (Vorbis Comments)
- MP4/M4A (iTunes-style tags)
Constructor
Absolute or relative path to the audio file to load
Methods
load_file()
Loads the audio file using mutagen. Called automatically by the constructor.No return value. Sets
self.audio to the loaded mutagen file object.get_tag()
Retrieves a tag value from the audio file.Tag name (e.g., ‘title’, ‘artist’, ‘album’)
Tag value as a string, or empty string if tag doesn’t exist
set_tag()
Sets a tag value in the audio file metadata.Tag name to set
Value to set. If a list is provided, duplicates are automatically removed while preserving order.
No return value. Modifies metadata in memory (call
save() to persist changes).save()
Saves all metadata changes to the audio file. Also auto-saves lyrics to .lrc file if present.Writes changes to disk
save_lyrics_file()
Saves lyrics to a.lrc file with the same name as the audio file.
Creates/overwrites a .lrc file in the same directory as the audio file
save_cover_file()
Saves cover art tocover.jpg in the same directory as the audio file.
Image data to save. If None, extracts from existing tags.
Whether to overwrite existing cover.jpg file
Creates/overwrites cover.jpg file
get_cover()
Extracts cover art from the audio file’s embedded metadata.Raw image data (JPEG or PNG), or None if no cover art is embedded
set_cover()
Embeds cover art into the audio file with optional automatic resizing.Raw image data (JPEG, PNG, or other PIL-supported format)
Maximum width/height in pixels. Images larger than this are automatically resized while maintaining aspect ratio. Set to 0 or None to disable resizing.
Embeds image into metadata (call
save() to persist)Properties
All properties support both reading and writing. Changes are not persisted untilsave() is called.
Metadata Properties
Track title
Track artist(s)
Album name
Album artist (for compilations)
Release year or full date (maps to ‘date’ tag)
Music genre
Track comment/description
Track Numbering Properties
Track number (e.g., “1” or “1/12”)
Total tracks on disc (FLAC/OGG only). Stored as separate TRACKTOTAL tag.
Disc number (e.g., “1” or “1/2”). For FLAC/OGG, automatically combines with DISCTOTAL if available.
Advanced Metadata Properties
Tempo in beats per minute
Musical key (e.g., “Am”, “C#m”)
International Standard Recording Code
Record label/publisher (maps to ‘organization’ tag)
Lyrics Property
Song lyrics. Supports format-specific storage:
- MP3: USLT (Unsynchronized Lyrics) ID3 frame
- FLAC/OGG: LYRICS Vorbis comment
- MP4: ©lyr atom
Read-Only Audio Properties
These properties extract technical information from the audio file and cannot be modified.Track duration in seconds
Audio bitrate in kbps (kilobits per second)
Sample rate in kHz (kilohertz)
File size in MB (megabytes), rounded to 2 decimal places
Complete Example
Format-Specific Notes
MP3 (ID3)
- Uses EasyID3 for standard tags
- Lyrics stored in USLT frames
- Cover art stored in APIC frames (always converted to JPEG)
- UTF-8 encoding (ID3v2.4)
FLAC
- Uses Vorbis Comments
- Supports TRACKTOTAL and DISCTOTAL separate tags
- Cover art stored as Picture blocks
- Lossless metadata storage
OGG Vorbis
- Uses Vorbis Comments (same as FLAC)
- Supports TRACKTOTAL and DISCTOTAL
- Cover art stored as Picture blocks
MP4/M4A
- Uses iTunes-style atoms
- Lyrics stored in ©lyr atom
- Cover art stored in ‘covr’ atom
- Automatically converts images to JPEG format
