From 0e96ff1b7274803aa450d6a91b87a18f80fd0029 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:52:43 +0300 Subject: [PATCH 1/6] Update .gitignore --- .gitignore | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 42d67d1..014f0e2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,6 @@ __pycache__/ # C extensions *.so -# idea -.idea/ - # media files media/ @@ -158,5 +155,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ -utils/video_converter.py +.idea/ From 6096f6a6302bad18c4296fd9a024f506deefbe65 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:52:50 +0300 Subject: [PATCH 2/6] Create video_converter.py --- utils/video_converter.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 utils/video_converter.py diff --git a/utils/video_converter.py b/utils/video_converter.py new file mode 100644 index 0000000..e69de29 From cbcf8003de24df86a6079df9af13e08a04acf575 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 13:20:09 +0300 Subject: [PATCH 3/6] Video converter --- utils/video_converter.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/utils/video_converter.py b/utils/video_converter.py index e69de29..1c4fc11 100644 --- a/utils/video_converter.py +++ b/utils/video_converter.py @@ -0,0 +1,33 @@ +import subprocess +from config import CONFIG + + +class VideoConverter: + def __init__(self, path): + self.input_path = path + self.output_path = f"{path.split('.')[0]}" + ".{}" + + async def convert_video(self, output_format): + self.output_path = self.output_path.format(output_format) + if output_format == "gif": + subprocess.run( + [CONFIG.ffmpeg_path, "-i", self.input_path, + "-r", "15", + "-filter_complex", "fps=60,scale=320:-1", + self.output_path]) + elif output_format == "mp3": + subprocess.run( + [CONFIG.ffmpeg_path, "-i", self.input_path, + "-b:a", "192k", + "-vn", + self.output_path]) + elif output_format == "webm": + subprocess.run( + [CONFIG.ffmpeg_path, "-i", self.input_path, + "-vcodec", "libvpx", + "-acodec", "libvorbis", + self.output_path]) + else: + subprocess.run( + [CONFIG.ffmpeg_path, "-i", self.input_path, + self.output_path]) From 15797ef02ca038ff4b4937887daa810c69d02ade Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 13:20:15 +0300 Subject: [PATCH 4/6] Update choose_formats.py --- keyboards/choose_formats.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/keyboards/choose_formats.py b/keyboards/choose_formats.py index 9980c85..faf239c 100644 --- a/keyboards/choose_formats.py +++ b/keyboards/choose_formats.py @@ -8,8 +8,11 @@ photo_formats = ['PNG', 'JPG', 'JPEG', 'BMP', 'GIF', 'WEBP', 'SVG', 'ICO', 'TIFF'] +video_formats = ["MP4", "MP3", "GIF", + "AVI", "MOV", "WEBM", + "M4A", "MPEG", "WMV"] - +#refactor 3 builders into one def audio_formats_builder(): builder = InlineKeyboardBuilder() for format in audio_formats: @@ -30,3 +33,14 @@ def photo_formats_builder(): builder.adjust(3) return builder + + +def video_formats_builder(): + builder = InlineKeyboardBuilder() + for format in video_formats: + builder.button(text=format, callback_data=FormatCallback( + format=format.lower(), type='VIDEO' + ).pack()) + builder.adjust(3) + + return builder From 6bfddffab098f8b06313fdd85117e1fef306a525 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 13:20:18 +0300 Subject: [PATCH 5/6] Update media_handler.py --- handlers/media_handler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/handlers/media_handler.py b/handlers/media_handler.py index 42f3224..64b48f3 100644 --- a/handlers/media_handler.py +++ b/handlers/media_handler.py @@ -1,6 +1,6 @@ from aiogram import Router, F from aiogram.types import Message -from keyboards.choose_formats import photo_formats_builder, audio_formats_builder +from keyboards.choose_formats import photo_formats_builder, audio_formats_builder, video_formats_builder router = Router() @@ -16,3 +16,9 @@ async def convert_photo(message: Message): async def convert_photo(message: Message): await message.reply("Formats: ", reply_markup=audio_formats_builder().as_markup()) + + +@router.message(F.video) +async def convert_photo(message: Message): + await message.reply("Formats: ", + reply_markup=video_formats_builder().as_markup()) From 9e6ec8cd156687bb6e4a1eeacde4d1dacce3fc72 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 13:20:25 +0300 Subject: [PATCH 6/6] Update converter_callback.py --- handlers/callbacks/converter_callback.py | 41 ++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/handlers/callbacks/converter_callback.py b/handlers/callbacks/converter_callback.py index 39a891f..88c7fc9 100644 --- a/handlers/callbacks/converter_callback.py +++ b/handlers/callbacks/converter_callback.py @@ -3,12 +3,12 @@ from aiogram.filters.callback_data import CallbackData from utils.photo_converter import PhotoConverter from utils.audio_converter import AudioConverter +from utils.video_converter import VideoConverter from pathlib import Path from config import CONFIG import main import os - router = Router() @@ -27,7 +27,8 @@ async def process_image_convert(callback: CallbackQuery, file_id = callback.message.reply_to_message.photo[-1].file_id file = await main.bot.get_file(file_id) file_path = file.file_path - file_on_disk = Path(f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}") + file_on_disk = Path( + f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}") await main.bot.download_file(file_path, destination=file_on_disk) # convertation @@ -52,7 +53,8 @@ async def process_image_convert(callback: CallbackQuery, file_id = callback.message.reply_to_message.audio.file_id file = await main.bot.get_file(file_id) file_path = file.file_path - file_on_disk = Path(f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}") + file_on_disk = Path( + f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}") await main.bot.download_file(file_path, destination=file_on_disk) # convertation @@ -66,3 +68,36 @@ async def process_image_convert(callback: CallbackQuery, await callback.answer() os.remove(file_on_disk) os.remove(converter.output_path) + + +@router.callback_query( + FormatCallback.filter(F.type.in_(['VIDEO'])) +) +async def process_image_convert(callback: CallbackQuery, + callback_data: FormatCallback): + await callback.message.edit_text(text="Working...") + + file_id = callback.message.reply_to_message.video.file_id + file = await main.bot.get_file(file_id) + file_path = file.file_path + file_on_disk = Path( + f"{CONFIG.media_full_path}{file_id}.{file_path.split('.')[-1]}") + await main.bot.download_file(file_path, destination=file_on_disk) + + # convertation + converter = VideoConverter(file_on_disk.__str__()) + await converter.convert_video(callback_data.format) + + if callback_data.format == "mp3": + await main.bot.send_audio(chat_id=callback.from_user.id, + audio=FSInputFile(converter.output_path)) + elif callback_data.format == "gif": + await main.bot.send_document(chat_id=callback.from_user.id, + document=FSInputFile( + converter.output_path)) + else: + await main.bot.send_video(chat_id=callback.from_user.id, + video=FSInputFile(converter.output_path)) + + os.remove(file_on_disk) + os.remove(converter.output_path)