-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
97 lines (75 loc) · 3.19 KB
/
utils.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
import os
import shutil
from tkinter import Tk
from tkinter.filedialog import askopenfilename, askdirectory
from occlude import occlude_faces, integrate_audio
import time
class ProcessResults:
def __init__(self):
self.total_duration: float = None
self.output_files = []
def parse_settings():
settings = {}
with open(".settings") as myfile:
for line in myfile:
name, var = line.split("=")
settings[name.strip()] = var.strip()
return settings
SETTINGS = parse_settings()
VALID_FILE_EXTENSION = '.mp4'
def ask_for_file_path() -> str:
filename = askopenfilename()
return filename
def make_video_copies(video_path, num_copies, file_path_prefix=""):
file_name, file_extension = os.path.splitext(video_path)
for i in range(num_copies):
new_file_name = f"{file_path_prefix}{file_name}_copy_{i+1}{file_extension}"
shutil.copy2(video_path, new_file_name)
print(f"Copy {i+1} created: {new_file_name}")
def handle_video_processing(suffix="", count_time=False):
files = askopenfilename(initialdir='.', multiple=True)
output_folder = askdirectory(initialdir='.')
return process_videos(files, output_folder, suffix, count_time)
def process_videos(files, output_folder, suffix="", count_time=False, keep_audio=False, show=False, features={'face'}):
results = ProcessResults()
if count_time:
results.total_duration = 0
for index, filepath in enumerate(files):
if count_time:
start_time = time.time()
print(f"Processing file {index+1} of {len(files)}")
file = os.path.basename(filepath)
filename, file_extension = os.path.splitext(file)
output_path = os.path.join(
output_folder, f'{filename}{suffix}{file_extension}')
print(output_path)
occlude_faces(input_video_path=filepath,
output_video_path=output_path, show=show, features=features)
if keep_audio:
integrate_audio(filepath, output_path)
if count_time:
time_taken = time.time() - start_time
results.total_duration += time_taken
print(f"Time taken to process video: {time_taken} seconds")
print(f"All videos processed and can be found in: {output_folder}")
return results
def process_video(input: str, output: str, keep_audio=False, show=False, features={'face'}):
occlude_faces(input_video_path=input, output_video_path=output,
show=show, features=features)
if keep_audio:
integrate_audio(input, output)
def process_camera(output=None, show=False, features={'face'}):
occlude_faces(output_video_path=output, show=show, features=features)
# TODO: Figure out how to get audio from camera feed
# if keep_audio:
# integrate_audio(output, output)
def main():
# video_path = ask_for_file_path()
# num_copies = int(input("Enter the number of copies you want to create: "))
# make_video_copies(video_path, num_copies)
results = process_videos(count_time=True)
print(
"Total time taken to process all videos: " +
f"{time.strftime('%H:%M:%S', time.gmtime(results.total_duration))}")
if __name__ == '__main__':
main()