6 sys.stderr.write("tag.py: %s\n" % msg)
7 sys.stderr.write("Usage: tag.py [-a artist] [-l album] [-g genre] [-n track] [-t title] [-y year] [-c charset] filename\n")
10 from getopt import getopt, GetoptError
12 optlist, args = getopt(sys.argv[1:], "a:g:l:n:t:y:c:",
13 ["artist=", "album=", "genre=", "track=", "title=", "year=", "charset="]
16 usage("iinvalid option")
26 for _opt, _arg in optlist:
27 if _opt in ("-a", "--artist"):
29 elif _opt in ("-l", "--album"):
31 elif _opt in ("-g", "--genre"):
33 if _arg in ID3.genres:
36 raise ValueError, "unknown genre `%s'" % _arg
37 elif _opt in ("-n", "--track"):
42 elif _opt in ("-t", "--title"):
44 elif _opt in ("-y", "--year"):
46 elif _opt in ("-c", "--charset"):
55 usage("too many arguments")
60 usage("too few arguments")
64 from os.path import getmtime
65 mtime = getmtime(filename)
68 vf = ogg.vorbis.VorbisFile(filename)
73 if album: tag.add_tag('ALBUM', album.decode(charset).encode('utf-8'))
74 if artist: tag.add_tag('ARTIST', artist.decode(charset).encode('utf-8'))
75 if track: tag.add_tag('TRACKNUMBER', str(track))
76 if title: tag.add_tag('TITLE', title.decode(charset).encode('utf-8'))
77 if genre: tag.add_tag('GENRE', genre)
78 if year: tag.add_tag('DATE', year)
80 tag.write_to(filename)
81 utime(filename, (mtime, mtime))