# chenwr727/ai-short-video-engine architecture

> Architecture diagram of the GitHub repository chenwr727/ai-short-video-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chenwr727/ai-short-video-engine
- Repository: https://github.com/chenwr727/ai-short-video-engine

## Overview

1. **整体定位**
   - 这是一个“文本/URL → 脚本 → 配音 → 视频成片”的生成式视频流水线。
   - 核心不是传统内容管理，而是围绕“短视频自动生产”组织：输入内容后，系统自动完成理解、改写、素材检索、TTS、字幕和视频合成。

2. **三个入口层**
   - `main.py`：命令行入口，适合单次或批处理式生成。
   - `app.py`：FastAPI 后端入口，提供程序化 API 和任务管理能力。
   - `web.py`：Streamlit 前端入口，提供交互式网页操作界面。
   - 这三个入口共享同一套业务服务层和工具层，说明前后端是“共享核心引擎”的结构。

3. **API/任务管理子系统**
   - `api/` 下包含 `router.py`, `service.py`, `crud.py`, `models.py`, `database.py`, `schemas.py`。
   - 这一层负责把视频生成流程包装成可持久化任务，并通过数据库保存任务状态、结果和元数据。
   - 从文件结构看，FastAPI 主要承担“任务调度/查询/状态更新”，而不是直接包含所有生成逻辑。

4. **核心业务编排层**
   - `services/video.py` 很可能是整条生成链路的编排中心。
   - 它连接 LLM、素材服务、TTS、字幕和视频工具，负责把多个独立步骤串成一个完整的视频生成流水线。
   - 这层是架构中的关键边界：上游接收业务请求，下游调用外部能力与本地媒体处理工具。

5. **LLM 与内容理解层**
   - `services/llm.py` 和 `services/yuanbao.py` 表明系统通过大模型生成视频脚本、角色对话和结构化内容。
   - `prompts/` 下的 `podcast.toml`, `crosstalk.toml`, `talkshow.toml` 表示支持多种内容风格/角色形态。
   - 这意味着“文案生成”不是单一摘要，而是面向不同短视频脚本模板的内容生成。

6. **素材检索层**
   - `services/material/` 中的 `pexels.py` 与 `pixabay.py` 是外部素材源适配器。
   - 作用是根据脚本语义或关键词获取图片/视频素材，补足自动生成视频所需的画面内容。
   - `base.py` 说明这里采用统一接口，方便在多个素材源之间切换或扩展。

7. **语音合成层**
   - `services/tts/` 下的 `cosyvoice.py` 和 `qwen.py` 说明支持多种 TTS 后端。
   - `base.py` 表明语音生成同样采用抽象接口，便于为不同角色、不同音色或不同供应商做适配。
   - 这层输出的是可用于后续剪辑的音频片段，是视频流水线的重要中间产物。

8. **视频处理与合成层**
   - `services/video.py`、`utils/video.py` 以及 `resource/bgm.mp3`、`fonts/DreamHanSans-W20.ttc` 指向本地视频渲染与后期处理。
   - 系统需要把素材、配音、背景音乐、字幕和字体统一合成为最终视频文件。
   - README 提到 FFmpeg 和 ImageMagick，因此视频/图像处理主要依赖本地媒体工具链，而不是纯 Python 内存处理。

9. **文本、字幕和 URL 预处理工具**
   - `utils/text.py`, `utils/subtitle.py`, `utils/url.py` 分别处理文本清洗、字幕切分/对齐、URL 解析与内容提取。
   - 这些工具位于独立层，说明系统对“输入内容标准化”很重视：无论输入是文章链接还是主题文本，都先转成统一中间表示。
   - 这有助于让后续 LLM、TTS 和视频合成步骤复用同一数据结构。

10. **配置、日志与运行边界**
   - `utils/config.py`、`schemas/config.py`、`config-template.toml`、`auth.yaml` 体现出配置驱动架构。
   - `utils/log.py` 提供统一日志，便于追踪长链路生成任务。
   - 这种设计将 API Key、模型参数、素材源和运行参数与业务逻辑分离，降低部署耦合。

