This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollect-fingerprints-of-songs.py
80 lines (59 loc) · 2.5 KB
/
collect-fingerprints-of-songs.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
#!/usr/bin/python3
import os
import libs.fingerprint as fingerprint
from termcolor import colored
from libs.reader_file import FileReader
from libs.db_sqlite import SqliteDatabase
from libs.config import get_config
if __name__ == "__main__":
config = get_config()
db = SqliteDatabase()
path = "mp3/"
# fingerprint all files in a directory
for filename in os.listdir(path):
if filename.endswith(".mp3"):
reader = FileReader(path + filename)
audio = reader.parse_audio()
song = db.get_song_by_filehash(audio["file_hash"])
song_id = db.add_song(filename, audio["file_hash"])
msg = " * %s %s: %s" % (
colored("id=%s", "white", attrs=["dark"]), # id
colored("channels=%d", "white", attrs=["dark"]), # channels
colored("%s", "white", attrs=["bold"]), # filename
)
print(msg % (song_id, len(audio["channels"]), filename))
if song:
hash_count = db.get_song_hashes_count(song_id)
if hash_count > 0:
msg = " already exists (%d hashes), skip" % hash_count
print(colored(msg, "red"))
continue
print(colored(" new song, going to analyze..", "green"))
hashes = set()
channel_amount = len(audio["channels"])
for channeln, channel in enumerate(audio["channels"]):
msg = " fingerprinting channel %d/%d"
print(
colored(msg, attrs=["dark"])
% (channeln + 1, channel_amount)
)
channel_hashes = fingerprint.fingerprint(
channel,
Fs=audio["Fs"],
plots=config["fingerprint.show_plots"],
)
channel_hashes = set(channel_hashes)
msg = " finished channel %d/%d, got %d hashes"
print(
colored(msg, attrs=["dark"])
% (channeln + 1, channel_amount, len(channel_hashes))
)
hashes |= channel_hashes
msg = " finished fingerprinting, got %d unique hashes"
values = []
for hash, offset in hashes:
values.append((song_id, hash, offset))
msg = " storing %d hashes in db" % len(values)
print(colored(msg, "green"))
db.store_fingerprints(values)
print("end")