Python library for handling audio metadata across multiple formats including MP3, FLAC, MP4, OGG and many others
npx @tessl/cli install tessl/pypi-mutagen@1.47.0Mutagen is a comprehensive Python library for handling audio metadata across 20+ different audio formats. It provides both low-level format-specific APIs and high-level easy-to-use interfaces for reading, writing, and manipulating audio file tags and stream information.
Package: mutagen
Version: 1.47.0
PyPI: https://pypi.org/project/mutagen/
# Main entry point with auto-detection
import mutagen
from mutagen import File
# Base classes and exceptions
from mutagen import FileType, StreamInfo, Tags, Metadata, PaddingInfo, MutagenError
# Version information
from mutagen import version, version_stringimport mutagen
# Automatically detect format and load appropriate handler
audio_file = mutagen.File("song.mp3") # Returns MP3 instance
audio_file = mutagen.File("song.flac") # Returns FLAC instance
audio_file = mutagen.File("song.m4a") # Returns MP4 instance
# Access metadata like a dictionary
print(audio_file["TIT2"]) # Title for MP3/ID3
print(audio_file["TITLE"]) # Title for FLAC/Vorbis
# Stream information
info = audio_file.info
print(f"Length: {info.length} seconds")
print(f"Bitrate: {info.bitrate} bps")from mutagen.mp3 import MP3
from mutagen.flac import FLAC
from mutagen.mp4 import MP4
mp3_file = MP3("song.mp3")
flac_file = FLAC("song.flac")
mp4_file = MP4("song.m4a")FileType Classes: Each supported format has a main class (MP3, FLAC, MP4, etc.) that inherits from FileType. These provide format-specific tag access and stream information.
Auto-Detection: The File() function examines file headers to automatically select the appropriate format handler, returning the correct FileType instance.
Tag Containers: Different formats use different tag systems (ID3 for MP3, Vorbis Comments for FLAC/OGG, iTunes metadata for MP4). Mutagen provides native access to each system.
Easy Interfaces: Simplified dictionary-like interfaces (EasyID3, EasyMP4) that normalize tag names across formats for easier cross-format development.
Stream Information: All formats provide .info attribute containing technical details like bitrate, sample rate, duration, and encoding parameters.
Mutagen supports 20+ audio formats organized into these categories:
# File detection and base classes
def File(filething, options=None, easy=False) -> FileType | None:
"""Auto-detect audio format and return appropriate FileType instance.
Args:
filething: File path string or file-like object
options: Sequence of FileType implementations, defaults to all included ones
easy: If True, use easy interface when available
Returns:
FileType instance for the detected format, or None if type couldn't be determined
Raises:
MutagenError: if the detected type fails to load the file
"""
class FileType:
"""Abstract base class for all audio file types.
Attributes:
tags: Tag container for the file (format-specific)
info: StreamInfo instance with technical details
"""
def save(self, **kwargs) -> None:
"""Save tags back to file"""
def delete(self) -> None:
"""Remove all tags from file"""
class StreamInfo:
"""Abstract base class for audio stream information.
Attributes:
length: Duration in seconds (float)
bitrate: Bitrate in bits per second (int)
"""# Dictionary-like tag access
audio_file["TITLE"] = ["Song Title"]
audio_file["ARTIST"] = ["Artist Name"]
audio_file["ALBUM"] = ["Album Name"]
# Save changes
audio_file.save()
# Remove specific tag
del audio_file["GENRE"]
# Remove all tags
audio_file.delete()For simplified cross-format development → Easy Interfaces
from mutagen.easyid3 import EasyID3
from mutagen.easymp4 import EasyMP4
# Normalized tag names across formats
easy_id3 = EasyID3("song.mp3")
easy_mp4 = EasyMP4("song.m4a")
# Same tag names work for both
easy_id3["title"] = ["Song Title"]
easy_mp4["title"] = ["Song Title"]# Embedded artwork (format-dependent)
from mutagen.id3 import APIC
from mutagen.mp4 import MP4Cover
# ID3 artwork
mp3_file["APIC:"] = APIC(data=image_bytes, type=3, desc="Cover")
# MP4 artwork
mp4_file["covr"] = [MP4Cover(image_bytes, MP4Cover.FORMAT_JPEG)]
# Custom metadata
mp4_file["----:com.apple.iTunes:CUSTOM"] = [b"Custom Value"]from mutagen import MutagenError
try:
audio_file = mutagen.File("corrupted.mp3")
if audio_file is None:
print("Unsupported or corrupted file")
except MutagenError as e:
print(f"Mutagen error: {e}")
except IOError as e:
print(f"File error: {e}")