Skip to content

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

fix(cli): allow the video clip speed from the command line

c0206279a676abdd8e7
diff --git a/cli.py b/cli.py
index 6dfd567f..b32ef14c 100644
--- a/cli.py
+++ b/cli.py
@@ -97,6 +97,23 @@ def _positive_float(value: str) -> float:
return parsed
+# 片段播放速度的取值范围。app/utils/utils.py 的 normalize_clip_speed() 用同一
+# 范围做限幅,webui/Main.py 的滑块也使用 0.5~2.0。集中定义后,单任务参数、
+# 批量清单校验和 [ui] 保存值不会再次出现取值不一致。
+_CLIP_SPEED_MIN = 0.5
+_CLIP_SPEED_MAX = 2.0
+
+
+def _clip_speed(value: str) -> float:
+ parsed = float(value)
+ if not math.isfinite(parsed) or not _CLIP_SPEED_MIN <= parsed <= _CLIP_SPEED_MAX:
+ raise argparse.ArgumentTypeError(
+ "video-clip-speed must be a finite number between "
+ f"{_CLIP_SPEED_MIN} and {_CLIP_SPEED_MAX}, got {value!r}"
+ )
+ return parsed
+
+
def _percent_position(value: str) -> float:
parsed = float(value)
if not math.isfinite(parsed) or parsed < 0 or parsed > 100:
@@ -386,6 +403,15 @@ Batch manifests:
"maximum duration of each source clip in seconds, at least 1 (default: 5)"
),
)
+ video_group.add_argument(
+ "--video-clip-speed",
+ type=_clip_speed,
+ default=None,
+ help=(
+ "playback speed multiplier applied to every source clip, between "
+ "0.5 and 2.0 (default: 1.0)"
+ ),
+ )
video_group.add_argument(
"--match-materials-to-script",
default=None,
@@ -834,6 +860,7 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
"video_concat_mode",
"video_transition_mode",
"video_clip_duration",
+ "video_clip_speed",
"match_materials_to_script",
"n_threads",
"voice_volume",
@@ -860,7 +887,11 @@ def build_video_params(args: argparse.Namespace) -> VideoParams:
# 没有显式传入命令行参数时,使用 WebUI 保存的值。只补充上面尚未由命令行
# 设置的字段;若保存值缺失,则继续沿用 VideoParams 的默认值。
+ # VideoParams 里多数可保存字段(subtitle_position、custom_position 等)在
+ # 模型导入时就读取 config.ui,video_clip_speed 却是硬编码的 1.0,因此必须
+ # 在这里显式补上,否则 WebUI 保存的速度在命令行会被静默丢弃。
ui_defaults = (
+ ("video_clip_speed", float, _clip_speed),
("video_fit_mode", str, _video_fit_mode),
("font_name", str, None),
("text_fore_color", str, _hex_color),
@@ -1126,9 +1157,12 @@ def _validate_batch_task_params(
raise ValueError("custom_position must be a finite number between 0 and 100")
if params.video_clip_speed is not None and (
not math.isfinite(params.video_clip_speed)
- or not 0.5 <= params.video_clip_speed <= 2.0
+ or not _CLIP_SPEED_MIN <= params.video_clip_speed <= _CLIP_SPEED_MAX
):
- raise ValueError("video_clip_speed must be a finite number between 0.5 and 2.0")
+ raise ValueError(
+ "video_clip_speed must be a finite number between "
+ f"{_CLIP_SPEED_MIN} and {_CLIP_SPEED_MAX}"
+ )
if params.text_background_color is False and params.rounded_subtitle_background:
raise ValueError(
"rounded_subtitle_background requires an enabled subtitle background"
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
index 413dd651..1fb890c8 100644
--- a/test/services/test_cli.py
+++ b/test/services/test_cli.py
@@ -1348,6 +1348,44 @@ class TestCli(unittest.TestCase):
self.assertEqual(code, 2)
start.assert_not_called()
+ def test_video_clip_speed_is_reachable_from_the_cli(self):
+ """
+ WebUI 的滑块把 video_clip_speed 写进 config.toml,批量清单也接受该字段
+ (见 _validate_batch_task_params),但 VideoParams 把它硬编码成 1.0 而不是
+ 读取 config.ui,因此单任务命令行此前既没有对应参数、也拿不到保存值。
+ """
+ explicit_params = cli.build_video_params(
+ cli.parse_args(["--video-subject", "test", "--video-clip-speed", "1.5"])
+ )
+ self.assertEqual(explicit_params.video_clip_speed, 1.5)
+
+ saved_args = cli.parse_args(["--video-subject", "test"])
+
+ with patch.dict(app_config.ui, {"video_clip_speed": 1.25}, clear=True):
+ saved_params = cli.build_video_params(saved_args)
+
+ self.assertEqual(saved_params.video_clip_speed, 1.25)
+
+ def test_video_clip_speed_range_matches_the_runtime_normalizer(self):
+ """
+ CLI 的取值范围必须与 app/utils/utils.py 的 normalize_clip_speed() 以及
+ WebUI 滑块一致,否则命令行会接受运行时随后改写的取值。
+ """
+ from app.utils import utils
+
+ self.assertEqual(cli._CLIP_SPEED_MIN, utils._CLIP_SPEED_MIN)
+ self.assertEqual(cli._CLIP_SPEED_MAX, utils._CLIP_SPEED_MAX)
+
+ for value in ("0.4", "3.0", "nan", "inf"):
+ error_output = io.StringIO()
+ with redirect_stderr(error_output):
+ with self.assertRaises(SystemExit) as cm:
+ cli.parse_args(
+ ["--video-subject", "test", "--video-clip-speed", value]
+ )
+ self.assertEqual(cm.exception.code, 2)
+ self.assertIn("video-clip-speed", error_output.getvalue())
+
def test_later_null_runtime_field_prevents_every_batch_task_from_starting(self):
for field_name in ("video_aspect", "video_concat_mode"):
with self.subTest(field_name=field_name), tempfile.TemporaryDirectory() as temp_dir: