Advertisement
Guest User

tag.py

a guest
Oct 1st, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. import os
  2. import eyed3
  3.  
  4. # Change this to your own music library's location.
  5. # I've changed to using forward slashes, so you don't need to use backslashes anymore.
  6.  
  7. rootdir = r"C:/Users/UserName/Music/"
  8.  
  9. def main():
  10.     # Loop through files in library location.
  11.     for subdir, dirs, files in os.walk(rootdir):
  12.         for file in files:
  13.             # Try/except for catching errors.
  14.             try:
  15.                 # Load our file for this iteration.
  16.                 audiofile = eyed3.load(subdir + "\\" + file)
  17.                 print "LOADED:\t" + file + "."
  18.  
  19.                 # If the total discs is one or nil, leave our disc tag empty.
  20.                 if audiofile.tag.disc_num[1] <= 1:
  21.                     audiofile.tag.disc_num = (None, None)
  22.  
  23.                 # Otherwise, add trailing zeroes.
  24.                 else:
  25.                     # Convert to str to read.
  26.                     d = str(audiofile.tag.disc_num)
  27.  
  28.                     # Initialise variables for disc index and total, respectively.
  29.                     i, t = "", ""
  30.  
  31.                     # Should it be a tuple, strip it of its parentheses and use the resulting string to read from.
  32.                     if type(eval(d)) == tuple:
  33.                         s = d.lstrip("(").rstrip(")")
  34.  
  35.                         # Read our index from the tuple.
  36.                         i = s[0:s.find(",")]
  37.  
  38.                         # Remember, trailing zeroes are not necessary for indices of 10 or above.
  39.                         if int(i) < 10:
  40.                             # Neither is it necessary to have multiple trailing zeroes for our disc numbers.
  41.                             if i[0] != "0":
  42.                                 i = "0" + i
  43.  
  44.                     # Set our disc number; bizarrely, the title automatically trails given a track index which is trailed. This still has to be saved later.
  45.                     audiofile.tag.disc_num = (i, audiofile.tag.disc_num[1])
  46.  
  47.                 # String-ify the track number property.
  48.                 v = str(audiofile.tag.track_num)
  49.  
  50.                 # Initialise some variables for index and total amount of tracks.
  51.                 i, t = "", ""
  52.  
  53.                 # If there is not already a trailing zero then ...
  54.                 if v[0] != "0":
  55.                     # Should our track number be a tuple, strip its parentheses and make it readable as a string.
  56.                     if type(eval(v)) == tuple:
  57.                         s = v.lstrip("(").rstrip(")")
  58.  
  59.                         # Find our index from this string-ified tuple.
  60.                         i = s[0:s.find(",")]
  61.  
  62.                         # Once again, we do not need to trail zeroes unless our track is between 0-9, or is already trailed.
  63.                         if int(i) < 10:
  64.                             if i[0] != "0":
  65.                                 i = "0" + i
  66.  
  67.                     # Set our track number. As above, no need to parse or rewrite the total.
  68.                     audiofile.tag.track_num = (i, audiofile.tag.track_num[1])
  69.  
  70.                 # Rename and re-tag untitled tracks.
  71.                 try:
  72.                     # Firstly, lower() the title, and see if it is titled like a typical untitled track.
  73.                     if audiofile.tag.title.lower() == "[untitled]" or audiofile.tag.title.lower() == "untitled":
  74.                         # We save the title tag with unicode-encoded strings.
  75.                         audiofile.tag.title = u"Untitled"
  76.                 except TypeError:
  77.                     # If it is unreadable as a string, then that probably warrants that it be titled blank, too.
  78.                     audiofile.tag.title = u"Untitled"
  79.  
  80.                 # Save our tracks and their tags; the fruits of our labour.
  81.                 # We use os.path.normpath(path) to localise the file location's string to the directory structure of the host system. This is to avoid quite unlikely but pretty annoying errors that would occur from using "/" on a Windows System. Screw you, Microsoft.
  82.                 audiofile.tag.save(
  83.                  os.path.normpath(subdir + "/" + file),
  84.                  eyed3.id3.ID3_V2_3
  85.                 )
  86.  
  87.                 # We print our result!
  88.                 print "SAVED:\t" + subdir + "/" + file + "."
  89.  
  90.             except AttributeError as e:
  91.                 # We don't want to print attribute errors when the file isn't even an mp3. This happens when our function loops through some other files that may be in the directory -- for example, "folder.png" or similar.
  92.                 if file[len(file)-4:len(file)] == ".mp3":
  93.                     print "AttributeError:\t" + str(e)
  94.  
  95.             # Print our other exceptions.
  96.             except IOError as e:
  97.                 print "IOError:\t" + str(e)
  98.  
  99.             except UnicodeEncodeError as e:
  100.                 print "UnicodeEncodeError:\t" + str(e)
  101.  
  102. # Call main().
  103. if __name__ == '__main__':
  104.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement