-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg-videosticker-formatter.py
70 lines (49 loc) · 1.82 KB
/
tg-videosticker-formatter.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
import os
import mimetypes
from alive_progress import alive_bar
from argparser import parser
from formatter import VideoStickerFormatter
def is_video_file(file_name):
return mimetypes.guess_type(file_name)[0].startswith("video")
def collect_video_paths(folder):
return [
os.path.join(folder, file)
for file in os.listdir(folder)
if os.path.isfile(os.path.join(folder, file)) and is_video_file(file)
]
def process_video(input_path, output_path):
video_formatter = VideoStickerFormatter(input_path)
video_formatter.format_video()
video_formatter.save_to_file(output_path)
def check_video(*args):
input_path = args[0]
raise NotImplementedError
def get_action(action_type):
if action_type == "format":
return process_video
if action_type == "check":
return check_video
def run(input_path, output_folder, action_type, batch):
video_paths = collect_video_paths(input_path) if batch else [input_path]
action_func = get_action(action_type)
print(f"collected {len(video_paths)} video file(s)")
print(f"executing {action_type} action\n")
with alive_bar(len(video_paths)) as progress_bar:
for path in video_paths:
try:
action_func(
path,
os.path.join(
output_folder,
os.path.basename(path).split(".")[0] + "-tg-video-sticker.webm",
),
)
except OSError as e:
print(e)
print("Skipping...")
progress_bar()
if __name__ == "__main__":
args = vars(parser.parse_args())
batch_mode = os.path.isdir(args["input_path"])
action = args["action-type"]
run(args["input_path"], args["output_path"], args["action-type"], batch_mode)