Skip to content

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

feat(cli): apply saved WebUI settings as CLI defaults

Jeff Block60126654b440
diff --git a/README-en.md b/README-en.md
index 61cc8d0a..3bb08a11 100644
--- a/README-en.md
+++ b/README-en.md
@@ -347,6 +347,10 @@ command line. The simplest complete generation command is:
uv run python cli.py --video-subject "How AI is changing everyday life"
```
+Subtitle style and voice options you do not pass explicitly fall back to the WebUI
+settings stored under `[ui]` in `config.toml`, so both entry points produce
+consistent videos.
+
For the complete command reference, parameter descriptions, and usage instructions,
run:
diff --git a/README.md b/README.md
index 1bffc636..6d31d20c 100644
--- a/README.md
+++ b/README.md
@@ -358,6 +358,9 @@ python main.py
uv run python cli.py --video-subject "人工智能如何改变日常生活"
```
+未在命令行显式指定的字幕样式和语音参数,会沿用 WebUI 保存在 `config.toml`
+`[ui]` 中的设置,因此两种方式生成的视频保持一致。
+
如需查看完整命令、参数说明和使用方法,可以执行:
```shell
diff --git a/cli.py b/cli.py
index afa1c0f7..dc57ec0a 100644
--- a/cli.py
+++ b/cli.py
@@ -285,10 +285,12 @@ Output and exit status:
audio_group = parser.add_argument_group("voiceover and background music")
audio_group.add_argument(
"--voice-name",
- default=DEFAULT_VOICE_NAME,
+ default=None,
help=(
- "TTS voice identifier; use 'no-voice' for silent output. Provider-specific "
- "identifiers use prefixes such as gemini:, mimo:, elevenlabs:, and chatterbox:"
+ f"TTS voice identifier. Defaults to [ui].voice_name from "
+ f"config.toml, otherwise {DEFAULT_VOICE_NAME}. "
+ "Use 'no-voice' for silent output. Provider-specific identifiers "
+ "use prefixes such as gemini:, mimo:, elevenlabs:, and chatterbox:"
),
)
audio_group.add_argument(
@@ -366,7 +368,8 @@ Output and exit status:
default=None,
help=(
"subtitle font filename inside resource/fonts "
- "(default: STHeitiMedium.ttc)"
+ "(default: [ui].font_name from config.toml; "
+ "STHeitiMedium.ttc when unset)"
),
)
subtitle_group.add_argument(
@@ -394,14 +397,18 @@ Output and exit status:
default=None,
help=(
"subtitle text color in #RRGGBB format; quote the value in shells "
- "that treat # as a comment (default: #FFFFFF)"
+ "that treat # as a comment (default: [ui].text_fore_color from "
+ "config.toml; #FFFFFF when unset)"
),
)
subtitle_group.add_argument(
"--font-size",
type=_positive_int,
default=None,
- help="subtitle font size (default: 60)",
+ help=(
+ "subtitle font size (default: [ui].font_size from config.toml; "
+ "60 when unset)"
+ ),
)
subtitle_group.add_argument(
"--stroke-color",
@@ -423,20 +430,28 @@ Output and exit status:
action=argparse.BooleanOptionalAction,
help=(
"enable subtitle background; use --no-subtitle-background-enabled to "
- "disable (default: enabled)"
+ "disable (default: [ui].subtitle_background_enabled from "
+ "config.toml; disabled when unset)"
),
)
subtitle_group.add_argument(
"--subtitle-background-color",
type=_hex_color,
default=None,
- help="subtitle background color in #RRGGBB format (default: #000000)",
+ help=(
+ "subtitle background color in #RRGGBB format (default: "
+ "[ui].subtitle_background_color from config.toml)"
+ ),
)
subtitle_group.add_argument(
"--rounded-subtitle-background",
default=None,
action=argparse.BooleanOptionalAction,
- help="use a rounded subtitle background (default: disabled)",
+ help=(
+ "use a rounded subtitle background (default: "
+ "[ui].rounded_subtitle_background from config.toml; "
+ "disabled when unset)"
+ ),
)
execution_group = parser.add_argument_group("execution")
@@ -497,11 +512,37 @@ Output and exit status:
return args
+def _ui_config_value(ui_config, key: str, expected_type):
+ """
+ Liest einen in ``[ui]`` gespeicherten WebUI-Wert oder ``None``.
+
+ Die Sektion wird auch von Hand gepflegt, deshalb werden unbrauchbare
+ Eintraege verworfen statt weitergereicht: der Aufrufer faellt dann auf den
+ eingebauten Default zurueck, anstatt einen Traceback oder ein
+ Validierungsfehler in ``VideoParams`` zu erzeugen.
+ """
+ value = ui_config.get(key)
+ if value is None:
+ return None
+ # ``isinstance(True, int)`` ist wahr, deshalb muss bool explizit
+ # ausgeschlossen werden, wenn eine Zahl erwartet wird.
+ if isinstance(value, bool) != (expected_type is bool):
+ return None
+ if not isinstance(value, expected_type):
+ return None
+ if expected_type is str and not value.strip():
+ return None
+ return value
+
+
def build_video_params(args: argparse.Namespace) -> VideoParams:
# 参数帮助和校验不需要加载应用配置。仅在真正构建任务参数时导入模型,
# 避免执行 ``cli.py -h`` 时产生配置初始化日志。
+ from app.config import config
from app.models.schema import MaterialInfo, VideoParams
+ ui_config = config.ui
+
video_terms = args.video_terms
if video_terms:
video_terms = [
@@ -526,7 +567,11 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
"video_materials": video_materials,
"video_count": args.video_count,
"video_aspect": args.video_aspect,
- "voice_name": args.voice_name,
+ "voice_name": (
+ args.voice_name
+ or _ui_config_value(ui_config, "voice_name", str)
+ or DEFAULT_VOICE_NAME
+ ),
"subtitle_enabled": args.subtitle_enabled,
}
@@ -561,6 +606,22 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
if value is not None:
params_kwargs[name] = value
+ # Ohne explizites Flag gelten die in der WebUI gespeicherten Werte. Nur
+ # Felder, die oben nicht schon aus der Kommandozeile gesetzt wurden,
+ # werden ergaenzt; fehlt der Eintrag, bleibt der Default aus VideoParams.
+ ui_defaults = (
+ ("font_name", str),
+ ("text_fore_color", str),
+ ("font_size", int),
+ ("rounded_subtitle_background", bool),
+ )
+ for name, expected_type in ui_defaults:
+ if name in params_kwargs:
+ continue
+ value = _ui_config_value(ui_config, name, expected_type)
+ if value is not None:
+ params_kwargs[name] = value
+
if args.subtitle_background_enabled is False:
params_kwargs["text_background_color"] = False
params_kwargs["rounded_subtitle_background"] = False
@@ -568,6 +629,23 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
params_kwargs["text_background_color"] = args.subtitle_background_color
elif args.subtitle_background_enabled is True:
params_kwargs["text_background_color"] = True
+ else:
+ # Als Flag-Kombination ist "Hintergrund aus" plus Farbe ein
+ # Argumentfehler. Als gespeicherte Einstellung darf dieselbe
+ # Kombination den Lauf nicht abbrechen, sondern nur den Hintergrund
+ # deaktivieren.
+ ui_enabled = _ui_config_value(
+ ui_config, "subtitle_background_enabled", bool
+ )
+ ui_color = _ui_config_value(ui_config, "subtitle_background_color", str)
+ if ui_enabled is False:
+ params_kwargs["text_background_color"] = False
+ if args.rounded_subtitle_background is None:
+ params_kwargs["rounded_subtitle_background"] = False
+ elif ui_color is not None:
+ params_kwargs["text_background_color"] = ui_color
+ elif ui_enabled is True:
+ params_kwargs["text_background_color"] = True
return VideoParams(**params_kwargs)
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
index 9311ad70..91ab79bd 100644
--- a/test/services/test_cli.py
+++ b/test/services/test_cli.py
@@ -12,9 +12,21 @@ from uuid import uuid4
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
import cli
+from app.config import config as app_config
class TestCli(unittest.TestCase):
+ def setUp(self):
+ # ``build_video_params`` uebernimmt Defaults aus der [ui]-Sektion der
+ # lokalen config.toml. Ohne Isolation haengen diese Tests am Zustand
+ # der Entwicklermaschine: ein dort gespeicherter, inzwischen
+ # geloeschter Font laesst die Font-Pruefung in ``prepare_cli_files``
+ # anschlagen und bringt thematisch unabhaengige Tests mit
+ # irrefuehrender Meldung zum Fehlschlagen.
+ ui_patch = patch.dict(app_config.ui, {}, clear=True)
+ ui_patch.start()
+ self.addCleanup(ui_patch.stop)
+
def test_default_voice_is_valid_edge_tts_voice(self):
args = cli.parse_args(["--video-subject", "测试主题"])
params = cli.build_video_params(args)
@@ -595,5 +607,117 @@ class TestCli(unittest.TestCase):
self.assertEqual(result.stderr, "")
+class TestCliUiDefaults(unittest.TestCase):
+ """
+ CLI-Defaults sollen der WebUI folgen: explizites Flag, dann [ui] aus
+ config.toml, dann der eingebaute Default.
+ """
+
+ UI_CONFIG = {
+ "font_name": "MicrosoftYaHeiBold.ttc",
+ "text_fore_color": "#123456",
+ "font_size": 48,
+ "subtitle_background_enabled": True,
+ "subtitle_background_color": "#654321",
+ "rounded_subtitle_background": True,
+ "voice_name": "gemini:Puck-Male",
+ }
+
+ def test_ui_config_supplies_subtitle_defaults(self):
+ args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(app_config.ui, self.UI_CONFIG, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.font_name, "MicrosoftYaHeiBold.ttc")
+ self.assertEqual(params.text_fore_color, "#123456")
+ self.assertEqual(params.font_size, 48)
+ self.assertEqual(params.text_background_color, "#654321")
+ self.assertTrue(params.rounded_subtitle_background)
+
+ def test_ui_config_supplies_voice_name(self):
+ args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(app_config.ui, self.UI_CONFIG, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.voice_name, "gemini:Puck-Male")
+
+ def test_cli_flags_take_precedence_over_ui_config(self):
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--font-name",
+ "STHeitiLight.ttc",
+ "--text-fore-color",
+ "#AABBCC",
+ "--font-size",
+ "72",
+ "--voice-name",
+ "no-voice",
+ "--no-rounded-subtitle-background",
+ ]
+ )
+
+ with patch.dict(app_config.ui, self.UI_CONFIG, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.font_name, "STHeitiLight.ttc")
+ self.assertEqual(params.text_fore_color, "#AABBCC")
+ self.assertEqual(params.font_size, 72)
+ self.assertEqual(params.voice_name, "no-voice")
+ self.assertFalse(params.rounded_subtitle_background)
+
+ def test_builtin_defaults_apply_when_ui_config_is_empty(self):
+ args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(app_config.ui, {}, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.font_name, "STHeitiMedium.ttc")
+ self.assertEqual(params.text_fore_color, "#FFFFFF")
+ self.assertEqual(params.font_size, 60)
+ self.assertFalse(params.text_background_color)
+ self.assertFalse(params.rounded_subtitle_background)
+
+ def test_ui_config_can_disable_subtitle_background(self):
+ """
+ Eine widersprüchliche [ui]-Sektion darf die CLI nicht abbrechen lassen:
+ `--no-subtitle-background-enabled` plus Farbe ist als Flag-Kombination
+ ein Argumentfehler, als gespeicherte Einstellung aber nur ein
+ deaktivierter Hintergrund.
+ """
+ ui_config = dict(self.UI_CONFIG)
+ ui_config["subtitle_background_enabled"] = False
+
+ args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(app_config.ui, ui_config, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertFalse(params.text_background_color)
+ self.assertFalse(params.rounded_subtitle_background)
+
+ def test_unusable_ui_config_values_fall_back_to_builtin_defaults(self):
+ """Eine beschädigte config.toml darf keinen Traceback auslösen."""
+ ui_config = {
+ "font_size": "sechzig",
+ "text_fore_color": 42,
+ "font_name": "",
+ "voice_name": None,
+ }
+
+ args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(app_config.ui, ui_config, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.font_size, 60)
+ self.assertEqual(params.text_fore_color, "#FFFFFF")
+ self.assertEqual(params.font_name, "STHeitiMedium.ttc")
+ self.assertEqual(params.voice_name, "zh-CN-XiaoxiaoNeural-Female")
+
+
if __name__ == "__main__":
unittest.main()