feat(editor): from-assets支持同素材多片段+随机起始时间+去重+required_clips_count #1512

Merged
xiaoxia merged 5 commits from fix/editor-clips-random-start-time into develop 2026-08-26 22:40:02 +08:00
Owner

问题

create_clips_from_assets_editor 接口存在多个问题:

  1. duration 硬编码为 5.0
  2. 每个素材只能创建1个片段(素材数 < 片段数时无法满足模板要求)
  3. 没有调用 _calc_random_start_time
  4. 没有使用 used_segments 追踪已使用区间
  5. order 有 bug(使用了不存在的 body.start_order,hasattr 永远 False,回退到 i)

修复

Schema

  • ClipsFromAssetsRequest 新增 required_clips_count: Optional[int]
    • 不传时默认等于素材数量(向后兼容)
    • 传入时严格创建指定数量的片段

端点逻辑

  1. 素材时长从数据库获取:遍历 asset_repo.get() 获取实际 duration
  2. 严格按 required_clips_count 创建片段:模板要 N 个就创建 N 个
  3. 轮询分配素材asset_ids[i % len(asset_ids)],素材不足时同一素材切多个片段
  4. 随机起始时间 + 去重:调用 _calc_random_start_timeused_segments 追踪已用区间
  5. 时长自适应:素材不足 5s 时 clip_duration = min(5.0, asset_total)
  6. order 修复:从 0 开始连续递增

测试(7个)

  • test_creates_exactly_required_clips_count:2素材×4片段,验证轮询分配
  • test_orders_are_sequential:order 0,1,2 连续
  • test_backward_compatible_default_count:不传参数时片段数=素材数
  • test_clip_duration_shortened_for_short_assets:3s素材→duration=3.0
  • test_start_time_passed_to_create_clip:验证随机start_time传入
  • test_asset_durations_fetched_from_db:验证从DB获取时长
  • test_used_segments_passed_to_calc:验证used_segments逐步累积
