-
Notifications
You must be signed in to change notification settings - Fork 20
/
ACRAPI.py
88 lines (59 loc) · 2.98 KB
/
ACRAPI.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
from acrcloud.recognizer import ACRCloudRecognizer
from acrcloud.recognizer import ACRCloudRecognizeType
from SongIDCore import *
# Get the ACRCloud config
with open('data/acrcloud.json', 'r') as f:
config = json.load(f)
logger.info('Loaded: ACR Config')
# Get the ACRCloud access & private keys
acrkey = all_tokens["acr"]
config_clear = config["clear"]
config_clear["access_key"] = acrkey["clear"]["access_key"]
config_clear["access_secret"] = acrkey["clear"]["access_secret"]
config_clear["recognize_type"] = ACRCloudRecognizeType.ACR_OPT_REC_AUDIO
config_clear["debug"] = False
config_noisy = config["noisy"]
config_noisy["access_key"] = acrkey["noisy"]["access_key"]
config_noisy["access_secret"] = acrkey["noisy"]["access_secret"]
config_noisy["recognize_type"] = ACRCloudRecognizeType.ACR_OPT_REC_AUDIO
config_noisy["debug"] = False
config_hum = config["hum"]
config_hum["access_key"] = acrkey["hum"]["access_key"]
config_hum["access_secret"] = acrkey["hum"]["access_secret"]
config_hum["recognize_type"] = ACRCloudRecognizeType.ACR_OPT_REC_BOTH
config_hum["debug"] = False
# Functions for sending files to the ACRCloud API and getting a response
# These functions were pre-made on the ACRCloud GitHub: https://github.com/acrcloud/acrcloud_sdk_python/blob/master/windows/win64/python3/test.py
class ACRAPI():
#def clear(filePath):
# Not currently using clear-audio detection, so the function is not necessary
def noisy(filePath):
config = config_noisy
'''This module can recognize ACRCloud by most of audio/video file.
Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
Video: mp4, mkv, wmv, flv, ts, avi ...'''
re = ACRCloudRecognizer(config)
#recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1].
#re.recognize_by_file(filePath, 0, 10)
logger.info('ACR: Processing request...')
buf = open(filePath, 'rb').read()
#recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1].
data = re.recognize_by_filebuffer(buf, 0, 60)
data = json.loads(data)
logger.info('ACR: Processing complete!')
return data
def hum(filePath):
config = config_hum
'''This module can recognize ACRCloud by most of audio/video file.
Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
Video: mp4, mkv, wmv, flv, ts, avi ...'''
re = ACRCloudRecognizer(config)
#recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1].
#re.recognize_by_file(filePath, 0, 10)
logger.info('ACR: Processing request...')
buf = open(filePath, 'rb').read()
#recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1].
data = re.recognize_by_filebuffer(buf, 0, 10)
data = json.loads(data)
logger.info('ACR: Processing complete!')
return data