The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a Python script to batch-tag MP3s

Here’s a Python script that allows you to batch tag MP3 files using the eyed3 library. This script can update metadata like title, artist, album, genre, and year for all MP3 files in a directory.

Requirements

  • Python 3.x

  • eyed3 library (pip install eyed3)

Script: batch_tag_mp3s.py

python
import os import eyed3 def batch_tag_mp3s(directory, artist=None, album=None, genre=None, year=None): if not os.path.exists(directory): print(f"Directory not found: {directory}") return mp3_files = [f for f in os.listdir(directory) if f.lower().endswith('.mp3')] if not mp3_files: print("No MP3 files found in the directory.") return for filename in mp3_files: filepath = os.path.join(directory, filename) audiofile = eyed3.load(filepath) if audiofile is None: print(f"Could not load file: {filename}") continue if audiofile.tag is None: audiofile.initTag() title = os.path.splitext(filename)[0] audiofile.tag.title = title if artist: audiofile.tag.artist = artist if album: audiofile.tag.album = album if genre: audiofile.tag.genre = genre if year: audiofile.tag.recording_date = eyed3.core.Date(year) audiofile.tag.save() print(f"Tagged: {filename}") if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="Batch tag MP3 files in a directory.") parser.add_argument("directory", help="Directory containing MP3 files") parser.add_argument("--artist", help="Artist name") parser.add_argument("--album", help="Album name") parser.add_argument("--genre", help="Genre") parser.add_argument("--year", type=int, help="Year") args = parser.parse_args() batch_tag_mp3s( directory=args.directory, artist=args.artist, album=args.album, genre=args.genre, year=args.year )

How to Use

bash
python batch_tag_mp3s.py /path/to/mp3s --artist "Artist Name" --album "Album Name" --genre "Rock" --year 2023
  • It sets the title based on the filename by default.

  • Add or omit metadata arguments as needed.

Let me know if you want to extend it to read tags from a CSV file or rename files based on tags.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About