11. **数据模型与持久化**
   - `schemas/video.py` 等 schema 文件定义了跨层传递的数据契约。
   - `api/database.py`、`api/models.py`、`api/crud.py` 表明持久化层使用 SQLite（README 明确提到）保存任务或视频记录。
   - 这意味着系统有“生成前的请求态”和“生成后的结果态”两类数据，便于查询、重试和审计。

12. **外部依赖与服务边界**
   - README 指出主要外部能力包括 DeepSeek API、Tongyi TTS、Pexels/Pixabay，以及搜索能力依赖 `yuanbao-free-api`。
   - 因此系统的边界很清晰：本地负责编排、渲染和状态管理，云端负责语言理解、素材检索与语音生成。
   - 这类分工决定了架构图上应把 LLM/TTS/素材服务画成外部依赖，而不是内部模块。

13. **资源与模板层**
   - `prompts/`、`fonts/`、`resource/` 是静态资产层，服务于内容风格控制和渲染效果。
   - Prompt 文件决定脚本结构和角色风格；字体和背景音乐则直接影响成片表现。
   - 这些资源不参与业务编排，但对最终输出质量有明显影响。

14. **推荐的架构视角**
   - 画图时可将系统拆成四块：入口层（CLI/FastAPI/Streamlit）、编排层（视频生成服务）、能力适配层（LLM/TTS/素材）、基础设施层（SQLite/本地媒体工具/静态资源）。
   - 主数据流是：输入 URL/主题文本 → 内容解析与脚本生成 → 素材检索与 TTS → 字幕/配乐/合成 → 输出视频并写入任务记录。
   - 这个仓库的关键特点是“重编排、轻界面”，真正的核心价值在 `services/` 与 `api/` 之间的流水线整合。

Main entry points: [`services/video.py`](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/video.py) (Video flow), [`services/llm.py`](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/llm.py) (LLM service), [`api/service.py`](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/service.py) (Job service), [`services/material/base.py`](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/base.py) (Material base)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entrypoints"]
  node_main["CLI<br/>entrypoint<br/>[main.py]"]
  node_api_app["API app<br/>entrypoint<br/>[app.py]"]
  node_web_app["Web app<br/>entrypoint<br/>[web.py]"]
end

subgraph group_core["Core pipeline"]
  node_api_router["Router<br/>api route<br/>[router.py]"]
  node_api_service["Job service<br/>api service<br/>[service.py]"]
  node_video_service{{"Video flow<br/>orchestrator<br/>[video.py]"}}
end

subgraph group_adapters["Adapters"]
  node_llm_service["LLM service<br/>llm adapter<br/>[llm.py]"]
  node_yuanbao["Yuanbao<br/>llm backend<br/>[yuanbao.py]"]
  node_mat_base["Material base<br/>material adapter<br/>[base.py]"]
  node_pexels["Pexels<br/>material source<br/>[pexels.py]"]
  node_pixabay["Pixabay<br/>material source<br/>[pixabay.py]"]
  node_tts_base["TTS base<br/>tts adapter<br/>[base.py]"]
  node_cosyvoice["CosyVoice<br/>tts backend<br/>[cosyvoice.py]"]
  node_qwen_tts["Qwen TTS<br/>tts backend<br/>[qwen.py]"]
end

