Skip to content

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

feat: add command line generation entry

copilot-swe-agent[bot]7a0793586645
diff --git a/README-en.md b/README-en.md
index a930df69..54bdd268 100644
--- a/README-en.md
+++ b/README-en.md
@@ -239,6 +239,24 @@ If you have already activated the virtual environment manually, you can still ru
python main.py
```
+#### ④ Pure CLI Mode (No Browser) ⌨️
+
+If you cannot use a browser or port forwarding, you can generate videos directly from the command line:
+
+```shell
+uv run python cli.py --video-subject "The Role of Money"
+```
+
+You can also provide local materials and control the stop stage:
+
+```shell
+uv run python cli.py \
+ --video-subject "The Role of Money" \
+ --video-source local \
+ --video-materials "storage/local_videos/1.mp4,storage/local_videos/2.mp4" \
+ --stop-at video
+```
+
## Special Thanks 🙏
Due to the **deployment** and **usage** of this project, there is a certain threshold for some beginner users. We would
diff --git a/README.md b/README.md
index a71963b9..a8b8eaa8 100644
--- a/README.md
+++ b/README.md
@@ -245,6 +245,24 @@ uv run python main.py
python main.py
```
+#### ④ 纯命令行方式(无浏览器)⌨️
+
+如果你无法使用浏览器或端口转发,可以直接在命令行生成视频:
+
+```shell
+uv run python cli.py --video-subject "金钱的作用"
+```
+
+也可以指定本地素材并仅运行到某个阶段:
+
+```shell
+uv run python cli.py \
+ --video-subject "金钱的作用" \
+ --video-source local \
+ --video-materials "storage/local_videos/1.mp4,storage/local_videos/2.mp4" \
+ --stop-at video
+```
+
## 特别感谢 🙏
由于该项目的 **部署** 和 **使用**,对于一些小白用户来说,还是 **有一定的门槛**,在此特别感谢
diff --git a/cli.py b/cli.py
new file mode 100644
index 00000000..3ab96fb6
--- /dev/null
+++ b/cli.py
@@ -0,0 +1,85 @@
+import argparse
+import json
+from typing import Sequence
+
+from loguru import logger
+
+from app.models.schema import MaterialInfo, VideoParams
+from app.services import task as tm
+from app.utils import utils
+
+
+def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
+ parser = argparse.ArgumentParser(
+ description="MoneyPrinterTurbo command line video generation"
+ )
+ parser.add_argument("--video-subject", required=True, help="video subject")
+ parser.add_argument("--video-script", default="", help="custom script")
+ parser.add_argument("--video-terms", default=None, help="comma-separated terms")
+ parser.add_argument(
+ "--video-source",
+ default="pexels",
+ choices=["pexels", "pixabay", "local"],
+ help="video material source",
+ )
+ parser.add_argument(
+ "--video-materials",
+ default="",
+ help="comma-separated local material paths",
+ )
+ parser.add_argument(
+ "--stop-at",
+ default="video",
+ choices=["script", "terms", "audio", "subtitle", "materials", "video"],
+ help="pipeline stop stage",
+ )
+ parser.add_argument("--video-count", type=int, default=1, help="output video count")
+ parser.add_argument("--video-aspect", default="9:16", help="video aspect ratio")
+ parser.add_argument("--voice-name", default="", help="tts voice name")
+ parser.add_argument("--subtitle-enabled", action="store_true", default=False)
+ parser.add_argument("--task-id", default="", help="custom task id")
+ return parser.parse_args(argv)
+
+
+def build_video_params(args: argparse.Namespace) -> VideoParams:
+ video_terms = args.video_terms
+ if video_terms:
+ video_terms = [term.strip() for term in video_terms.split(",") if term.strip()]
+
+ video_materials = None
+ if args.video_materials.strip():
+ video_materials = [
+ MaterialInfo(provider="local", url=item.strip(), duration=0)
+ for item in args.video_materials.split(",")
+ if item.strip()
+ ]
+
+ return VideoParams(
+ video_subject=args.video_subject,
+ video_script=args.video_script,
+ video_terms=video_terms,
+ video_source=args.video_source,
+ video_materials=video_materials,
+ video_count=args.video_count,
+ video_aspect=args.video_aspect,
+ voice_name=args.voice_name,
+ subtitle_enabled=args.subtitle_enabled,
+ )
+
+
+def run_cli(argv: Sequence[str] | None = None) -> int:
+ args = parse_args(argv)
+ params = build_video_params(args)
+ task_id = args.task_id or utils.get_uuid()
+ logger.info(f"start cli task: {task_id}, stop_at: {args.stop_at}")
+ result = tm.start(task_id=task_id, params=params, stop_at=args.stop_at)
+ if not result:
+ logger.error("video generation failed")
+ return 1
+
+ print(json.dumps({"task_id": task_id, "result": result}, ensure_ascii=False))
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(run_cli())
diff --git a/test/services/test_cli.py b/test/services/test_cli.py
new file mode 100644
index 00000000..8db50923
--- /dev/null
+++ b/test/services/test_cli.py
@@ -0,0 +1,49 @@
+import sys
+import unittest
+from pathlib import Path
+from unittest.mock import patch
+
+sys.path.insert(0, str(Path(__file__).parent.parent.parent))
+
+import cli
+
+
+class TestCli(unittest.TestCase):
+ def test_build_video_params_with_local_materials(self):
+ args = cli.parse_args(
+ [
+ "--video-subject",
+ "测试主题",
+ "--video-source",
+ "local",
+ "--video-materials",
+ "a.mp4,b.jpg",
+ "--video-terms",
+ "foo, bar",
+ ]
+ )
+
+ params = cli.build_video_params(args)
+
+ self.assertEqual(params.video_subject, "测试主题")
+ self.assertEqual(params.video_source, "local")
+ self.assertEqual([m.url for m in params.video_materials], ["a.mp4", "b.jpg"])
+ self.assertEqual(params.video_terms, ["foo", "bar"])
+
+ def test_run_cli_dispatches_task_start(self):
+ with patch.object(cli.tm, "start", return_value={"script": "ok"}) as start, patch.object(
+ cli.utils, "get_uuid", return_value="task-123"
+ ), patch("builtins.print") as print_mock:
+ code = cli.run_cli(["--video-subject", "命令行测试", "--stop-at", "script"])
+
+ self.assertEqual(code, 0)
+ self.assertTrue(start.called)
+ kwargs = start.call_args.kwargs
+ self.assertEqual(kwargs["task_id"], "task-123")
+ self.assertEqual(kwargs["stop_at"], "script")
+ self.assertEqual(kwargs["params"].video_subject, "命令行测试")
+ print_mock.assert_called_once()
+
+
+if __name__ == "__main__":
+ unittest.main()