From 4e6ced91a0275cbf12e38219285c8400807b0197 Mon Sep 17 00:00:00 2001 From: X-Gorn Date: Wed, 11 Oct 2023 02:05:55 +0900 Subject: [PATCH] feat: upload mp4 as video --- Dockerfile | 1 + bot.py | 76 ++++++++++++++++++++++++++++-------------------- requirements.txt | 1 + 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/Dockerfile b/Dockerfile index 091eec1..c8c9c91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,5 +3,6 @@ COPY . /app/ WORKDIR /app/ ENV PYTHONUNBUFFERED=1 COPY requirements.txt . +RUN apt-get install -y ffmpeg RUN pip3 install --no-cache-dir -r requirements.txt CMD python3 bot.py \ No newline at end of file diff --git a/bot.py b/bot.py index dfa681e..cb86b6d 100644 --- a/bot.py +++ b/bot.py @@ -2,8 +2,10 @@ from urllib.parse import unquote from urllib.error import HTTPError from pyrogram import Client, filters -from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery +from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery, Message from pyrogram.errors import BadRequest +from hachoir.metadata import extractMetadata +from hachoir.parser import createParser from typing import Tuple from dotenv import load_dotenv @@ -120,6 +122,45 @@ async def run_cmd(cmd: str) -> Tuple[str, str, int, int]: ) +# Send media; required ffmpeg +async def send_media(file_name: str, update: Message) -> bool: + if os.path.isfile(file_name): + files = file_name + pablo = update + if not '/' in files: + caption = files + else: + caption = files.split('/')[-1] + progress_args = ('Uploading...', pablo, time.time()) + if files.lower().endswith(('.mkv', '.mp4')): + metadata = extractMetadata(createParser(files)) + duration = 0 + if metadata is not None: + if metadata.has("duration"): + duration = metadata.get('duration').seconds + rndmtime = str(random.randint(0, duration)) + await run_cmd(f'ffmpeg -ss {rndmtime} -i "{files}" -vframes 1 thumbnail.jpg') + await update.reply_video(files, caption=caption, duration=duration, thumb='thumbnail.jpg', progress=progress_for_pyrogram, progress_args=progress_args) + os.remove('thumbnail.jpg') + elif files.lower().endswith(('.jpg', '.jpeg', '.png')): + try: + await update.reply_photo(files, caption=caption, progress=progress_for_pyrogram, progress_args=progress_args) + except Exception as e: + print(e) + await update.reply_document(files, caption=caption, progress=progress_for_pyrogram, progress_args=progress_args) + elif files.lower().endswith(('.mp3')): + try: + await update.reply_audio(files, caption=caption, progress=progress_for_pyrogram, progress_args=progress_args) + except Exception as e: + print(e) + await update.reply_document(files, caption=caption, progress=progress_for_pyrogram, progress_args=progress_args) + else: + await update.reply_document(files, caption=caption, progress=progress_for_pyrogram, progress_args=progress_args) + return True + else: + return False + + async def download_file(url, dl_path): command = [ 'yt-dlp', @@ -205,16 +246,7 @@ async def linkloader(bot, update): rm, total, up = len(dldirs), len(dldirs), 0 await pablo.edit_text(f"Total: {total}\nUploaded: {up}\nUploading: {rm}") for files in dldirs: - start_time = time.time() - await update.reply_document( - files, - progress=progress_for_pyrogram, - progress_args=( - 'Uploading...', - pablo, - start_time - ) - ) + await send_media(files, pablo) up+=1 rm-=1 try: @@ -276,16 +308,7 @@ async def loader(bot, update): rm, total, up = len(dldirs), len(dldirs), 0 await pablo.edit_text(f"Total: {total}\nUploaded: {up}\nUploading: {rm}") for files in dldirs: - start_time = time.time() - await update.reply_document( - files, - progress=progress_for_pyrogram, - progress_args=( - 'Uploading...', - pablo, - start_time - ) - ) + await send_media(files, pablo) up+=1 rm-=1 try: @@ -360,16 +383,7 @@ async def callbacks(bot: Client, updatex: CallbackQuery): rm, total, up = len(dldirs), len(dldirs), 0 await pablo.edit_text(f"Total: {total}\nUploaded: {up}\nUploading: {rm}") for files in dldirs: - start_time = time.time() - await update.reply_document( - files, - progress=progress_for_pyrogram, - progress_args=( - 'Uploading...', - pablo, - start_time - ) - ) + await send_media(files, pablo) up+=1 rm-=1 try: diff --git a/requirements.txt b/requirements.txt index 4b0b265..b9cdaf3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ Pyrogram TgCrypto pyromod yt-dlp +hachoir python-dotenv \ No newline at end of file