Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save toolness/43eb6a06048ebe3a662b719b4354e1a1 to your computer and use it in GitHub Desktop.
Save toolness/43eb6a06048ebe3a662b719b4354e1a1 to your computer and use it in GitHub Desktop.
Script to rewrite the MP3 metadata in Ken Burns' Jazz album to be based on the length of the whole album
# The Ken Burns Jazz album MP3s from Amazon have separate track and disc
# number metadata, but many MP3 players don't look at the disc number,
# which makes it hard to play the album's tracks in order.
#
# This script goes through all the MP3's of the album and removes the
# disc number and rewrites the track number to be based on all the
# tracks in the album, rather than all the tracks on a particular disc.
#
# This script could theoretically be used on any other multi-disc MP3
# album, but I've only tried it on the Ken Burns Jazz MP3 album in
# particular.
#
# Note that this code was informed by the following:
#
# https://methodmatters.github.io/editing-id3-tags-mp3-meta-data-in-python/
from pathlib import Path
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
import glob
ALBUM_MP3_GLOB = Path("album") / "*.mp3"
mp3_files = glob.glob(str(ALBUM_MP3_GLOB))
tracks = []
for mp3_file in mp3_files:
p = Path(mp3_file)
mp3 = MP3(mp3_file, ID3=EasyID3)
if 'discnumber' not in mp3:
raise Exception(
'No "discnumber" in MP3 metadata! Has this script '
'already been run?'
)
disc = int(mp3['discnumber'][0].split('/')[0])
track = int(mp3['tracknumber'][0].split('/')[0])
disctrack = (disc * 100) + track
tracks.append((disctrack, mp3))
tracks.sort(key=lambda x: x[0])
total_tracks = len(tracks)
tracknum = 0
print("Rewriting tracks to be based on length of whole album.")
for _, mp3 in tracks:
tracknum += 1
newtrack = f'{tracknum}/{total_tracks}'
del mp3['discnumber']
mp3['tracknumber'] = newtrack
print(newtrack, mp3['title'][0])
mp3.save()
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment