From 9c39bd41f7b2f46a41a5ac091be2c8b5d1bfe658 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 19:45:24 +0200 Subject: [PATCH 1/7] Add FFMpeg hardware acceleration --- core/utils.py | 20 ++++++++++++-------- run.py | 11 ++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/core/utils.py b/core/utils.py index 50aca35..3cf7407 100644 --- a/core/utils.py +++ b/core/utils.py @@ -29,26 +29,30 @@ def detect_fps(input_path): return 30, 30 -def set_fps(input_path, output_path, fps): +def set_fps(input_path, output_path, fps, use_gpu): input_path, output_path = path(input_path), path(output_path) - os.system(f'ffmpeg -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') -def create_video(video_name, fps, output_dir): +def create_video(video_name, fps, output_dir, use_gpu): output_dir = path(output_dir) - os.system(f'ffmpeg -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') -def extract_frames(input_path, output_dir): +def extract_frames(input_path, output_dir, use_gpu): input_path, output_dir = path(input_path), path(output_dir) - os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png"') -def add_audio(output_dir, target_path, video, keep_frames, output_file): +def add_audio(output_dir, target_path, video, keep_frames, output_file, use_gpu): video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/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}"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -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 + "/output.mp4", save_to) if not keep_frames: diff --git a/run.py b/run.py index 2099fdc..10435b1 100755 --- a/run.py +++ b/run.py @@ -71,6 +71,7 @@ def pre_check(): if not os.path.isfile(model_path): quit('File "inswapper_128.onnx" does not exist!') if '--gpu' in sys.argv: + core.globals.use_gpu = True NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] if len(list(set(core.globals.providers) - set(NVIDIA_PROVIDERS))) == 1: CUDA_VERSION = torch.version.cuda @@ -90,7 +91,7 @@ def pre_check(): def start_processing(): - if args['gpu']: + if core.globals.use_gpu: process_video(args['source_img'], args["frame_paths"]) return frame_paths = args["frame_paths"] @@ -207,12 +208,12 @@ def start(): fps, exact_fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: this_path = output_dir + "/" + video_name + ".mp4" - set_fps(target_path, this_path, 30) + set_fps(target_path, this_path, 30, core.globals.use_gpu) target_path, exact_fps = this_path, 30 else: shutil.copy(target_path, output_dir) status("extracting frames...") - extract_frames(target_path, output_dir) + extract_frames(target_path, output_dir, core.globals.use_gpu) args['frame_paths'] = tuple(sorted( glob.glob(output_dir + "/*.png"), key=lambda x: int(x.split(sep)[-1].replace(".png", "")) @@ -220,9 +221,9 @@ def start(): status("swapping in progress...") start_processing() status("creating video...") - create_video(video_name, exact_fps, output_dir) + create_video(video_name, exact_fps, output_dir, core.globals.use_gpu) status("adding audio...") - add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file']) + add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], core.globals.use_gpu) save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n") status("swap successful!") From b7ce758c991ac8b77a63075f899ae7894fa776d5 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 20:29:37 +0200 Subject: [PATCH 2/7] Fix namespace from core to roop --- roop/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/roop/core.py b/roop/core.py index 3f6ff38..8b53fef 100644 --- a/roop/core.py +++ b/roop/core.py @@ -75,7 +75,7 @@ def pre_check(): if not os.path.isfile(model_path): quit('File "inswapper_128.onnx" does not exist!') if '--gpu' in sys.argv: - core.globals.use_gpu = True + roop.globals.use_gpu = True NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] if len(list(set(roop.globals.providers) - set(NVIDIA_PROVIDERS))) == 1: CUDA_VERSION = torch.version.cuda @@ -100,7 +100,7 @@ def start_processing(): frame_paths = args["frame_paths"] n = len(frame_paths) // (args['cores_count']) # single thread - if core.globals.use_gpu or n < 2: + if roop.globals.use_gpu or n < 2: process_video(args['source_img'], args["frame_paths"]) return # multithread if total frames to cpu cores ratio is greater than 2 @@ -217,12 +217,12 @@ def start(): fps, exact_fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: this_path = output_dir + "/" + video_name + ".mp4" - set_fps(target_path, this_path, 30, core.globals.use_gpu) + set_fps(target_path, this_path, 30, roop.globals.use_gpu) target_path, exact_fps = this_path, 30 else: shutil.copy(target_path, output_dir) status("extracting frames...") - extract_frames(target_path, output_dir, core.globals.use_gpu) + extract_frames(target_path, output_dir, roop.globals.use_gpu) args['frame_paths'] = tuple(sorted( glob.glob(output_dir + "/*.png"), key=lambda x: int(x.split(sep)[-1].replace(".png", "")) @@ -230,9 +230,9 @@ def start(): status("swapping in progress...") start_processing() status("creating video...") - create_video(video_name, exact_fps, output_dir, core.globals.use_gpu) + create_video(video_name, exact_fps, output_dir, roop.globals.use_gpu) status("adding audio...") - add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], core.globals.use_gpu) + add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], roop.globals.use_gpu) save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n") status("swap successful!") From 37a71f661ce654da340665e1791956dd58f737e6 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 20:40:02 +0200 Subject: [PATCH 3/7] Use flags as global --- roop/core.py | 8 ++++---- roop/utils.py | 17 +++++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/roop/core.py b/roop/core.py index 8b53fef..2629c37 100644 --- a/roop/core.py +++ b/roop/core.py @@ -217,12 +217,12 @@ def start(): fps, exact_fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: this_path = output_dir + "/" + video_name + ".mp4" - set_fps(target_path, this_path, 30, roop.globals.use_gpu) + set_fps(target_path, this_path, 30) target_path, exact_fps = this_path, 30 else: shutil.copy(target_path, output_dir) status("extracting frames...") - extract_frames(target_path, output_dir, roop.globals.use_gpu) + extract_frames(target_path, output_dir) args['frame_paths'] = tuple(sorted( glob.glob(output_dir + "/*.png"), key=lambda x: int(x.split(sep)[-1].replace(".png", "")) @@ -230,9 +230,9 @@ def start(): status("swapping in progress...") start_processing() status("creating video...") - create_video(video_name, exact_fps, output_dir, roop.globals.use_gpu) + create_video(video_name, exact_fps, output_dir) status("adding audio...") - add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], roop.globals.use_gpu) + add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file']) save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n") status("swap successful!") diff --git a/roop/utils.py b/roop/utils.py index 69c0aad..479ddb8 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -1,5 +1,6 @@ import os import shutil +import roop.globals sep = "/" if os.name == "nt": @@ -29,29 +30,29 @@ def detect_fps(input_path): return 30, 30 -def set_fps(input_path, output_path, fps, use_gpu): +def set_fps(input_path, output_path, fps): input_path, output_path = path(input_path), path(output_path) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}" -loglevel error') -def create_video(video_name, fps, output_dir, use_gpu): +def create_video(video_name, fps, output_dir): output_dir = path(output_dir) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4" -loglevel error') -def extract_frames(input_path, output_dir, use_gpu): +def extract_frames(input_path, output_dir): input_path, output_dir = path(input_path), path(output_dir) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png" -loglevel error') -def add_audio(output_dir, target_path, video, keep_frames, output_file, use_gpu): +def add_audio(output_dir, target_path, video, keep_frames, output_file): video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -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}" -loglevel error') if not os.path.isfile(save_to): shutil.move(output_dir + "/output.mp4", save_to) From 74783142e6fdb484d9d021e51e07fc0978e21fb5 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 20:53:01 +0200 Subject: [PATCH 4/7] Little ffmpeg refactor --- roop/globals.py | 1 + roop/utils.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/roop/globals.py b/roop/globals.py index b237e8a..9972193 100644 --- a/roop/globals.py +++ b/roop/globals.py @@ -2,6 +2,7 @@ import onnxruntime use_gpu = False all_faces = False +debug = False providers = onnxruntime.get_available_providers() if 'TensorrtExecutionProvider' in providers: diff --git a/roop/utils.py b/roop/utils.py index 479ddb8..0aed392 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -29,31 +29,36 @@ def detect_fps(input_path): pass return 30, 30 +def get_ffmpeg_cmd(): + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' + log_level = '-loglevel error' if roop.globals.debug else '-loglevel quiet' + + return f'ffmpeg {hwaccel_option} {log_level}' def set_fps(input_path, output_path, fps): + basic_command = get_ffmpeg_cmd() input_path, output_path = path(input_path), path(output_path) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}" -loglevel error') + os.system(f'{basic_command} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): + basic_command = get_ffmpeg_cmd() output_dir = path(output_dir) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4" -loglevel error') + os.system(f'{basic_command} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): + basic_command = get_ffmpeg_cmd() input_path, output_dir = path(input_path), path(output_dir) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png" -loglevel error') + os.system(f'{basic_command} -i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file): + basic_command = get_ffmpeg_cmd() video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -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}" -loglevel error') + os.system(f'{basic_command} -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 + "/output.mp4", save_to) if not keep_frames: From 29d65d8e794d3403e1af60a32de92f6644d527a0 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 21:12:25 +0200 Subject: [PATCH 5/7] Centraliza ffmpeg run command --- roop/globals.py | 2 +- roop/utils.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/roop/globals.py b/roop/globals.py index 9972193..d0f621a 100644 --- a/roop/globals.py +++ b/roop/globals.py @@ -2,7 +2,7 @@ import onnxruntime use_gpu = False all_faces = False -debug = False +log_level = 'quiet' providers = onnxruntime.get_available_providers() if 'TensorrtExecutionProvider' in providers: diff --git a/roop/utils.py b/roop/utils.py index 0aed392..a92f7fc 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -29,28 +29,28 @@ def detect_fps(input_path): pass return 30, 30 -def get_ffmpeg_cmd(): +def run_ffmpeg(args): hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - log_level = '-loglevel error' if roop.globals.debug else '-loglevel quiet' + log_level = '-loglevel {roop.globals.log_level}' - return f'ffmpeg {hwaccel_option} {log_level}' + os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') def set_fps(input_path, output_path, fps): basic_command = get_ffmpeg_cmd() input_path, output_path = path(input_path), path(output_path) - os.system(f'{basic_command} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') + run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): basic_command = get_ffmpeg_cmd() output_dir = path(output_dir) - os.system(f'{basic_command} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') + run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): basic_command = get_ffmpeg_cmd() input_path, output_dir = path(input_path), path(output_dir) - os.system(f'{basic_command} -i "{input_path}" "{output_dir}{sep}%04d.png"') + run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file): @@ -58,7 +58,7 @@ def add_audio(output_dir, target_path, video, keep_frames, output_file): video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - os.system(f'{basic_command} -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}"') + run_ffmpeg(f'-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 + "/output.mp4", save_to) if not keep_frames: From 338ea8f50b25991b725d3cde97bcdd728cfc023d Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 21:17:22 +0200 Subject: [PATCH 6/7] Removed unused code --- roop/utils.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/roop/utils.py b/roop/utils.py index a92f7fc..3325864 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -36,25 +36,21 @@ def run_ffmpeg(args): os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') def set_fps(input_path, output_path, fps): - basic_command = get_ffmpeg_cmd() input_path, output_path = path(input_path), path(output_path) run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): - basic_command = get_ffmpeg_cmd() output_dir = path(output_dir) run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): - basic_command = get_ffmpeg_cmd() input_path, output_dir = path(input_path), path(output_dir) run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file): - basic_command = get_ffmpeg_cmd() video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) From 107259474fab77cef10b821832ba820ee1b71d4c Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 21:19:28 +0200 Subject: [PATCH 7/7] Fix log level --- roop/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roop/utils.py b/roop/utils.py index 3325864..670385a 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -31,7 +31,7 @@ def detect_fps(input_path): def run_ffmpeg(args): hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - log_level = '-loglevel {roop.globals.log_level}' + log_level = f'-loglevel {roop.globals.log_level}' os.system(f'ffmpeg {hwaccel_option} {log_level} {args}')