From d135265798da8c2698f363237f4cb74b682f3f9e Mon Sep 17 00:00:00 2001 From: salmonik Date: Fri, 17 Apr 2026 15:36:12 +0200 Subject: make voice great again Signed-off-by: salmonik --- cogs/music/media_processing.py | 67 +++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 27 deletions(-) (limited to 'cogs/music/media_processing.py') diff --git a/cogs/music/media_processing.py b/cogs/music/media_processing.py index 430b51f..e04911c 100644 --- a/cogs/music/media_processing.py +++ b/cogs/music/media_processing.py @@ -3,26 +3,26 @@ # Copyright (C) 2026 Salmonik @ salmonik@tuta.io # https://codeberg.org/salmonik/senator-sperma -import os import asyncio import logging -from pathlib import Path +import os import yt_dlp from youtubesearchpython import VideosSearch logger = logging.getLogger(__name__) + class MediaDownloader: """Handles downloading media from various sources.""" - + @staticmethod async def download(ctx, url): """Download audio from YouTube URL.""" # Create temp directory if it doesn't exist - temp_dir = os.path.join('cogs', 'temp', 'music') + temp_dir = os.path.join("cogs", "temp", "music") os.makedirs(temp_dir, exist_ok=True) - + ytdl_opts = { "outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"), "postprocessors": [ @@ -62,9 +62,10 @@ class MediaDownloader: await ctx.respond("nic ni ma", ephemeral=True) return None + class AudioCompressor: """Handles audio compression using FFmpeg.""" - + @staticmethod async def compress(ctx, filename): """Compress audio file to opus format.""" @@ -73,51 +74,63 @@ class AudioCompressor: return None # Create temp directory if it doesn't exist - temp_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'temp', 'compressed') + temp_dir = os.path.join( + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), + "temp", + "compressed", + ) os.makedirs(temp_dir, exist_ok=True) - + # Create a temporary output filename in the temp directory base_name = os.path.basename(filename) - output_filename = os.path.join(temp_dir, f"{os.path.splitext(base_name)[0]}_compressed.opus") - + output_filename = os.path.join( + temp_dir, f"{os.path.splitext(base_name)[0]}_compressed.opus" + ) + # Ensure the output directory exists - os.makedirs(os.path.dirname(output_filename) or '.', exist_ok=True) - + os.makedirs(os.path.dirname(output_filename) or ".", exist_ok=True) + # Use proper quoting for filenames with spaces ffmpeg_command = [ - 'ffmpeg', - '-y', # Overwrite output file if it exists - '-i', filename, - '-ac', '1', # Mono audio - '-b:a', '500', # 500k bitrate - '-acodec', 'libopus', - output_filename + "ffmpeg", + "-y", # Overwrite output file if it exists + "-i", + filename, + "-ac", + "1", # Mono audio + "-b:a", + "500", # 500k bitrate + "-acodec", + "libopus", + output_filename, ] - + try: logger.info(f"Running FFmpeg command: {' '.join(ffmpeg_command)}") process = await asyncio.create_subprocess_exec( *ffmpeg_command, stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE + stderr=asyncio.subprocess.PIPE, ) - + _, stderr = await process.communicate() - + if process.returncode != 0: error_msg = stderr.decode() logger.error(f"FFmpeg error ({process.returncode}): {error_msg}") await ctx.respond("eror kompresujync pilk", ephemeral=True) return None - + if not os.path.exists(output_filename): logger.error(f"Output file was not created: {output_filename}") - await ctx.respond("eror kompresujync pilk - output file not created", ephemeral=True) + await ctx.respond( + "eror kompresujync pilk - output file not created", ephemeral=True + ) return None - + logger.info(f"Successfully compressed file: {output_filename}") return output_filename - + except Exception as e: logger.error(f"Error compressing file: {str(e)}", exc_info=True) await ctx.respond(f"eror kompresujync pilk: {str(e)}", ephemeral=True) -- cgit v1.2.3