-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
264 lines (230 loc) · 9.16 KB
/
main.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
import time
from pathlib import Path
from slugify import slugify
from utils.common import convert_bytes, has_transparency
from utils.display import Write, display_file_info
from generators import File, VTT, Thumbnail, MP4, WEBM, GIF, PNG, JPG, WEBP
"""
args:
slugify: bool
skip_existing: bool
skip_gif: bool
no_copy: bool
image_formats: list
video_formats: list
no_thumbnail: bool
no_vtt: bool
"""
__version__ = "1.1.0"
defaults = {"mp4": {"video_codec": "h264", "audio_codec": "aac"}}
async def loop_structure(args):
if type(args) is not dict:
raise ValueError("Arguments must be of type dict!")
args.update(defaults)
input_length = len(list(args["input"].glob("**/*")))
old_combined_file_size = 0
new_combined_file_size = 0
start_time = time.time()
# Loop every file/folder
for index, input_file in enumerate(args["input"].glob("**/*")):
Write.yellow("Processing file {} of {}".format(index + 1, input_length))
Write.gray(">>> ", input_file)
# Skip all dot files, like `.DS_Store`
if input_file.name.startswith("."):
Write.blue("Skipping dotfile")
Write.line()
continue
# Create output directory/path
output_file = Path(
str(input_file).replace(str(args["input"]), str(args["output"]))
)
# Slugify filename
if args["slugify"] and input_file.is_file():
output_file = Path(
output_file.parent,
slugify(input_file.stem, lowercase=False) + input_file.suffix,
)
# Skip process if file exist already otherwise override it
if args["skip_existing"] and output_file.exists():
Write.blue("Skipping existing")
Write.line()
continue
# Input file is directory, create it, even if it exists and continue
if not input_file.is_file():
output_file.mkdir(parents=True, exist_ok=True)
Write.blue("Skipping dir '{}'".format(input_file.name))
Write.line()
continue
Write.gray("<<< ", str(output_file).replace(output_file.suffix, "..."))
old_combined_file_size += input_file.stat().st_size
# VIDEOS
if input_file.suffix in [".mp4", ".wmv", ".webm", ".mov", ".vlf", ".mkv"]:
input_group = "video"
input_type = None
display_file_info(input_group, input_type, input_file)
# MP4
input_type = "mp4"
if input_type in args["video_formats"]:
generator = MP4(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
args["mp4"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
new_combined_file_size += output.stat().st_size
# WEBM
input_type = "webm"
if input_type in args["video_formats"]:
generator = WEBM(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
if "mp4" not in args["video_formats"]:
new_combined_file_size += output.stat().st_size
# VTT
input_type = "vtt"
if args["vtt"]:
generator = VTT(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
# Thumbnail
input_type = "thumb"
if args["thumbnail"]:
generator = Thumbnail(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
# IMAGES
elif input_file.suffix in [".png", ".jpeg", ".jpg", ".webp"]:
input_group = "image"
input_type = None
display_file_info(input_type, input_type, input_file)
if ["jpg", "png"] == sorted(args["image_formats"]):
if has_transparency(input_file):
# PNG
input_type = "png"
generator = PNG(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
else:
# JPG
input_type = "jpg"
generator = JPG(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
if "webp" not in args["image_formats"]:
new_combined_file_size += output.stat().st_size
else:
# PNG
input_type = "png"
if input_type in args["image_formats"]:
generator = PNG(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
if not any(f in ["jpg", "webp"] for f in args["image_formats"]):
new_combined_file_size += output.stat().st_size
# JPG
input_type = "jpg"
if input_type in args["image_formats"]:
generator = JPG(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
if not any(f in ["png", "webp"] for f in args["image_formats"]):
new_combined_file_size += output.stat().st_size
# WEBP
input_type = "webp"
if input_type in args["image_formats"]:
generator = WEBP(
input_group,
input_type,
input_file,
output_file,
args["skip_existing"],
)
output = await generator.run()
display_file_info(input_group, input_type, output)
if not all(f in ["jpg", "png"] for f in args["image_formats"]):
new_combined_file_size += output.stat().st_size
# GIF
elif input_file.suffix == ".gif" and args["gif"]:
input_group = "gif"
input_type = None
display_file_info(input_type, input_type, input_file)
# GIF
input_type = "gif"
generator = GIF(
input_group, input_type, input_file, output_file, args["skip_existing"]
)
output = await generator.run()
display_file_info(input_group, input_type, output)
new_combined_file_size += output.stat().st_size
# All other files
elif args["copy"]:
input_group = "file"
input_type = None
display_file_info(input_group, input_type, input_file)
# Unknown
input_type = input_file.suffix.replace(".", "")
generator = File(
input_group, input_type, input_file, output_file, args["skip_existing"]
)
output = await generator.run()
display_file_info(input_group, input_type, output)
new_combined_file_size += output.stat().st_size
else:
Write.blue("Skipping")
pass
Write.text() # empty
elapsed_time = time.time() - start_time
elapsed_time = round(elapsed_time // 60)
if elapsed_time > 1:
Write.green("Done after " + str(elapsed_time) + " minutes")
else:
Write.green("Done after some seconds")
Write.line()
Write.text("Original size: {}".format(convert_bytes(old_combined_file_size)))
Write.text("Compressed size: {}".format(convert_bytes(new_combined_file_size)))