Skip to content

Commit aa03e98

Browse files
authored
refactor: adjust rendering logic (#253)
1 parent 9e43dea commit aa03e98

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/burn/render_command.py

+31-19
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,61 @@ def render_command(in_video_path, out_video_path, in_subtitle_font_size, in_subt
1414
in_subtitle_margin_v: str, the bottom margin of subtitles
1515
"""
1616
in_ass_path = in_video_path[:-4] + '.ass'
17+
if not os.path.isfile(in_ass_path):
18+
scan_log.warning("Cannot find danmaku file, return directly")
19+
subprocess.run(['mv', in_video_path, out_video_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
20+
return
21+
1722
in_srt_path = in_video_path[:-4] + '.srt'
18-
19-
if GPU_EXIST and os.path.isfile(in_srt_path):
20-
if os.path.isfile(in_ass_path):
21-
scan_log.info("Current Mode: GPU with danmaku")
22-
command = [
23+
if os.path.isfile(in_srt_path):
24+
if GPU_EXIST:
25+
scan_log.info("Current Mode: GPU with subtitles")
26+
gpu_srt_ass_command = [
2327
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
2428
'-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
2529
]
2630
try:
27-
result = subprocess.run(command, check=True, capture_output=True, text=True)
31+
result = subprocess.run(gpu_srt_ass_command, check=True, capture_output=True, text=True)
2832
scan_log.debug(f"FFmpeg output: {result.stdout}")
2933
if result.stderr:
3034
scan_log.debug(f"FFmpeg debug: {result.stderr}")
3135
except subprocess.CalledProcessError as e:
3236
scan_log.error(f"Error: {e.stderr}")
33-
3437
else:
35-
scan_log.info("Current Mode: GPU without danmaku")
36-
command_no_danmaku = [
37-
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
38-
'-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
38+
scan_log.info("Current Mode: CPU with subtitles")
39+
cpu_srt_ass_command = [
40+
'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
3941
]
4042
try:
41-
result = subprocess.run(command_no_danmaku, check=True, capture_output=True, text=True)
43+
result = subprocess.run(cpu_srt_ass_command, check=True, capture_output=True, text=True)
4244
scan_log.debug(f"FFmpeg output: {result.stdout}")
4345
if result.stderr:
4446
scan_log.debug(f"FFmpeg debug: {result.stderr}")
4547
except subprocess.CalledProcessError as e:
4648
scan_log.error(f"Error: {e.stderr}")
4749
else:
48-
if os.path.isfile(in_ass_path):
49-
scan_log.info("Current Mode: CPU with danmaku")
50-
command_without_gpu = [
51-
'ffmpeg', '-y', '-i', in_video_path, '-vf', f'ass={in_ass_path}', '-preset', 'ultrafast', out_video_path
50+
if GPU_EXIST:
51+
scan_log.info("Current Mode: GPU without subtitles")
52+
gpu_ass_command = [
53+
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
54+
'-c:v', 'h264_nvenc', '-vf', f"ass={in_ass_path}", out_video_path
5255
]
5356
try:
54-
result = subprocess.run(command_without_gpu, check=True, capture_output=True, text=True)
57+
result = subprocess.run(gpu_ass_command, check=True, capture_output=True, text=True)
5558
scan_log.debug(f"FFmpeg output: {result.stdout}")
5659
if result.stderr:
5760
scan_log.debug(f"FFmpeg debug: {result.stderr}")
5861
except subprocess.CalledProcessError as e:
5962
scan_log.error(f"Error: {e.stderr}")
6063
else:
61-
scan_log.info("Current Mode: CPU without danmaku")
62-
subprocess.run(['mv', in_video_path, out_video_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
64+
scan_log.info("Current Mode: CPU without subtitles")
65+
cpu_ass_command = [
66+
'ffmpeg', '-y', '-i', in_video_path, '-vf', f"ass={in_ass_path}", '-preset', 'ultrafast', out_video_path
67+
]
68+
try:
69+
result = subprocess.run(cpu_ass_command, check=True, capture_output=True, text=True)
70+
scan_log.debug(f"FFmpeg output: {result.stdout}")
71+
if result.stderr:
72+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
73+
except subprocess.CalledProcessError as e:
74+
scan_log.error(f"Error: {e.stderr}")

0 commit comments

Comments
 (0)