视频人物替换智能体 - 具备学习积累能力的多 Agent 系统
- ✅ 视频人物智能识别
- ✅ 灵活的人物替换方案(文本描述/参考图/预设模板)
- ✅ 支持多个视频编辑服务(RunwayML、Pika Labs)
- ✅ 并行处理提升效率
- ✅ 完整的流程追踪(TrackID 系统)
- ✅ 学习积累能力(经验库)
- 🧠 模式识别: 自动识别相似任务
- 📚 经验复用: 利用历史成功经验
- 🔄 自动优化: 根据反馈持续改进
- 📊 质量验证: 自动验证生成视频质量
- ⚡ 并行执行: 多 Agent 并行处理任务
- VideoAnalyzer Agent - 视频分析专家
- CharacterDetector Agent - 人物识别专家
- SceneSegmenter Agent - 场景分割专家
- ReferenceImageGenerator Agent - 参考图生成专家
- PromptEngineer Agent - 提示词优化专家
- PresetManager Agent - 预设模板管理专家
- RunwayEditor Agent - RunwayML 编辑专家
- PikaEditor Agent - Pika 编辑专家
- FallbackOrchestrator Agent - 故障转移协调专家
- QualityValidator Agent - 质量验证专家
- ExperienceManager Agent - 经验管理专家
- PatternMatcher Agent - 模式匹配专家
阶段1: 视频分析(并行)
├─ VideoAnalyzer → 提取关键帧
├─ CharacterDetector → 识别人物
└─ SceneSegmenter → 场景分割
阶段2: 准备工作(并行)
├─ ReferenceImageGenerator → 生成参考图
└─ PromptEngineer → 优化 prompt
阶段3: 视频编辑(并行)
├─ RunwayEditor → 处理场景1-3
└─ PikaEditor → 处理场景4-5
阶段4: 质量验证(流水线)
└─ QualityValidator → 边生成边验证
# 创建所有必要的表
node -r tsx/register src/agents/characterReplacement/migration.ts
# 或者(如果项目已构建)
node build/agents/characterReplacement/migration.js在数据库中插入默认 Prompt:
INSERT INTO t_prompts (code, name, category, defaultValue)
VALUES (
'character-replacement-main',
'人物替换主 Agent Prompt',
'characterReplacement',
'你是一个视频人物替换专家助手。你的任务是帮助用户将视频中的人物替换为其他角色(如动物、外国人、恐龙等)。工作流程:
1. 首先调用 getVideoInfo 获取视频信息
2. 然后调用 analyzeVideo 分析视频中的人物和场景
3. 根据用户需求调用 setReplacements 设置替换配置
4. 最后调用 executeReplacement 执行替换并生成新视频
请始终保持专业、友好的态度,及时向用户报告进度。'
);在环境变量中配置服务 API:
# RunwayML
RUNWAY_API_KEY=your_runway_api_key
RUNWAY_API_URL=https://api.runwayml.com/v1
# Pika Labs
PIKA_API_KEY=your_pika_api_key
PIKA_API_URL=https://api.pika.labs/v1# 创建人物替换任务
curl -X POST http://localhost:60000/api/characterReplacement \
-H "Content-Type: application/json" \
-d '{
"projectId": 1,
"videoId": 10,
"message": "请把视频中的人物替换为熊猫"
}'
# 响应:
{
"success": true,
"data": {
"trackId": "CR-20250205-143025-AB12",
"videoId": 10,
"projectId": 1,
"message": "人物替换任务已创建,正在执行中..."
}
}
# 获取任务进度
curl http://localhost:60000/api/characterReplacement/10/progress?projectId=1
# 获取任务详情
curl http://localhost:60000/api/characterReplacement/track/CR-20250205-143025-AB12创建人物替换任务
请求体:
{
"projectId": 1,
"videoId": 10,
"message": "请把视频中的人物替换为熊猫"
}响应:
{
"success": true,
"data": {
"trackId": "CR-20250205-143025-AB12",
"videoId": 10,
"projectId": 1,
"message": "人物替换任务已创建,正在执行中..."
}
}获取任务进度
查询参数:
projectId(number) - 项目ID
响应:
{
"success": true,
"data": {
"trackId": "CR-20250205-143025-AB12",
"projectId": 1,
"videoId": 10,
"status": "running",
"currentStage": "分析视频内容",
"progress": 45,
"startTime": "2025-02-05T14:30:25.000Z",
"steps": [...]
}
}获取任务详情(包含所有步骤)
响应:
{
"success": true,
"data": {
"trackId": "CR-20250205-143025-AB12",
"status": "completed",
"progress": 100,
"steps": [
{
"id": 1,
"agentType": "main",
"stage": "preparation",
"action": "获取视频信息",
"status": "completed",
"duration": 125,
"timestamp": "2025-02-05T14:30:25.000Z"
}
]
}
}任务追踪主表
| 字段 | 类型 | 说明 |
|---|---|---|
| trackId | string(50) | 任务追踪ID(主键) |
| projectId | integer | 项目ID |
| videoId | integer | 视频ID |
| status | string(20) | 任务状态 |
| currentStage | string(100) | 当前阶段 |
| progress | integer | 进度百分比 |
| startTime | datetime | 开始时间 |
| endTime | datetime | 结束时间 |
| workflowConfig | text | 工作流配置 JSON |
| executionResult | text | 执行结果 JSON |
流程步骤详细记录
| 字段 | 类型 | 说明 |
|---|---|---|
| id | integer | 步骤ID(主键) |
| trackId | string(50) | 任务追踪ID |
| agentType | string(50) | Agent 类型 |
| stage | string(100) | 阶段名称 |
| action | string(200) | 动作描述 |
| status | string(20) | 状态 |
| input | text | 输入数据 JSON |
| output | text | 输出数据 JSON |
| error | text | 错误信息 |
| duration | integer | 耗时(毫秒) |
| timestamp | datetime | 时间戳 |
经验学习库
| 字段 | 类型 | 说明 |
|---|---|---|
| id | integer | 经验ID(主键) |
| taskSignature | text | 任务特征 JSON |
| successfulWorkflow | text | 成功的执行方案 JSON |
| executionResult | text | 执行结果 JSON |
| projectId | integer | 项目ID |
| videoId | integer | 视频ID |
| usageCount | integer | 被复用次数 |
| effectiveness | float | 有效性评分(0-1) |
| createdAt | datetime | 创建时间 |
| updatedAt | datetime | 更新时间 |
{
videoStyle: 'ancient', // 视频风格
characterCount: 3, // 人物数量
replacementType: 'animal', // 替换类型
targetCategory: '熊猫', // 目标类别
sceneComplexity: 'medium', // 场景复杂度
averageSceneDuration: 15 // 平均场景时长
}当新任务到来时,系统会:
- 提取任务特征
- 在经验库中搜索相似任务
- 计算相似度分数(加权算法)
- 选择最有效的历史经验
如果找到相似度 > 70% 的历史经验:
- ✅ 直接使用历史配置
- ✅ 跳过参数优化步骤
- ⚡ 效率提升 60-75%
每次执行后:
- 保存新经验(如果是新方案)
- 更新经验有效性评分
- 根据用户反馈调整权重
// 前端调用
const response = await fetch('/api/characterReplacement', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
projectId: 1,
videoId: 10,
message: '请把关羽张飞刘备三个人物都替换为熊猫'
})
});
const { trackId } = await response.json();
console.log('任务追踪ID:', trackId);
// 轮询进度
const interval = setInterval(async () => {
const progress = await fetch(`/api/characterReplacement/10/progress?projectId=1`);
const data = await progress.json();
console.log('进度:', data.data.progress, '%');
if (data.data.status === 'completed') {
clearInterval(interval);
console.log('任务完成!');
}
}, 2000);const response = await fetch('/api/characterReplacement', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
projectId: 1,
videoId: 10,
message: '请使用提供的参考图,把人物替换为类似风格的熊猫',
referenceImage: 'https://example.com/panda-reference.jpg'
})
});src/agents/characterReplacement/
├── index.ts # 主 Agent 类
├── types.ts # 类型定义
├── migration.ts # 数据库迁移脚本
├── README.md # 本文档
├── track/
│ └── tracker.ts # 流程追踪系统
├── sub-agents/ # Sub-Agent 实现(待开发)
│ ├── videoAnalyzer.ts
│ ├── characterDetector.ts
│ ├── sceneSegmenter.ts
│ ├── referenceImageGen.ts
│ ├── promptEngineer.ts
│ ├── presetManager.ts
│ ├── runwayEditor.ts
│ ├── pikaEditor.ts
│ ├── qualityValidator.ts
│ ├── fallbackOrchestrator.ts
│ ├── patternMatcher.ts
│ └── experienceManager.ts
└── utils/ # 工具函数(待开发)
├── runwayAPI.ts
└── pikaAPI.ts
- 实现 11 个 Sub-Agent
- 集成 RunwayML API
- 集成 Pika Labs API
- 实现完整的经验学习系统
- WebSocket 实时推送
- 前端界面集成
本项目继承 Toonflow 项目的 AGPL-3.0 许可证。