From 3153713b43ba5fe281ebb672cd3b0378de580027 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:48:14 +0300 Subject: [PATCH 1/9] Update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 11f99ab..44cf594 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ __pycache__/ # idea .idea/ +# media files +media/ + # Distribution / packaging .Python build/ From c9feff7cb74cefe11e98d3ab6faeec2c849013be Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:48:32 +0300 Subject: [PATCH 2/9] Variable for ffmpeg path/command --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index e824f0c..53e88e6 100644 --- a/config.py +++ b/config.py @@ -5,7 +5,7 @@ class Settings(BaseSettings): bot_token: SecretStr - # ffmpeg_path: Optional[str] + ffmpeg_path: Optional[str] media_full_path: Optional[str] class Config: From 3273aea0f51852c406cea98d0b09dee843b39416 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:48:41 +0300 Subject: [PATCH 3/9] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3ec3f49..e6f994e 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ 1. Python 3.11 or higher ### Basic startup +If you have FFMPEG in your $PATH, then in FFMPEG_PATH put just 'ffmpeg' Just fill .env and run this commands: ```bash pip install -r requirements.txt From e7f61333fe5081e083b0b61479e6d1c94d691801 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:48:55 +0300 Subject: [PATCH 4/9] Fix --- utils/photo_converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/photo_converter.py b/utils/photo_converter.py index 135d5e5..9e80d9b 100644 --- a/utils/photo_converter.py +++ b/utils/photo_converter.py @@ -5,7 +5,7 @@ class PhotoConverter: def __init__(self, path: str): self.input_path = path self.output_path = "" - self.image = image = Image.open(path) + self.image = Image.open(path) async def convert_photo(self, output_format): self.output_path = f"{self.input_path.split('.')[0]}.{output_format}" From 210159cd92a1bd90e5ef9789ed723eeb67ece34e Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:49:24 +0300 Subject: [PATCH 5/9] Update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 44cf594..736bc71 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,9 @@ __pycache__/ # media files media/ +# ffmpeg +utils/ffmpeg.exe + # Distribution / packaging .Python build/ From 18f8720637943683abf2d87e3e091532f020f5c3 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:49:30 +0300 Subject: [PATCH 6/9] Create audio_converter.py --- utils/audio_converter.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 utils/audio_converter.py diff --git a/utils/audio_converter.py b/utils/audio_converter.py new file mode 100644 index 0000000..baf82c4 --- /dev/null +++ b/utils/audio_converter.py @@ -0,0 +1,15 @@ +import subprocess +from config import CONFIG + + +class AudioConverter: + def __init__(self, path, song_name): + self.input_path = path + self.output_path = f"{CONFIG.media_full_path}{song_name.split('.')[0]}." + "{}" + + async def convert_audio(self, output_format): + self.output_path = self.output_path.format(output_format) + subprocess.run( + [CONFIG.ffmpeg_path, "-i", self.input_path, "-vn", "-ar", "44100", + "-ac", + "2", "-b:a", "192k", self.output_path]) From 465865d987840fc31f734a3db328864811d33545 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:49:38 +0300 Subject: [PATCH 7/9] 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 e4d98d3..42f3224 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 +from keyboards.choose_formats import photo_formats_builder, audio_formats_builder router = Router() @@ -10,3 +10,9 @@ async def convert_photo(message: Message): await message.reply("Formats: ", reply_markup=photo_formats_builder().as_markup()) + + +@router.message(F.audio or F.voice) +async def convert_photo(message: Message): + await message.reply("Formats: ", + reply_markup=audio_formats_builder().as_markup()) From 8d7c3e34e8fdcfddea1c5b90d254d96bcb04eca4 Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:49:44 +0300 Subject: [PATCH 8/9] Update converter_callback.py --- handlers/callbacks/converter_callback.py | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/handlers/callbacks/converter_callback.py b/handlers/callbacks/converter_callback.py index 96864dc..39a891f 100644 --- a/handlers/callbacks/converter_callback.py +++ b/handlers/callbacks/converter_callback.py @@ -2,6 +2,7 @@ from aiogram.types import CallbackQuery, FSInputFile from aiogram.filters.callback_data import CallbackData from utils.photo_converter import PhotoConverter +from utils.audio_converter import AudioConverter from pathlib import Path from config import CONFIG import main @@ -38,3 +39,30 @@ 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_(['AUDIO'])) +) +async def process_image_convert(callback: CallbackQuery, + callback_data: FormatCallback): + await callback.message.edit_text(text="Working...") + + 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]}") + await main.bot.download_file(file_path, destination=file_on_disk) + + # convertation + converter = AudioConverter(file_on_disk.__str__(), + song_name=callback.message.reply_to_message.audio.file_name) + await converter.convert_audio(callback_data.format) + + await main.bot.send_audio(chat_id=callback.from_user.id, + audio=FSInputFile(converter.output_path)) + + await callback.answer() + os.remove(file_on_disk) + os.remove(converter.output_path) From c4e3e307ca43e07df6d94bd72e372937040f6f8d Mon Sep 17 00:00:00 2001 From: Fvbvke Date: Sat, 9 Sep 2023 11:50:32 +0300 Subject: [PATCH 9/9] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 736bc71..42d67d1 100644 --- a/.gitignore +++ b/.gitignore @@ -159,3 +159,4 @@ cython_debug/ # 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