Home » Linux » Python: MP3 ID3 tags

Now, generally I don’t bother much with ID3 tags. This afternoon however I had occasion to create some 130 MP3s, some of which derived their proper tags from CD-TEXT, some of which did not. As a result I had to perform two tasks. Whilst this could be achieved in just a few lines of BASH… I have forgotten much syntax. Python to the rescue (it’s more readable, and easier to write)!

Pseudocode:
– Take a directory path and find the MP3 files therein
– Strip all ID3v1/ID3v2 tags
– Apply new tags, prompting for Artist and Album, but using the filename itself as the Song title

Python (developed in 10mins…so it’s quick, dirty, E&OE…):

#!/usr/bin/env python
import os
import sys
from subprocess import check_output

def get_filepaths(directory):
  """
  This function will generate the file names in a directory
  tree by walking the tree either top-down or bottom-up. For each
  directory in the tree rooted at directory top (including top itself),
  it yields a 3-tuple (dirpath, dirnames, filenames).
  """
  file_paths = []  # List which will store all of the full filepaths.

  # Walk the tree.
  for root, directories, files in os.walk(directory):
    for filename in files:
      # Join the two strings in order to form the full filepath.
      filepath = os.path.join(root, filename)
      file_paths.append(filepath)  # Add it to the list.

  return file_paths  # Self-explanatory.

if len(sys.argv) == 1:
  path = raw_input("Input the path to directory containing MP3 files?:")
else:
  path = sys.argv[1]

dir_contents = get_filepaths(path)
mp3_files = []

for file in dir_contents:
  if ".mp3" in file or ".MP3" in file:
    mp3_files.append(file)
    print(file)

choice = raw_input("Strip all ID3v1 and ID3v2 tags from these files (y or n)?:")

if choice == "y":
  for file in mp3_files:
    check_output(["id3v2", " -D " + str(file)])
  print("Tags stripped!")
  print

newtags = raw_input("Write new tags? (y or n)?:")
if newtags == "y":
  ARTIST = raw_input("Enter ARTIST\t:")
  ALBUM = raw_input("Enter ALBUM\t:")
elif choice == "n":
  print("Exiting...")
  exit()
else:
  print("Invalid selection, exiting!")
  exit()

autotitle = raw_input("Do you want to automatically title each track using its filename (y or n)?:")
if autotitle == "y":
  for file in mp3_files:
    TITLE = file.split("/")[-1]
    print("Writing tag to file\t: " + file + "\t(artist=" + ARTIST + "album=" + ALBUM + "title=" + TITLE + ")")
    check_output(["id3v2", " -a " + ARTIST + " -A " + ALBUM + " -t " + TITLE])
elif autotitle == "n":
  manuallytitle = raw_input("So, do you want to enter each track title by hand (y or n)?:")
  if manuallytitle == "y":
    for file in mp3_files:
      TITLE = raw_input("Enter title for\t:" + file)
      print("Writing tag to file\t: " + file + "\t(artist=" + ARTIST + " album=" + ALBUM + " title=" + TITLE + ")")
      check_output(["id3v2", " -a " + ARTIST + " -A " + ALBUM + " -t " + TITLE])
else:
  print("Invalid selection, exiting!")
  exit()

Example output:

niall@concrescence ~ $ ./id3strip_tag.py /media/niall/4GB/
/media/niall/4GB/025.MP3
/media/niall/4GB/000.MP3
/media/niall/4GB/001.MP3
/media/niall/4GB/002.MP3
/media/niall/4GB/003.MP3
/media/niall/4GB/004.MP3
/media/niall/4GB/005.MP3
/media/niall/4GB/006.MP3
/media/niall/4GB/007.MP3
/media/niall/4GB/008.MP3
/media/niall/4GB/009.MP3
/media/niall/4GB/010.MP3
/media/niall/4GB/011.MP3
/media/niall/4GB/012.MP3
/media/niall/4GB/013.MP3
/media/niall/4GB/014.MP3
/media/niall/4GB/015.MP3
/media/niall/4GB/016.MP3
/media/niall/4GB/017.MP3
/media/niall/4GB/018.MP3
/media/niall/4GB/019.MP3
/media/niall/4GB/020.MP3
/media/niall/4GB/021.MP3
/media/niall/4GB/022.MP3
/media/niall/4GB/023.MP3
/media/niall/4GB/024.MP3
Strip all ID3v1 and ID3v2 tags from these files (y or n)?:y
Tags stripped!

Write new tags? (y or n)?:y
Enter ARTIST	:spam
Enter ALBUM	:ham
Do you want to automatically title each track using its filename (y or n)?:y
Writing tag to file	: /media/niall/4GB/025.MP3	(artist=spam album=ham title=025.MP3)
Writing tag to file	: /media/niall/4GB/000.MP3	(artist=spam album=ham title=000.MP3)
Writing tag to file	: /media/niall/4GB/001.MP3	(artist=spam album=ham title=001.MP3)
Writing tag to file	: /media/niall/4GB/002.MP3	(artist=spam album=ham title=002.MP3)
Writing tag to file	: /media/niall/4GB/003.MP3	(artist=spam album=ham title=003.MP3)
Writing tag to file	: /media/niall/4GB/004.MP3	(artist=spam album=ham title=004.MP3)
Writing tag to file	: /media/niall/4GB/005.MP3	(artist=spam album=ham title=005.MP3)
Writing tag to file	: /media/niall/4GB/006.MP3	(artist=spam album=ham title=006.MP3)
Writing tag to file	: /media/niall/4GB/007.MP3	(artist=spam album=ham title=007.MP3)
Writing tag to file	: /media/niall/4GB/008.MP3	(artist=spam album=ham title=008.MP3)
Writing tag to file	: /media/niall/4GB/009.MP3	(artist=spam album=ham title=009.MP3)
Writing tag to file	: /media/niall/4GB/010.MP3	(artist=spam album=ham title=010.MP3)
Writing tag to file	: /media/niall/4GB/011.MP3	(artist=spam album=ham title=011.MP3)
Writing tag to file	: /media/niall/4GB/012.MP3	(artist=spam album=ham title=012.MP3)
Writing tag to file	: /media/niall/4GB/013.MP3	(artist=spam album=ham title=013.MP3)
Writing tag to file	: /media/niall/4GB/014.MP3	(artist=spam album=ham title=014.MP3)
Writing tag to file	: /media/niall/4GB/015.MP3	(artist=spam album=ham title=015.MP3)
Writing tag to file	: /media/niall/4GB/016.MP3	(artist=spam album=ham title=016.MP3)
Writing tag to file	: /media/niall/4GB/017.MP3	(artist=spam album=ham title=017.MP3)
Writing tag to file	: /media/niall/4GB/018.MP3	(artist=spam album=ham title=018.MP3)
Writing tag to file	: /media/niall/4GB/019.MP3	(artist=spam album=ham title=019.MP3)
Writing tag to file	: /media/niall/4GB/020.MP3	(artist=spam album=ham title=020.MP3)
Writing tag to file	: /media/niall/4GB/021.MP3	(artist=spam album=ham title=021.MP3)
Writing tag to file	: /media/niall/4GB/022.MP3	(artist=spam album=ham title=022.MP3)
Writing tag to file	: /media/niall/4GB/023.MP3	(artist=spam album=ham title=023.MP3)
Writing tag to file	: /media/niall/4GB/024.MP3	(artist=spam album=ham title=024.MP3)