diff --git a/.gitignore b/.gitignore index 11f99ab..42d67d1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,12 @@ __pycache__/ # idea .idea/ +# media files +media/ + +# ffmpeg +utils/ffmpeg.exe + # Distribution / packaging .Python build/ @@ -153,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 diff --git a/README.md b/README.md index 6fe105a..6fa6a6f 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 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: 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) 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()) 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]) 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}"