aboutsummaryrefslogtreecommitdiffstats
path: root/cogs
diff options
context:
space:
mode:
Diffstat (limited to 'cogs')
-rw-r--r--cogs/compression.py2
-rw-r--r--cogs/conversion.py80
2 files changed, 81 insertions, 1 deletions
diff --git a/cogs/compression.py b/cogs/compression.py
index dc8e632..2ee36bb 100644
--- a/cogs/compression.py
+++ b/cogs/compression.py
@@ -29,7 +29,7 @@ class Compression(commands.Cog):
29 description="skompresuj wideło albo dźwienk :)", 29 description="skompresuj wideło albo dźwienk :)",
30 ) 30 )
31 @discord.option( 31 @discord.option(
32 "plik", 32 name="plik",
33 description="plik do skompresowania", 33 description="plik do skompresowania",
34 required=True, 34 required=True,
35 type=discord.Attachment 35 type=discord.Attachment
diff --git a/cogs/conversion.py b/cogs/conversion.py
new file mode 100644
index 0000000..4d5d4d9
--- /dev/null
+++ b/cogs/conversion.py
@@ -0,0 +1,80 @@
1import discord
2from discord.ext import commands
3import logging
4import os
5import subprocess
6import mimetypes
7
8logger = logging.getLogger(__name__)
9
10def filetype(filename):
11 mime_type, _ = mimetypes.guess_type(filename)
12 if mime_type:
13 if mime_type.startswith('video/'):
14 return "Video"
15 elif mime_type.startswith('audio/'):
16 return "Audio"
17 elif mime_type.startswith('image/'):
18 return "Image"
19 return "Unknown"
20
21class Conversion(commands.Cog):
22 def __init__(self, bot):
23 self.bot = bot
24
25 @commands.slash_command(name="konwersja", description="konwertuj plik na inny format")
26 @discord.option(
27 "plik",
28 description="plik do konwersji",
29 required=True,
30 type=discord.Attachment
31 )
32 @discord.option(
33 "format",
34 description="format konwersji",
35 required=True,
36 type=str
37 )
38 async def convert(self, ctx, plik, output_format):
39 await ctx.defer()
40 # Save files to the temp directory inside the bot's root directory
41 bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
42 temp_dir = os.path.join(bot_dir, "temp")
43 if not os.path.exists(temp_dir):
44 os.makedirs(temp_dir)
45 file_path = os.path.join(temp_dir, plik.filename)
46 filename_noext = os.path.splitext(plik.filename)[0]
47 await plik.save(file_path)
48
49 input_path = file_path
50 output_path = os.path.join(temp_dir, f"{filename_noext}.{output_format}")
51 file_type = filetype(file_path)
52 logger.info(f"Conversion started for: {file_type}")
53 if file_type == "Video" or file_type == "Audio":
54 # Execute ffmpeg command to compress video
55 ffmpeg_command = f"ffmpeg -i {input_path} {output_path}"
56 elif file_type == "Image":
57 img_command = f"magick {input_path} {output_path}"
58 ffmpeg_command = img_command
59 else:
60 await ctx.respond("Plik jest nieobsługiwany", ephemeral=True)
61 logger.info("Unsupported file type")
62 return
63
64 try:
65 subprocess.run(ffmpeg_command, check=True, shell=True)
66 # Upload the compressed file
67 await ctx.respond(file=discord.File(output_path))
68 logger.info(f"Conversion completed for: {file_type}")
69 # Delete compressed file
70 os.remove(input_path)
71 os.remove(output_path)
72 except subprocess.CalledProcessError as e:
73 await ctx.respond(f"Conversion failed: {str(e)}", ephemeral=True)
74 if os.path.exists(input_path):
75 os.remove(input_path)
76 if os.path.exists(output_path):
77 os.remove(output_path)
78
79def setup(bot):
80 bot.add_cog(Conversion(bot)) \ No newline at end of file