Mutagen Audio Tagging Library

1.47.0 · active · verified Thu Apr 09

Mutagen is a Python library to read and write audio tags for many formats, including MP3, FLAC, Ogg Vorbis/Opus/FLAC, M4A, ASF, and more. It provides both high-level access for common tags and low-level access to manipulate specific frame types. The library is actively maintained, with minor versions released every few months, currently at 1.47.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a dummy MP3 file, add ID3 tags using `mutagen.id3.ID3` frame objects, save them, then read and update tags using the simpler `mutagen.File(..., easy=True)` interface. Remember to always call `.save()` to persist changes.

import os
import mutagen
from mutagen.id3 import ID3, TIT2, TPE1, TALB
from mutagen.mp3 import MP3

# --- Create a dummy MP3 file for demonstration ---
# In a real scenario, you would use an actual audio file.
# This creates a minimal, valid-ish MP3 structure that mutagen can parse.
temp_mp3_path = "temp_quickstart_audio.mp3"
try:
    with open(temp_mp3_path, "wb") as f:
        f.write(b'\xFF\xFB\x30\x00' + b'\x00' * 1024)

    # --- Initializing and Adding Tags (using ID3 frames) ---
    # mutagen.File() is the primary entry point; for MP3s, it returns an MP3 object.
    audio_file = mutagen.File(temp_mp3_path)
    if audio_file is None:
        raise ValueError("Could not open dummy MP3 file.")

    # If the file has no ID3 tags, create a new ID3 object for it.
    if audio_file.tags is None:
        audio_file.tags = ID3()
    
    # Set common tags using ID3 frame objects
    audio_file.tags.add(TIT2(encoding=3, text=["My New Track Title"])) # Title
    audio_file.tags.add(TPE1(encoding=3, text=["Example Artist"])) # Artist
    audio_file.tags.add(TALB(encoding=3, text=["Demo Album"])) # Album
    
    audio_file.save() # Crucial: Saves changes to the file

    print(f"Tags written to '{temp_mp3_path}' using ID3 frames.")

    # --- Reading Tags (using EasyID3 for simpler access) ---
    # Open the file again, using easy=True for a dictionary-like interface
    easy_audio = mutagen.File(temp_mp3_path, easy=True)
    if easy_audio is None:
        raise ValueError("Could not re-open dummy MP3 with easy=True.")

    print("\n--- Reading Tags (using EasyID3) ---")
    print(f"Title: {easy_audio.get('title', ['N/A'])[0]}")
    print(f"Artist: {easy_audio.get('artist', ['N/A'])[0]}")
    print(f"Album: {easy_audio.get('album', ['N/A'])[0]}")
    
    # --- Updating Tags (using EasyID3) ---
    easy_audio["artist"] = ["Updated Artist Name"]
    easy_audio.save() # Save the update
    
    reloaded_easy_audio = mutagen.File(temp_mp3_path, easy=True)
    print(f"\nEasyID3 Artist (updated): {reloaded_easy_audio.get('artist', ['N/A'])[0]}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure you have write permissions in the current directory and the file can be created/accessed.")
finally:
    # Clean up the dummy file
    if os.path.exists(temp_mp3_path):
        os.remove(temp_mp3_path)
        print(f"\nCleaned up '{temp_mp3_path}'.")

view raw JSON →