-
Notifications
You must be signed in to change notification settings - Fork 0
/
recieveAsVideoTGbot.py
62 lines (55 loc) · 2.45 KB
/
recieveAsVideoTGbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from telegram import Update, Bot
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import os
import subprocess
TOKEN = '5261340309:AAHnAdbOBJ7V4gUhk-HcU6zbtDrIXP_RiCM'
def start(update: Update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text='Привет! Я бот, который может отправлять видео.')
def encode_video(update: Update, context):
video_file = context.bot.getFile(update.message.video.file_id)
file_name = os.path.basename(video_file.file_path)
temp_file_path = f'temp/{file_name}'
video_file.download(temp_file_path)
out_file_path = f'temp/{os.path.splitext(file_name)[0]}.mp4'
cmd = f'ffmpeg -i "{temp_file_path}" -c:v libx264 -c:a copy "{out_file_path}"'
process = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True
)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
percent = get_progress(output)
context.bot.send_message(chat_id=update.effective_chat.id, text=f'Прогресс: {percent}%')
context.bot.send_video(chat_id=update.effective_chat.id, video=open(out_file_path, 'rb'))
def get_progress(output):
"""
Получает процент выполнения команды FFmpeg из строки вывода.
:param output: строка вывода из процесса FFmpeg.
:return: процент выполнения команды.
"""
if "Duration" in output:
duration = output.split("Duration: ")[1].split(",")[0]
h, m, s = duration.split(":")
total_seconds = int(h) * 3600 + int(m) * 60 + float(s)
if "time=" in output:
time = output.split("time=")[1].split(" ")[0]
h, m, s = time.split(":")
current_seconds = int(h) * 3600 + int(m) * 60 + float(s)
percent = round(current_seconds / total_seconds * 100)
return percent
return None
def main():
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.video & (Filters.mime_type("video/avi") | Filters.mime_type("video/x-matroska")), encode_video))
dispatcher.add_handler(CommandHandler('start', start))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()