-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fingerprint.py
93 lines (73 loc) · 3.04 KB
/
fingerprint.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
##########################################################
# Monkey patch for getting Dejabu running on Python 3.8+ #
# (platform.linux_distribution was removed in 3.8) #
import platform
try:
import distro
def linux_distribution():
return [distro.like()]
platform.linux_distribution = linux_distribution
except ImportError:
pass
##########################################################
import json
import glob
import os
import args
from dejavu import Dejavu
from dejavu.logic.recognizer.file_recognizer import FileRecognizer
# Load config file
config = json.load(open("config.json"))
openings_database_cfg = config["openings"]
endings_database_cfg = config["endings"]
# Setup Dejavu and FileRecognizer instances
openings_dejavu = Dejavu(openings_database_cfg)
openings_recognizer = FileRecognizer(openings_dejavu)
endings_dejavu = Dejavu(endings_database_cfg)
endings_recognizer = FileRecognizer(endings_dejavu)
def fingerprint_episodes(anidb_id, episodes):
if args.parsed_args.verbose:
print("[fingerprint.py] [INFO] Adding openings to fingerprint database")
openings_dejavu.fingerprint_directory("openings", [".mp3"])
if args.parsed_args.verbose:
print("[fingerprint.py] [INFO] Adding endings to fingerprint database")
endings_dejavu.fingerprint_directory("endings", [".mp3"])
# Clear the ending/opening folders after done
if args.parsed_args.verbose:
print("[fingerprint.py] [INFO] Clearing openings folder")
for f in glob.glob("./openings/*"):
os.remove(f)
if args.parsed_args.verbose:
print("[fingerprint.py] [INFO] Clearing endings folder")
for f in glob.glob("./endings/*"):
os.remove(f)
local_database_file = open("timestamps.json", "r+")
local_database = json.load(local_database_file)
if anidb_id not in local_database:
local_database[anidb_id] = []
series = local_database[anidb_id]
for episode in episodes:
if not any(e['episode_number'] == episode["episode_number"] for e in series):
# TODO: Handle if the timestamp isn't found
if args.parsed_args.verbose:
print("[fingerprint.py] [INFO] Checking episode audio for opening")
opening_results = openings_recognizer.recognize_file(episode["mp3_path"])
opening_start = int(abs(opening_results["results"][0]["offset_seconds"])) # convert to positive and round down
if args.parsed_args.verbose:
print("[fingerprint.py] [INFO] Checking episode audio for ending")
ending_results = endings_recognizer.recognize_file(episode["mp3_path"])
ending_start = int(abs(ending_results["results"][0]["offset_seconds"])) # convert to positive and round down
os.remove(episode["mp3_path"])
if args.parsed_args.verbose:
print(f"[fingerprint.py] [INFO] Opening start: {opening_start}. Ending start: {ending_start}")
series.append({
"source": "open_anime_timestamps",
"episode_number": episode["episode_number"],
"recap_start": -1,
"opening_start": opening_start,
"ending_start": ending_start,
"preview_start": -1
})
local_database_file.seek(0)
json.dump(local_database, local_database_file, indent=4)
local_database_file.close()