From 1ded70780ef37437daad93554364a8aecd2acf6f Mon Sep 17 00:00:00 2001 From: jasonkhew96 <29531167+JasonKhew96@users.noreply.github.com> Date: Tue, 4 May 2021 20:42:13 +0800 Subject: [PATCH] =?UTF-8?q?Use=20FastTelethon=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 +++ ytdl.py | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5fb21c9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "FastTelethon"] + path = FastTelethon + url = https://gist.github.com/painor/7e74de80ae0c819d3e9abcf9989a8dd6 diff --git a/ytdl.py b/ytdl.py index bc79340..e896aad 100644 --- a/ytdl.py +++ b/ytdl.py @@ -18,7 +18,13 @@ import functools import fakeredis import youtube_dl +from FastTelethon.FastTelethon import upload_file +from hachoir.metadata import extractMetadata +from hachoir.parser import createParser from telethon import TelegramClient, events +from telethon.tl.types import DocumentAttributeFilename, DocumentAttributeVideo +from telethon.utils import get_input_media +from hachoir.metadata.video import MkvMetadata from tgbot_ping import get_runtime logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s') @@ -150,8 +156,27 @@ async def echo_all(event): async with bot.action(chat_id, 'document'): video_path = result["filepath"] await bot.edit_message(chat_id, message, 'Download complete. Sending now...') - await bot.send_file(chat_id, video_path, - progress_callback=lambda x, y: upload_callback(x, y, chat_id, message)) + d, w, h, mime_type = get_metadata(video_path) + with open(video_path, 'rb') as f: + input_file = await upload_file( + bot, + f, + progress_callback=lambda x, y: upload_callback( + x, y, chat_id, message)) + input_media = get_input_media(input_file) + input_media.attributes = [ + DocumentAttributeVideo( + duration=d, + w=w, + h=h, + round_message=False, + supports_streaming=True + ), + DocumentAttributeFilename( + os.path.basename(video_path)), + ] + input_media.mime_type = mime_type + await bot.send_file(chat_id, input_media) await bot.edit_message(chat_id, message, 'Download success!✅') else: async with bot.action(chat_id, 'typing'): @@ -162,6 +187,24 @@ async def echo_all(event): temp_dir.cleanup() +def get_metadata(video_path): + try: + metadata = extractMetadata(createParser(video_path)) + if isinstance(metadata, MkvMetadata): + return (metadata.get('duration').seconds, + metadata['video[1]'].get('width'), + metadata['video[1]'].get('height'), + metadata.get('mime_type')) + else: + return (metadata.get('duration').seconds, + metadata.get('width'), + metadata.get('height'), + metadata.get('mime_type')) + except Exception as e: + logging.error(e) + return (0, 0, 0, 'application/octet-stream') + + if __name__ == '__main__': bot.start() bot.run_until_disconnected()