forked from albumidentify/albumidentify
/
albumidentify.py
executable file
·352 lines (328 loc) · 10.5 KB
/
albumidentify.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
#!/usr/bin/python2.5
import sys
import fingerprint
import musicdns
import os
import lookups
import parsemp3
import musicbrainz2
import itertools
import pickle
import md5
import random
import shelve
def output_list(l):
l.sort()
ret=[]
start=l[0]
end=l[0]
for i in l[1:]:
if end+1==i:
end=i
continue
if start!=end:
ret.append("%d-%d" % (start,end))
else:
ret.append("%d" % start)
start=i
end=i
if start!=end:
ret.append("%d-%d" % (start,end))
else:
ret.append("%d" % start)
return "[%s]" % (",".join(ret))
key = 'a7f6063296c0f1c9b75c7f511861b89b'
def decode(frommp3name, towavname):
if frommp3name.lower().endswith(".mp3"):
os.spawnlp(os.P_WAIT,"mpg123","mpg123","--quiet","--wav",
towavname,frommp3name)
elif frommp3name.lower().endswith(".flac"):
os.spawnlp(os.P_WAIT,"flac","flac","-d", "--totally-silent", "-o", towavname,
frommp3name)
fileinfocache=shelve.open(os.path.expanduser("~/.albumidentifycachedb"),"c")
class FingerprintFailed(Exception):
def __str__(self):
return "Failed to fingerprint track"
def get_file_info(fname):
print "identifying",fname
#sys.stdout.write("identifying "+os.path.basename(fname)+"\r\x1B[K")
#sys.stdout.flush()
fhash = md5.md5(open(fname,"r").read()).hexdigest()
if fhash in fileinfocache:
return fileinfocache[fhash]
# While testing this uses a fixed name in /tmp
# and checks if it exists, and doesn't decode if it does.
# This is for speed while debugging, should be changed with
# tmpname later
toname=os.path.join("/tmp/fingerprint.wav")
if not os.path.exists(toname):
sys.stdout.write("decoding"+os.path.basename(fname)+"\r\x1B[K")
sys.stdout.flush()
decode(fname,toname)
sys.stdout.write("Generating fingerprint\r")
sys.stdout.flush()
(fp, dur) = fingerprint.fingerprint(toname)
os.unlink(toname)
sys.stdout.write("Fetching fingerprint info\r")
sys.stdout.flush()
(trackname, artist, puid) = musicdns.lookup_fingerprint(fp, dur, key)
print "***",`artist`,`trackname`,puid
if puid is None:
raise FingerprintFailed()
sys.stdout.write("Looking up PUID\r")
sys.stdout.flush()
tracks = lookups.get_tracks_by_puid(puid)
data=(fname,artist,trackname,dur,tracks,puid)
if tracks!=[]:
fileinfocache[fhash]=data
else:
print "Musicbrainz doesn't know about this track, not caching"
return data
def get_dir_info(dirname):
files=os.listdir(dirname)
files.sort()
tracknum=0
trackinfo={}
for i in files:
if not (i.lower().endswith(".mp3") or i.lower().endswith(".flac")):
print "Skipping non mp3/flac file",`i`
continue
tracknum=tracknum+1
fname=os.path.join(dirname,i)
trackinfo[tracknum]=get_file_info(fname)
return trackinfo
def find_more_tracks(tracks):
# There is a n:n mapping of puid's to tracks.
# All puid's that match a track should be the same song.
# Thus if PUIDa maps to TrackA, which also has PUIDb
# and PUIDb maps to TrackB too, then PUIDa should map to
# TrackB too...
tracks=tracks[:]
donetracks=[]
donepuids=[]
donetrackids=[]
paths={}
while tracks!=[]:
t=tracks.pop()
donetracks.append(t)
yield t
newt = lookups.get_track_by_id(t.id)
for p in newt.puids:
if p in donepuids:
continue
donepuids.append(p)
ts = lookups.get_tracks_by_puid(p)
for u in ts:
if u.id in donetrackids:
continue
tracks.append(u)
donetrackids.append(u.id)
#print u.id,[y.title for y in u.releases]
if t.id in paths:
path=paths[t.id]
else:
path=[]
path=path+[u.releases[0].title]
#print path
paths[u.id]=path
def find_even_more_tracks(fname,tracknum,possible_releases):
gotone = False
if fname.lower().endswith(".flac"):
return
mp3data = parsemp3.parsemp3(fname)
if "TIT2" not in mp3data["v2"]:
return
ftrackname = mp3data["v2"]["TIT2"]
for (rid,v) in possible_releases.items():
release = lookups.get_release_by_releaseid(rid)
rtrackname = release.tracks[tracknum-1].title
if rtrackname.lower() == ftrackname.lower():
yield release.tracks[tracknum-1]
def guess_album2(trackinfo):
# trackinfo is
# <tracknum> => (fname,artist,trackname,dur,[mbtrackids])
#
# returns a list of possible release id's
#
# This version works by trying a breadth first search of releases to try
# and avoid wasting a lot of time finding releases which are going to
# be ignored.
#
# This function returns a list of release id's
possible_releases={}
failed_releases={}
impossible_releases=[]
track_generator={}
completed_releases=[]
for (tracknum,(fname,artist,trackname,dur,trackids,puid)) in trackinfo.iteritems():
track_generator[tracknum]=itertools.chain(
(track
for track in trackids),
find_more_tracks([
track for track in trackids]),
find_even_more_tracks(fname,
tracknum,
possible_releases)
)
track_counts={}
track_prob={}
for i in range(len(trackinfo)):
track_counts[i+1]=0
track_prob[i+1]=0
old_possible_releases=None
total=0
if track_generator=={}:
print "No tracks to identify?"
return
while track_generator!={}:
if 1 or old_possible_releases!=possible_releases:
total=0
track_prob={}
for i in range(len(trackinfo)):
if (i+1) not in track_generator:
continue
track_prob[i+1]=1
total=total+1
for j in possible_releases:
if (i+1) not in possible_releases[j]:
track_prob[i+1]+=len(possible_releases[j])
total+=len(possible_releases[j])
old_possible_releases=possible_releases.copy()
r=random.random()*total
tot=0
for tracknum in track_prob:
if tracknum not in track_generator:
continue
tot+=track_prob[tracknum]
if tot>=r:
if tot>=r:
break
try:
track = track_generator[tracknum].next()
except StopIteration, si:
# If there are no more tracks for this
# skip it and try more.
del track_generator[tracknum]
print
print "All possibilities for track",tracknum,"exhausted"
print "puid:",trackinfo[tracknum][5]#[0].puids
removed_releases={}
for i in possible_releases.keys():
# Ignore any release that doesn't have this
# track, since we can no longer find it.
if tracknum not in possible_releases[i]:
removed_releases[i]=possible_releases[i]
del possible_releases[i]
if possible_releases=={}:
print "No possible releases left"
if removed_releases:
print "Possible releases:"
for releaseid in removed_releases:
release = lookups.get_release_by_releaseid(releaseid)
print release.artist.name,"-",release.title
print "",release.tracks[tracknum-1].id
print "",output_list(removed_releases[releaseid])
return
#return
continue
for releaseid in (x.id for x in track.releases):
#releaseid = track.releases[0].id
if releaseid in impossible_releases:
continue
release = lookups.get_release_by_releaseid(releaseid)
if len(release.tracks) != len(trackinfo):
# Ignore release -- wrong number of tracks
sys.stdout.write(release.title.encode("ascii","ignore")[:40]+": wrong number of tracks (%d not %d)\x1B[K\r" % (len(release.tracks),len(trackinfo)))
sys.stdout.flush()
impossible_releases.append(releaseid)
continue
found_tracknumber=lookups.track_number(release.tracks, track)
if found_tracknumber != tracknum:
#impossible_releases.append(releaseid)
# Ignore release -- Wrong track position
if releaseid in failed_releases:
failed_releases[releaseid]= \
failed_releases[releaseid]+1
else:
failed_releases[releaseid]=1
if releaseid in possible_releases:
#print "No longer considering",release.title,": doesn't have track at the right position (",tracknum,"expected at",found_tracknumber,")"
#for i in possible_releases[releaseid]:
# track_counts[i]=track_counts[i]-1
#del possible_releases[releaseid]
pass
else:
sys.stdout.write(release.title.encode("ascii","ignore")[:40]+" (track at wrong position)\x1b[K\r")
sys.stdout.flush()
continue
if releaseid in possible_releases:
if tracknum not in possible_releases[releaseid]:
possible_releases[releaseid].append(tracknum)
track_counts[tracknum]=track_counts[tracknum]+1
print "Found track",tracknum,"on",release.title,"(tracks found: %s)\x1b[K" % (output_list(possible_releases[releaseid]))
else:
possible_releases[releaseid]=[tracknum]
track_counts[tracknum]=track_counts[tracknum]+1
print "Considering",release.title,"\x1b[K"
if possible_releases!={}:
print "Currently Considering:"
for i in possible_releases:
print "",lookups.get_release_by_releaseid(i).title,"(tracks found: %s)" % (output_list(possible_releases[i])),failed_releases.get(i,0)
print
if len(possible_releases[releaseid])==len(trackinfo) and releaseid not in completed_releases:
print release.title,"seems ok\x1b[K"
yield releaseid
completed_releases.append(releaseid)
def guess_album(trackinfo):
releasedata={}
for rid in guess_album2(trackinfo):
release = lookups.get_release_by_releaseid(rid)
albumartist=release.artist
if musicbrainz2.model.Release.TYPE_SOUNDTRACK in release.types:
directoryname = "Soundtrack"
else:
directoryname = albumartist.name
#print albumartist.name,":",release.title+" ("+rid+".html)"
releaseevents=release.getReleaseEvents()
#print "Release dates:"
#for ev in releaseevents:
# print " ",ev.date
#print "Track:"
tracks=release.getTracks()
trackdata=[]
for tracknum in range(len(tracks)):
trk=tracks[tracknum]
(fname,artist,trackname,dur,trackprints,puid) = trackinfo[tracknum+1]
if trk.artist is None:
artist=albumartist.name
sortartist=albumartist.sortName
artistid=albumartist.id
else:
artist=trk.artist.name
sortartist=trk.artist.sortName
artistid=trk.artist.id
#print " ",tracknum+1,"-",artist,"-",trk.title,"%2d:%06.3f" % (int(dur/60000),(dur%6000)/1000),`fname`
trackdata.append((tracknum+1,artist,sortartist,trk.title,dur,fname,artistid,trk.id))
asin = lookups.get_asin_from_release(release)
albuminfo = (
directoryname,
release.title,
rid+".html",
[x.date for x in releaseevents],
asin,
trackdata,
albumartist,
release.id,
)
yield albuminfo
if __name__=="__main__":
trackinfo=get_dir_info(sys.argv[1])
for (albumartist,release,rid,releases,asin,trackdata,albumartistid,releaseid) in guess_album(trackinfo):
print albumartist,"-",release
print "ASIN:",asin
print "Release Dates:",
for i in releases:
print "",i
for (tracknum,artist,sortartist,title,dur,fname,artistid,trkid) in trackdata:
print "",tracknum,"-",artist,"-",title,"%2d:%06.3f" % (int(dur/60000),(dur % 60000)/1000)
print " ",fname