Skip to content

Commit

Permalink
feat: upload mp4 as video
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Gorn committed Oct 10, 2023
1 parent 64d88d0 commit 4e6ced9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 45 additions & 31 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Pyrogram
TgCrypto
pyromod
yt-dlp
hachoir
python-dotenv

0 comments on commit 4e6ced9

Please sign in to comment.