diff options
| author | salmonik <raisersoft@proton.me> | 2026-07-08 02:13:35 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-07-08 02:13:35 +0200 |
| commit | cd8ed8bc5793985df2c1692072a932ceee517b95 (patch) | |
| tree | 42d18a4140259b5c68b4dd05b7fcb398f78b8d7f /cogs/music/media_processing.py | |
| parent | Merge pull request 'Voice related fixes, alongside simplifying assistant' (#5... (diff) | |
| download | senator-sperma-cd8ed8bc5793985df2c1692072a932ceee517b95.tar.gz senator-sperma-cd8ed8bc5793985df2c1692072a932ceee517b95.zip | |
OPEN THE RUST GATES
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to 'cogs/music/media_processing.py')
| -rw-r--r-- | cogs/music/media_processing.py | 135 |
1 files changed, 0 insertions, 135 deletions
diff --git a/cogs/music/media_processing.py b/cogs/music/media_processing.py deleted file mode 100644 index 91bd406..0000000 --- a/cogs/music/media_processing.py +++ /dev/null | |||
| @@ -1,135 +0,0 @@ | |||
| 1 | # This file is part of Senator Sperma | ||
| 2 | # Licensed under the GPL-3.0 License | ||
| 3 | # Copyright (C) 2026 Salmonik @ salmonik@tuta.io | ||
| 4 | # https://codeberg.org/salmonik/senator-sperma | ||
| 5 | |||
| 6 | import asyncio | ||
| 7 | import logging | ||
| 8 | import os | ||
| 9 | |||
| 10 | import yt_dlp | ||
| 11 | from youtubesearchpython import VideosSearch | ||
| 12 | from cogs.utils.url_helper import clean_url_for_ytdlp | ||
| 13 | |||
| 14 | logger = logging.getLogger(__name__) | ||
| 15 | |||
| 16 | |||
| 17 | class MediaDownloader: | ||
| 18 | """Handles downloading media from various sources.""" | ||
| 19 | |||
| 20 | @staticmethod | ||
| 21 | async def download(ctx, url): | ||
| 22 | """Download audio from YouTube URL.""" | ||
| 23 | from cogs.utils.temp import get_temp_dir | ||
| 24 | |||
| 25 | temp_dir = get_temp_dir("senator_sperma_music") | ||
| 26 | |||
| 27 | ytdl_opts = { | ||
| 28 | "outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"), | ||
| 29 | "postprocessors": [ | ||
| 30 | { | ||
| 31 | "key": "FFmpegExtractAudio", | ||
| 32 | "preferredcodec": "mp3", | ||
| 33 | "preferredquality": "192", | ||
| 34 | } | ||
| 35 | ], | ||
| 36 | "force_ipv4": True, | ||
| 37 | } | ||
| 38 | |||
| 39 | try: | ||
| 40 | url = clean_url_for_ytdlp(url) | ||
| 41 | with yt_dlp.YoutubeDL(ytdl_opts) as ydl: | ||
| 42 | info_dict = ydl.extract_info(url, download=True) | ||
| 43 | # Get the base filename without extension | ||
| 44 | base_filename = os.path.splitext(ydl.prepare_filename(info_dict))[0] | ||
| 45 | # Add .mp3 extension since we're extracting audio | ||
| 46 | filename = f"{base_filename}.mp3" | ||
| 47 | logger.info(f"Downloaded file: {filename}") | ||
| 48 | return filename | ||
| 49 | except Exception as e: | ||
| 50 | logger.error(f"Error downloading video: {e}") | ||
| 51 | await ctx.respond("eror pobirajync wideło", ephemeral=True) | ||
| 52 | |||
| 53 | @staticmethod | ||
| 54 | async def get_url(ctx, query): | ||
| 55 | """Search YouTube and get URL for a query.""" | ||
| 56 | search = VideosSearch(query, limit=1) | ||
| 57 | if search.result()["result"]: | ||
| 58 | title = search.result()["result"][0]["title"] | ||
| 59 | url = search.result()["result"][0]["link"] | ||
| 60 | logger.info(f"Found video: {title}") | ||
| 61 | return url, title | ||
| 62 | else: | ||
| 63 | logger.warning("No results found for the query.") | ||
| 64 | await ctx.respond("nic ni ma", ephemeral=True) | ||
| 65 | return None | ||
| 66 | |||
| 67 | |||
| 68 | class AudioCompressor: | ||
| 69 | """Handles audio compression using FFmpeg.""" | ||
| 70 | |||
| 71 | @staticmethod | ||
| 72 | async def compress(ctx, filename): | ||
| 73 | """Compress audio file to opus format.""" | ||
| 74 | from cogs.utils.temp import get_temp_dir | ||
| 75 | |||
| 76 | if not filename: | ||
| 77 | await ctx.respond("dzie wideło", ephemeral=True) | ||
| 78 | return None | ||
| 79 | |||
| 80 | temp_dir = get_temp_dir("senator_sperma_compressed") | ||
| 81 | |||
| 82 | # Create a temporary output filename in the temp directory | ||
| 83 | base_name = os.path.basename(filename) | ||
| 84 | output_filename = os.path.join( | ||
| 85 | temp_dir, f"{os.path.splitext(base_name)[0]}_compressed.opus" | ||
| 86 | ) | ||
| 87 | |||
| 88 | # Ensure the output directory exists | ||
| 89 | os.makedirs(os.path.dirname(output_filename) or ".", exist_ok=True) | ||
| 90 | |||
| 91 | # Use proper quoting for filenames with spaces | ||
| 92 | ffmpeg_command = [ | ||
| 93 | "ffmpeg", | ||
| 94 | "-y", # Overwrite output file if it exists | ||
| 95 | "-i", | ||
| 96 | filename, | ||
| 97 | "-ac", | ||
| 98 | "1", # Mono audio | ||
| 99 | "-b:a", | ||
| 100 | "500", # 500k bitrate | ||
| 101 | "-acodec", | ||
| 102 | "libopus", | ||
| 103 | output_filename, | ||
| 104 | ] | ||
| 105 | |||
| 106 | try: | ||
| 107 | logger.info(f"Running FFmpeg command: {' '.join(ffmpeg_command)}") | ||
| 108 | process = await asyncio.create_subprocess_exec( | ||
| 109 | *ffmpeg_command, | ||
| 110 | stdout=asyncio.subprocess.PIPE, | ||
| 111 | stderr=asyncio.subprocess.PIPE, | ||
| 112 | ) | ||
| 113 | |||
| 114 | _, stderr = await process.communicate() | ||
| 115 | |||
| 116 | if process.returncode != 0: | ||
| 117 | error_msg = stderr.decode() | ||
| 118 | logger.error(f"FFmpeg error ({process.returncode}): {error_msg}") | ||
| 119 | await ctx.respond("eror kompresujync pilk", ephemeral=True) | ||
| 120 | return None | ||
| 121 | |||
| 122 | if not os.path.exists(output_filename): | ||
| 123 | logger.error(f"Output file was not created: {output_filename}") | ||
| 124 | await ctx.respond( | ||
| 125 | "eror kompresujync pilk - output file not created", ephemeral=True | ||
| 126 | ) | ||
| 127 | return None | ||
| 128 | |||
| 129 | logger.info(f"Successfully compressed file: {output_filename}") | ||
| 130 | return output_filename | ||
| 131 | |||
| 132 | except Exception as e: | ||
| 133 | logger.error(f"Error compressing file: {str(e)}", exc_info=True) | ||
| 134 | await ctx.respond(f"eror kompresujync pilk: {str(e)}", ephemeral=True) | ||
| 135 | return None | ||
