Spaces:
Running
Running
Update roop/utilities.py
Browse files- roop/utilities.py +0 -17
roop/utilities.py
CHANGED
@@ -19,7 +19,6 @@ TEMP_DIRECTORY = 'temp'
|
|
19 |
if platform.system().lower() == 'darwin':
|
20 |
ssl._create_default_https_context = ssl._create_unverified_context
|
21 |
|
22 |
-
|
23 |
def run_ffmpeg(args: List[str]) -> bool:
|
24 |
commands = ['ffmpeg', '-hide_banner', '-hwaccel', 'auto', '-loglevel', roop.globals.log_level]
|
25 |
commands.extend(args)
|
@@ -30,7 +29,6 @@ def run_ffmpeg(args: List[str]) -> bool:
|
|
30 |
pass
|
31 |
return False
|
32 |
|
33 |
-
|
34 |
def detect_fps(target_path: str) -> float:
|
35 |
command = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=r_frame_rate', '-of', 'default=noprint_wrappers=1:nokey=1', target_path]
|
36 |
output = subprocess.check_output(command).decode().strip().split('/')
|
@@ -41,41 +39,34 @@ def detect_fps(target_path: str) -> float:
|
|
41 |
pass
|
42 |
return 30.0
|
43 |
|
44 |
-
|
45 |
def extract_frames(target_path: str) -> None:
|
46 |
temp_directory_path = get_temp_directory_path(target_path)
|
47 |
run_ffmpeg(['-i', target_path, '-pix_fmt', 'rgb24', os.path.join(temp_directory_path, '%04d.png')])
|
48 |
|
49 |
-
|
50 |
def create_video(target_path: str, fps: float = 30.0) -> None:
|
51 |
temp_output_path = get_temp_output_path(target_path)
|
52 |
temp_directory_path = get_temp_directory_path(target_path)
|
53 |
run_ffmpeg(['-r', str(fps), '-i', os.path.join(temp_directory_path, '%04d.png'), '-c:v', roop.globals.video_encoder, '-crf', str(roop.globals.video_quality), '-pix_fmt', 'yuv420p', '-vf', 'colorspace=bt709:iall=bt601-6-625:fast=1', '-y', temp_output_path])
|
54 |
|
55 |
-
|
56 |
def restore_audio(target_path: str, output_path: str) -> None:
|
57 |
temp_output_path = get_temp_output_path(target_path)
|
58 |
done = run_ffmpeg(['-i', temp_output_path, '-i', target_path, '-c:v', 'copy', '-map', '0:v:0', '-map', '1:a:0', '-y', output_path])
|
59 |
if not done:
|
60 |
move_temp(target_path, output_path)
|
61 |
|
62 |
-
|
63 |
def get_temp_frame_paths(target_path: str) -> List[str]:
|
64 |
temp_directory_path = get_temp_directory_path(target_path)
|
65 |
return glob.glob((os.path.join(glob.escape(temp_directory_path), '*.png')))
|
66 |
|
67 |
-
|
68 |
def get_temp_directory_path(target_path: str) -> str:
|
69 |
target_name, _ = os.path.splitext(os.path.basename(target_path))
|
70 |
target_directory_path = os.path.dirname(target_path)
|
71 |
return os.path.join(target_directory_path, TEMP_DIRECTORY, target_name)
|
72 |
|
73 |
-
|
74 |
def get_temp_output_path(target_path: str) -> str:
|
75 |
temp_directory_path = get_temp_directory_path(target_path)
|
76 |
return os.path.join(temp_directory_path, TEMP_FILE)
|
77 |
|
78 |
-
|
79 |
def normalize_output_path(source_path: str, target_path: str, output_path: str) -> Any:
|
80 |
if source_path and target_path:
|
81 |
source_name, _ = os.path.splitext(os.path.basename(source_path))
|
@@ -84,12 +75,10 @@ def normalize_output_path(source_path: str, target_path: str, output_path: str)
|
|
84 |
return os.path.join(output_path, source_name + '-' + target_name + target_extension)
|
85 |
return output_path
|
86 |
|
87 |
-
|
88 |
def create_temp(target_path: str) -> None:
|
89 |
temp_directory_path = get_temp_directory_path(target_path)
|
90 |
Path(temp_directory_path).mkdir(parents=True, exist_ok=True)
|
91 |
|
92 |
-
|
93 |
def move_temp(target_path: str, output_path: str) -> None:
|
94 |
temp_output_path = get_temp_output_path(target_path)
|
95 |
if os.path.isfile(temp_output_path):
|
@@ -97,7 +86,6 @@ def move_temp(target_path: str, output_path: str) -> None:
|
|
97 |
os.remove(output_path)
|
98 |
shutil.move(temp_output_path, output_path)
|
99 |
|
100 |
-
|
101 |
def clean_temp(target_path: str) -> None:
|
102 |
temp_directory_path = get_temp_directory_path(target_path)
|
103 |
parent_directory_path = os.path.dirname(temp_directory_path)
|
@@ -106,25 +94,21 @@ def clean_temp(target_path: str) -> None:
|
|
106 |
if os.path.exists(parent_directory_path) and not os.listdir(parent_directory_path):
|
107 |
os.rmdir(parent_directory_path)
|
108 |
|
109 |
-
|
110 |
def has_image_extension(image_path: str) -> bool:
|
111 |
return image_path.lower().endswith(('png', 'jpg', 'jpeg', 'webp'))
|
112 |
|
113 |
-
|
114 |
def is_image(image_path: str) -> bool:
|
115 |
if image_path and os.path.isfile(image_path):
|
116 |
mimetype, _ = mimetypes.guess_type(image_path)
|
117 |
return bool(mimetype and mimetype.startswith('image/'))
|
118 |
return False
|
119 |
|
120 |
-
|
121 |
def is_video(video_path: str) -> bool:
|
122 |
if video_path and os.path.isfile(video_path):
|
123 |
mimetype, _ = mimetypes.guess_type(video_path)
|
124 |
return bool(mimetype and mimetype.startswith('video/'))
|
125 |
return False
|
126 |
|
127 |
-
|
128 |
def conditional_download(download_directory_path: str, urls: List[str]) -> None:
|
129 |
if not os.path.exists(download_directory_path):
|
130 |
os.makedirs(download_directory_path)
|
@@ -136,6 +120,5 @@ def conditional_download(download_directory_path: str, urls: List[str]) -> None:
|
|
136 |
with tqdm(total=total, desc='Downloading', unit='B', unit_scale=True, unit_divisor=1024) as progress:
|
137 |
urllib.request.urlretrieve(url, download_file_path, reporthook=lambda count, block_size, total_size: progress.update(block_size)) # type: ignore[attr-defined]
|
138 |
|
139 |
-
|
140 |
def resolve_relative_path(path: str) -> str:
|
141 |
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
|
|
|
19 |
if platform.system().lower() == 'darwin':
|
20 |
ssl._create_default_https_context = ssl._create_unverified_context
|
21 |
|
|
|
22 |
def run_ffmpeg(args: List[str]) -> bool:
|
23 |
commands = ['ffmpeg', '-hide_banner', '-hwaccel', 'auto', '-loglevel', roop.globals.log_level]
|
24 |
commands.extend(args)
|
|
|
29 |
pass
|
30 |
return False
|
31 |
|
|
|
32 |
def detect_fps(target_path: str) -> float:
|
33 |
command = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=r_frame_rate', '-of', 'default=noprint_wrappers=1:nokey=1', target_path]
|
34 |
output = subprocess.check_output(command).decode().strip().split('/')
|
|
|
39 |
pass
|
40 |
return 30.0
|
41 |
|
|
|
42 |
def extract_frames(target_path: str) -> None:
|
43 |
temp_directory_path = get_temp_directory_path(target_path)
|
44 |
run_ffmpeg(['-i', target_path, '-pix_fmt', 'rgb24', os.path.join(temp_directory_path, '%04d.png')])
|
45 |
|
|
|
46 |
def create_video(target_path: str, fps: float = 30.0) -> None:
|
47 |
temp_output_path = get_temp_output_path(target_path)
|
48 |
temp_directory_path = get_temp_directory_path(target_path)
|
49 |
run_ffmpeg(['-r', str(fps), '-i', os.path.join(temp_directory_path, '%04d.png'), '-c:v', roop.globals.video_encoder, '-crf', str(roop.globals.video_quality), '-pix_fmt', 'yuv420p', '-vf', 'colorspace=bt709:iall=bt601-6-625:fast=1', '-y', temp_output_path])
|
50 |
|
|
|
51 |
def restore_audio(target_path: str, output_path: str) -> None:
|
52 |
temp_output_path = get_temp_output_path(target_path)
|
53 |
done = run_ffmpeg(['-i', temp_output_path, '-i', target_path, '-c:v', 'copy', '-map', '0:v:0', '-map', '1:a:0', '-y', output_path])
|
54 |
if not done:
|
55 |
move_temp(target_path, output_path)
|
56 |
|
|
|
57 |
def get_temp_frame_paths(target_path: str) -> List[str]:
|
58 |
temp_directory_path = get_temp_directory_path(target_path)
|
59 |
return glob.glob((os.path.join(glob.escape(temp_directory_path), '*.png')))
|
60 |
|
|
|
61 |
def get_temp_directory_path(target_path: str) -> str:
|
62 |
target_name, _ = os.path.splitext(os.path.basename(target_path))
|
63 |
target_directory_path = os.path.dirname(target_path)
|
64 |
return os.path.join(target_directory_path, TEMP_DIRECTORY, target_name)
|
65 |
|
|
|
66 |
def get_temp_output_path(target_path: str) -> str:
|
67 |
temp_directory_path = get_temp_directory_path(target_path)
|
68 |
return os.path.join(temp_directory_path, TEMP_FILE)
|
69 |
|
|
|
70 |
def normalize_output_path(source_path: str, target_path: str, output_path: str) -> Any:
|
71 |
if source_path and target_path:
|
72 |
source_name, _ = os.path.splitext(os.path.basename(source_path))
|
|
|
75 |
return os.path.join(output_path, source_name + '-' + target_name + target_extension)
|
76 |
return output_path
|
77 |
|
|
|
78 |
def create_temp(target_path: str) -> None:
|
79 |
temp_directory_path = get_temp_directory_path(target_path)
|
80 |
Path(temp_directory_path).mkdir(parents=True, exist_ok=True)
|
81 |
|
|
|
82 |
def move_temp(target_path: str, output_path: str) -> None:
|
83 |
temp_output_path = get_temp_output_path(target_path)
|
84 |
if os.path.isfile(temp_output_path):
|
|
|
86 |
os.remove(output_path)
|
87 |
shutil.move(temp_output_path, output_path)
|
88 |
|
|
|
89 |
def clean_temp(target_path: str) -> None:
|
90 |
temp_directory_path = get_temp_directory_path(target_path)
|
91 |
parent_directory_path = os.path.dirname(temp_directory_path)
|
|
|
94 |
if os.path.exists(parent_directory_path) and not os.listdir(parent_directory_path):
|
95 |
os.rmdir(parent_directory_path)
|
96 |
|
|
|
97 |
def has_image_extension(image_path: str) -> bool:
|
98 |
return image_path.lower().endswith(('png', 'jpg', 'jpeg', 'webp'))
|
99 |
|
|
|
100 |
def is_image(image_path: str) -> bool:
|
101 |
if image_path and os.path.isfile(image_path):
|
102 |
mimetype, _ = mimetypes.guess_type(image_path)
|
103 |
return bool(mimetype and mimetype.startswith('image/'))
|
104 |
return False
|
105 |
|
|
|
106 |
def is_video(video_path: str) -> bool:
|
107 |
if video_path and os.path.isfile(video_path):
|
108 |
mimetype, _ = mimetypes.guess_type(video_path)
|
109 |
return bool(mimetype and mimetype.startswith('video/'))
|
110 |
return False
|
111 |
|
|
|
112 |
def conditional_download(download_directory_path: str, urls: List[str]) -> None:
|
113 |
if not os.path.exists(download_directory_path):
|
114 |
os.makedirs(download_directory_path)
|
|
|
120 |
with tqdm(total=total, desc='Downloading', unit='B', unit_scale=True, unit_divisor=1024) as progress:
|
121 |
urllib.request.urlretrieve(url, download_file_path, reporthook=lambda count, block_size, total_size: progress.update(block_size)) # type: ignore[attr-defined]
|
122 |
|
|
|
123 |
def resolve_relative_path(path: str) -> str:
|
124 |
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
|