-
Notifications
You must be signed in to change notification settings - Fork 4
/
file2gif.py
32 lines (29 loc) · 1.1 KB
/
file2gif.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
import inotify.adapters
import os
import telegram
import ffmpy
TOKEN = os.getenv('BOT_TOKEN')
FOLDER = os.getenv('FOLDER', '/tmp/')
EXTENSION = os.getenv('EXTENSION', 'mp4')
DESTINATION = os.getenv('DESTINATION')
bot = telegram.Bot(TOKEN)
notifier = inotify.adapters.InotifyTree(FOLDER)
for event in notifier.event_gen():
if event is not None:
if 'IN_CLOSE_WRITE' in event[1] and EXTENSION in event[3]:
file_path = event[2] + '/' + event[3]
try:
gif_path = '/tmp/file2gif.gif'
ff = ffmpy.FFmpeg(
inputs={file_path: None},
outputs={gif_path: None}
)
ff.run()
file_open = open(gif_path, 'rb')
bot.send_chat_action(DESTINATION, 'upload_video')
bot.send_animation(DESTINATION, file_open, timeout=600)
except:
file_open = open(file_path, 'rb')
bot.send_chat_action(DESTINATION, 'upload_document')
bot.send_document(DESTINATION, file_open, timeout=600)
os.remove(gif_path)