Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A crude 'as is' Python script for bulk tagging 'blank' mp3 files from a spreadsheet
- #
- # Usage:
- # Requires the Xlrd and EyeD3 packages
- # The user creates a 'files' subdirectory containing the following two entries...
- # 1) An xlxs spreadsheet named 'tag_file' (no file extension)
- # each spreadsheet row contains the following six cells: title, artist, album artist, album, genre, year.
- # there's a row for each mp3 file
- #
- # 2) An 'unknown_album' folder containing the untagged Mp3 files in the order they are to be tagged from the spreadsheet,
- # NOTE: this can be tricky if your CD ripper doesn't follow the leading zeros "01_..., 02_..., 03_..." type format
- # Cross fingers, Run the script
- #
- # Disclaimer: there's NO error checking in this script, the user must ensure that number of tags == number of Mp3 files
- # I'm a seat of the pants type programmer, if this script fails, erases your precious music collection
- # or burns your house down I will not be held responsible
- # Yours Midge xx 2015
- import os
- import eyed3 # EyeD3 Id3 modifying package
- import xlrd # Xlrd Spreadsheet reading package
- spreadsheet_row=0 # Spreadsheet row counter set to zero
- # set up paths
- current_directory=os.getcwd() # Get current directory
- mp3_path= (current_directory +"\\files\unknown_album") # path to unknown_album folder full of untagged files IN ORDER
- spreadsheet_path= (current_directory +"\\files\\tag_file") # path to spreadsheet file of tagging info, rows cells are: artist, title, album artist, album, genre, year
- #Note: it's up to user to ensure number of Spreadsheet rows == number of Mp3 files
- book = xlrd.open_workbook(spreadsheet_path) # Open spreadsheet workbook
- sh = book.sheet_by_index(0) # Open first sheet in book
- for root, dirs, files in os.walk(mp3_path): # Walk through unknown album files
- for mp3_name in files: # select each file in unknown album one by one
- if mp3_name.endswith(".mp3"): #check we are reading MP3 files
- # Get data from spreadsheet
- Track_number = int(spreadsheet_row + 1) # generate track number as an int (ie: not 6.00)
- Artist = sh.cell_value(rowx=spreadsheet_row, colx=0)
- Artist_wsp=Artist.replace(" ", "_") # replace any whitespace in artist with underscore
- Track = sh.cell_value(rowx=spreadsheet_row, colx=1)
- Track_wsp = Track.replace(" ", "_") # replace any whitespace in track title with underscore
- Album_artist = sh.cell_value(rowx=spreadsheet_row, colx=2)
- Album= sh.cell_value(rowx=spreadsheet_row, colx=3)
- Genre= sh.cell_value(rowx=spreadsheet_row, colx=4)
- Year= int(sh.cell_value(rowx=spreadsheet_row, colx=5))
- # construct artist and title file name (without any whitespace)
- new_filename = '{:02}'.format(Track_number)+"."+Artist_wsp+"-"+Track_wsp+".mp3" #NB '01,02,03 format is important!
- # Print info
- print "******File Information*********************"
- print "Track number: {0} \nArtist: {1} \nTrack: {2} \nAlbum: {4}".format(Track_number, Artist, Track, Album_artist, Album, Year)
- print "New filename will be: {0}".format(new_filename) #print new filename
- print "****************************************** \n \n"
- audiofile = eyed3.load(mp3_path+"\\"+mp3_name) # Load mp3 file for tagging
- # write tags to Mp3 file
- audiofile.tag.track_num = Track_number
- audiofile.tag.artist = Artist
- audiofile.tag.album_artist = Album_artist
- audiofile.tag.album = Album
- audiofile.tag.title = Track
- audiofile.tag.genre = Genre
- audiofile.recording_date = str(Year) #Note: Year tag doesn't appear to work
- audiofile.tag.save() # update file
- os.rename(mp3_path+ "\\"+ mp3_name, mp3_path+"\\"+ new_filename) #Rename file
- spreadsheet_row += 1 # Increment spreadsheet row
- print ("finished")
Add Comment
Please, Sign In to add comment