Skip to content

Commit

Permalink
Merge pull request #2 from krystlepalace/video-convertation
Browse files Browse the repository at this point in the history
Video convertation
  • Loading branch information
krystlepalace authored Sep 9, 2023
2 parents f7eeaa7 + 9e6ec8c commit 3f2dc35
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 10 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ __pycache__/
# C extensions
*.so

# idea
.idea/

# media files
media/

Expand Down Expand Up @@ -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/
41 changes: 38 additions & 3 deletions handlers/callbacks/converter_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
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, audio_formats_builder
from keyboards.choose_formats import photo_formats_builder, audio_formats_builder, video_formats_builder


router = Router()
Expand All @@ -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())
16 changes: 15 additions & 1 deletion keyboards/choose_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
33 changes: 33 additions & 0 deletions utils/video_converter.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 3f2dc35

Please sign in to comment.