From 47180d6ffe94ca97576476d7b635f393de0293e0 Mon Sep 17 00:00:00 2001 From: Samlon Date: Thu, 21 Aug 2025 12:21:53 +0200 Subject: play.py rework --- cogs/music/play_compressed.py | 101 ------------------------------------------ 1 file changed, 101 deletions(-) delete mode 100644 cogs/music/play_compressed.py (limited to 'cogs/music/play_compressed.py') diff --git a/cogs/music/play_compressed.py b/cogs/music/play_compressed.py deleted file mode 100644 index 2bae269..0000000 --- a/cogs/music/play_compressed.py +++ /dev/null @@ -1,101 +0,0 @@ -import asyncio -import logging -import os -import subprocess - -import discord -from discord.ext import commands -from youtubesearchpython import VideosSearch - -logger = logging.getLogger(__name__) - - -class GrajcujZkompresowane(commands.Cog): - def __init__(self, bot): - self.bot = bot - - @staticmethod - def prepend_title_to_file(file_path, title): - try: - with open(file_path, 'rb') as original: - data = original.read() - with open(file_path, 'wb') as modified: - modified.write(f"{title}\n".encode('utf-8')) - modified.write(data) - except Exception as e: - logger.error(f"Failed to prepend title: {e}", exc_info=True) - - @staticmethod - def compress_audio(input_path): - base, ext = os.path.splitext(input_path) - output_path = f"{base}_compressed.opus" - ffmpeg_command = f"ffmpeg -i {input_path} -ac 1 -b:a 500 -acodec libopus {output_path}" - subprocess.run(ffmpeg_command, check=True, shell=True) - logger.info(f"Audio compressed to {output_path}") - return output_path - - @commands.slash_command(name="grajcuj_zkompresowane", description="grajcuj zkompresowane melodyje") - async def grajcuj_zkompresowane(self, ctx, *, search: str): - await ctx.defer() - if not (ctx.author.voice and ctx.author.voice.channel): - await ctx.followup.send("nie jesteś w kanale głosowym") - logger.info("User is not in a voice channel") - return - channel = ctx.author.voice.channel - if ctx.voice_client is not None: - vc = ctx.voice_client - logger.info(f"User is already in a voice channel, moving to new {channel}") - await vc.move_to(channel) - else: - vc = await channel.connect() - bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - music_dir = os.path.join(bot_dir, "temp", "music") - if not os.path.exists(music_dir): - os.makedirs(music_dir) - videos_search = VideosSearch(search, limit=1) - result = videos_search.result() - if not result['result']: - await ctx.followup.send("Could not find a video for your search.", ephemeral=True) - return - video = result['result'][0] - video_id = video['id'] - title = video['title'] - url = video['link'] - filename = f"{video_id}.mp3" - file_path = os.path.join(music_dir, filename) - compressed_file_path = os.path.join(music_dir, f"{video_id}_compressed.opus") - if os.path.exists(compressed_file_path): - found_file = compressed_file_path - found_title = title - else: - if not os.path.exists(file_path): - cmd = [ - "yt-dlp", - "-x", - "--audio-format", "mp3", - "-o", file_path, - url - ] - process = await asyncio.create_subprocess_exec(*cmd) - await process.communicate() - self.prepend_title_to_file(file_path, title) - if not os.path.exists(compressed_file_path): - try: - compressed_file_path = self.compress_audio(file_path) - except subprocess.CalledProcessError as e: - await ctx.followup.send(f"skompresowanie nie powiodło się: {str(e)}", ephemeral=True) - logger.error(f"Failed to compress audio: {e}", exc_info=True) - return - found_file = compressed_file_path - found_title = title - title = found_title - - if vc.is_playing() or vc.is_paused(): - vc.stop() - vc.play(discord.FFmpegPCMAudio(found_file)) - await ctx.followup.send(f"gra (zkompresowane): {title}") - logger.info(f"Playing audio: {title}") - - -def setup(bot): - bot.add_cog(GrajcujZkompresowane(bot)) -- cgit v1.2.3