How chenwr727/ai-short-video-engine works
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: video.py Video flow · llm.py LLM service · service.py Job service · base.py Material base
Components 31
Entrypoints
CLI, API, and web UI entry points
Core pipeline
Orchestration and shared processing services
- Router · HTTP endpoints for job control
api/router.py - Job service · Task lifecycle and orchestration over API
api/service.py - Video flow · End-to-end short video pipeline
services/video.py
Adapters
LLM, TTS, and material source integrations
- LLM service · Script and structure generation
services/llm.py - Yuanbao · LLM backend integration
services/yuanbao.py - Material base · Common interface for media sources
services/material/base.py - Pexels · External image/video source
services/material/pexels.py - Pixabay · External image/video source
services/material/pixabay.py - TTS base · Common voice synthesis interface
services/tts/base.py - CosyVoice · Speech synthesis backend
services/tts/cosyvoice.py - Qwen TTS · Speech synthesis backend
services/tts/qwen.py
Data & runtime
Schemas, persistence, utilities, and assets
- CRUD · Task record reads and writes
api/crud.py - DB models · Persistent task and result entities
api/models.py - Database · SQLite connection and session setup
api/database.py - API schemas · Request and response payloads
api/schemas.py - Text utils · Content cleaning and normalization
utils/text.py - URL utils · URL parsing and source extraction
utils/url.py - Subtitle utils · Subtitle segmentation and alignment
utils/subtitle.py - Media utils · Local video and image helpers
utils/video.py - Config · Configuration loading and access
utils/config.py - Logging · Unified logging for long jobs
utils/log.py - Video schema · Shared video generation data model
schemas/video.py - Config schema · Shared configuration data model
schemas/config.py - Podcast prompt · Conversation-style script template
prompts/podcast.toml - Crosstalk prompt · Dialog-style script template
prompts/crosstalk.toml - Talkshow prompt · Talk-show style script template
prompts/talkshow.toml - BGM · Background music resource
resource/bgm.mp3 - Font · Subtitle and title font resource
fonts/DreamHanSans-W20.ttc
Connections 31
- 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