Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Removes unwanted tags from mp3s
  4.  
  5. import mutagen, os, re, sys
  6.  
  7. from mutagen.easyid3 import EasyID3
  8.  
  9. # Path to recursively walk
  10. inputPath = "/media/Brick/Music/Pub/"
  11.  
  12. # List to be populated by dirWalk function
  13. fileList = []
  14.  
  15. # File type
  16. fileType = '.mp3'
  17.  
  18. # Tags to save
  19. validKeys = ['artist', 'album', 'title', 'genre', 'organization', 'date', 'tracknumber']
  20.  
  21.  
  22. # Looks for a certain type of file and adds the dir/filename to fileList
  23. def dirWalk(inputPath):
  24.     # Make sure path is a directory . . .
  25.     if os.path.isdir(inputPath) == 0:
  26.         print "%s is not a directory" % inputPath
  27.     # . . . if so, continue!
  28.     else:
  29.         walkList = os.listdir(inputPath)
  30.         for item in walkList:
  31.             # if the item is a directory send it to dirWalk function to walk . . .
  32.             if os.path.isdir(os.path.join(inputPath, item)) == 1:
  33.                 #print "%s is a directory..." % item
  34.                 dirWalk(os.path.join(inputPath, item))
  35.             # if the item is a file, check to make sure it is an mp3
  36.             elif item.endswith(fileType):
  37.                 fileList.append([inputPath, item])
  38.  
  39. def updateTag(pathName, fileName):
  40.     pathName = pathName.decode('utf8')
  41.     fileName = fileName.decode('utf8')
  42.  
  43.     trackTags = dict()
  44.  
  45.     # open file with EasyID3
  46.     track = EasyID3(os.path.join(pathName, fileName))
  47.  
  48.     # Copy tags we want to keep to trackTags dict
  49.     for key, value in track.iteritems():
  50.         if key in validKeys:
  51.             trackTags[key] = value
  52.  
  53.     # Delete all tags
  54.     track.delete()
  55.  
  56.     print track
  57.  
  58.     # Copy saved tags from trackTags dict back to file
  59.     track.update(trackTags)
  60.  
  61.     track.save()
  62.  
  63. dirWalk(inputPath)
  64. print fileList
  65. for item in fileList:
  66.     updateTag(item[0], item[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement