# meta pic: https://r2.fakecrime.bio/uploads/54b3c78d-38cb-4970-b925-18b7ec2b268d.jpg
# meta banner: https://r2.fakecrime.bio/uploads/54b3c78d-38cb-4970-b925-18b7ec2b268d.jpg
# requires: https://files.pythonhosted.org/packages/2f/66/31ecae67c373421db10f250a83d80653d6908f7d95080c46816102bd1fda/shazamio-0.8.1.tar.gz https://files.pythonhosted.org/packages/dd/4d/7ecffb341d646e016be76e36f5a42cb32f409c9ca21a57b68f067fad3fc7/python_ffmpeg-2.0.12.tar.gz
# meta developer: @SunnexGB
#current version
__version__ = (1, 0, 0)
from .. import loader, utils
import os
import asyncio
from shazamio import Shazam
@loader.tds
class Shazamio(loader.Module):
"""Music recognition module"""
strings = {
"name": "Shazamio",
"processing": "Processing 🫥",
"shazaming": "🔈| Shazaming...",
"no_reply": "🚫| Reply to a video message.",
"no_video": "🚫| Reply must be to a video message.",
"ffmpeg_error": "🚫| Failed to read audio. Make sure ffmpeg is installed.",
"not_found": "✖️| Sorry, could not recognize the song.",
"result": "🔈| Song recognized:\n\n"
"🔈Artist:{artist}\n"
"🚮Title:{title}",
"result_url": "〰️Song recognized:\n\n"
"🚮Artist:{artist}\n"
"🔈Title:{title}\n\n"
"🔗Listen on Shazam",
"shazam_history": "〰️| Your last 10 recognised songs", # i put it off for later and then forgot i wanted to implement it
"no_history": "〰️| What do you want to see here?", # i put it off for later and then forgot i wanted to implement it
}
strings_ru = {
"name": "Shazamio",
"_cls_doc": "Модуль для распознования музыки",
"processing": "Обработка 🫥",
"shazaming": "🔈| Шазамлю...",
"no_reply": "🚫| Ответьте на сообщение с видео.",
"no_video": "🚫| Ответ должен быть на видео",
"ffmpeg_error": "🚫| Неудачное чтение аудио. Убедитесь что ffmpeg установлен.Инструкция по установке",
"not_found": "✖️| Простите, песня не была найдена.",
"result": "🔈| Песня найдена:\n\n"
"🔈Исполнитель:{artist}\n"
"🚮Название:{title}",
"result_url": "〰️Песня найдена:\n\n"
"🚮Исполнитель:{artist}\n"
"🔈Название:{title}\n\n"
"🔗Слушайте на Shazam",
"shazam_history": "〰️| Твои 10 последних распознаных треков", # на потом,я забыл что я хотел это реализовать
"no_history": "〰️| Ну и что ты тут хотел увидеть?", # на потом,я забыл что я хотел это реализовать
}
def __init__(self):
self.config = loader.ModuleConfig(
"ffmpeg_path",
"ffmpeg",
"Path to ffmpeg executable",
)
@loader.command(ru_doc="Распознать музыку (Ответом на видео)")
async def shazam(self, message):
"""Recognize music (Reply in video)"""
reply = await message.get_reply_message()
if not reply:
await utils.answer(message, self.strings["no_reply"])
return
if not reply.video:
await utils.answer(message, self.strings["no_video"])
return
await utils.answer(message, self.strings["processing"])
downloaded_path = await message.client.download_media(reply.video)
video_path = os.path.abspath(downloaded_path)
base, _ = os.path.splitext(video_path)
audio_path = f"{base}.mp3"
try:
cmd = (
f'{self.config["ffmpeg_path"]} -i "{video_path}" '
f'-y -vn -ab 128k -ar 44100 -f mp3 "{audio_path}"'
)
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await proc.communicate()
if not os.path.exists(audio_path):
await utils.answer(message, self.strings["ffmpeg_error"])
return
await utils.answer(message, self.strings["shazaming"])
shazam = Shazam()
result = await shazam.recognize(audio_path)
track = result.get("track")
if track:
title = track.get("title", "Unknown Title")
artist = track.get("subtitle", "Unknown Artist")
url = track.get("url")
if url:
text = self.strings["result_url"].format(
title=title, artist=artist, url=url
)
else:
text = self.strings["result"].format(
title=title, artist=artist
)
await utils.answer(message, text)
else:
await utils.answer(message, self.strings["not_found"])
finally:
if os.path.exists(video_path):
os.remove(video_path)
if os.path.exists(audio_path):
os.remove(audio_path)