forked from yunpengn/AudioDup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
73 lines (57 loc) · 2.11 KB
/
recognize.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
import os
import uuid
from dejavu import Dejavu
from dejavu.recognize import FileRecognizer
from pydub import AudioSegment
# Database connection config.
config = {
"database": {
"host": "localhost",
"user": "dejavu",
"passwd": "dejavu",
"db": "dejavu",
},
"database_type": "mysql",
}
confidence_threshold = 100
mp3_temp_file_format = "%s.mp3"
# Creates a new instance.
djv = Dejavu(config)
# Collects a new audio from a given MP4 file and inserts fingerprints into database.
def collect_mp4(file_name):
# Extracts the audio.
video = AudioSegment.from_file(file_name, "mp4")
audio_file_name = mp3_temp_file_format % uuid.uuid4()
video.export(audio_file_name, format="mp3")
# Collects the audio.
djv.fingerprint_file(audio_file_name)
# Removes the fingerprints of a given song name from database.
def remove_mp4(file_name):
song_name = os.path.splitext(os.path.basename(file_name))[0]
pass
# Returns true if a potential duplicate is found for the sound track of the given MP4 file.
def recognize_mp4(file_name):
# Extracts audio.
video = AudioSegment.from_file(file_name, "mp4")
audio_file_name = mp3_temp_file_format % uuid.uuid4()
video.export(audio_file_name, format="mp3")
# Recognizes the audio.
result = djv.recognize(FileRecognizer, audio_file_name)
os.remove(audio_file_name)
# Returns the result.
return_val = []
if result is not None and result['confidence'] > confidence_threshold:
is_similar = 0
if result['confidence'] > confidence_threshold:
is_similar = 1
return_val.append([result['song_name'].decode('utf-8'), is_similar, result['confidence']])
return return_val
# Returns true if a potential duplicate is found for the given MP3 file.
def recognize_mp3(file_name):
song = djv.recognize(FileRecognizer, file_name)
print("Recognition result is %s" % song)
return song is None
def recognize_mp3_for_song(song_id, file_name):
song = djv.recognize_for_song(FileRecognizer, song_id, file_name)
print("Recognition result is %s" % song)
return song is None