Skip to content

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

fix(cli): allow the two-thirds subtitle position

c020627a076facc6c0d
diff --git a/cli.py b/cli.py
index b2d2f203..bccf812c 100644
--- a/cli.py
+++ b/cli.py
@@ -116,9 +116,10 @@ def _hex_color(value: str) -> str:
def _subtitle_position(value: str) -> str:
"""校验保存的字幕位置,取值范围与命令行参数保持一致。"""
- if value not in ("top", "center", "bottom", "custom"):
+ if value not in _SUBTITLE_POSITION_VALUES:
raise argparse.ArgumentTypeError(
- f"subtitle-position must be one of: top, center, bottom, custom, got {value!r}"
+ "subtitle-position must be one of: "
+ f"{', '.join(_SUBTITLE_POSITION_VALUES)}, got {value!r}"
)
return value
@@ -144,6 +145,17 @@ _TRANSITION_MODE_VALUES = {
"zoom-out": "ZoomOut",
}
+# 单任务 argparse 和批量清单必须共享同一份字幕位置取值,避免再次出现
+# 「WebUI 能保存、CLI 却拒绝」的落差。取值需与 app/services/video.py 的
+# 渲染分支及 webui/Main.py 的下拉框保持一致。
+_SUBTITLE_POSITION_VALUES = (
+ "top",
+ "center",
+ "bottom",
+ "two_thirds_bottom",
+ "custom",
+)
+
def _transition_mode(value: str) -> str | None:
normalized = value.strip().lower()
@@ -479,7 +491,7 @@ Batch manifests:
)
subtitle_group.add_argument(
"--subtitle-position",
- choices=["top", "center", "bottom", "custom"],
+ choices=_SUBTITLE_POSITION_VALUES,
default=None,
help=(
"subtitle vertical position (default: [ui].subtitle_position from "
@@ -1073,9 +1085,10 @@ def _validate_batch_task_params(
if stop_at == "subtitle" and not params.subtitle_enabled:
raise ValueError("stop_at=subtitle cannot be combined with disabled subtitles")
- if params.subtitle_position not in {"top", "center", "bottom", "custom"}:
+ if params.subtitle_position not in _SUBTITLE_POSITION_VALUES:
raise ValueError(
- "subtitle_position must be one of: top, center, bottom, custom"
+ "subtitle_position must be one of: "
+ + ", ".join(_SUBTITLE_POSITION_VALUES)
)
if custom_position_is_explicit and params.subtitle_position != "custom":
raise ValueError("custom_position requires subtitle_position=custom")
diff --git a/config.example.toml b/config.example.toml
index 5cca1b22..5ed53407 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -637,7 +637,7 @@ open_task_folder_on_completion = true
# text_fore_color = "#FFFFFF"
# stroke_color = "#000000"
# stroke_width = 1.5
-# subtitle_position = "bottom" # "top", "center", "bottom", or "custom"
+# subtitle_position = "bottom" # "top", "center", "bottom", "two_thirds_bottom", or "custom"
# custom_position = 70.0 # Percentage from the top when using "custom"
# subtitle_background_enabled = false
# subtitle_background_color = "#000000"
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
index 8bc55ffd..e234304f 100644
--- a/test/services/test_cli.py
+++ b/test/services/test_cli.py
@@ -1,3 +1,4 @@
+import ast
import io
import json
import os
@@ -1904,6 +1905,57 @@ class TestCliUiDefaults(unittest.TestCase):
self.assertEqual(params.subtitle_position, "custom")
self.assertEqual(params.custom_position, 42.5)
+ def test_two_thirds_bottom_subtitle_position_is_supported(self):
+ """
+ WebUI 的「距底部 2/3」会把 two_thirds_bottom 写进 config.toml,
+ app/services/video.py 也按该取值渲染。CLI 此前只认识另外四个位置,
+ 导致同一个 config.toml 在两个入口产出不同画面。
+ """
+ explicit_params = cli.build_video_params(
+ cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--subtitle-position",
+ "two_thirds_bottom",
+ ]
+ )
+ )
+ self.assertEqual(explicit_params.subtitle_position, "two_thirds_bottom")
+
+ saved_args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(
+ app_config.ui, {"subtitle_position": "two_thirds_bottom"}, clear=True
+ ):
+ saved_params = cli.build_video_params(saved_args)
+
+ self.assertEqual(saved_params.subtitle_position, "two_thirds_bottom")
+
+ def test_subtitle_positions_stay_aligned_with_the_webui(self):
+ """
+ 两个入口共享同一份 config.toml,因此 CLI 必须接受 WebUI 能保存的每一个
+ 字幕位置,否则保存值会在命令行入口被静默换成 bottom。
+ """
+ source = (Path(__file__).parent.parent.parent / "webui" / "Main.py").read_text(
+ encoding="utf-8"
+ )
+ webui_positions = set()
+ for node in ast.walk(ast.parse(source)):
+ if not isinstance(node, ast.Assign) or not any(
+ isinstance(target, ast.Name) and target.id == "subtitle_positions"
+ for target in node.targets
+ ):
+ continue
+ webui_positions = {
+ ast.literal_eval(element.elts[1]) for element in node.value.elts
+ }
+
+ self.assertTrue(webui_positions)
+ self.assertEqual(
+ sorted(webui_positions - set(cli._SUBTITLE_POSITION_VALUES)), []
+ )
+
def test_unusable_saved_subtitle_position_falls_back(self):
"""超出取值范围的保存位置回退到内置默认值。"""
ui_config = {"subtitle_position": "diagonal", "custom_position": 150.0}