subgraph group_data["Data & runtime"]
  node_api_crud[("CRUD<br/>persistence<br/>[crud.py]")]
  node_api_models["DB models<br/>persistence model<br/>[models.py]"]
  node_api_db[("Database<br/>[database.py]")]
  node_api_schemas["API schemas<br/>contract<br/>[schemas.py]"]
  node_text_utils["Text utils<br/>text processing<br/>[text.py]"]
  node_url_utils["URL utils<br/>ingest<br/>[url.py]"]
  node_sub_utils["Subtitle utils<br/>subtitle processing<br/>[subtitle.py]"]
  node_video_utils["Media utils<br/>media processing<br/>[video.py]"]
  node_config_utils["Config<br/>[config.py]"]
  node_log_utils["Logging<br/>[log.py]"]
  node_video_schema["Video schema<br/>domain contract<br/>[video.py]"]
  node_config_schema["Config schema<br/>domain contract<br/>[config.py]"]
  node_podcast_prompt["Podcast prompt<br/>prompt template<br/>[podcast.toml]"]
  node_crosstalk_prompt["Crosstalk prompt<br/>prompt template<br/>[crosstalk.toml]"]
  node_talkshow_prompt["Talkshow prompt<br/>prompt template<br/>[talkshow.toml]"]
  node_bgm["BGM<br/>asset<br/>[bgm.mp3]"]
  node_font["Font<br/>asset"]
end

node_main -->|"runs"| node_video_service
node_api_app -->|"serves"| node_api_router
node_web_app -->|"invokes"| node_video_service
node_api_router -->|"delegates"| node_api_service
node_api_service -->|"persists"| node_api_crud
node_api_crud -->|"maps"| node_api_models
node_api_crud -->|"uses"| node_api_db
node_api_service -->|"starts"| node_video_service
node_video_service -->|"ingests"| node_url_utils
node_video_service -->|"prepares"| node_text_utils
node_video_service -->|"scripts"| node_llm_service
node_llm_service -->|"backed by"| node_yuanbao
node_video_service -->|"fetches"| node_mat_base
node_mat_base -->|"implements"| node_pexels
node_mat_base -->|"implements"| node_pixabay
node_video_service -->|"voices"| node_tts_base
node_tts_base -->|"implements"| node_cosyvoice
node_tts_base -->|"implements"| node_qwen_tts
node_video_service -->|"subtitles"| node_sub_utils
node_video_service -->|"renders"| node_video_utils
node_video_service -->|"configures"| node_config_utils
node_video_service -->|"logs"| node_log_utils
node_video_service -->|"contracts"| node_video_schema
node_config_utils -->|"validates"| node_config_schema
node_llm_service -->|"templates"| node_podcast_prompt
node_llm_service -->|"templates"| node_crosstalk_prompt
node_llm_service -->|"templates"| node_talkshow_prompt
node_video_utils -->|"uses"| node_bgm
node_video_utils -->|"uses"| node_font
node_api_app -->|"reads"| node_api_db
node_web_app -.->|"queries"| node_api_service

