Wednesday, August 21, 2019

Rename export from JB7, Brennan

I found myself having to migrate a MP3 collection off a Brennan JB7. This was good new as the JB7 wasn't fit for purpose in this case. I exported the MP3s, and the catalogue to a text file. I was surprised to discover the JB7 does not apparently use MP3 ID3 tags.

The script below corrects this by applying the catalogue data from the text file to the MP3 metadata. Use at your own risk!

import re
import eyed3
from pathlib import Path

if __name__ == "__main__":

    file = open('jb7_catalogue.txt', 'r')
    big_list = file.read().split('\n')
    cd_dict = {}
    lineno = 1    for line in big_list:
        m = re.match('^(\d)+$', line)
        if m:
            title = big_list[lineno - 2]
            if re.match('^(\d|[A-Z])+$', title):
                title = big_list[lineno - 3]
            artist = title
            if '/' in title:
                artist = title.split(' / ')[0]
                title = title.split(' / ')[1]
            if artist not in cd_dict:
                cd_dict[artist] = {}
            cd_dict[artist][title] = big_list[lineno:lineno+int(line)]
        lineno += 1
    processed = 0    for artist, album in cd_dict.items():
        print("processing {} of {}: {} .".format(processed, len(cd_dict.keys()), artist))
        for title, tracks in album.items():
            print("processing album {}".format(title))
            trackcounter = 0            for track in tracks:
                for f in list(Path("/path/to/brennan/exported/mp3/directory/").rglob("{}.mp3".format(track))):
                    if artist in str(f) and title in str(f):
                        trackcounter += 1                        mp3file = eyed3.load(f)
                        mp3file.initTag()
                        mp3file.tag.artist = artist
                        mp3file.tag.album = title
                        mp3file.tag.title = track
                        mp3file.tag.track_num = trackcounter
                        mp3file.tag.save()
        processed += 1    print("Done" )