Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

feat(cli): expose the subtitle display mode and animation

c020627b00bde601f11
diff --git a/cli.py b/cli.py
index b32ef14c..3f21647e 100644
--- a/cli.py
+++ b/cli.py
@@ -141,6 +141,26 @@ def _subtitle_position(value: str) -> str:
return value
+def _subtitle_display_mode(value: str) -> str:
+ """校验保存的字幕展示模式,取值范围与命令行参数保持一致。"""
+ if value not in _SUBTITLE_DISPLAY_MODE_VALUES:
+ raise argparse.ArgumentTypeError(
+ "subtitle-display-mode must be one of: "
+ f"{', '.join(_SUBTITLE_DISPLAY_MODE_VALUES)}, got {value!r}"
+ )
+ return value
+
+
+def _subtitle_animation(value: str) -> str:
+ """校验保存的字幕动画,取值范围与命令行参数保持一致。"""
+ if value not in _SUBTITLE_ANIMATION_VALUES:
+ raise argparse.ArgumentTypeError(
+ "subtitle-animation must be one of: "
+ f"{', '.join(_SUBTITLE_ANIMATION_VALUES)}, got {value!r}"
+ )
+ return value
+
+
def _task_id(value: str) -> str:
"""CLI 自定义任务标识只接受 UUID,避免该值被解释为文件系统路径。"""
try:
@@ -173,6 +193,12 @@ _SUBTITLE_POSITION_VALUES = (
"custom",
)
+# 字幕展示模式与入场动画由 WebUI 保存进 [ui]。两者随「逐词字幕 + 弹跳动画」
+# 加入时只改了 WebUI 与模型字段默认值,命令行一直缺少开关。取值需与
+# app/models/schema.py 的 _SUBTITLE_DISPLAY_MODES / _SUBTITLE_ANIMATIONS 一致。
+_SUBTITLE_DISPLAY_MODE_VALUES = ("sentence", "word_by_word")
+_SUBTITLE_ANIMATION_VALUES = ("none", "pop_spring")
+
def _transition_mode(value: str) -> str | None:
normalized = value.strip().lower()
@@ -541,6 +567,24 @@ Batch manifests:
"config.toml; bottom when unset)"
),
)
+ subtitle_group.add_argument(
+ "--subtitle-display-mode",
+ choices=_SUBTITLE_DISPLAY_MODE_VALUES,
+ default=None,
+ help=(
+ "subtitle timing: sentence by sentence, or one word at a time "
+ "(default: [ui].subtitle_display_mode from config.toml; sentence)"
+ ),
+ )
+ subtitle_group.add_argument(
+ "--subtitle-animation",
+ choices=_SUBTITLE_ANIMATION_VALUES,
+ default=None,
+ help=(
+ "subtitle entrance animation (default: [ui].subtitle_animation from "
+ "config.toml; none when unset)"
+ ),
+ )
subtitle_group.add_argument(
"--custom-position",
type=_percent_position,
@@ -873,6 +917,8 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
"video_music_prompt",
"font_name",
"subtitle_position",
+ "subtitle_display_mode",
+ "subtitle_animation",
"custom_position",
"text_fore_color",
"font_size",
@@ -894,6 +940,8 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
("video_clip_speed", float, _clip_speed),
("video_fit_mode", str, _video_fit_mode),
("font_name", str, None),
+ ("subtitle_display_mode", str, _subtitle_display_mode),
+ ("subtitle_animation", str, _subtitle_animation),
("text_fore_color", str, _hex_color),
("font_size", int, _positive_int),
("rounded_subtitle_background", bool, None),
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
index 1fb890c8..c4598b8b 100644
--- a/test/services/test_cli.py
+++ b/test/services/test_cli.py
@@ -15,6 +15,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent))
import cli
from app.config import config as app_config
+from app.models import schema as app_schema
from app.models.schema import VideoTransitionMode
@@ -2040,6 +2041,57 @@ class TestCliUiDefaults(unittest.TestCase):
sorted(webui_positions - set(cli._SUBTITLE_POSITION_VALUES)), []
)
+ def test_subtitle_style_options_follow_the_flag_then_the_saved_value(self):
+ """
+ 展示模式与入场动画随「逐词字幕 + 弹跳动画」加入,当时只改了 WebUI 与模型
+ 字段默认值:命令行既没有开关,也不显式读取 [ui] 保存值。
+ """
+ for flag, field, value in (
+ ("--subtitle-display-mode", "subtitle_display_mode", "word_by_word"),
+ ("--subtitle-animation", "subtitle_animation", "pop_spring"),
+ ):
+ with self.subTest(field=field):
+ explicit = cli.build_video_params(
+ cli.parse_args(["--video-subject", "test", flag, value])
+ )
+ self.assertEqual(getattr(explicit, field), value)
+
+ args = cli.parse_args(["--video-subject", "test"])
+ with patch.dict(app_config.ui, {field: value}, clear=True):
+ saved = cli.build_video_params(args)
+ self.assertEqual(getattr(saved, field), value)
+
+ def test_subtitle_style_values_stay_aligned_with_the_model_and_the_webui(self):
+ """
+ 取值必须与 app/models/schema.py 的权威枚举一致,并覆盖 WebUI 下拉框能保存
+ 的每一个值;将来新增模式时这里会先失败,避免再次出现「WebUI 支持、命令行
+ 不支持」的落差。
+ """
+ for cli_values, model_values in (
+ (cli._SUBTITLE_DISPLAY_MODE_VALUES, app_schema._SUBTITLE_DISPLAY_MODES),
+ (cli._SUBTITLE_ANIMATION_VALUES, app_schema._SUBTITLE_ANIMATIONS),
+ ):
+ self.assertEqual(sorted(cli_values), sorted(model_values))
+
+ source = (Path(__file__).parent.parent.parent / "webui" / "Main.py").read_text(
+ encoding="utf-8"
+ )
+ for name, cli_values in (
+ ("subtitle_display_modes", cli._SUBTITLE_DISPLAY_MODE_VALUES),
+ ("subtitle_animations", cli._SUBTITLE_ANIMATION_VALUES),
+ ):
+ webui_values = set()
+ for node in ast.walk(ast.parse(source)):
+ if any(
+ isinstance(target, ast.Name) and target.id == name
+ for target in getattr(node, "targets", [])
+ ):
+ webui_values = {
+ ast.literal_eval(element.elts[1])
+ for element in node.value.elts
+ }
+ self.assertEqual(sorted(webui_values - set(cli_values)), [], name)
+
def test_unusable_saved_subtitle_position_falls_back(self):
"""超出取值范围的保存位置回退到内置默认值。"""
ui_config = {"subtitle_position": "diagonal", "custom_position": 150.0}