Skip to content

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

fix(cli): honour saved no-voice and background colour settings

Jeff Block5e76c9992f1e
diff --git a/cli.py b/cli.py
index dc57ec0a..a1d539e0 100644
--- a/cli.py
+++ b/cli.py
@@ -16,6 +16,12 @@ if TYPE_CHECKING:
DEFAULT_VOICE_NAME = "zh-CN-XiaoxiaoNeural-Female"
+# 对应 webui/Main.py 的 VOICE_MODE_NONE 和 VOICE_MODE_UPLOAD。两端目前没有
+# 共享这些常量,因此在这里保留字面值并注明来源。
+UI_VOICE_MODE_NONE = "none"
+UI_VOICE_MODE_UPLOAD = "upload"
+# 这两种保存的配音方式都表示不要自动配音。
+UI_VOICE_MODES_WITHOUT_TTS = frozenset({UI_VOICE_MODE_NONE, UI_VOICE_MODE_UPLOAD})
_PIPELINE_STAGES = ("script", "terms", "audio", "subtitle", "materials", "video")
_CUSTOM_AUDIO_EXTENSIONS = {".mp3", ".wav", ".m4a", ".aac", ".flac", ".ogg"}
@@ -288,7 +294,9 @@ Output and exit status:
default=None,
help=(
f"TTS voice identifier. Defaults to [ui].voice_name from "
- f"config.toml, otherwise {DEFAULT_VOICE_NAME}. "
+ f"config.toml, otherwise {DEFAULT_VOICE_NAME}. A saved "
+ "[ui].voice_mode of 'none' or 'upload' resolves to no-voice "
+ "instead, unless this option is given. "
"Use 'no-voice' for silent output. Provider-specific identifiers "
"use prefixes such as gemini:, mimo:, elevenlabs:, and chatterbox:"
),
@@ -514,18 +522,16 @@ Output and exit status:
def _ui_config_value(ui_config, key: str, expected_type):
"""
- Liest einen in ``[ui]`` gespeicherten WebUI-Wert oder ``None``.
+ 读取 ``[ui]`` 中保存的 WebUI 设置,取不到可用值时返回 ``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.
+ 该配置段也可能被手工编辑,因此这里直接丢弃类型不符的条目,让调用方回退
+ 到内置默认值,而不是把脏数据继续传下去,触发 traceback 或 ``VideoParams``
+ 的校验错误。
"""
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.
+ # ``isinstance(True, int)`` 为真,所以在期望数字时必须显式排除 bool。
if isinstance(value, bool) != (expected_type is bool):
return None
if not isinstance(value, expected_type):
@@ -535,6 +541,26 @@ def _ui_config_value(ui_config, key: str, expected_type):
return value
+def _resolve_voice_name(args: argparse.Namespace, ui_config) -> str:
+ """
+ 按优先级解析音色:命令行 > WebUI 保存的配音方式和音色 > 内置默认值。
+
+ WebUI 把“无配音”保存为独立的 voice_mode,同时保留用户上一次真正选择的
+ 音色,以便切回自动配音后恢复。所以这里不能只读 voice_name,否则保存的
+ 无配音状态会被忽略,并可能重新触发付费供应商请求。
+ """
+ from app.services.voice import NO_VOICE_NAME
+
+ if args.voice_name:
+ return args.voice_name
+ # 无配音和上传自备音频都表示用户不想要自动配音。上传模式的文件路径不会
+ # 写入 [ui],CLI 无法复现该上传;此时沿用保存的音色会静默触发付费 TTS
+ # 请求,因此两种模式都映射为 no-voice,需要配音时显式传 --voice-name。
+ if _ui_config_value(ui_config, "voice_mode", str) in UI_VOICE_MODES_WITHOUT_TTS:
+ return NO_VOICE_NAME
+ return _ui_config_value(ui_config, "voice_name", str) or DEFAULT_VOICE_NAME
+
+
def build_video_params(args: argparse.Namespace) -> VideoParams:
# 参数帮助和校验不需要加载应用配置。仅在真正构建任务参数时导入模型,
# 避免执行 ``cli.py -h`` 时产生配置初始化日志。
@@ -567,11 +593,7 @@ 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
- or _ui_config_value(ui_config, "voice_name", str)
- or DEFAULT_VOICE_NAME
- ),
+ "voice_name": _resolve_voice_name(args, ui_config),
"subtitle_enabled": args.subtitle_enabled,
}
@@ -606,9 +628,8 @@ 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.
+ # 没有显式传入命令行参数时,使用 WebUI 保存的值。只补充上面尚未由命令行
+ # 设置的字段;若保存值缺失,则继续沿用 VideoParams 的默认值。
ui_defaults = (
("font_name", str),
("text_fore_color", str),
@@ -628,12 +649,14 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
elif args.subtitle_background_color is not None:
params_kwargs["text_background_color"] = args.subtitle_background_color
elif args.subtitle_background_enabled is True:
- params_kwargs["text_background_color"] = True
+ # 用户只开启了背景而没有覆盖颜色,因此优先沿用 WebUI 保存的颜色,
+ # 只有在没有可用保存值时才回退到默认背景。
+ params_kwargs["text_background_color"] = (
+ _ui_config_value(ui_config, "subtitle_background_color", str) or 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
)
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
index 91ab79bd..8ae34d80 100644
--- a/test/services/test_cli.py
+++ b/test/services/test_cli.py
@@ -17,12 +17,10 @@ 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.
+ # ``build_video_params`` 会读取本地 config.toml 的 [ui] 段作为默认值。
+ # 不做隔离时,这些测试就依赖开发机的状态:配置里保存的字体一旦已被
+ # 删除,``prepare_cli_files`` 的字体校验就会触发,让与此无关的测试以
+ # 误导性的报错失败。
ui_patch = patch.dict(app_config.ui, {}, clear=True)
ui_patch.start()
self.addCleanup(ui_patch.stop)
@@ -609,8 +607,8 @@ class TestCli(unittest.TestCase):
class TestCliUiDefaults(unittest.TestCase):
"""
- CLI-Defaults sollen der WebUI folgen: explizites Flag, dann [ui] aus
- config.toml, dann der eingebaute Default.
+ CLI 默认值应跟随 WebUI:显式命令行参数优先,其次是 config.toml 的 [ui]
+ 保存值,最后才是内置默认值。
"""
UI_CONFIG = {
@@ -683,10 +681,9 @@ class TestCliUiDefaults(unittest.TestCase):
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] 配置不应让 CLI 中断:
+ `--no-subtitle-background-enabled` 加上颜色作为命令行组合是参数错误,
+ 但作为保存的设置只表示背景被禁用。
"""
ui_config = dict(self.UI_CONFIG)
ui_config["subtitle_background_enabled"] = False
@@ -700,7 +697,7 @@ class TestCliUiDefaults(unittest.TestCase):
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."""
+ """损坏的 config.toml 不应触发 traceback。"""
ui_config = {
"font_size": "sechzig",
"text_fore_color": 42,
@@ -718,6 +715,133 @@ class TestCliUiDefaults(unittest.TestCase):
self.assertEqual(params.font_name, "STHeitiMedium.ttc")
self.assertEqual(params.voice_name, "zh-CN-XiaoxiaoNeural-Female")
+ def test_saved_no_voice_mode_disables_tts(self):
+ """
+ WebUI 把无配音作为独立的 voice_mode 保存,同时保留用户上一次真正选择
+ 的音色,以便切回自动配音。CLI 必须遵循该模式,否则会重新启用 TTS 并
+ 可能触发付费供应商请求。
+ """
+ ui_config = {"voice_mode": "none", "voice_name": "gemini:Puck-Male"}
+
+ 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.voice_name, "no-voice")
+
+ def test_explicit_voice_name_overrides_saved_no_voice_mode(self):
+ """命令行显式指定的音色优先级最高,保存的无配音模式不得覆盖它。"""
+ ui_config = {"voice_mode": "none", "voice_name": "gemini:Puck-Male"}
+
+ args = cli.parse_args(
+ ["--video-subject", "test", "--voice-name", "zh-CN-XiaoxiaoNeural-Female"]
+ )
+
+ with patch.dict(app_config.ui, ui_config, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.voice_name, "zh-CN-XiaoxiaoNeural-Female")
+
+ def test_saved_tts_mode_keeps_saved_voice(self):
+ """voice_mode 为自动配音时,保存的音色仍然生效。"""
+ ui_config = {"voice_mode": "tts", "voice_name": "gemini:Puck-Male"}
+
+ 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.voice_name, "gemini:Puck-Male")
+
+ def test_enabling_background_without_color_keeps_saved_color(self):
+ """
+ 只传 --subtitle-background-enabled 时用户并未覆盖颜色,应沿用 WebUI
+ 保存的颜色,而不是回退成黑色背景。
+ """
+ ui_config = {"subtitle_background_color": "#654321"}
+
+ args = cli.parse_args(
+ ["--video-subject", "test", "--subtitle-background-enabled"]
+ )
+
+ with patch.dict(app_config.ui, ui_config, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.text_background_color, "#654321")
+
+ def test_enabling_background_falls_back_to_default_without_saved_color(self):
+ """没有可用的保存颜色时,仅开启背景应回退为默认背景。"""
+ args = cli.parse_args(
+ ["--video-subject", "test", "--subtitle-background-enabled"]
+ )
+
+ with patch.dict(app_config.ui, {}, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertIs(params.text_background_color, True)
+
+ def test_explicit_background_color_overrides_saved_color(self):
+ """命令行显式指定的背景颜色优先于保存值。"""
+ ui_config = {"subtitle_background_color": "#654321"}
+
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "test",
+ "--subtitle-background-enabled",
+ "--subtitle-background-color",
+ "#ABCDEF",
+ ]
+ )
+
+ with patch.dict(app_config.ui, ui_config, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.text_background_color, "#ABCDEF")
+
+ def test_saved_upload_mode_disables_tts(self):
+ """
+ 上传自备音频的模式同样表示“不要自动配音”,而 [ui] 不保存文件路径,
+ CLI 无法复现该上传。此时沿用保存的音色会静默触发付费 TTS 请求,
+ 因此与无配音一样映射为 no-voice;需要配音时显式传 --voice-name。
+ """
+ ui_config = {"voice_mode": "upload", "voice_name": "gemini:Puck-Male"}
+
+ 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.voice_name, "no-voice")
+
+ def test_explicit_voice_name_overrides_saved_upload_mode(self):
+ """命令行显式指定的音色同样优先于保存的上传模式。"""
+ ui_config = {"voice_mode": "upload", "voice_name": "gemini:Puck-Male"}
+
+ args = cli.parse_args(
+ ["--video-subject", "test", "--voice-name", "mimo:Female"]
+ )
+
+ with patch.dict(app_config.ui, ui_config, clear=True):
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.voice_name, "mimo:Female")
+
+ def test_saved_color_alone_enables_background(self):
+ """
+ WebUI 总是同时写入开关和颜色,只保存颜色属于手工编辑的配置。
+ 此时保存的颜色本身就表明用户想要背景,因此按开启处理。
+ """
+ ui_config = {"subtitle_background_color": "#654321"}
+
+ 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.text_background_color, "#654321")
+
if __name__ == "__main__":
unittest.main()