diff options
| author | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-21 12:21:53 +0200 |
|---|---|---|
| committer | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-21 12:21:53 +0200 |
| commit | 47180d6ffe94ca97576476d7b635f393de0293e0 (patch) | |
| tree | e2a835b806c5a46034464974c647391ea02d4f51 /cogs/music/play_compressed.py | |
| parent | play.py fixing (diff) | |
| download | senator-sperma-47180d6ffe94ca97576476d7b635f393de0293e0.tar.gz senator-sperma-47180d6ffe94ca97576476d7b635f393de0293e0.zip | |
play.py rework
Diffstat (limited to '')
| -rw-r--r-- | cogs/music/play_compressed.py | 101 |
1 files changed, 0 insertions, 101 deletions
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 @@ | |||
| 1 | import asyncio | ||
| 2 | import logging | ||
| 3 | import os | ||
| 4 | import subprocess | ||
| 5 | |||
| 6 | import discord | ||
| 7 | from discord.ext import commands | ||
| 8 | from youtubesearchpython import VideosSearch | ||
| 9 | |||
| 10 | logger = logging.getLogger(__name__) | ||
| 11 | |||
| 12 | |||
| 13 | class GrajcujZkompresowane(commands.Cog): | ||
| 14 | def __init__(self, bot): | ||
| 15 | self.bot = bot | ||
| 16 | |||
| 17 | @staticmethod | ||
| 18 | def prepend_title_to_file(file_path, title): | ||
| 19 | try: | ||
| 20 | with open(file_path, 'rb') as original: | ||
| 21 | data = original.read() | ||
| 22 | with open(file_path, 'wb') as modified: | ||
| 23 | modified.write(f"{title}\n".encode('utf-8')) | ||
| 24 | modified.write(data) | ||
| 25 | except Exception as e: | ||
| 26 | logger.error(f"Failed to prepend title: {e}", exc_info=True) | ||
| 27 | |||
| 28 | @staticmethod | ||
| 29 | def compress_audio(input_path): | ||
| 30 | base, ext = os.path.splitext(input_path) | ||
| 31 | output_path = f"{base}_compressed.opus" | ||
| 32 | ffmpeg_command = f"ffmpeg -i {input_path} -ac 1 -b:a 500 -acodec libopus {output_path}" | ||
| 33 | subprocess.run(ffmpeg_command, check=True, shell=True) | ||
| 34 | logger.info(f"Audio compressed to {output_path}") | ||
| 35 | return output_path | ||
| 36 | |||
| 37 | @commands.slash_command(name="grajcuj_zkompresowane", description="grajcuj zkompresowane melodyje") | ||
| 38 | async def grajcuj_zkompresowane(self, ctx, *, search: str): | ||
| 39 | await ctx.defer() | ||
| 40 | if not (ctx.author.voice and ctx.author.voice.channel): | ||
| 41 | await ctx.followup.send("nie jesteś w kanale głosowym") | ||
| 42 | logger.info("User is not in a voice channel") | ||
| 43 | return | ||
| 44 | channel = ctx.author.voice.channel | ||
| 45 | if ctx.voice_client is not None: | ||
| 46 | vc = ctx.voice_client | ||
| 47 | logger.info(f"User is already in a voice channel, moving to new {channel}") | ||
| 48 | await vc.move_to(channel) | ||
| 49 | else: | ||
| 50 | vc = await channel.connect() | ||
| 51 | bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 52 | music_dir = os.path.join(bot_dir, "temp", "music") | ||
| 53 | if not os.path.exists(music_dir): | ||
| 54 | os.makedirs(music_dir) | ||
| 55 | videos_search = VideosSearch(search, limit=1) | ||
| 56 | result = videos_search.result() | ||
| 57 | if not result['result']: | ||
| 58 | await ctx.followup.send("Could not find a video for your search.", ephemeral=True) | ||
| 59 | return | ||
| 60 | video = result['result'][0] | ||
| 61 | video_id = video['id'] | ||
| 62 | title = video['title'] | ||
| 63 | url = video['link'] | ||
| 64 | filename = f"{video_id}.mp3" | ||
| 65 | file_path = os.path.join(music_dir, filename) | ||
| 66 | compressed_file_path = os.path.join(music_dir, f"{video_id}_compressed.opus") | ||
| 67 | if os.path.exists(compressed_file_path): | ||
| 68 | found_file = compressed_file_path | ||
| 69 | found_title = title | ||
| 70 | else: | ||
| 71 | if not os.path.exists(file_path): | ||
| 72 | cmd = [ | ||
| 73 | "yt-dlp", | ||
| 74 | "-x", | ||
| 75 | "--audio-format", "mp3", | ||
| 76 | "-o", file_path, | ||
| 77 | url | ||
| 78 | ] | ||
| 79 | process = await asyncio.create_subprocess_exec(*cmd) | ||
| 80 | await process.communicate() | ||
| 81 | self.prepend_title_to_file(file_path, title) | ||
| 82 | if not os.path.exists(compressed_file_path): | ||
| 83 | try: | ||
| 84 | compressed_file_path = self.compress_audio(file_path) | ||
| 85 | except subprocess.CalledProcessError as e: | ||
| 86 | await ctx.followup.send(f"skompresowanie nie powiodło się: {str(e)}", ephemeral=True) | ||
| 87 | logger.error(f"Failed to compress audio: {e}", exc_info=True) | ||
| 88 | return | ||
| 89 | found_file = compressed_file_path | ||
| 90 | found_title = title | ||
| 91 | title = found_title | ||
| 92 | |||
| 93 | if vc.is_playing() or vc.is_paused(): | ||
| 94 | vc.stop() | ||
| 95 | vc.play(discord.FFmpegPCMAudio(found_file)) | ||
| 96 | await ctx.followup.send(f"gra (zkompresowane): {title}") | ||
| 97 | logger.info(f"Playing audio: {title}") | ||
| 98 | |||
| 99 | |||
| 100 | def setup(bot): | ||
| 101 | bot.add_cog(GrajcujZkompresowane(bot)) | ||
