-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
401 lines (368 loc) · 12.5 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from tkFileDialog import askdirectory
from eyed3 import load as eyed3load
import coverimage
import extra
from os import listdir as os_listdir, rename as os_rename
from Tkinter import Tk
extra.cls()
# Initial Welcome Message
print '''
This program can be used to quickly edit tags.
Works great with MP3 folder
NOT TO BE USED WITH FOLDER, CONTAINING MP3 FROM DIFF ALBUMs
Press any key to continue, and select the folder to start the process
>> ''',
raw_input()
root = Tk()
root.withdraw()
extra.clearlog()
dirname = askdirectory(parent=root, title='Please select a directory')
extra.log("Dirname = "+dirname)
extra.cls()
# dirname = r'/home/ashwani/Music'
final = dict()
extra.log(str(final))
def getlistofMP3s(dirname):
'''
Returns a list of MP3 file names
'''
l = []
for i in os_listdir(dirname):
if str(i[-3:]).lower() == "mp3":
l += [i]
return l
def getTag(mp3, type):
'''
Return tag from MP3 file
allowed value of type 'album','artist','album_artist','title',
'track_num','genre','year','total_track'
'''
if type == "album":
if mp3.tag.album is None:
return ""
else:
return mp3.tag.album
elif type == "artist":
if mp3.tag.artist is None:
return ""
else:
return mp3.tag.artist
elif type == "album_artist":
if mp3.tag.album_artist is None:
return ""
else:
return mp3.tag.album_artist
elif type == "title":
if mp3.tag.title is None:
return ""
else:
return mp3.tag.title
elif type == "track_num":
if mp3.tag.track_num[0] is None:
return ""
else:
return str(mp3.tag.track_num[0])
elif type == "genre":
try:
mp3.tag.genre.name
except:
return ""
else:
return mp3.tag.genre.name
elif type == "year":
if mp3.tag.best_release_date is None:
return ""
else:
return str(mp3.tag.best_release_date)
elif type == "total_track":
if mp3.tag.track_num[1] is None:
return ""
else:
return str(mp3.tag.track_num[1])
def getFinalTag(mp3, type):
'''
Return tag from local execution scope
allowed value of type 'album','artist','album_artist','title',
'track_num','genre','year','total_track'
'''
if type == "album":
try:
final['album']
except:
return album
else:
return final['album']
elif type == "artist":
try:
final['artist']
except:
try:
artistini
except:
return artist
else:
return artist. \
split(" ", artistini)[artistini]. \
rsplit(" ", artistfin)[0]
else:
return final['artist']
elif type == "album_artist":
try:
final['album_artist']
except:
try:
album_artistini
except:
return album_artist
else:
return album_artist. \
split(" ", album_artistini)[album_artistini]. \
rsplit(" ", album_artistfin)[0]
else:
return final['album_artist']
elif type == "title":
try:
titleini
except:
return title
else:
return title. \
split(" ", titleini)[titleini]. \
rsplit(" ", titlefin)[0]
elif type == "track_num":
try:
track_numini, track_numfin
except:
return track_num
else:
return track_num. \
split(" ", track_numini)[track_numini]. \
rsplit(" ", track_numfin)[0]
elif type == "genre":
try:
final['genre']
except:
return genre
else:
return final['genre']
elif type == "year":
try:
final['year']
except:
return year
else:
return final['year']
elif type == "total_track":
try:
final['total_track']
except:
return total_track
else:
return final['total_track']
def setTag(mp3, type):
'''
Set tag from MP3 file (mp3 = eyed3.load("File.mp3"))
allowed value of type 'album','artist','album_artist','title',
'track_num','genre','year','total_track'
'''
if type != 'track':
extra.log(type, "=", getFinalTag(mp3, type))
if type == "album":
mp3.tag.album = unicode(getFinalTag(mp3, 'album'))
elif type == "artist":
mp3.tag.artist = unicode(getFinalTag(mp3, 'artist'))
elif type == "album_artist":
mp3.tag.album_artist = unicode(getFinalTag(mp3, 'album_artist'))
elif type == "title":
mp3.tag.title = unicode(getFinalTag(mp3, 'title'))
elif type == "track":
# Track_num = (TrackNumber, Total Track) //Tuple
track_num = (getFinalTag(mp3, 'track_num'))
total_track = (getFinalTag(mp3, 'total_track'))
extra.log(type, "=", track_num, total_track)
mp3.tag.track_num = (track_num, total_track)
elif type == "genre":
mp3.tag.genre = unicode(getFinalTag(mp3, 'genre'))
elif type == "year":
mp3.tag.release_date = getFinalTag(mp3, 'year')
def printMenu(mp31):
extra.cls()
print '''
+-------------------------------+
+ +
+ MP3 Tag Auto-editor +
+ +
+-------------------------------+
1. Select Album ('''+getFinalTag(mp31, 'album')+''')
2. Edit Artist Pattern ('''+getFinalTag(mp31, 'artist')+''')
3. Edit Title Pattern ('''+getFinalTag(mp31, 'title')+''')
4. Select Genre ('''+getFinalTag(mp31, 'genre')+''')
5. Select Year ('''+getFinalTag(mp31, 'year')+''')
6. Edit Album-Artist Pattern ('''+getFinalTag(mp31, 'album_artist')+''')
7. Edit Track Number Pattern ('''+getFinalTag(mp31, 'track_num')+''')
8. Select Total Track Number ('''+getFinalTag(mp31, 'total_track')+''')
9. Select Artist ('''+getFinalTag(mp31, 'artist')+''') (Change for all MP3s)
10. Select Album-Artist ('''+getFinalTag(mp31, 'album_artist')+''') (Change for all MP3s)
11. Select Cover-Art ('coverart.jpg' will be used by default in selected folder)
0. ~~~ Do the Magic ~~~
Enter Choice >> ''',
mp3files = getlistofMP3s(dirname)
extra.log(str(mp3files))
mp31 = eyed3load(dirname+"/"+mp3files[0])
extra.log("eyed3loaded = ", mp3files[0])
title = getTag(mp31, 'title')
album = getTag(mp31, 'album')
album_artist = getTag(mp31, 'album_artist')
artist = getTag(mp31, 'artist')
genre = getTag(mp31, 'genre')
year = getTag(mp31, 'year')
track_num = getTag(mp31, 'track_num')
total_track = getTag(mp31, 'total_track')
ch = 10
while ch != 0:
extra.log("Entered in loop ch = ", ch)
printMenu(mp31)
ch = int(input())
if ch == 1:
''' Abum Name '''
final['album'] = raw_input("Enter Album Name >> ")
extra.log('changed final[album] = '+final['album'])
elif ch == 2:
''' Artist Pattern '''
while True:
print "Enter Initial word count, End word count (0 if none) >> ",
artistini, artistfin = [int(x) for x in raw_input().split()]
print str(mp31.tag.artist). \
split(" ", artistini)[artistini]. \
rsplit(" ", artistfin)[0]
okprompt = raw_input("Is it OK ? (Y or N) >> ").lower()
if okprompt == 'y':
extra.log('changed artistini,fin ', artistini, artistfin)
break
elif ch == 3:
''' Title Pattern '''
while True:
print "Enter Initial word count, End word count (0 if none) >> ",
titleini, titlefin = [int(x) for x in raw_input().split()]
print str(mp31.tag.title). \
split(" ", titleini)[titleini]. \
rsplit(" ", titlefin)[0]
okprompt = raw_input("Is it OK ? (Y or N) >> ").lower()
if okprompt == 'y':
extra.log('changed titleini,fin', titleini, titlefin)
break
elif ch == 4:
''' Genre '''
final['genre'] = raw_input("Enter Genre >> ")
extra.log('changed final[genre] = ', final['genre'])
elif ch == 5:
''' Release Year '''
final['year'] = raw_input("Enter Release Year >> ")
extra.log('changed final[year] = ', final['year'])
elif ch == 6:
''' Album-Artist Pattern '''
while True:
print "Enter Initial word count, End word count (0 if none) >> ",
album_artistini, album_artistfin = [int(
x) for x in raw_input().split()]
print str(mp31.tag.album_artist). \
split(" ", album_artistini)[album_artistini]. \
rsplit(" ", album_artistfin)[0]
okprompt = raw_input("Is it OK ? (Y or N) >> ").lower()
if okprompt == 'y':
extra.log('changed al_artini,fin =',
album_artistini, album_artistfin)
break
elif ch == 7:
''' Track Number Pattern '''
while True:
print "Enter Initial word count, End word count (0 if none) >> ",
track_numini, track_numfin = [int(x) for x in raw_input().split()]
print str(mp31.tag.track_num[0]). \
split(" ", track_numini)[track_numini]. \
rsplit(" ", track_numfin)[0]
okprompt = raw_input("Is it OK ? (Y or N) >> ").lower()
if okprompt == 'y':
extra.log('change track_num pattern = ',
track_numini, track_numfin)
break
elif ch == 8:
''' Total Track Number '''
final['total_track'] = raw_input("Enter total track number >> ")
extra.log("changed fina[track_num] = ", final['total_track'])
elif ch == 9:
''' Artist for all MP3s '''
final['artist'] = raw_input("Enter Artist >> ")
extra.log('changed final[artist] = ', final['artist'])
elif ch == 10:
''' Album-Artist for all MP3s '''
final['album_artist'] = raw_input("Enter Album-Artist >> ")
extra.log('changed final[album_artist] = ', final['album_artist'])
elif ch == 11:
final_image = coverimage.getCoverImage(
dirname, getFinalTag(mp31, 'album'))
else:
pass
for i in mp3files:
aud = eyed3load(dirname+"/"+i)
extra.log(i, " eyed3loaded")
# Save the tags first
title = getTag(aud, 'title')
album = getTag(aud, 'album')
album_artist = getTag(aud, 'album_artist')
artist = getTag(aud, 'artist')
genre = getTag(aud, 'genre')
year = getTag(aud, 'year')
track_num = getTag(aud, 'track_num')
total_track = getTag(aud, 'total_track')
# Remove all the tags (everything)
extra.log(title, album, album_artist, artist)
extra.log(track_num, total_track, year)
aud.tag.clear()
# Update New Tags
setTag(aud, 'title')
setTag(aud, 'artist')
setTag(aud, 'album')
setTag(aud, 'album_artist')
setTag(aud, 'track')
setTag(aud, 'genre')
setTag(aud, 'year')
try:
final_image
except:
final_image = coverimage.getCoverImage(dirname, album)
if final_image:
extra.log("saving cover")
aud.tag.images.set(3, final_image, "image/jpeg", u"")
# Save the tags to file,
# After using tag.clear() method, need to supply file name to save tag
aud.tag.save(dirname+"/"+i, version=(1, None, None))
aud.tag.save(dirname+"/"+i, version=(2, 4, 0))
# Finally, Rename file as format '%tn %title'
extra.log(aud.tag.track_num[0], aud.tag.title)
# Ask if user want to rename files
extra.cls()
ch = raw_input("Do you wish to rename files? >> (Y/N) ").lower()
if ch == 'y':
print '''\n
Select Pattern
1. <Track Number> <Title> (default)
2. <Artist> - <Title>\n
>> ''',
ch = raw_input()
for i in mp3files:
aud = eyed3load(dirname+"/"+i)
if ch == '2':
ren_pattern = str(aud.tag.artist)+" - "+aud.tag.title
else:
ren_pattern = str(aud.tag.track_num[0])+" "+aud.tag.title
new_name = dirname+"/"+ren_pattern+".mp3"
os_rename(dirname+"/"+i, new_name)
extra.cls()
print '''
All Set, Exiting the program,
If you've faced any problem, please send log.txt file to author
Else delete that file
Press Any Key to Exit.......
''',
raw_input()