click node_main "https://github.com/chenwr727/ai-short-video-engine/blob/main/main.py"
click node_api_app "https://github.com/chenwr727/ai-short-video-engine/blob/main/app.py"
click node_web_app "https://github.com/chenwr727/ai-short-video-engine/blob/main/web.py"
click node_api_router "https://github.com/chenwr727/ai-short-video-engine/blob/main/api/router.py"
click node_api_service "https://github.com/chenwr727/ai-short-video-engine/blob/main/api/service.py"
click node_api_crud "https://github.com/chenwr727/ai-short-video-engine/blob/main/api/crud.py"
click node_api_models "https://github.com/chenwr727/ai-short-video-engine/blob/main/api/models.py"
click node_api_db "https://github.com/chenwr727/ai-short-video-engine/blob/main/api/database.py"
click node_api_schemas "https://github.com/chenwr727/ai-short-video-engine/blob/main/api/schemas.py"
click node_video_service "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/video.py"
click node_llm_service "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/llm.py"
click node_yuanbao "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/yuanbao.py"
click node_mat_base "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/base.py"
click node_pexels "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/pexels.py"
click node_pixabay "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/pixabay.py"
click node_tts_base "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/tts/base.py"
click node_cosyvoice "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/tts/cosyvoice.py"
click node_qwen_tts "https://github.com/chenwr727/ai-short-video-engine/blob/main/services/tts/qwen.py"
click node_text_utils "https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/text.py"
click node_url_utils "https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/url.py"
click node_sub_utils "https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/subtitle.py"
click node_video_utils "https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/video.py"
click node_config_utils "https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/config.py"
click node_log_utils "https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/log.py"
click node_video_schema "https://github.com/chenwr727/ai-short-video-engine/blob/main/schemas/video.py"
click node_config_schema "https://github.com/chenwr727/ai-short-video-engine/blob/main/schemas/config.py"
click node_podcast_prompt "https://github.com/chenwr727/ai-short-video-engine/blob/main/prompts/podcast.toml"
click node_crosstalk_prompt "https://github.com/chenwr727/ai-short-video-engine/blob/main/prompts/crosstalk.toml"
click node_talkshow_prompt "https://github.com/chenwr727/ai-short-video-engine/blob/main/prompts/talkshow.toml"
click node_bgm "https://github.com/chenwr727/ai-short-video-engine/blob/main/resource/bgm.mp3"
click node_font "https://github.com/chenwr727/ai-short-video-engine/blob/main/fonts/DreamHanSans-W20.ttc"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_api_app,node_web_app toneBlue
class node_api_router,node_api_service,node_video_service toneAmber
class node_llm_service,node_yuanbao,node_mat_base,node_pexels,node_pixabay,node_tts_base,node_cosyvoice,node_qwen_tts toneMint
class node_api_crud,node_api_models,node_api_db,node_api_schemas,node_text_utils,node_url_utils,node_sub_utils,node_video_utils,node_config_utils,node_log_utils,node_video_schema,node_config_schema,node_podcast_prompt,node_crosstalk_prompt,node_talkshow_prompt,node_bgm,node_font toneRose
```

## Components

### Entrypoints

CLI, API, and web UI entry points

- [CLI](https://github.com/chenwr727/ai-short-video-engine/blob/main/main.py): entrypoint · Command-line generation entry · `main.py`
- [API app](https://github.com/chenwr727/ai-short-video-engine/blob/main/app.py): entrypoint · FastAPI application entry · `app.py`
- [Web app](https://github.com/chenwr727/ai-short-video-engine/blob/main/web.py): entrypoint · Streamlit interface entry · `web.py`

### Core pipeline

Orchestration and shared processing services

- [Router](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/router.py): api route · HTTP endpoints for job control · `api/router.py`
- [Job service](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/service.py): api service · Task lifecycle and orchestration over API · `api/service.py`
- [Video flow](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/video.py): orchestrator · End-to-end short video pipeline · `services/video.py`

### Adapters

LLM, TTS, and material source integrations

- [LLM service](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/llm.py): llm adapter · Script and structure generation · `services/llm.py`
- [Yuanbao](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/yuanbao.py): llm backend · LLM backend integration · `services/yuanbao.py`
- [Material base](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/base.py): material adapter · Common interface for media sources · `services/material/base.py`
- [Pexels](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/pexels.py): material source · External image/video source · `services/material/pexels.py`
- [Pixabay](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/material/pixabay.py): material source · External image/video source · `services/material/pixabay.py`
- [TTS base](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/tts/base.py): tts adapter · Common voice synthesis interface · `services/tts/base.py`
- [CosyVoice](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/tts/cosyvoice.py): tts backend · Speech synthesis backend · `services/tts/cosyvoice.py`
- [Qwen TTS](https://github.com/chenwr727/ai-short-video-engine/blob/main/services/tts/qwen.py): tts backend · Speech synthesis backend · `services/tts/qwen.py`

### Data & runtime

Schemas, persistence, utilities, and assets

- [CRUD](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/crud.py): persistence · Task record reads and writes · `api/crud.py`
- [DB models](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/models.py): persistence model · Persistent task and result entities · `api/models.py`
- [Database](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/database.py): database · SQLite connection and session setup · `api/database.py`
- [API schemas](https://github.com/chenwr727/ai-short-video-engine/blob/main/api/schemas.py): contract · Request and response payloads · `api/schemas.py`
- [Text utils](https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/text.py): text processing · Content cleaning and normalization · `utils/text.py`
- [URL utils](https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/url.py): ingest · URL parsing and source extraction · `utils/url.py`
- [Subtitle utils](https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/subtitle.py): subtitle processing · Subtitle segmentation and alignment · `utils/subtitle.py`
- [Media utils](https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/video.py): media processing · Local video and image helpers · `utils/video.py`
- [Config](https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/config.py): runtime config · Configuration loading and access · `utils/config.py`
- [Logging](https://github.com/chenwr727/ai-short-video-engine/blob/main/utils/log.py): runtime logging · Unified logging for long jobs · `utils/log.py`
- [Video schema](https://github.com/chenwr727/ai-short-video-engine/blob/main/schemas/video.py): domain contract · Shared video generation data model · `schemas/video.py`
- [Config schema](https://github.com/chenwr727/ai-short-video-engine/blob/main/schemas/config.py): domain contract · Shared configuration data model · `schemas/config.py`
- [Podcast prompt](https://github.com/chenwr727/ai-short-video-engine/blob/main/prompts/podcast.toml): prompt template · Conversation-style script template · `prompts/podcast.toml`
- [Crosstalk prompt](https://github.com/chenwr727/ai-short-video-engine/blob/main/prompts/crosstalk.toml): prompt template · Dialog-style script template · `prompts/crosstalk.toml`
- [Talkshow prompt](https://github.com/chenwr727/ai-short-video-engine/blob/main/prompts/talkshow.toml): prompt template · Talk-show style script template · `prompts/talkshow.toml`
- [BGM](https://github.com/chenwr727/ai-short-video-engine/blob/main/resource/bgm.mp3): asset · Background music resource · `resource/bgm.mp3`
- [Font](https://github.com/chenwr727/ai-short-video-engine/blob/main/fonts/DreamHanSans-W20.ttc): asset · Subtitle and title font resource · `fonts/DreamHanSans-W20.ttc`

## Connections

- CLI → Video flow: runs · CLI triggers the generation pipeline
- API app → Router: serves · FastAPI exposes task endpoints
- Web app → Video flow: invokes · Web UI drives the shared pipeline
- Router → Job service: delegates · Routes hand off to task service
- Job service → CRUD: persists · Task state is stored and updated
- CRUD → DB models: maps · CRUD operates on DB entities
- CRUD → Database: uses · CRUD uses the database session
- Job service → Video flow: starts · Task service launches generation flow
- Video flow → URL utils: ingests · Input URLs are normalized and extracted
- Video flow → Text utils: prepares · Theme text is cleaned for generation
- Video flow → LLM service: scripts · LLM creates structure and narration
- LLM service → Yuanbao: backed by · LLM calls use a concrete backend
- Video flow → Material base: fetches · Pipeline requests supporting media
- Material base → Pexels: implements · One media source adapter
- Material base → Pixabay: implements · Another media source adapter
- Video flow → TTS base: voices · Pipeline generates narration audio
- TTS base → CosyVoice: implements · One speech synthesis backend
- TTS base → Qwen TTS: implements · Another speech synthesis backend
- Video flow → Subtitle utils: subtitles · Subtitle timing and segmentation
- Video flow → Media utils: renders · Local media composition helpers
- Video flow → Config: configures · Runtime settings drive the flow
- Video flow → Logging: logs · Pipeline execution is centrally logged
- Video flow → Video schema: contracts · Shared generation data model
- Config → Config schema: validates · Configuration shape is defined in schema
- LLM service → Podcast prompt: templates · Prompt templates shape script style
- LLM service → Crosstalk prompt: templates · Prompt templates shape script style
- LLM service → Talkshow prompt: templates · Prompt templates shape script style
- Media utils → BGM: uses · Background music is mixed in
- Media utils → Font: uses · Font resource is used for text rendering
- API app → Database: reads · API layer queries persisted tasks
- Web app → Job service: queries · UI may inspect task progress and results
