Advertisement
opexxx

id3-tagger.py

May 22nd, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # ID3 module example program
  4. # $Id: id3-tagger.py,v 1.5 2002/04/05 02:33:39 che_fox Exp $
  5.        
  6. # version 1.2
  7. # written 2 May 1999 by Ben Gertzfield <che@debian.org>
  8.  
  9. # This program is released under the GNU GPL, version 2 or later.
  10.  
  11. import getopt, string, re, sys
  12. from ID3 import *
  13.  
  14. version = 1.2
  15. name = 'id3-tagger.py'
  16.  
  17. def usage():
  18.     sys.stderr.write(
  19. "This is %s version %0.1f, a tool for setting ID3 tags in MP3 files.\n\n\
  20. Usage: %s [-t title] [-a artist] [-A album] [-y year] [-c comment] \n\
  21.       %s [-g genre] [-T tracknum] [-d] [-h] [-v] file1 [file2 ...]\n\n\
  22. -d: Delete the ID3 tag from specified file(s) completely\n\
  23. -h: Display this text\n\
  24. -v: Display the version of this program\n\n\
  25. With no arguments, display the ID3 tag of the given file(s).\n" %
  26.        (name, version, name, ' ' * len(name)))
  27.  
  28. def main():
  29.     options = {}
  30.  
  31.     try:
  32.     opts, args = getopt.getopt(sys.argv[1:], 't:a:A:y:c:g:T:dhvl')
  33.     except getopt.error, msg:
  34.     print msg
  35.     usage()
  36.     sys.exit(2)
  37.  
  38.     for opt, arg in opts:
  39.     if opt == '-v':
  40.         sys.stderr.write("This is %s version %0.1f.\n" % (name, version))
  41.         sys.exit(0)
  42.     if opt == '-h':
  43.         usage()
  44.         sys.exit(0)
  45.     if opt == '-t':
  46.         options['TITLE'] = arg
  47.     if opt == '-a':
  48.         options['ARTIST'] = arg
  49.     if opt == '-A':
  50.         options['ALBUM'] = arg
  51.     if opt == '-y':
  52.         options['YEAR'] = arg
  53.     if opt == '-c':
  54.         options['COMMENT'] = arg
  55.     if opt == '-g':
  56.         options['GENRE'] = arg
  57.         if opt == '-T':
  58.             options['TRACKNUMBER'] = arg
  59.     if opt == '-d':
  60.         options['delete'] = 1
  61.  
  62.     if len(args) == 0:
  63.     usage()
  64.     sys.exit(2)
  65.  
  66.     for file in args:
  67.     try:
  68.         id3info = ID3(file)
  69.  
  70.             needs_write = 0
  71.  
  72.             if len(options.keys()) > 0:
  73.                 needs_write = 1
  74.  
  75.             for k, v in options.items():
  76.                 if k == 'GENRE' and re.match("\d+$", v):
  77.                     id3info[k] = string.atoi(v)
  78.                 else:
  79.                     id3info[k] = v
  80.  
  81.         if options.has_key('delete'):
  82.         id3info.delete()
  83.  
  84.         print id3info
  85.  
  86.             if needs_write:
  87.                 id3info.write()
  88.  
  89.     except InvalidTagError, msg:
  90.         print "Invalid ID3 tag:", msg
  91.         continue
  92.  
  93. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement