From 6daee960292dfc55ab17db932b11923c09847ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 5 Jun 2023 11:57:11 +0200 Subject: [PATCH 1/5] ffmpeg platform-agnostic hardware-acceleration --- roop/utils.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/roop/utils.py b/roop/utils.py index 3ec6872..450a7ed 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -31,8 +31,7 @@ def detect_fps(input_path): def run_ffmpeg(args): - log_level = f'-loglevel {roop.globals.log_level}' - run_command(f'ffmpeg {log_level} {args}') + run_command(f'ffmpeg -hide_banner -hwaccel auto -loglevel {roop.globals.log_level} {args}') def set_fps(input_path, output_path, fps): @@ -41,15 +40,13 @@ def set_fps(input_path, output_path, fps): def create_video(video_name, fps, output_dir): - hwaccel_option = '-hwaccel cuda' if roop.globals.gpu_vendor == 'nvidia' else '' output_dir = path(output_dir) - run_ffmpeg(f'{hwaccel_option} -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): - hwaccel_option = '-hwaccel cuda' if roop.globals.gpu_vendor == 'nvidia' else '' input_path, output_dir = path(input_path), path(output_dir) - run_ffmpeg(f' {hwaccel_option} -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): From c59400f6a6aa5a21836f8245b5e148cd9421da2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 5 Jun 2023 11:58:24 +0200 Subject: [PATCH 2/5] clear CUDA cache after swapping on low VRAM + ffmpeg cuda acceleration, clearing cache prevent cuda out-of-memory error --- roop/core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/roop/core.py b/roop/core.py index c14bc48..b667a97 100755 --- a/roop/core.py +++ b/roop/core.py @@ -209,6 +209,7 @@ def start(preview_callback = None): process_video_multi_cores(args.source_img, args.frame_paths) else: process_video(args.source_img, args.frame_paths) + torch.cuda.empty_cache() status("creating video...") create_video(video_name, exact_fps, output_dir) status("adding audio...") From 36683605207bc7de28ee27c634dcb641aa4bc044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 5 Jun 2023 12:21:37 +0200 Subject: [PATCH 3/5] check torch gpu before clearing cache --- roop/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/roop/core.py b/roop/core.py index b667a97..22113c2 100755 --- a/roop/core.py +++ b/roop/core.py @@ -209,7 +209,8 @@ def start(preview_callback = None): process_video_multi_cores(args.source_img, args.frame_paths) else: process_video(args.source_img, args.frame_paths) - torch.cuda.empty_cache() + if torch.cuda.is_available() and args.gpu_vendor in ['amd', 'nvidia']: + torch.cuda.empty_cache() status("creating video...") create_video(video_name, exact_fps, output_dir) status("adding audio...") From b9476034416850ca69258e7acaa7c257af944ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 5 Jun 2023 12:25:35 +0200 Subject: [PATCH 4/5] torch check nvidia only --- roop/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roop/core.py b/roop/core.py index 22113c2..f392a2a 100755 --- a/roop/core.py +++ b/roop/core.py @@ -209,7 +209,7 @@ def start(preview_callback = None): process_video_multi_cores(args.source_img, args.frame_paths) else: process_video(args.source_img, args.frame_paths) - if torch.cuda.is_available() and args.gpu_vendor in ['amd', 'nvidia']: + if args.gpu_vendor = 'nvidia': torch.cuda.empty_cache() status("creating video...") create_video(video_name, exact_fps, output_dir) From 71af0fac69deeec3612ceb9cabaa04cbd1e381d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Tu=E1=BA=A5n=20Anh?= Date: Mon, 5 Jun 2023 12:29:30 +0200 Subject: [PATCH 5/5] syntax error --- roop/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roop/core.py b/roop/core.py index f392a2a..7a48f96 100755 --- a/roop/core.py +++ b/roop/core.py @@ -209,8 +209,8 @@ def start(preview_callback = None): process_video_multi_cores(args.source_img, args.frame_paths) else: process_video(args.source_img, args.frame_paths) - if args.gpu_vendor = 'nvidia': - torch.cuda.empty_cache() + if args.gpu_vendor == 'nvidia': + torch.cuda.empty_cache() # prevent CUDA OOM when using ffmpeg cuda accel status("creating video...") create_video(video_name, exact_fps, output_dir) status("adding audio...")