From 9efbc1a2c67d476e2511dcc7034e436a2f4bdedf Mon Sep 17 00:00:00 2001 From: henryruhs Date: Tue, 15 Aug 2023 22:14:27 +0200 Subject: [PATCH] Add video settings component to UI --- roop/core.py | 4 +-- roop/uis/__components__/output.py | 25 +++++++------ roop/uis/__components__/output_settings.py | 42 ++++++++++++++++++++++ roop/uis/__components__/temp_frame.py | 5 ++- roop/uis/__layouts__/default.py | 6 ++-- roop/utilities.py | 4 +-- 6 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 roop/uis/__components__/output_settings.py diff --git a/roop/core.py b/roop/core.py index 2fcbcbb64..784ead0d9 100755 --- a/roop/core.py +++ b/roop/core.py @@ -47,9 +47,9 @@ def parse_args() -> None: program.add_argument('--trim-frame-start', help='start frame use for extraction', dest='trim_frame_start', type=int) program.add_argument('--trim-frame-end', help='end frame use for extraction', dest='trim_frame_end', type=int) program.add_argument('--temp-frame-format', help='image format used for frame extraction', dest='temp_frame_format', default='jpg', choices=['jpg', 'png']) - program.add_argument('--temp-frame-quality', help='image quality used for frame extraction', dest='temp_frame_quality', type=int, default=0, choices=range(101), metavar='[0-100]') + program.add_argument('--temp-frame-quality', help='image quality used for frame extraction', dest='temp_frame_quality', type=int, default=100, choices=range(101), metavar='[0-100]') program.add_argument('--output-video-encoder', help='encoder used for the output video', dest='output_video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc']) - program.add_argument('--output-video-quality', help='quality used for the output video', dest='output_video_quality', type=int, default=35, choices=range(101), metavar='[0-100]') + program.add_argument('--output-video-quality', help='quality used for the output video', dest='output_video_quality', type=int, default=90, choices=range(101), metavar='[0-100]') program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int) program.add_argument('--execution-providers', help='list of available execution providers (choices: cpu, ...)', dest='execution_providers', default=['cpu'], choices=suggest_execution_providers(), nargs='+') program.add_argument('--execution-thread-count', help='number of execution threads', dest='execution_thread_count', type=int, default=suggest_execution_thread_count()) diff --git a/roop/uis/__components__/output.py b/roop/uis/__components__/output.py index fa0aef157..ce782af62 100644 --- a/roop/uis/__components__/output.py +++ b/roop/uis/__components__/output.py @@ -18,19 +18,18 @@ def render() -> None: global OUTPUT_IMAGE global OUTPUT_VIDEO - with gradio.Column(): - with gradio.Row(): - START_BUTTON = gradio.Button('Start') - CLEAR_BUTTON = gradio.Button('Clear') - with gradio.Row(): - with gradio.Box(): - OUTPUT_IMAGE = gradio.Image( - label='OUTPUT' - ) - OUTPUT_VIDEO = gradio.Video( - label='OUTPUT', - visible=False - ) + with gradio.Row(): + with gradio.Box(): + OUTPUT_IMAGE = gradio.Image( + label='OUTPUT', + visible=False + ) + OUTPUT_VIDEO = gradio.Video( + label='OUTPUT' + ) + with gradio.Row(): + START_BUTTON = gradio.Button('Start') + CLEAR_BUTTON = gradio.Button('Clear') def listen() -> None: diff --git a/roop/uis/__components__/output_settings.py b/roop/uis/__components__/output_settings.py new file mode 100644 index 000000000..abc42a77d --- /dev/null +++ b/roop/uis/__components__/output_settings.py @@ -0,0 +1,42 @@ +from typing import Optional +import gradio + +import roop.globals +from roop.typing import OutputVideoEncoder + +from roop.uis.typing import Update + +OUTPUT_VIDEO_ENCODER_DROPDOWN: Optional[gradio.Dropdown] = None +OUTPUT_VIDEO_QUALITY_SLIDER: Optional[gradio.Slider] = None + + +def render() -> None: + global OUTPUT_VIDEO_ENCODER_DROPDOWN + global OUTPUT_VIDEO_QUALITY_SLIDER + + with gradio.Box(): + OUTPUT_VIDEO_ENCODER_DROPDOWN = gradio.Dropdown( + label='OUTPUT VIDEO ENCODER', + choices=['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc'], + value=roop.globals.output_video_encoder + ) + OUTPUT_VIDEO_QUALITY_SLIDER = gradio.Slider( + label='OUTPUT VIDEO QUALITY', + value=roop.globals.output_video_quality, + step=1 + ) + + +def listen() -> None: + OUTPUT_VIDEO_ENCODER_DROPDOWN.select(update_output_video_encoder, inputs=OUTPUT_VIDEO_ENCODER_DROPDOWN, outputs=OUTPUT_VIDEO_ENCODER_DROPDOWN) + OUTPUT_VIDEO_QUALITY_SLIDER.change(update_output_video_quality, inputs=OUTPUT_VIDEO_QUALITY_SLIDER, outputs=OUTPUT_VIDEO_QUALITY_SLIDER) + + +def update_output_video_encoder(output_video_encoder: OutputVideoEncoder) -> Update: + roop.globals.output_video_encoder = output_video_encoder + return gradio.update(value=output_video_encoder) + + +def update_output_video_quality(output_video_quality: int) -> Update: + roop.globals.output_video_quality = output_video_quality + return gradio.update(value=output_video_quality) diff --git a/roop/uis/__components__/temp_frame.py b/roop/uis/__components__/temp_frame.py index 6c2d35793..d3933e8ab 100644 --- a/roop/uis/__components__/temp_frame.py +++ b/roop/uis/__components__/temp_frame.py @@ -1,10 +1,9 @@ -from typing import List, Optional +from typing import Optional import gradio import roop.globals -from roop.processors.frame.core import list_frame_processors_names, load_frame_processor_module, clear_frame_processors_modules from roop.typing import TempFrameFormat -from roop.uis import core as ui + from roop.uis.typing import Update TEMP_FRAME_FORMAT_DROPDOWN: Optional[gradio.Dropdown] = None diff --git a/roop/uis/__layouts__/default.py b/roop/uis/__layouts__/default.py index 5e46e5bb3..37d7ef985 100644 --- a/roop/uis/__layouts__/default.py +++ b/roop/uis/__layouts__/default.py @@ -1,6 +1,6 @@ import gradio -from roop.uis.__components__ import processor, execution, temp_frame, settings, source, target, preview, trim_frame, face_analyser, face_selector, output +from roop.uis.__components__ import processor, execution, temp_frame, settings, source, target, preview, trim_frame, face_analyser, face_selector, output_settings, output def render() -> gradio.Blocks: @@ -11,9 +11,10 @@ def render() -> gradio.Blocks: execution.render() temp_frame.render() settings.render() - with gradio.Column(scale=1): + with gradio.Column(scale=2): source.render() target.render() + output_settings.render() output.render() with gradio.Column(scale=3): preview.render() @@ -34,4 +35,5 @@ def listen() -> None: trim_frame.listen() face_selector.listen() face_analyser.listen() + output_settings.listen() output.listen() diff --git a/roop/utilities.py b/roop/utilities.py index b64152936..df6fb002b 100644 --- a/roop/utilities.py +++ b/roop/utilities.py @@ -42,7 +42,7 @@ def detect_fps(target_path: str) -> float: def extract_frames(target_path: str, fps: float = 30) -> bool: temp_directory_path = get_temp_directory_path(target_path) - temp_frame_quality = roop.globals.temp_frame_quality * 31 // 100 + temp_frame_quality = round(31 - (roop.globals.temp_frame_quality * 0.31)) trim_frame_start = roop.globals.trim_frame_start trim_frame_end = roop.globals.trim_frame_end commands = ['-hwaccel', 'auto', '-i', target_path, '-q:v', str(temp_frame_quality), '-pix_fmt', 'rgb24'] @@ -61,7 +61,7 @@ def extract_frames(target_path: str, fps: float = 30) -> bool: def create_video(target_path: str, fps: float = 30) -> bool: temp_output_path = get_temp_output_path(target_path) temp_directory_path = get_temp_directory_path(target_path) - output_video_quality = (roop.globals.output_video_quality + 1) * 51 // 100 + output_video_quality = round(51 - (roop.globals.output_video_quality * 0.5)) commands = ['-hwaccel', 'auto', '-r', str(fps), '-i', os.path.join(temp_directory_path, '%04d.' + roop.globals.temp_frame_format), '-c:v', roop.globals.output_video_encoder] if roop.globals.output_video_encoder in ['libx264', 'libx265', 'libvpx']: commands.extend(['-crf', str(output_video_quality)])