forked from MikeWent/webm2mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·309 lines (263 loc) · 10.5 KB
/
bot.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python3
# https://github.com/MikeWent/webm2mp4
# https://t.me/webm2mp4bot
import os
import random
import re
import string
import subprocess
import threading
import time
import requests
import telebot
from hurry.filesize import size, alternative
# SETTINGS
TEMP_FOLDER = "/tmp/"
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
"Accept-Encoding": "identity"
}
MAXIMUM_FILESIZE_ALLOWED = 50*1024*1024 # ~50 MB
ALLOWED_MIME_TYPES = ("video/webm", "application/octet-stream")
FFMPEG_THREADS = 2
# MESSAGES
error_wrong_code = "❗️ Resource returned HTTP {} code. Maybe link is broken"
error_downloading = "⚠️ Unable to download file"
error_converting = "⚠️ Sorry, <code>ffmpeg</code> seems unable to convert this file to MP4. Please, contact @Mike_Went"
error_generating_thumbnail = "⚠️ Sorry, <code>ffmpeg</code> seems unable to generate a thumbnail image for this file. Please, contact @Mike_Went"
error_wrong_url = "👀 This URL does not look like a .webm file"
error_huge_file = "🍉 File is bigger than 50 MB. Telegram <b>does not<b> allow me to upload huge files, sorry."
error_no_header = "🔬 WTF? I do not understand what server tries to give me instead of .webm file"
error_file_not_webm = "👀 This is not a .webm file. If you are sure it's an error, contact @Mike_Went"
error_converting = "⚠️ Sorry, <code>ffmpeg</code> seems unable to convert this file to MP4. Please, contact @Mike_Went"
message_start = """Hello! I am WebM to MP4 (H.264) converter bot 📺
You can send .webm files up to 20 MB via Telegram and receive converted videos up to ☁️ 50 MB back (from any source — link/document)."""
message_help = "Send me a link (http://...) to <b>webm</b> file or just .webm <b>document</b>"
message_starting = "🚀 Starting..."
message_converting = "☕️ Converting... {}"
message_generating_thumbnail = "🖼 Generating thumbnail.."
message_uploading = "☁️ Uploading to Telegram..."
def update_status_message(message, text):
try:
bot.edit_message_text(chat_id=message.chat.id,
message_id=message.message_id,
text=text, parse_mode="HTML")
except:
pass
def rm(filename):
"""Delete file (like 'rm' command)"""
try:
os.remove(filename)
except:
pass
def random_string(length=12):
"""Random string of uppercase ASCII and digits"""
return "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(length))
def download_file(request, pipe_write):
"""Pass remote file to the local pipe"""
with open(pipe_write, "wb") as f:
for chunk in request:
f.write(chunk)
def webm2mp4_worker(message, url):
"""Generic process spawned every time user sends a link or a file"""
global telegram_token
filename = "".join([TEMP_FOLDER, random_string(), ".mp4"])
# Tell user that we are working
status_message = bot.reply_to(message, message_starting, parse_mode="HTML")
# Try to download URL
try:
r = requests.get(url, stream=True, headers=HEADERS)
except:
update_status_message(status_message, error_downloading)
return
# Something went wrong on the server side
if r.status_code != 200:
update_status_message(status_message, error_wrong_code.format(r.status_code))
return
# Is it a webm file?
if r.headers["Content-Type"] not in ALLOWED_MIME_TYPES and message.document.mime_type not in ALLOWED_MIME_TYPES:
update_status_message(status_message, error_file_not_webm)
return
# Can't determine file size
if not "Content-Length" in r.headers or not "Content-Type" in r.headers:
update_status_message(status_message, error_no_header)
return
# Check file size
webm_size = int(r.headers["Content-Length"])
if webm_size >= MAXIMUM_FILESIZE_ALLOWED:
update_status_message(status_message, error_huge_file)
return
# Create a pipe to pass downloading file to ffmpeg without delays
pipe_read, pipe_write = os.pipe()
# Start ffmpeg
ffmpeg_process = subprocess.Popen(["ffmpeg",
"-v", "error",
"-threads", str(FFMPEG_THREADS),
"-i", "pipe:0", # read input from stdin
"-map", "V:0?", # select video stream
"-map", "0:a?", # ignore audio if doesn't exist
"-c:v", "libx264", # specify video encoder
"-max_muxing_queue_size", "9999", # https://trac.ffmpeg.org/ticket/6375
"-movflags", "+faststart", # optimize for streaming
"-preset", "slow", # https://trac.ffmpeg.org/wiki/Encode/H.264#a2.Chooseapresetandtune
"-timelimit", "900", # prevent DoS (exit after 15 min)
"-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2", # https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2#20848224
filename
], stdin=pipe_read)
# Download file in and pass it to ffmpeg with pipe
try:
threading.Thread(
target=download_file,
kwargs={
"request": r,
"pipe_write": pipe_write
}
).start()
# Initial delay to start downloading
time.sleep(1)
except:
update_status_message(status_message, error_downloading)
# Close pipe explicitly
os.close(pipe_read)
return
# While ffmpeg process is alive (i.e. is working)
old_progress = ""
while ffmpeg_process.poll() == None:
try:
output_file_size = os.stat(filename).st_size
except FileNotFoundError:
output_file_size = 0
mp4_size = size(output_file_size, system=alternative)
webm_size = size(int(r.headers["Content-Length"]), system=alternative)
human_readable_progress = " ".join([mp4_size, "/", webm_size])
if human_readable_progress != old_progress:
update_status_message(status_message, message_converting.format(human_readable_progress))
old_prpgress = human_readable_progress
time.sleep(3)
# Exit in case of error with ffmpeg
if ffmpeg_process.returncode != 0:
update_status_message(status_message, error_converting)
# Clean up and close pipe explicitly
rm(filename)
os.close(pipe_read)
return
# Check output file size
mp4_size = os.path.getsize(filename)
if mp4_size >= MAXIMUM_FILESIZE_ALLOWED:
update_status_message(status_message, error_huge_file)
# Clean up and close pipe explicitly
rm(filename)
os.close(pipe_read)
return
# Close pipe after using
os.close(pipe_read)
# 1. Get video duration in seconds
video_duration = subprocess.run(["ffprobe",
"-v", "error",
"-select_streams", "v:0",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
filename
], stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
video_duration = round(float(video_duration))
# 2. Get video height and width
video_props = subprocess.run(["ffprobe",
"-v", "error",
"-select_streams", "v:0",
"-show_entries","stream=width,height",
"-of", "csv=s=x:p=0",
filename
], stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
video_width, video_height = video_props.split("x")
# 3. Take one frame from the middle of the video
update_status_message(status_message, message_generating_thumbnail)
thumbnail = "".join([TEMP_FOLDER, random_string(), ".jpg"])
generate_thumbnail_process = subprocess.Popen(["ffmpeg",
"-v", "error",
"-i", filename,
"-vcodec", "mjpeg",
"-vframes", "1",
"-an", "-f", "rawvideo",
"-ss", str(int(video_duration/2)),
# keep the limit of 90px height/width (Telegram API) while preserving the aspect ratio
"-vf", "scale='if(gt(iw,ih),90,trunc(oh*a/2)*2)':'if(gt(iw,ih),trunc(ow/a/2)*2,90)'",
thumbnail
])
# While process is alive (i.e. is working)
while generate_thumbnail_process.poll() == None:
time.sleep(1)
# Exit in case of error with ffmpeg
if generate_thumbnail_process.returncode != 0:
update_status_message(status_message, error_generating_thumbnail)
# Clean up
rm(filename)
rm(thumbnail)
return
# Upload to Telegram
update_status_message(status_message, message_uploading)
mp4 = open(filename, "rb")
thumb = open(thumbnail, "rb")
requests.post(
f"https://api.telegram.org/bot{telegram_token}/sendVideo",
data={
"chat_id": message.chat.id,
"duration": video_duration,
"width": video_width,
"height": video_height,
"reply_to_message_id": message.message_id,
"supports_streaming": True
},
files=[
("video", (random_string()+".mp4", mp4, "video/mp4")),
("thumb", (random_string()+".jpg", thumb, "image/jpeg"))
]
)
bot.delete_message(message.chat.id, status_message.message_id)
# Clean up
mp4.close()
thumb.close()
rm(filename)
rm(thumbnail)
### Telegram interaction below ###
try:
with open("token.txt", "r") as f:
telegram_token = f.read().strip()
except FileNotFoundError:
print("Put your Telegram bot token to 'token.txt' file")
exit(1)
bot = telebot.TeleBot(telegram_token)
@bot.message_handler(commands=["start", "help"])
def start_help(message):
bot.send_message(message.chat.id, message_start, parse_mode="HTML")
bot.send_message(message.chat.id, message_help, parse_mode="HTML")
# Handle URLs
URL_REGEXP = r"(http.?:\/\/.*\.webm)"
@bot.message_handler(regexp=URL_REGEXP)
def handle_urls(message):
# Grab first found link
url = re.findall(URL_REGEXP, message.text)[0]
threading.Thread(
target=webm2mp4_worker,
kwargs={
"message": message,
"url": url
}
).start()
# Handle files
@bot.message_handler(content_types=["document"])
def handle_files(message):
file_id = message.document.file_id
file_info = bot.get_file(file_id)
if message.document.mime_type not in ALLOWED_MIME_TYPES:
if message.chat.type == "private":
bot.reply_to(message, error_file_not_webm, parse_mode="HTML")
return
url = "https://api.telegram.org/file/bot{0}/{1}".format(telegram_token, file_info.file_path)
threading.Thread(
target=webm2mp4_worker,
kwargs={
"message": message,
"url": url
}
).start()
bot.polling(none_stop=True)