Skip to content

Commit

Permalink
Merge pull request #1 from krystlepalace/audio-convertation
Browse files Browse the repository at this point in the history
Audio convertation
  • Loading branch information
krystlepalace authored Sep 9, 2023
2 parents 9e843ba + c4e3e30 commit f7eeaa7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ __pycache__/
# idea
.idea/

# media files
media/

# ffmpeg
utils/ffmpeg.exe

# Distribution / packaging
.Python
build/
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions handlers/callbacks/converter_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion handlers/media_handler.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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())
15 changes: 15 additions & 0 deletions utils/audio_converter.py
Original file line number Diff line number Diff line change
@@ -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])
2 changes: 1 addition & 1 deletion utils/photo_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit f7eeaa7

Please sign in to comment.