-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathff_mpeg.py
182 lines (170 loc) · 5.69 KB
/
ff_mpeg.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
"""FFMpeg for @UniBorg
"""
import asyncio
import io
import os
import time
from datetime import datetime
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from ULTRA.utils import admin_cmd, progress
FF_MPEG_DOWN_LOAD_MEDIA_PATH = "uniborg.media.ffmpeg"
@borg.on(admin_cmd("ffmpegsave"))
async def ff_mpeg_trim_cmd(event):
if event.fwd_from:
return
if not os.path.exists(FF_MPEG_DOWN_LOAD_MEDIA_PATH):
if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
if event.reply_to_msg_id:
start = datetime.now()
reply_message = await event.get_reply_message()
try:
c_time = time.time()
downloaded_file_name = await borg.download_media(
reply_message,
FF_MPEG_DOWN_LOAD_MEDIA_PATH,
)
except Exception as e: # pylint:disable=C0103,W0703
await event.edit(str(e))
else:
end = datetime.now()
ms = (end - start).seconds
await event.edit("Downloaded to `{}` in {} seconds.".format(downloaded_file_name, ms))
else:
await event.edit("Reply to a Telegram media file")
else:
await event.edit(f"a media file already exists in path. Please remove the media and try again!\n`.exec rm {FF_MPEG_DOWN_LOAD_MEDIA_PATH}`")
@borg.on(admin_cmd("ffmpegtrim"))
async def ff_mpeg_trim_cmd(event):
if event.fwd_from:
return
if not os.path.exists(FF_MPEG_DOWN_LOAD_MEDIA_PATH):
await event.edit(f"a media file needs to be downloaded, and saved to the following path: `{FF_MPEG_DOWN_LOAD_MEDIA_PATH}`")
return
current_message_text = event.raw_text
cmt = current_message_text.split(" ")
logger.info(cmt)
start = datetime.now()
if len(cmt) == 3:
# output should be video
cmd, start_time, end_time = cmt
o = await cult_small_video(
FF_MPEG_DOWN_LOAD_MEDIA_PATH,
Config.TMP_DOWNLOAD_DIRECTORY,
start_time,
end_time
)
logger.info(o)
try:
c_time = time.time()
await borg.send_file(
event.chat_id,
o,
caption=" ".join(cmt[1:]),
force_document=False,
supports_streaming=True,
allow_cache=False,
# reply_to=event.message.id,
)
os.remove(o)
except Exception as e:
logger.info(str(e))
elif len(cmt) == 2:
# output should be image
cmd, start_time = cmt
o = await take_screen_shot(
FF_MPEG_DOWN_LOAD_MEDIA_PATH,
Config.TMP_DOWNLOAD_DIRECTORY,
start_time
)
logger.info(o)
try:
c_time = time.time()
await borg.send_file(
event.chat_id,
o,
caption=" ".join(cmt[1:]),
force_document=True,
# supports_streaming=True,
allow_cache=False,
# reply_to=event.message.id,
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(d, t, event, c_time, "trying to upload")
)
)
os.remove(o)
except Exception as e:
logger.info(str(e))
else:
await event.edit("RTFM")
return
end = datetime.now()
ms = (end - start).seconds
await event.edit(f"Completed Process in {ms} seconds")
async def take_screen_shot(video_file, output_directory, ttl):
# https://stackoverflow.com/a/13891070/4723940
out_put_file_name = output_directory + \
"/" + str(time.time()) + ".jpg"
file_genertor_command = [
"ffmpeg",
"-ss",
str(ttl),
"-i",
video_file,
"-vframes",
"1",
out_put_file_name
]
# width = "90"
process = await asyncio.create_subprocess_exec(
*file_genertor_command,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
e_response = stderr.decode().strip()
t_response = stdout.decode().strip()
if os.path.lexists(out_put_file_name):
return out_put_file_name
else:
logger.info(e_response)
logger.info(t_response)
return None
# https://github.com/Nekmo/telegram-upload/blob/master/telegram_upload/video.py#L26
async def cult_small_video(video_file, output_directory, start_time, end_time):
# https://stackoverflow.com/a/13891070/4723940
out_put_file_name = output_directory + \
"/" + str(round(time.time())) + ".mp4"
file_genertor_command = [
"ffmpeg",
"-i",
video_file,
"-ss",
start_time,
"-to",
end_time,
"-async",
"1",
"-strict",
"-2",
out_put_file_name
]
process = await asyncio.create_subprocess_exec(
*file_genertor_command,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
e_response = stderr.decode().strip()
t_response = stdout.decode().strip()
if os.path.lexists(out_put_file_name):
return out_put_file_name
else:
logger.info(e_response)
logger.info(t_response)
return None