-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
55 lines (43 loc) · 1.52 KB
/
audio.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
from pydub import AudioSegment
import io
import requests
import os
import json
frame_duration = 10
file_path = os.getcwd()
try:
# Open the audio file using pydub
audio = AudioSegment.from_file(os.path.join(file_path, "uploaded_file.mp3"))
# Calculate the start and end times of the frame
start_time = 0
end_time = frame_duration * 1000
# Extract the frame from the audio using pydub
frame = audio[start_time:end_time]
# Export the frame as an mp3 file
frame.export("frame.mp3", format="mp3")
# Read the frame audio file as binary data
with open("frame.mp3", 'rb') as file:
audio_data = file.read()
# API endpoint for music recognition
url = "https://api.audd.io/"
# Parameters for the API request
params = {
'api_token': "<YOUR API KEY>",
'return': 'timecode',
}
# Send the API request with the frame audio data
response = requests.post(url, params=params, files={'file': io.BytesIO(audio_data)})
# Process the response
if response.status_code == 200:
result = response.json()
with open("results.json","w") as json_file:
json.dump(result,json_file)
print("success")
# Extract the recognized song details from the response
# and do further processing as needed
print(result)
else:
print('Error:', response.status_code)
print(response.text)
except Exception as e:
print('An error occurred:', str(e))