## 问题 `create_clips_from_assets_editor` 接口存在多个问题: 1. duration 硬编码为 5.0 2. 每个素材只能创建1个片段(素材数 < 片段数时无法满足模板要求) 3. 没有调用 `_calc_random_start_time` 4. 没有使用 `used_segments` 追踪已使用区间 5. `order` 有 bug(使用了不存在的 `body.start_order`,hasattr 永远 False,回退到 i) ## 修复 ### Schema - `ClipsFromAssetsRequest` 新增 `required_clips_count: Optional[int]` - 不传时默认等于素材数量(向后兼容) - 传入时严格创建指定数量的片段 ### 端点逻辑 1. **素材时长从数据库获取**:遍历 `asset_repo.get()` 获取实际 duration 2. **严格按 required_clips_count 创建片段**:模板要 N 个就创建 N 个 3. **轮询分配素材**:`asset_ids[i % len(asset_ids)]`,素材不足时同一素材切多个片段 4. **随机起始时间 + 去重**:调用 `_calc_random_start_time`,`used_segments` 追踪已用区间 5. **时长自适应**:素材不足 5s 时 `clip_duration = min(5.0, asset_total)` 6. **order 修复**:从 0 开始连续递增 ## 测试(7个) - `test_creates_exactly_required_clips_count`:2素材×4片段,验证轮询分配 - `test_orders_are_sequential`:order 0,1,2 连续 - `test_backward_compatible_default_count`:不传参数时片段数=素材数 - `test_clip_duration_shortened_for_short_assets`:3s素材→duration=3.0 - `test_start_time_passed_to_create_clip`:验证随机start_time传入 - `test_asset_durations_fetched_from_db`:验证从DB获取时长 - `test_used_segments_passed_to_calc`:验证used_segments逐步累积
xiaoxia added 1 commit 2026-08-26 21:06:51 +08:00
fix(editor): from-assets端点接入随机起始时间+去重+素材时长自适应
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 23s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / PR Build API Image (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Web Image (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
PR Automation / Auto Approve on CI Green (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
Preview Deploy / Deploy Preview Environment (pull_request) Has been cancelled
4e4f456bbe
- 从数据库获取素材实际时长(不再硬编码 duration=5.0)
- 调用 _calc_random_start_time 为每个clip计算随机start_time
- 使用 used_segments 追踪已使用时间段,防止重复
- 素材时长不足5s时自动缩短clip duration
- 新增4个单测覆盖核心逻辑
xiaoxia added 1 commit 2026-08-26 21:21:54 +08:00
feat(editor): 支持同一素材切多个片段+required_clips_count
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 2m20s
AI Code Review / AI Code Review (pull_request) Successful in 3m5s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m26s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m41s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2m26s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 5m7s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 6m53s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 6m6s
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 1m47s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 56s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 11m35s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 9m1s
CI/CD Pipeline / CI Gate (pull_request) Failing after 26s
6fc73567c6
- ClipsFromAssetsRequest 增加 required_clips_count 可选参数
- 模板要求N个片段时严格创建N个,素材不足时轮询同一素材
- order 从0连续递增(修复之前的 start_order bug)
- 每个片段从素材随机选不重复时间段,used_segments 去重
- 素材时长不足5s时缩短clip duration
- 向后兼容:不传 required_clips_count 时等于素材数量
- 7个单测覆盖:数量/轮询/顺序/时长缩短/start_time/used_segments/兼容
xiaoxia changed title from fix(editor): from-assets端点接入随机起始时间+去重+素材时长自适应 to feat(editor): from-assets支持同素材多片段+随机起始时间+去重+required_clips_count 2026-08-26 21:22:12 +08:00

🚀 预览环境已部署

项目 详情
PR号 #1512
预览链接 https://pr-1512.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1512 | | 预览链接 | [https://pr-1512.preview.xiaoxiajianji.com](https://pr-1512.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot added 1 commit 2026-08-26 21:38:48 +08:00
style: auto-format with black + isort + prettier [skip ci-format-check]
CI/CD Pipeline / Check if frontend-only change (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Lint (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / PR Build API Image (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Web Image (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Staging API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m20s
AI Code Review / AI Code Review (pull_request) Failing after 4m2s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 3m54s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 3m33s
ce635a0976
xiaoxia added 1 commit 2026-08-26 22:00:00 +08:00
fix(editor): 修复AI Review 3个阻塞问题
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
PR Automation / Auto Approve on CI Green (pull_request) Failing after 0s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2m21s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
AI Code Review / AI Code Review (pull_request) Failing after 3m49s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 3m48s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 3m57s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m36s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 2m11s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 5m32s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 6m6s
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
dc3ce8f114
1. order追加到时间线末尾:查询现有clips最大order+1,不再从0开始覆盖
2. start_time=None时抛HTTPException 400(素材时长缺失),不再强制0.0导致重叠
3. except ValueError记录logger.error并抛400,不再裸except吞异常
4. asset_ids去重后批量查询时长,避免重复DB查询
5. 测试增至10个:新增order追加/空时间线/400异常/去重场景
xiaoxia added 1 commit 2026-08-26 22:06:19 +08:00
fix(editor): 响应AI Review;默认时长提为模块常量;list_clips显式List类型标注
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2m22s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 3m42s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 3m59s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m52s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 4m49s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 2m34s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 5m21s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 6m1s
AI Code Review / AI Code Review (pull_request) Successful in 7m33s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 19m42s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 17m20s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 8m25s
CI/CD Pipeline / CI Gate (pull_request) Successful in 29s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 4m9s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 8m53s
46fc84902e
Collaborator

代码审查结果 - PR #1512

⚠️ 问题(2个需要修改)

  1. apps/api/app/api/routes/templates_editor/clips.py 第373-387行:并发竞态条件导致数据冲突

    • 问题描述:代码先查询 list_clips 计算最大 order,随后在循环中使用 order=next_order + i 创建片段。在高并发场景下,两个请求可能同时读取到相同的最大 order,导致创建的片段 order 重复(主键冲突或业务逻辑混乱)。
    • 后果:可能导致数据库唯一索引冲突报错,或时间线片段顺序错乱。
    • 修改建议:应在数据库层面使用原子操作(如 SELECT ... FOR UPDATE 锁定 plan 记录)或利用数据库的自增/序列机制来保证 order 的唯一性和连续性,避免在应用层计算。
  2. apps/api/app/api/routes/templates_editor/clips.py 第357-397行:随机去重逻辑遗漏已存在片段

    • 问题描述used_segments 仅用于记录本次请求中新生成的片段时间段,未包含 existing_clips_list 中已存在的片段。如果素材已被用于创建片段,再次调用该接口时,新计算的随机 start_time 可能与旧片段的时间段重叠。
    • 后果:同一素材生成的多个片段在时间轴上内容重叠,违反“随机选取不重复时间段”的业务预期。
    • 修改建议:在初始化 used_segments 时,应先遍历 existing_clips_list,将已存在的同素材片段时间段填入 used_segments,确保全局去重。

💡 建议(2个可选)

  1. apps/api/app/api/routes/templates_editor/clips.py 第373行:性能优化建议

    • 具体内容plan_svc.list_clips(plan_id) 会拉取该 plan 下的所有片段对象,仅为了计算 max(order)。当片段数量较多时,这会造成不必要的内存和查询开销。建议改为直接执行 SELECT MAX(order) FROM ... 查询。
  2. apps/api/app/api/routes/templates_editor/clips.py 第159、273、303行:代码可读性建议

    • 具体内容:部分代码将多行逻辑压缩为单行(如列表推导式和 HTTPException 抛出),虽然符合语法,但降低了可读性并导致单行过长。建议适当展开以保持代码清晰。

格式检查通过 | 逻辑审查需修改 | ⚠️ 建议关注性能


🤖 由 AI 代码审查机器人自动生成 | 2026-08-26 14:13:52 | 模型:

## 代码审查结果 - PR #1512 ### ⚠️ 问题(2个需要修改) 1. **apps/api/app/api/routes/templates_editor/clips.py 第373-387行**:并发竞态条件导致数据冲突 - **问题描述**:代码先查询 `list_clips` 计算最大 `order`,随后在循环中使用 `order=next_order + i` 创建片段。在高并发场景下,两个请求可能同时读取到相同的最大 `order`,导致创建的片段 `order` 重复(主键冲突或业务逻辑混乱)。 - **后果**:可能导致数据库唯一索引冲突报错,或时间线片段顺序错乱。 - **修改建议**:应在数据库层面使用原子操作(如 `SELECT ... FOR UPDATE` 锁定 plan 记录)或利用数据库的自增/序列机制来保证 `order` 的唯一性和连续性,避免在应用层计算。 2. **apps/api/app/api/routes/templates_editor/clips.py 第357-397行**:随机去重逻辑遗漏已存在片段 - **问题描述**:`used_segments` 仅用于记录本次请求中新生成的片段时间段,未包含 `existing_clips_list` 中已存在的片段。如果素材已被用于创建片段,再次调用该接口时,新计算的随机 `start_time` 可能与旧片段的时间段重叠。 - **后果**:同一素材生成的多个片段在时间轴上内容重叠,违反“随机选取不重复时间段”的业务预期。 - **修改建议**:在初始化 `used_segments` 时,应先遍历 `existing_clips_list`,将已存在的同素材片段时间段填入 `used_segments`,确保全局去重。 ### 💡 建议(2个可选) 1. **apps/api/app/api/routes/templates_editor/clips.py 第373行**:性能优化建议 - **具体内容**:`plan_svc.list_clips(plan_id)` 会拉取该 plan 下的所有片段对象,仅为了计算 `max(order)`。当片段数量较多时,这会造成不必要的内存和查询开销。建议改为直接执行 `SELECT MAX(order) FROM ...` 查询。 2. **apps/api/app/api/routes/templates_editor/clips.py 第159、273、303行**:代码可读性建议 - **具体内容**:部分代码将多行逻辑压缩为单行(如列表推导式和 HTTPException 抛出),虽然符合语法,但降低了可读性并导致单行过长。建议适当展开以保持代码清晰。 --- ❌ 格式检查通过 | ❌ 逻辑审查需修改 | ⚠️ 建议关注性能 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-08-26 14:13:52 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
xiaoxia merged commit 2c1af458b9 into develop 2026-08-26 22:40:02 +08:00

🗑️ 预览环境已清理

PR #1512 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1512 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.