Midge

Basic Python Bulk Mp3 Tagger

Jan 15th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. # A crude 'as is' Python script for bulk tagging 'blank' mp3 files from a spreadsheet
  2. #
  3. # Usage:
  4. # Requires the Xlrd and EyeD3 packages
  5. # The user creates a 'files' subdirectory containing the following two entries...
  6. # 1) An xlxs spreadsheet named 'tag_file' (no file extension)
  7. # each spreadsheet row contains the following six cells: title, artist, album artist, album, genre, year.
  8. # there's a row for each mp3 file
  9. #
  10. # 2) An 'unknown_album' folder containing the untagged Mp3 files in the order they are to be tagged from the spreadsheet,      
  11. # NOTE: this can be tricky if your CD ripper doesn't follow the leading zeros "01_..., 02_..., 03_..." type format
  12. # Cross fingers, Run the script
  13. #
  14. # Disclaimer: there's NO error checking in this script, the user must ensure that number of tags == number of Mp3 files
  15. # I'm a seat of the pants type programmer, if this script fails, erases your precious music collection
  16. # or burns your house down I will not be held responsible
  17. # Yours Midge xx 2015
  18.  
  19.  
  20.  
  21. import os
  22. import eyed3 # EyeD3 Id3 modifying package
  23. import xlrd  # Xlrd Spreadsheet reading package
  24.  
  25. spreadsheet_row=0 # Spreadsheet row counter set to zero
  26.  
  27. # set up paths
  28. current_directory=os.getcwd() # Get current directory
  29. mp3_path= (current_directory +"\\files\unknown_album") # path to unknown_album folder full of untagged files IN ORDER
  30. spreadsheet_path= (current_directory +"\\files\\tag_file") # path to spreadsheet file of tagging info, rows cells are: artist, title, album artist, album, genre, year
  31.  #Note: it's up to user to ensure number of Spreadsheet rows == number of Mp3 files
  32.  
  33. book = xlrd.open_workbook(spreadsheet_path) # Open spreadsheet workbook
  34. sh = book.sheet_by_index(0)                 # Open first sheet in book
  35.  
  36. for root, dirs, files in os.walk(mp3_path):  # Walk through unknown album files
  37.     for mp3_name in files: # select each file in unknown album one by one
  38.         if mp3_name.endswith(".mp3"):  #check we are reading MP3 files
  39.        
  40.             # Get data from spreadsheet
  41.             Track_number = int(spreadsheet_row + 1) # generate track number as an int (ie: not 6.00)
  42.             Artist = sh.cell_value(rowx=spreadsheet_row, colx=0)
  43.             Artist_wsp=Artist.replace(" ", "_")  # replace any whitespace in artist with underscore
  44.             Track = sh.cell_value(rowx=spreadsheet_row, colx=1)
  45.             Track_wsp = Track.replace(" ", "_") # replace any whitespace in track title with underscore
  46.             Album_artist = sh.cell_value(rowx=spreadsheet_row, colx=2)
  47.             Album= sh.cell_value(rowx=spreadsheet_row, colx=3)
  48.             Genre= sh.cell_value(rowx=spreadsheet_row, colx=4)
  49.             Year= int(sh.cell_value(rowx=spreadsheet_row, colx=5))
  50.            
  51.             # construct artist and title file name (without any whitespace)
  52.             new_filename = '{:02}'.format(Track_number)+"."+Artist_wsp+"-"+Track_wsp+".mp3" #NB '01,02,03 format is important!
  53.            
  54.             # Print info
  55.             print "******File Information*********************"
  56.             print "Track number: {0} \nArtist: {1} \nTrack: {2} \nAlbum: {4}".format(Track_number, Artist, Track, Album_artist, Album, Year)
  57.             print "New filename will be: {0}".format(new_filename) #print new filename
  58.             print "****************************************** \n \n"
  59.            
  60.             audiofile = eyed3.load(mp3_path+"\\"+mp3_name) # Load mp3 file for tagging
  61.             # write tags to Mp3 file
  62.             audiofile.tag.track_num = Track_number
  63.             audiofile.tag.artist = Artist
  64.             audiofile.tag.album_artist = Album_artist
  65.             audiofile.tag.album = Album
  66.             audiofile.tag.title = Track
  67.             audiofile.tag.genre = Genre
  68.             audiofile.recording_date = str(Year) #Note: Year tag doesn't appear to work
  69.             audiofile.tag.save() # update file
  70.            
  71.             os.rename(mp3_path+ "\\"+ mp3_name, mp3_path+"\\"+ new_filename) #Rename file
  72.            
  73.             spreadsheet_row += 1 # Increment spreadsheet row
  74.            
  75.            
  76.            
  77. print ("finished")
Add Comment
Please, Sign In to add comment