imseldrith commited on
Commit
2c82039
1 Parent(s): 405416c

Upload 3 files

Browse files
helper_funcs/display_progress.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # (c) Shrimadhav U K
4
+
5
+ # the logging things
6
+ import logging
7
+ logging.basicConfig(level=logging.DEBUG,
8
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
9
+ logger = logging.getLogger(__name__)
10
+
11
+ import math, os, time, shutil
12
+
13
+
14
+ from config import Config
15
+ # the Strings used for this "thing"
16
+ from translation import Translation
17
+
18
+
19
+ async def progress_for_pyrogram(
20
+ current,
21
+ total,
22
+ ud_type,
23
+ message,
24
+ start
25
+ ):
26
+ now = time.time()
27
+ diff = now - start
28
+ if round(diff % 10.00) == 0 or current == total:
29
+ # if round(current / total * 100, 0) % 5 == 0:
30
+ percentage = current * 100 / total
31
+ speed = current / diff
32
+ elapsed_time = round(diff) * 1000
33
+ time_to_completion = round((total - current) / speed) * 1000
34
+ estimated_total_time = elapsed_time + time_to_completion
35
+
36
+ elapsed_time = TimeFormatter(milliseconds=elapsed_time)
37
+ estimated_total_time = TimeFormatter(milliseconds=estimated_total_time)
38
+
39
+ progress = "[{0}{1}] \nP: {2}%\n".format(
40
+ ''.join(["█" for i in range(math.floor(percentage / 5))]),
41
+ ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
42
+ round(percentage, 2))
43
+
44
+ tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
45
+ humanbytes(current),
46
+ humanbytes(total),
47
+ humanbytes(speed),
48
+ # elapsed_time if elapsed_time != '' else "0 s",
49
+ estimated_total_time if estimated_total_time != '' else "0 s"
50
+ )
51
+ try:
52
+ await message.edit(
53
+ text="{}\n {}".format(
54
+ ud_type,
55
+ tmp
56
+ )
57
+ )
58
+ except:
59
+ pass
60
+
61
+
62
+ def humanbytes(size):
63
+ # https://stackoverflow.com/a/49361727/4723940
64
+ # 2**10 = 1024
65
+ if not size:
66
+ return ""
67
+ power = 2**10
68
+ n = 0
69
+ Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
70
+ while size > power:
71
+ size /= power
72
+ n += 1
73
+ return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
74
+
75
+
76
+ def TimeFormatter(milliseconds: int) -> str:
77
+ seconds, milliseconds = divmod(int(milliseconds), 1000)
78
+ minutes, seconds = divmod(seconds, 60)
79
+ hours, minutes = divmod(minutes, 60)
80
+ days, hours = divmod(hours, 24)
81
+ tmp = ((str(days) + "d, ") if days else "") + \
82
+ ((str(hours) + "h, ") if hours else "") + \
83
+ ((str(minutes) + "m, ") if minutes else "") + \
84
+ ((str(seconds) + "s, ") if seconds else "") + \
85
+ ((str(milliseconds) + "ms, ") if milliseconds else "")
86
+ return tmp[:-2]
87
+
helper_funcs/help_Nekmo_ffmpeg.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # (c) Shrimadhav U K
4
+
5
+ # the logging things
6
+ import logging
7
+ logging.basicConfig(level=logging.DEBUG,
8
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ import asyncio
13
+ import os
14
+ import time
15
+ from hachoir.metadata import extractMetadata
16
+ from hachoir.parser import createParser
17
+
18
+
19
+ async def place_water_mark(input_file, output_file, water_mark_file):
20
+ watermarked_file = output_file + ".watermark.png"
21
+ metadata = extractMetadata(createParser(input_file))
22
+ width = metadata.get("width")
23
+ # https://stackoverflow.com/a/34547184/4723940
24
+ shrink_watermark_file_genertor_command = [
25
+ "ffmpeg",
26
+ "-i", water_mark_file,
27
+ "-y -v quiet",
28
+ "-vf",
29
+ "scale={}*0.5:-1".format(width),
30
+ watermarked_file
31
+ ]
32
+ # print(shrink_watermark_file_genertor_command)
33
+ process = await asyncio.create_subprocess_exec(
34
+ *shrink_watermark_file_genertor_command,
35
+ # stdout must a pipe to be accessible as process.stdout
36
+ stdout=asyncio.subprocess.PIPE,
37
+ stderr=asyncio.subprocess.PIPE,
38
+ )
39
+ # Wait for the subprocess to finish
40
+ stdout, stderr = await process.communicate()
41
+ e_response = stderr.decode().strip()
42
+ t_response = stdout.decode().strip()
43
+ commands_to_execute = [
44
+ "ffmpeg",
45
+ "-i", input_file,
46
+ "-i", watermarked_file,
47
+ "-filter_complex",
48
+ # https://stackoverflow.com/a/16235519
49
+ # "\"[0:0] scale=400:225 [wm]; [wm][1:0] overlay=305:0 [out]\"",
50
+ # "-map \"[out]\" -b:v 896k -r 20 -an ",
51
+ "\"overlay=(main_w-overlay_w):(main_h-overlay_h)\"",
52
+ # "-vf \"drawtext=text='@FFMovingPictureExpertGroupBOT':x=W-(W/2):y=H-(H/2):fontfile=" + Config.FONT_FILE + ":fontsize=12:fontcolor=white:shadowcolor=black:shadowx=5:shadowy=5\"",
53
+ output_file
54
+ ]
55
+ # print(commands_to_execute)
56
+ process = await asyncio.create_subprocess_exec(
57
+ *commands_to_execute,
58
+ # stdout must a pipe to be accessible as process.stdout
59
+ stdout=asyncio.subprocess.PIPE,
60
+ stderr=asyncio.subprocess.PIPE,
61
+ )
62
+ # Wait for the subprocess to finish
63
+ stdout, stderr = await process.communicate()
64
+ e_response = stderr.decode().strip()
65
+ t_response = stdout.decode().strip()
66
+ return output_file
67
+
68
+
69
+ async def take_screen_shot(video_file, output_directory, ttl):
70
+ # https://stackoverflow.com/a/13891070/4723940
71
+ out_put_file_name = output_directory + \
72
+ "/" + str(time.time()) + ".jpg"
73
+ file_genertor_command = [
74
+ "ffmpeg",
75
+ "-ss",
76
+ str(ttl),
77
+ "-i",
78
+ video_file,
79
+ "-vframes",
80
+ "1",
81
+ out_put_file_name
82
+ ]
83
+ # width = "90"
84
+ process = await asyncio.create_subprocess_exec(
85
+ *file_genertor_command,
86
+ # stdout must a pipe to be accessible as process.stdout
87
+ stdout=asyncio.subprocess.PIPE,
88
+ stderr=asyncio.subprocess.PIPE,
89
+ )
90
+ # Wait for the subprocess to finish
91
+ stdout, stderr = await process.communicate()
92
+ e_response = stderr.decode().strip()
93
+ t_response = stdout.decode().strip()
94
+ if os.path.lexists(out_put_file_name):
95
+ return out_put_file_name
96
+ else:
97
+ return None
98
+
99
+ # https://github.com/Nekmo/telegram-upload/blob/master/telegram_upload/video.py#L26
100
+
101
+ async def cult_small_video(video_file, output_directory, start_time, end_time):
102
+ # https://stackoverflow.com/a/13891070/4723940
103
+ out_put_file_name = output_directory + \
104
+ "/" + str(round(time.time())) + ".mp4"
105
+ file_genertor_command = [
106
+ "ffmpeg",
107
+ "-i",
108
+ video_file,
109
+ "-ss",
110
+ start_time,
111
+ "-to",
112
+ end_time,
113
+ "-async",
114
+ "1",
115
+ "-strict",
116
+ "-2",
117
+ out_put_file_name
118
+ ]
119
+ process = await asyncio.create_subprocess_exec(
120
+ *file_genertor_command,
121
+ # stdout must a pipe to be accessible as process.stdout
122
+ stdout=asyncio.subprocess.PIPE,
123
+ stderr=asyncio.subprocess.PIPE,
124
+ )
125
+ # Wait for the subprocess to finish
126
+ stdout, stderr = await process.communicate()
127
+ e_response = stderr.decode().strip()
128
+ t_response = stdout.decode().strip()
129
+ if os.path.lexists(out_put_file_name):
130
+ return out_put_file_name
131
+ else:
132
+ return None
133
+
134
+
135
+ async def generate_screen_shots(
136
+ video_file,
137
+ output_directory,
138
+ is_watermarkable,
139
+ wf,
140
+ min_duration,
141
+ no_of_photos
142
+ ):
143
+ metadata = extractMetadata(createParser(video_file))
144
+ duration = 0
145
+ if metadata is not None:
146
+ if metadata.has("duration"):
147
+ duration = metadata.get('duration').seconds
148
+ if duration > min_duration:
149
+ images = []
150
+ ttl_step = duration // no_of_photos
151
+ current_ttl = ttl_step
152
+ for looper in range(0, no_of_photos):
153
+ ss_img = await take_screen_shot(video_file, output_directory, current_ttl)
154
+ current_ttl = current_ttl + ttl_step
155
+ if is_watermarkable:
156
+ ss_img = await place_water_mark(ss_img, output_directory + "/" + str(time.time()) + ".jpg", wf)
157
+ images.append(ss_img)
158
+ return images
159
+ else:
160
+ return None
helper_funcs/help_uploadbot.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # (c) Shrimadhav U K
4
+
5
+ # the logging things
6
+ import logging
7
+ logging.basicConfig(level=logging.DEBUG,
8
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
9
+ logger = logging.getLogger(__name__)
10
+
11
+ import os
12
+ import requests
13
+
14
+ def DetectFileSize(url):
15
+ r = requests.get(url, allow_redirects=True, stream=True)
16
+ total_size = int(r.headers.get("content-length", 0))
17
+ return total_size
18
+
19
+
20
+ def DownLoadFile(url, file_name, chunk_size, client, ud_type, message_id, chat_id):
21
+ if os.path.exists(file_name):
22
+ os.remove(file_name)
23
+ if not url:
24
+ return file_name
25
+ r = requests.get(url, allow_redirects=True, stream=True)
26
+ # https://stackoverflow.com/a/47342052/4723940
27
+ total_size = int(r.headers.get("content-length", 0))
28
+ downloaded_size = 0
29
+ with open(file_name, 'wb') as fd:
30
+ for chunk in r.iter_content(chunk_size=chunk_size):
31
+ if chunk:
32
+ fd.write(chunk)
33
+ downloaded_size += chunk_size
34
+ if client is not None:
35
+ if ((total_size // downloaded_size) % 5) == 0:
36
+ time.sleep(0.3)
37
+ try:
38
+ client.edit_message_text(
39
+ chat_id,
40
+ message_id,
41
+ text="{}: {} of {}".format(
42
+ ud_type,
43
+ humanbytes(downloaded_size),
44
+ humanbytes(total_size)
45
+ )
46
+ )
47
+ except:
48
+ pass
49
+ return file_name