aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/music/play_compressed.py
diff options
context:
space:
mode:
authorSamlonDev <stankiewiczgracjan2019@gmail.com>2025-05-20 05:45:23 +0200
committerSamlonDev <stankiewiczgracjan2019@gmail.com>2025-05-20 05:45:23 +0200
commitfcc3635b9f24051ae07ed597632597e20372c889 (patch)
tree20a29a1a784f218b3623ff03ceb8084839ff606a /cogs/music/play_compressed.py
downloadsenator-sperma-fcc3635b9f24051ae07ed597632597e20372c889.tar.gz
senator-sperma-fcc3635b9f24051ae07ed597632597e20372c889.zip
initial push
Diffstat (limited to 'cogs/music/play_compressed.py')
-rw-r--r--cogs/music/play_compressed.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/cogs/music/play_compressed.py b/cogs/music/play_compressed.py
new file mode 100644
index 0000000..3297110
--- /dev/null
+++ b/cogs/music/play_compressed.py
@@ -0,0 +1,88 @@
1import discord
2from discord.ext import commands
3import asyncio
4import os
5import subprocess
6from youtubesearchpython import VideosSearch
7
8class GrajcujZkompresowane(commands.Cog):
9 def __init__(self, bot):
10 self.bot = bot
11
12 def prepend_title_to_file(self, file_path, title):
13 try:
14 with open(file_path, 'rb') as original:
15 data = original.read()
16 with open(file_path, 'wb') as modified:
17 modified.write(f"{title}\n".encode('utf-8'))
18 modified.write(data)
19 except Exception as e:
20 print(f"Failed to prepend title: {e}")
21
22 def compress_audio(self, input_path):
23 base, ext = os.path.splitext(input_path)
24 output_path = f"{base}_compressed{ext}"
25 ffmpeg_command = f"ffmpeg -i {input_path} -ar 8000 -ac 1 -b:a 500 -acodec libmp3lame -q:a 9 {output_path}"
26 subprocess.run(ffmpeg_command, check=True, shell=True)
27 return output_path
28
29 @commands.slash_command(name="grajcuj_zkompresowane", description="grajcuj zkompresowane melodyje")
30 async def grajcuj_zkompresowane(self, ctx, *, search: str):
31 await ctx.defer()
32 if not (ctx.author.voice and ctx.author.voice.channel):
33 await ctx.followup.send("nie jesteś w kanale głosowym")
34 return
35 channel = ctx.author.voice.channel
36 if ctx.voice_client is not None:
37 vc = ctx.voice_client
38 await vc.move_to(channel)
39 else:
40 vc = await channel.connect()
41 bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
42 music_dir = os.path.join(bot_dir, "temp", "music")
43 if not os.path.exists(music_dir):
44 os.makedirs(music_dir)
45 videos_search = VideosSearch(search, limit=1)
46 result = videos_search.result()
47 if not result['result']:
48 await ctx.followup.send("Could not find a video for your search.", ephemeral=True)
49 return
50 video = result['result'][0]
51 video_id = video['id']
52 title = video['title']
53 url = video['link']
54 filename = f"{video_id}.mp3"
55 file_path = os.path.join(music_dir, filename)
56 compressed_file_path = os.path.join(music_dir, f"{video_id}_compressed.mp3")
57 if os.path.exists(compressed_file_path):
58 found_file = compressed_file_path
59 found_title = title
60 else:
61 if not os.path.exists(file_path):
62 cmd = [
63 "yt-dlp",
64 "-x",
65 "--audio-format", "mp3",
66 "-o", file_path,
67 url
68 ]
69 process = await asyncio.create_subprocess_exec(*cmd)
70 await process.communicate()
71 self.prepend_title_to_file(file_path, title)
72 if not os.path.exists(compressed_file_path):
73 try:
74 compressed_file_path = self.compress_audio(file_path)
75 except subprocess.CalledProcessError as e:
76 await ctx.followup.send(f"skompresowanie nie powiodło się: {str(e)}", ephemeral=True)
77 return
78 found_file = compressed_file_path
79 found_title = title
80 title = found_title
81 if not vc.is_playing():
82 vc.play(discord.FFmpegPCMAudio(found_file))
83 await ctx.followup.send(f"gra (zkompresowane): {title}")
84 else:
85 await ctx.followup.send(f"dodano do koleyjki (zkompresowane): {title}")
86
87def setup(bot):
88 bot.add_cog(GrajcujZkompresowane(bot)) \ No newline at end of file