-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
45 lines (33 loc) · 1.26 KB
/
utils.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
import os
from shazamio import Shazam
import asyncio
def process_audio_async(audio_content):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(split_session_recognize(audio_content))
async def identify_song(path):
print("identificando canción")
shazam = Shazam()
out = await shazam.recognize(f"{path}")
if os.path.exists(path):
os.remove(path)
return out
async def split_session_recognize(audio):
audio_length = len(audio)
segment_duration = 30 * 1000
song_list = []
for start_time in range(0, audio_length, segment_duration):
end_time = start_time + segment_duration
if end_time > audio_length:
end_time = audio_length
segment = audio[start_time:end_time]
output_file = f"segment.mp3"
segment.export(output_file, format="mp3")
song_dict = {"title": "", "artists": ""}
song = await identify_song("segment.mp3")
song_dict["title"] = song.get("track", {}).get("title", "")
song_dict["artists"] = song.get("track", {}).get("subtitle", "")
if song_dict in song_list and song_dict != {"title": "", "artists": ""}:
continue
song_list.append(song_dict)
return song_list