From afb7b00c29584d92f82a776d67048d314f11562a Mon Sep 17 00:00:00 2001 From: John Howe <89397553+timerring@users.noreply.github.com> Date: Thu, 3 Apr 2025 16:38:46 +0800 Subject: [PATCH] refactor: adjust rendering logic --- src/burn/render_command.py | 50 +++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/burn/render_command.py b/src/burn/render_command.py index 1cead21..373c678 100644 --- a/src/burn/render_command.py +++ b/src/burn/render_command.py @@ -14,49 +14,61 @@ def render_command(in_video_path, out_video_path, in_subtitle_font_size, in_subt in_subtitle_margin_v: str, the bottom margin of subtitles """ in_ass_path = in_video_path[:-4] + '.ass' + if not os.path.isfile(in_ass_path): + scan_log.warning("Cannot find danmaku file, return directly") + subprocess.run(['mv', in_video_path, out_video_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + return + in_srt_path = in_video_path[:-4] + '.srt' - - if GPU_EXIST and os.path.isfile(in_srt_path): - if os.path.isfile(in_ass_path): - scan_log.info("Current Mode: GPU with danmaku") - command = [ + if os.path.isfile(in_srt_path): + if GPU_EXIST: + scan_log.info("Current Mode: GPU with subtitles") + gpu_srt_ass_command = [ 'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path, '-c:v', 'h264_nvenc', '-vf', f"subtitles={in_srt_path}:force_style='Fontsize={in_subtitle_font_size},MarginV={in_subtitle_margin_v}',subtitles={in_ass_path}", out_video_path ] try: - result = subprocess.run(command, check=True, capture_output=True, text=True) + result = subprocess.run(gpu_srt_ass_command, check=True, capture_output=True, text=True) scan_log.debug(f"FFmpeg output: {result.stdout}") if result.stderr: scan_log.debug(f"FFmpeg debug: {result.stderr}") except subprocess.CalledProcessError as e: scan_log.error(f"Error: {e.stderr}") - else: - scan_log.info("Current Mode: GPU without danmaku") - command_no_danmaku = [ - 'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path, - '-c:v', 'h264_nvenc', '-vf', f"subtitles={in_srt_path}:force_style='Fontsize={in_subtitle_font_size},MarginV={in_subtitle_margin_v}'", out_video_path + scan_log.info("Current Mode: CPU with subtitles") + cpu_srt_ass_command = [ + 'ffmpeg', '-y', '-i', in_video_path, '-vf', f"subtitles={in_srt_path}:force_style='Fontsize={in_subtitle_font_size},MarginV={in_subtitle_margin_v}',subtitles={in_ass_path}", '-preset', 'ultrafast', out_video_path ] try: - result = subprocess.run(command_no_danmaku, check=True, capture_output=True, text=True) + result = subprocess.run(cpu_srt_ass_command, check=True, capture_output=True, text=True) scan_log.debug(f"FFmpeg output: {result.stdout}") if result.stderr: scan_log.debug(f"FFmpeg debug: {result.stderr}") except subprocess.CalledProcessError as e: scan_log.error(f"Error: {e.stderr}") else: - if os.path.isfile(in_ass_path): - scan_log.info("Current Mode: CPU with danmaku") - command_without_gpu = [ - 'ffmpeg', '-y', '-i', in_video_path, '-vf', f'ass={in_ass_path}', '-preset', 'ultrafast', out_video_path + if GPU_EXIST: + scan_log.info("Current Mode: GPU without subtitles") + gpu_ass_command = [ + 'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path, + '-c:v', 'h264_nvenc', '-vf', f"ass={in_ass_path}", out_video_path ] try: - result = subprocess.run(command_without_gpu, check=True, capture_output=True, text=True) + result = subprocess.run(gpu_ass_command, check=True, capture_output=True, text=True) scan_log.debug(f"FFmpeg output: {result.stdout}") if result.stderr: scan_log.debug(f"FFmpeg debug: {result.stderr}") except subprocess.CalledProcessError as e: scan_log.error(f"Error: {e.stderr}") else: - scan_log.info("Current Mode: CPU without danmaku") - subprocess.run(['mv', in_video_path, out_video_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) \ No newline at end of file + scan_log.info("Current Mode: CPU without subtitles") + cpu_ass_command = [ + 'ffmpeg', '-y', '-i', in_video_path, '-vf', f"ass={in_ass_path}", '-preset', 'ultrafast', out_video_path + ] + try: + result = subprocess.run(cpu_ass_command, check=True, capture_output=True, text=True) + scan_log.debug(f"FFmpeg output: {result.stdout}") + if result.stderr: + scan_log.debug(f"FFmpeg debug: {result.stderr}") + except subprocess.CalledProcessError as e: + scan_log.error(f"Error: {e.stderr}")