-
Notifications
You must be signed in to change notification settings - Fork 1
/
prittag.py
executable file
·269 lines (249 loc) · 9.8 KB
/
prittag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#############################################################################
# prittag - a tool for tagging mp3, ogg and aac files #
# #
# Copyright (c) 2010 Nils Mehrtens #
# #
# prittag is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License #
# as published by the Free Software Foundation; #
# either version 3 of the License, or (at your option) any later version. #
# prittag is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# See the GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, see <http://www.gnu.org/licenses/>. #
#############################################################################
import sys
import os
import base64
import string
import re
import traceback
import subprocess
from xml.etree import ElementTree
from mutagen.mp3 import MP3
from mutagen.oggvorbis import OggVorbis
from mutagen.mp4 import MP4, MP4Cover
import mutagen.id3 as id3
def parse_xml(path):
tags = {}
disable_strip_space_globally = False
try:
with open(path, 'r') as f:
data = f.read()
except:
print ('failed to read %s' % str(path))
sys.exit(1)
try:
xml = ElementTree.XML(data)
except:
print ('parsing XML failed!')
traceback.print_exc()
sys.exit(1)
if 'strip-space' in xml.keys():
if xml.get('strip-space') in ['No', 'no']:
disable_strip_space_globally = True
for child in xml.getchildren():
key = str(child.tag)
if key == 'chapters':
chapters = child.getchildren()
chapters2 = []
for chapter in chapters:
chapter = chapter.getchildren()
chapter2 = {}
for element in chapter:
if element.tag in ['title', 'time']:
chapter2[element.tag] = strip_string(unicode(element.text))
else:
print "Warning: malformed chapter tag '%s'" % str(element.tag)
if len(chapter2) == 2:
chapters2.append(chapter2)
value = chapters2
else:
value = unicode(child.text)
if disable_strip_space_globally:
if 'strip-space' in child.keys():
if child.get('strip-space') in ['Yes', 'yes']:
value = strip_string(value)
else:
if 'strip-space' in child.keys():
if child.get('strip-space') not in ['No', 'no']:
value = strip_string(value)
else:
value = strip_string(value)
tags[key] = value
if len(tags) < len(xml.getchildren()):
print ("Error: there are duplicate tags!")
sys.exit(1)
for tag in tags:
bad = False
if tag in ['track', 'number-of-tracks', 'disc', 'number-of-discs']:
value = tags[tag]
try:
value = int(value)
except:
bad = True
if value < 0:
bad = True
if bad:
print ('Bad value in <%s>: "%s" is not a positive integer!' % (str(tag), str(value)) )
sys.exit(1)
return tags
def strip_string(string):
if len(string.splitlines()) > 1:
new_string = '\n'.join([i.strip() for i in string.splitlines()])
new_string = new_string[1:] #remove \n at the beginning of the string
if string[len(string)-1] != '\n':
new_string = new_string[:len(new_string)-1]
#remove \n at the end of the string if wasn't present before
return new_string
else:
return string.strip()
def tag_file(path, tags):
file_type = get_file_type(path)
if file_type in ['ogg', 'oga']:
write_tags_to_ogg(path, tags)
elif file_type == 'mp3':
write_tags_to_mp3(path, tags)
elif file_type in ['mp4', 'm4a', 'aac']:
write_tags_to_mp4(path, tags)
else:
raise TypeError("%s is neither a mp3 nor an ogg nor a mp4 file" % path)
def get_file_type(path):
ext = os.path.splitext(path)[1]
ext = string.replace(ext, '.', '')
ext = ext.lower()
return ext
def write_tags_to_ogg(path, tags):
audio = OggVorbis(path)
for dest, source in [['TITLE', 'title'], ['COMPOSER', 'composer'],
['ALBUM', 'album'], ['DATE', 'date'],
['ARTIST', 'artist'], ['GENRE', 'genre'],
['ALBUMARTIST', 'album-artist'],
['TRACKNUMBER', 'track'],
['TRACKTOTAL', 'number-of-tracks'], ['DISCNUMBER',
'disc'], ['COMMENT', 'comment']]:
if source in tags:
audio[dest] = tags[source]
if 'cover' in tags:
audio['coverartmime'] = 'image/jpeg'
audio['coverartdescription'] = 'Cover'
audio['coverarttype'] = '3'
audio['coverart'] = get_ogg_coverart(tags['cover'])
audio.save()
def get_ogg_coverart(path):
with open(path, 'rb') as f:
data = f.read()
data = base64.b64encode(data)
return data
def write_tags_to_mp3(path, tags):
audio = MP3(path)
for i, tag in [['title', 'TIT2'], ['artist', 'TPE1'], ['album', 'TALB'],
['date', 'TDRC'], ['composer', 'TCOM'], ['genre', 'TCON'],
['lyrics', 'USLT'], ['album-artist', 'TPE2'],
['disc', 'TPOS'], ['comment', 'COMM']]:
if i in tags:
if tag == 'USLT':
tag = id3.Frames[tag](encoding=3, text=tags[i], desc='', lang='eng')
audio[tag.HashKey] = tag
elif tag == 'COMM':
tag = id3.Frames[tag](encoding=3, text=tags[i], lang='eng')
audio[tag.HashKey] = tag
else:
tag = id3.Frames[tag](encoding=3, text=tags[i])
audio[tag.HashKey] = tag
if 'track' in tags:
if 'number-of-tracks' in tags:
num_tracks = tags['number-of-tracks']
else:
num_tracks = 0
track_num = "%d/%d" % (int(tags['track']), int(num_tracks))
tag = id3.Frames['TRCK'](encoding=3, text=track_num)
audio[tag.HashKey] = tag
if 'cover' in tags:
image = get_mp3_coverart(tags['cover'])
image = id3.APIC(3, 'image/jpeg', 3, 'Cover', image)
audio[image.HashKey] = image
audio.save()
def get_mp3_coverart(path):
with open(path, 'rb') as f:
data = f.read()
return str(data)
def write_tags_to_mp4(path, tags):
audio = MP4(path)
for dest, source in [['\xa9nam', 'title'], ['\xa9wrt', 'composer'],
['\xa9alb', 'album'], ['\xa9day','date'],
['\xa9ART', 'artist'], ['\xa9gen', 'genre'],
['\xa9lyr', 'lyrics'], ['aART', 'album-artist'],
['\xa9cmt', 'comment']]:
if source in tags:
audio[dest] = [tags[source]]
if 'track' in tags:
if 'number-of-tracks' in tags:
num_tracks = int(tags['number-of-tracks'])
else:
num_tracks = 0
audio['trkn'] = [(int(tags['track']), num_tracks)]
if 'disc' in tags:
if 'number-of-discs' in tags:
num_disks = int(tags['number-of-discs'])
else:
num_disks = 0
audio['disk'] = [(int(tags['disc']), num_disks)]
if 'cover' in tags:
audio['covr'] = [get_mp4_coverart(tags['cover'])]
audio.save()
if 'chapters' in tags:
write_mp4_chapters(path, tags['chapters'])
def get_mp4_coverart(path):
with open(path, 'rb') as f:
data = f.read()
cover = MP4Cover(data)
return cover
def write_mp4_chapters(path, chapters):
chapter_path = os.path.splitext(path)[0] + '.chapters.txt'
data = ""
for chapter in chapters:
line = u"%s %s\n" % (chapter['time'], chapter['title'])
line = line.encode('utf-8')
data = ''.join((data, line))
if data:
try:
with open(chapter_path, 'w') as f:
f.write(data)
except:
print "couldn't write to %s" % chapter_path
else:
call_mp4_chaps(path)
os.remove(chapter_path)
def call_mp4_chaps(path):
popen = subprocess.Popen('mp4chaps -i %s' % path, shell = True)
popen.wait()
if __name__ == "__main__":
print ('''prittag Copyright (C) 2011 Nils Mehrtens
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see http://www.gnu.org/licenses/gpl.html for details.
''')
if len(sys.argv) < 3:
print ('usage: %s <xml file with tags> <file to tag> [<file to tag> ...]' % sys.argv[0])
sys.exit(1)
else:
args = sys.argv[1:]
for path in args:
if not os.path.exists(path):
print ("%s doesn't exist." % path)
sys.exit(1)
elif not os.path.isfile(path):
print ("%s is not a file." % path)
sys.exit(1)
config = args[0]
files = args[1:]
tags = parse_xml(config)
for file in files:
print ("Tagging %s ..." % file)
tag_file(file, tags)