diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/compression.py | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/cogs/compression.py b/cogs/compression.py index dfd96ae..ddfa545 100644 --- a/cogs/compression.py +++ b/cogs/compression.py | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | import discord | ||
| 2 | from discord.ext import commands | ||
| 3 | import asyncio | ||
| 4 | import mimetypes | 1 | import mimetypes |
| 5 | import os | 2 | import os |
| 6 | import subprocess | 3 | import subprocess |
| 7 | import tempfile | 4 | |
| 5 | import discord | ||
| 6 | from discord.ext import commands | ||
| 7 | |||
| 8 | 8 | ||
| 9 | def filetype(filename): | 9 | def filetype(filename): |
| 10 | mime_type, _ = mimetypes.guess_type(filename) | 10 | mime_type, _ = mimetypes.guess_type(filename) |
| @@ -17,10 +17,11 @@ def filetype(filename): | |||
| 17 | return "Image" | 17 | return "Image" |
| 18 | return "Unknown" | 18 | return "Unknown" |
| 19 | 19 | ||
| 20 | |||
| 20 | class Compression(commands.Cog): | 21 | class Compression(commands.Cog): |
| 21 | def __init__(self, bot): | 22 | def __init__(self, bot): |
| 22 | self.bot = bot | 23 | self.bot = bot |
| 23 | 24 | ||
| 24 | @commands.slash_command( | 25 | @commands.slash_command( |
| 25 | name="kompresja", | 26 | name="kompresja", |
| 26 | description="skompresuj wideło albo dźwienk :)", | 27 | description="skompresuj wideło albo dźwienk :)", |
| @@ -33,14 +34,14 @@ class Compression(commands.Cog): | |||
| 33 | ) | 34 | ) |
| 34 | async def compression(self, ctx, plik): | 35 | async def compression(self, ctx, plik): |
| 35 | await ctx.defer() | 36 | await ctx.defer() |
| 36 | # Save files to temp directory inside the bot's root directory | 37 | # Save files to the temp directory inside the bot's root directory |
| 37 | bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 38 | bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 38 | temp_dir = os.path.join(bot_dir, "temp") | 39 | temp_dir = os.path.join(bot_dir, "temp") |
| 39 | if not os.path.exists(temp_dir): | 40 | if not os.path.exists(temp_dir): |
| 40 | os.makedirs(temp_dir) | 41 | os.makedirs(temp_dir) |
| 41 | file_path = os.path.join(temp_dir, plik.filename) | 42 | file_path = os.path.join(temp_dir, plik.filename) |
| 42 | await plik.save(file_path) | 43 | await plik.save(file_path) |
| 43 | 44 | ||
| 44 | input_path = file_path | 45 | input_path = file_path |
| 45 | output_path = os.path.join(temp_dir, f"better_{plik.filename}") | 46 | output_path = os.path.join(temp_dir, f"better_{plik.filename}") |
| 46 | file_type = filetype(file_path) | 47 | file_type = filetype(file_path) |
| @@ -51,7 +52,7 @@ class Compression(commands.Cog): | |||
| 51 | ffmpeg_command = f"ffmpeg -i {input_path} -ar 8000 -ac 1 -b:a 16k -acodec libmp3lame -q:a 9 {output_path}" | 52 | ffmpeg_command = f"ffmpeg -i {input_path} -ar 8000 -ac 1 -b:a 16k -acodec libmp3lame -q:a 9 {output_path}" |
| 52 | elif file_type == "Image": | 53 | elif file_type == "Image": |
| 53 | # Use ImageMagick's magick to heavily compress image (JPEG/PNG/GIF) | 54 | # Use ImageMagick's magick to heavily compress image (JPEG/PNG/GIF) |
| 54 | # For JPEG: quality 5, strip metadata, resize to 30% of original size | 55 | # For JPEG: quality 5, strip metadata, resize to 30% of the original size |
| 55 | # For PNG: magick to JPEG, then compress | 56 | # For PNG: magick to JPEG, then compress |
| 56 | # For GIF: optimize and reduce colors | 57 | # For GIF: optimize and reduce colors |
| 57 | ext = os.path.splitext(input_path)[1].lower() | 58 | ext = os.path.splitext(input_path)[1].lower() |
| @@ -68,10 +69,10 @@ class Compression(commands.Cog): | |||
| 68 | else: | 69 | else: |
| 69 | await ctx.respond("Plik jest nieobsługiwany", ephemeral=True) | 70 | await ctx.respond("Plik jest nieobsługiwany", ephemeral=True) |
| 70 | return | 71 | return |
| 71 | 72 | ||
| 72 | try: | 73 | try: |
| 73 | subprocess.run(ffmpeg_command, check=True, shell=True) | 74 | subprocess.run(ffmpeg_command, check=True, shell=True) |
| 74 | # Upload compressed file | 75 | # Upload the compressed file |
| 75 | await ctx.respond(file=discord.File(output_path)) | 76 | await ctx.respond(file=discord.File(output_path)) |
| 76 | # Delete compressed file | 77 | # Delete compressed file |
| 77 | os.remove(input_path) | 78 | os.remove(input_path) |
| @@ -85,4 +86,4 @@ class Compression(commands.Cog): | |||
| 85 | 86 | ||
| 86 | 87 | ||
| 87 | def setup(bot): | 88 | def setup(bot): |
| 88 | bot.add_cog(Compression(bot)) \ No newline at end of file | 89 | bot.add_cog(Compression(bot)) |
