File size: 4,645 Bytes
3914b35 |
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 |
import glob
import os
import shutil
from typing import List, Optional
import facefusion.choices
def get_file_size(file_path : str) -> int:
if is_file(file_path):
return os.path.getsize(file_path)
return 0
def get_file_name(file_path : str) -> Optional[str]:
file_name, _ = os.path.splitext(os.path.basename(file_path))
if file_name:
return file_name
return None
def get_file_extension(file_path : str) -> Optional[str]:
_, file_extension = os.path.splitext(file_path)
if file_extension:
return file_extension.lower()
return None
def get_file_format(file_path : str) -> Optional[str]:
file_extension = get_file_extension(file_path)
if file_extension:
if file_extension == '.jpg':
return 'jpeg'
if file_extension == '.tif':
return 'tiff'
return file_extension.lstrip('.')
return None
def same_file_extension(first_file_path : str, second_file_path : str) -> bool:
first_file_extension = get_file_extension(first_file_path)
second_file_extension = get_file_extension(second_file_path)
if first_file_extension and second_file_extension:
return get_file_extension(first_file_path) == get_file_extension(second_file_path)
return False
def is_file(file_path : str) -> bool:
if file_path:
return os.path.isfile(file_path)
return False
def is_audio(audio_path : str) -> bool:
return is_file(audio_path) and get_file_format(audio_path) in facefusion.choices.audio_formats
def has_audio(audio_paths : List[str]) -> bool:
if audio_paths:
return any(map(is_audio, audio_paths))
return False
def are_audios(audio_paths : List[str]) -> bool:
if audio_paths:
return all(map(is_audio, audio_paths))
return False
def is_image(image_path : str) -> bool:
return is_file(image_path) and get_file_format(image_path) in facefusion.choices.image_formats
def has_image(image_paths : List[str]) -> bool:
if image_paths:
return any(is_image(image_path) for image_path in image_paths)
return False
def are_images(image_paths : List[str]) -> bool:
if image_paths:
return all(map(is_image, image_paths))
return False
def is_video(video_path : str) -> bool:
return is_file(video_path) and get_file_format(video_path) in facefusion.choices.video_formats
def has_video(video_paths : List[str]) -> bool:
if video_paths:
return any(map(is_video, video_paths))
return False
def are_videos(video_paths : List[str]) -> bool:
if video_paths:
return any(map(is_video, video_paths))
return False
def filter_audio_paths(paths : List[str]) -> List[str]:
if paths:
return [ path for path in paths if is_audio(path) ]
return []
def filter_image_paths(paths : List[str]) -> List[str]:
if paths:
return [ path for path in paths if is_image(path) ]
return []
def copy_file(file_path : str, move_path : str) -> bool:
if is_file(file_path):
shutil.copy(file_path, move_path)
return is_file(move_path)
return False
def move_file(file_path : str, move_path : str) -> bool:
if is_file(file_path):
shutil.move(file_path, move_path)
return not is_file(file_path) and is_file(move_path)
return False
def remove_file(file_path : str) -> bool:
if is_file(file_path):
os.remove(file_path)
return not is_file(file_path)
return False
def resolve_file_paths(directory_path : str) -> List[str]:
file_paths : List[str] = []
if is_directory(directory_path):
file_names_and_extensions = sorted(os.listdir(directory_path))
for file_name_and_extension in file_names_and_extensions:
if not file_name_and_extension.startswith(('.', '__')):
file_path = os.path.join(directory_path, file_name_and_extension)
file_paths.append(file_path)
return file_paths
def resolve_file_pattern(file_pattern : str) -> List[str]:
if in_directory(file_pattern):
return sorted(glob.glob(file_pattern))
return []
def is_directory(directory_path : str) -> bool:
if directory_path:
return os.path.isdir(directory_path)
return False
def in_directory(file_path : str) -> bool:
if file_path:
directory_path = os.path.dirname(file_path)
if directory_path:
return not is_directory(file_path) and is_directory(directory_path)
return False
def create_directory(directory_path : str) -> bool:
if directory_path and not is_file(directory_path):
os.makedirs(directory_path, exist_ok = True)
return is_directory(directory_path)
return False
def remove_directory(directory_path : str) -> bool:
if is_directory(directory_path):
shutil.rmtree(directory_path, ignore_errors = True)
return not is_directory(directory_path)
return False
def resolve_relative_path(path : str) -> str:
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
|