diff --git a/core/utils.py b/core/utils.py index a8e2407..cae9bb8 100644 --- a/core/utils.py +++ b/core/utils.py @@ -3,7 +3,14 @@ import shutil sep = "/" if os.name == "nt": - sep = "/" + sep = "\\" + + +def path(string): + if sep == "\\": + return string.replace("/", "\\") + return string + def run_command(command, mode="silent"): if mode == "debug": @@ -11,6 +18,7 @@ def run_command(command, mode="silent"): return os.popen(command).read() def detect_fps(input_path): + input_path = path(input_path) output = os.popen(f'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate "{input_path}"').read() if "/" in output: try: @@ -21,24 +29,28 @@ def detect_fps(input_path): def set_fps(input_path, output_path, fps): + input_path, output_path = path(input_path), path(output_path) os.system(f'ffmpeg -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): + output_dir = path(output_dir) os.system(f'ffmpeg -framerate {fps} -pattern_type glob -i "{output_dir}{sep}*.png" -c:v libx264 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): + input_path, output_dir = path(input_path), path(output_dir) os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, keep_frames, output_file): video = target_path.split(sep)[-1] video_name = video.split(".")[0] - save_to = output_file if output_file else output_dir + f"{sep}swapped-" + video_name + ".mp4" - os.system(f'ffmpeg -i "{output_dir}{sep}output.mp4" -i "{output_dir}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to}"') + save_to = output_file if output_file else output_dir + f"/swapped-" + video_name + ".mp4" + save_to_ff, output_dir_ff = path(save_to), path(output_dir) + os.system(f'ffmpeg -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"') if not os.path.isfile(save_to): - shutil.move(output_dir + f"{sep}output.mp4", save_to) + shutil.move(output_dir + f"/output.mp4", save_to) if not keep_frames: shutil.rmtree(output_dir) diff --git a/run.py b/run.py index e9d172a..3f143e8 100644 --- a/run.py +++ b/run.py @@ -35,9 +35,6 @@ parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_fr for name, value in vars(parser.parse_args()).items(): args[name] = value -sep = "/" # used for filepaths e.g. /root/path/output.mp4 -if sys.platform.startswith("win") or os.name == 'nt': - sep = "/" # for windows def start_processing(): if args['gpu']: @@ -89,27 +86,25 @@ def start(): if is_img(target_path): process_img(args['source_img'], target_path) return - video_name = target_path.split(sep)[-1].split(".")[0] - output_dir = target_path.replace(target_path.split(sep)[-1], "") + sep + video_name - if sep == "\\": - output_dir = output_dir.lstrip("\\").rstrip("\\") + video_name = target_path.split("/")[-1].split(".")[0] + output_dir = target_path.replace(target_path.split("/")[-1], "") + "/" + video_name Path(output_dir).mkdir(exist_ok=True) fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: - this_path = output_dir + sep + video_name + ".mp4" + this_path = output_dir + "/" + video_name + ".mp4" set_fps(target_path, this_path, 30) target_path, fps = this_path, 30 else: shutil.copy(target_path, output_dir) extract_frames(target_path, output_dir) args['frame_paths'] = tuple(sorted( - glob.glob(output_dir + f"{sep}*.png"), - key=lambda x: int(x.split(sep)[-1].replace(".png", "")) + glob.glob(output_dir + f"/*.png"), + key=lambda x: int(x.split("/")[-1].replace(".png", "")) )) start_processing() create_video(video_name, fps, output_dir) add_audio(output_dir, target_path, args['keep_frames'], args['output_file']) - save_path = args['output_file'] if args['output_file'] else output_dir + sep + video_name + ".mp4" + save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n")