mp3rename 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/python2
  2. import eyed3
  3. import sys
  4. def err(*args):
  5. sys.stderr.write(" ".join(args) + "\n")
  6. def die(*args):
  7. err(*args)
  8. sys.exit(1)
  9. USAGE='Usage: {0} [file]'.format(sys.argv[0])
  10. def to_file_case(name):
  11. for ch in name:
  12. if ord(ch) > 127:
  13. err("ERROR: {0} outside ASCII range".format(ord(ch)))
  14. raise Exception("NonAsciiName")
  15. return '_'.join(name.lower().split())
  16. filenames = sys.argv[1:]
  17. if not filenames:
  18. die(USAGE)
  19. def do_rename(filename):
  20. audiofile = eyed3.load(filename)
  21. (tn,_) = audiofile.tag.track_num
  22. if tn is None:
  23. err("ERROR: Unknown track number for file `{0}'".format(filename))
  24. title = audiofile.tag.title
  25. newname = '{0:02}_-_{1}'.format(tn, to_file_case(title))
  26. print "renaming: `{0}' -> `{1}'".format(filename, newname)
  27. audiofile.rename(newname)
  28. for f in filenames:
  29. try:
  30. do_rename(f)
  31. except:
  32. err("ERROR: Unable to rename `{0}'".format(f))