-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
353 lines (271 loc) · 11.5 KB
/
editor.py
File metadata and controls
353 lines (271 loc) · 11.5 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import magic
import os
import subprocess
import sys
import win32api
from send2trash import send2trash
def color(r, g, b):
return "\x1b[38;2;" + ";".join([str(r), str(g), str(b)]) + "m"
reset = "\x1b[0m"
red = color(255, 90, 90)
green = color(90, 255, 90)
blue = color(30, 120, 255)
yellow = color(255, 255, 30)
gray = color(127, 127, 127)
def press_enter():
input(f'\n {gray}[Press Enter]{reset}')
def header():
print(f'{blue}\n Mutater\'s Clip Editor{reset}')
def clear():
os.system("cls")
def delete(file):
os.system(f'del "{file}"')
def copy(source, destination):
os.system(f'copy "{source}" "{destination}" /y')
def play(file):
os.system('"' + file + '"')
def options(*args):
print("\n ", end="")
print(*args, sep="\n\n ")
return input("\n >>>")
def get_files(pathe=""):
return next(os.walk(pathe), (None, None, []))[2]
def get_file_names(path=""):
return [os.path.splitext(file)[0] for file in get_files(path)]
def exit_handler(*args):
e.delete_temps()
class Editor:
# - MAIN LOOP - #
def start(self):
if not self.setup():
return
while 1:
self.loop()
# If user doesn't want to continue editing in a new location, end the editor
if not self.edit_again():
break
def setup(self):
self.working_directory = os.getcwd() + "\\"
self.video_codec = (
open("config.txt", "r")
.read()
.split("\n")[0]
.split("=")[1]
)
os.system("PATH %PATH%;" + self.working_directory)
self.clip_path = ""
self.clip_extension = ""
self.type_checker = magic.Magic(magic.Magic(mime=True, uncompress=True))
self.input_directory = self.input_path(" of clips to edit")
self.output_directory = self.input_path(" to save clips")
self.current_clip = 0
os.chdir(os.getcwd())
self.load_clips()
if not self.clips:
clear()
header()
print(f'\n {red}Directory contains no clips.')
press_enter()
return False
return True
def loop(self):
for clip_name in self.clips:
self.current_clip += 1
self.clip_path = f'{self.input_directory}\\{clip_name}'
self.clip_extension = clip_name.split(".")[-1]
self.load_clip()
# If the user decides to delete or export the clip without editing,
# continue to next clip
if not self.if_edit_clip():
continue
while 1:
self.edit_clip()
play(f'{self.input_directory}\\temp_clip.{self.clip_extension}')
# If clip is exported, continue to next clip
if self.export_clip():
break
continue
def edit_again(self):
clear()
header()
print(f'\n No more clips detected!')
option = options(
f'Type {green}anything{reset} to continue editing.',
f'Press ENTER without typing to {red}exit{reset}.'
)
if option:
self.input_directory = self.input_path(" of clips to edit")
self.current_clip = 0
return True
return False
# - NESTED LOOP - #
def input_path(self, prompt):
while 1:
clear()
header()
option = options(f'Type the {green}path{reset}{prompt}.')
if os.path.isdir(option):
return option
else:
print(f'\n {red}The specified directory does not exist.{reset}')
press_enter()
def load_clip(self):
if "temp_clip" in get_file_names():
delete("temp_clip.*")
copy(self.clip_path, f'{self.input_directory}\\temp_clip.{self.clip_extension}')
def load_clips(self):
clear()
header()
print("\n Loading clips...")
clips = get_files(self.input_directory)
for i in range(len(clips)-1, -1, -1):
try:
query = self.type_checker.from_buffer(open(f'{self.input_directory}\\{clips[i]}', "rb").read(2048))
if "video" in query:
continue
except:
pass
clips.pop(i)
print("\n Done loading clips. If the program stays frozen, try editing config.txt.")
self.clips = clips
def if_edit_clip(self):
play(f'{self.input_directory}\\temp_clip.{self.clip_extension}')
while 1:
clear()
header()
option = options(
f'"{self.clip_path}".',
f'Clip {self.current_clip} / {len(self.clips)} ({len(self.clips) - self.current_clip} Remaining)',
f'Type the number for an option below.',
f' 1) {green}Edit{reset} the clip.',
f' 2) {yellow}Continue{reset} without editing.',
f' 3) {red}Delete{reset} the clip.'
)
if option == "1":
return True
elif option == "2":
if self.export_clip():
return False
return True
elif option == "3":
send2trash(self.clip_path)
return False
def edit_clip(self):
while 1:
while 1:
clear()
header()
start = options(f'Type start {green}time{reset} of subclip in {green}seconds{reset}.')
end = options(
f'Type end {green}time{reset} of subclip in {green}seconds{reset}.',
f'Press {yellow}ENTER{reset} without typing to {red}cancel{reset}.'
)
try:
start = float(start)
duration = float(end) - start + 1
break
except:
print(f'\n {reset}Input must be only numbers, e.g. "5" or "2.3" without quotes.')
press_enter()
while 1:
try:
source = f'"{self.input_directory}\\temp_clip.{self.clip_extension}"'
destination = f'"{self.input_directory}\\temp_clip2.{self.clip_extension}"'
ffmpeg = "ffmpeg"
os.system("cd")
command = f'{ffmpeg} -i {source} -ss {start} -t {duration} -vcodec {self.video_codec} -b:v 15m {destination} -y'
os.system(command)
copy(destination, source)
delete(f'{self.input_directory}\\temp_clip2.*')
press_enter()
return
except Exception as e:
print(f'\n {red}Unable to trim video; start and duration were incorrect or another error occured.{reset}')
print(f'\n {e}')
press_enter()
break
def export_clip(self):
while 1:
clear()
header()
option = options(
f'Type the number for an option below.',
f' 1) {green}Save{reset} the clip.',
f' 2) {green}Save{reset} the clip and {red}delete{reset} the original clip.',
f' 3) {yellow}Undo{reset} changes to the clip.'
)
recycle = option == "2"
if option in "12":
while 1:
clear()
header()
print(f'\n "{self.clip_path}"')
option = options(
f'Type the {blue}name{reset} for the clip.',
f'{red}WARNING{reset}: Make the file name Windows-friendly! {red}Don\'t{reset} include the file extension.'
)
export_path = option.replace("/", "\\")
export_path_as_list = export_path.split("\\")
export_name = export_path_as_list[-1]
# If subdirectory specified in clip name, format it to be added to output directory
export_subdirectory = "\\".join(export_path_as_list[:-1])
if export_subdirectory:
export_subdirectory = f'\\{export_subdirectory}'
export_directory = self.output_directory + export_subdirectory + "\\"
export_path = export_directory + export_name
# If subdirectory does not exist, continue
try:
os.listdir(export_directory)
except:
print(f'\n {red}Specified subdirectory path does not exist.{reset}')
press_enter()
continue
# If file exists, prompt for overwrite
overwrite = True
if f'{export_name}.{self.clip_extension}' in get_files(export_directory):
while 1:
clear()
header()
print(f'\n {red}File already exists in directory.{reset}')
option = options(
f'Type the number for an option below.',
f' 1) {green}Rename{reset} the clip.',
f' 2) {red}Overwrite{reset} the file.'
)
if option == "1":
overwrite = False
break
elif option == "2":
break
if not overwrite:
continue
source = f'{self.input_directory}\\temp_clip.{self.clip_extension}'
destination = f'{export_path}.{self.clip_extension}'
copy(source, destination)
delete(f'{self.input_directory}\\temp_clip.*')
if recycle:
send2trash(self.clip_path)
return True
elif option == "3":
while 1:
clear()
header()
option = options(
f'Type the number for an option below.',
f' 1) {green}Confirm{reset}.',
f' 2) {red}Cancel{reset}.'
)
if option == "1":
self.load_clip()
if self.if_edit_clip():
return False
return True
elif option == "2":
break
def delete_temps(self):
delete(e.input_directory + "\\temp_clip.*")
delete(e.input_directory + "\\temp_clip2.*")
if __name__ == "__main__":
win32api.SetConsoleCtrlHandler(exit_handler, True)
e = Editor()
e.start()