aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/music/play_utils.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--cogs/music/play_utils.py60
1 files changed, 50 insertions, 10 deletions
diff --git a/cogs/music/play_utils.py b/cogs/music/play_utils.py
index 6fa0edd..b3d81a8 100644
--- a/cogs/music/play_utils.py
+++ b/cogs/music/play_utils.py
@@ -108,30 +108,70 @@ class PlayUtils:
108 async def compress(ctx, filename): 108 async def compress(ctx, filename):
109 if not filename: 109 if not filename:
110 await ctx.respond("dzie wideło", ephemeral=True) 110 await ctx.respond("dzie wideło", ephemeral=True)
111 return 111 return None
112 112
113 ffmpeg_command = f"ffmpeg -i {filename} -ac 1 -b:a 500 -acodec libopus {filename}" 113 # Create temp directory if it doesn't exist
114 temp_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'temp', 'compressed')
115 os.makedirs(temp_dir, exist_ok=True)
116
117 # Create a temporary output filename in the temp directory
118 base_name = os.path.basename(filename)
119 output_filename = os.path.join(temp_dir, f"{os.path.splitext(base_name)[0]}_compressed.opus")
120
121 # Ensure the output directory exists
122 os.makedirs(os.path.dirname(output_filename) or '.', exist_ok=True)
123
124 # Use proper quoting for filenames with spaces
125 ffmpeg_command = [
126 'ffmpeg',
127 '-y', # Overwrite output file if it exists
128 '-i', filename,
129 '-ac', '1', # Mono audio
130 '-b:a', '500', # 500k bitrate
131 '-acodec', 'libopus',
132 output_filename
133 ]
134
114 try: 135 try:
115 process = await asyncio.create_subprocess_shell( 136 logger.info(f"Running FFmpeg command: {' '.join(ffmpeg_command)}")
116 ffmpeg_command, 137 process = await asyncio.create_subprocess_exec(
138 *ffmpeg_command,
117 stdout=asyncio.subprocess.PIPE, 139 stdout=asyncio.subprocess.PIPE,
118 stderr=asyncio.subprocess.PIPE 140 stderr=asyncio.subprocess.PIPE
119 ) 141 )
142
120 stdout, stderr = await process.communicate() 143 stdout, stderr = await process.communicate()
144
121 if process.returncode != 0: 145 if process.returncode != 0:
122 logger.error(f"FFmpeg error: {stderr.decode()}") 146 error_msg = stderr.decode()
147 logger.error(f"FFmpeg error ({process.returncode}): {error_msg}")
123 await ctx.respond("eror kompresujync pilk", ephemeral=True) 148 await ctx.respond("eror kompresujync pilk", ephemeral=True)
124 else: 149 return None
125 logger.info(f"Compressed file: {filename}") 150
151 if not os.path.exists(output_filename):
152 logger.error(f"Output file was not created: {output_filename}")
153 await ctx.respond("eror kompresujync pilk - output file not created", ephemeral=True)
154 return None
155
156 logger.info(f"Successfully compressed file: {output_filename}")
157 return output_filename
158
126 except Exception as e: 159 except Exception as e:
127 logger.error(f"Error compressing file: {e}") 160 logger.error(f"Error compressing file: {str(e)}", exc_info=True)
128 await ctx.respond("eror kompresujync pilk", ephemeral=True) 161 await ctx.respond(f"eror kompresujync pilk: {str(e)}", ephemeral=True)
162 return None
129 163
130 @staticmethod 164 @staticmethod
131 async def play(ctx, filepath): 165 async def play(ctx, filepath, delete_after_play=False):
132 # Check if user is in a voice channel 166 # Check if user is in a voice channel
133 if not ctx.author.voice or not ctx.author.voice.channel: 167 if not ctx.author.voice or not ctx.author.voice.channel:
134 await ctx.respond("Musisz być na kanale głosowym, żebym mógł dołączyć!", ephemeral=True) 168 await ctx.respond("Musisz być na kanale głosowym, żebym mógł dołączyć!", ephemeral=True)
169 if delete_after_play and os.path.exists(filepath):
170 try:
171 os.remove(filepath)
172 logger.info(f"Cleaned up temporary file: {filepath}")
173 except Exception as e:
174 logger.error(f"Error cleaning up file {filepath}: {str(e)}")
135 return 175 return
136 176
137 logger.info(f"Attempting to play: {filepath}") 177 logger.info(f"Attempting to play: {filepath}")