Merge pull request #298 from joskfg/feature/add-ffmpeg-gpu-acceleration

Add FFMpeg hardware acceleration
This commit is contained in:
Henry Ruhs 2023-06-02 21:43:16 +02:00 committed by GitHub
commit 9bc0f54fa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

5
roop/core.py Executable file → Normal file
View File

@ -75,6 +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:
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
@ -99,7 +100,7 @@ def start_processing():
frame_paths = args["frame_paths"]
n = len(frame_paths) // (args['cores_count'])
# single thread
if args['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
@ -289,4 +290,4 @@ def run():
status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436")
status_label.place(x=10,y=640,width=580,height=30)
window.mainloop()
window.mainloop()

View File

@ -2,6 +2,7 @@ import onnxruntime
use_gpu = False
all_faces = False
log_level = 'quiet'
providers = onnxruntime.get_available_providers()
if 'TensorrtExecutionProvider' in providers:

View File

@ -1,5 +1,6 @@
import os
import shutil
import roop.globals
sep = "/"
if os.name == "nt":
@ -28,27 +29,32 @@ def detect_fps(input_path):
pass
return 30, 30
def run_ffmpeg(args):
hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else ''
log_level = f'-loglevel {roop.globals.log_level}'
os.system(f'ffmpeg {hwaccel_option} {log_level} {args}')
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}" -loglevel error')
run_ffmpeg(f'-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}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4" -loglevel error')
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):
input_path, output_dir = path(input_path), path(output_dir)
os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png" -loglevel error')
run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"')
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'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}" -loglevel error')
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: