fix(render): 修复生成视频时长短于模板要求时长 #1614

Merged
xiaoxia merged 2 commits from fix/video-duration-mismatch into develop 2026-09-01 20:00:12 +08:00
Owner

问题

模板要求14s视频,渲染输出只有12s,少了2s。

根因

  1. 素材截短_resolve_clipsmin(duration, actual_duration) 在素材实际时长不足时截短 clip
  2. xfade offset 计算错误xfade_builder.pyoffset = cumulative - td * i 未考虑前序转场的累积时长缩减

修复

1. 素材不足时自动减速补偿(unified_render_service.py)

  • _resolve_clips: 当 final_duration > actual_duration 时,自动降低 playback_speed = actual_duration / final_duration
  • _clip_effective_duration: 减速场景(speed < 1)返回配置的 duration 而非 min(duration, actual)
  • 效果:配置4s但素材仅3s → speed=0.75x,用满3s素材达到4s输出

2. xfade offset 修正(xfade_builder.py)

  • 原公式:offset = cumulative - td * i(错误,offset 分布不均)
  • 新公式:offset = first_input_dur - td(正确,每个转场等间距)

3. from-assets 转场补偿(clips.py)

  • 分配 clip 时长时加入补偿:raw_duration += (n-1) * td / n
  • 确保渲染后视频总时长 = 模板设定总时长

4. Debug 日志

  • 每个 clip 的 duration/actual/effective/speed
  • 各图层时长明细和 estimated video_duration
  • xfade 链的 durations 和 estimated_dur

测试

  • 新增 test_duration_compensation.py(4 个测试用例)
  • 修复 PR #1611 遗留测试(SEGMENT_EDGE_GAP=1.5 适配)
  • 全量 13988 测试通过
## 问题 模板要求14s视频,渲染输出只有12s,少了2s。 ## 根因 1. **素材截短**:`_resolve_clips` 中 `min(duration, actual_duration)` 在素材实际时长不足时截短 clip 2. **xfade offset 计算错误**:`xfade_builder.py` 的 `offset = cumulative - td * i` 未考虑前序转场的累积时长缩减 ## 修复 ### 1. 素材不足时自动减速补偿(unified_render_service.py) - `_resolve_clips`: 当 `final_duration > actual_duration` 时,自动降低 `playback_speed = actual_duration / final_duration` - `_clip_effective_duration`: 减速场景(speed < 1)返回配置的 `duration` 而非 `min(duration, actual)` - 效果:配置4s但素材仅3s → speed=0.75x,用满3s素材达到4s输出 ### 2. xfade offset 修正(xfade_builder.py) - 原公式:`offset = cumulative - td * i`(错误,offset 分布不均) - 新公式:`offset = first_input_dur - td`(正确,每个转场等间距) ### 3. from-assets 转场补偿(clips.py) - 分配 clip 时长时加入补偿:`raw_duration += (n-1) * td / n` - 确保渲染后视频总时长 = 模板设定总时长 ### 4. Debug 日志 - 每个 clip 的 duration/actual/effective/speed - 各图层时长明细和 estimated video_duration - xfade 链的 durations 和 estimated_dur ## 测试 - 新增 `test_duration_compensation.py`(4 个测试用例) - 修复 PR #1611 遗留测试(SEGMENT_EDGE_GAP=1.5 适配) - 全量 13988 测试通过
xiaoxia added 1 commit 2026-09-01 19:36:39 +08:00
fix(render): 修复生成视频时长短于模板要求时长
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 0s
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 / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API 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 4m13s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 5m15s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 5m11s
AI Code Review / AI Code Review (pull_request) Successful in 6m12s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 6m13s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 6m40s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 7m39s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 9m26s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 10m8s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (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
4266c6f9b0
根因分析:
1. 素材实际时长 < 配置时长时,min(duration, actual) 截短 clip
2. xfade offset 计算错误,未考虑前序转场的累积时长缩减

修复:
- _resolve_clips: 素材不足时自动降低 playback_speed 补偿
  (如配置4s但素材仅3s → speed=0.75x,用满3s素材达到4s输出)
- _clip_effective_duration: 减速场景返回配置的 duration
- xfade_builder: offset = first_input_dur - td(正确公式)
  修复后 4 clips × 4s + 3 × 0.5s xfade = 14.5s(正确)
  修复前 = 14.5s 但 offset 分布不均,导致内容错位
- from-assets: 分配时加转场补偿 (n-1)*td/n
  确保最终输出 = 模板设定总时长

新增 debug 日志:
- 每个 clip 的 duration/actual_duration/effective_duration/speed
- 各图层时长明细和 estimated video_duration
- xfade 链的 durations 和 estimated_dur

同步修复 PR #1611 遗留的测试(SEGMENT_EDGE_GAP=1.5 适配)

🚀 预览环境已部署

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

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

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

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

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1614 | | 预览链接 | [https://pr-1614.preview.xiaoxiajianji.com](https://pr-1614.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot added 1 commit 2026-09-01 19:47:15 +08:00
style: auto-format with black + isort + prettier [skip ci-format-check]
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 3s
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 / Check if frontend-only change (pull_request) Successful in 3s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped 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 / PR Build API Image (pull_request) Successful in 1m25s
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
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 4m55s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 48s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 5m46s
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 / Integration Tests (pull_request) Successful in 5m22s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
PR Automation / Auto Approve on CI Green (pull_request) Successful in 6m44s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 7m15s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 7m15s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 7m41s
AI Code Review / AI Code Review (pull_request) Successful in 9m17s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 2m4s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 3m55s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 31m49s
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 / CI Gate (pull_request) Successful in 4s
c49fad83fd
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:否
  • 阻塞级问题数量:0 个

📊 审查概览

  • 整体评价:通过
  • 建议级问题数量:1 个

🔴 阻塞级问题(必须修复)

💡 改进建议(不阻塞合并)

  1. [apps/worker/video_processing/unified_render_service.py] 日志级别优化
    • 具体内容:代码中新增了大量用于调试的 logger.info 日志(如第203-218行、1449-1456行、1514-1523行、1544-1552行、1762-1769行)。在生产环境中,如果视频片段较多或并发渲染量大,这些详细日志会产生巨大的 I/O 开销和磁盘占用,影响性能。建议将所有标记为 [debug] 的日志级别调整为 logger.debug,以便仅在需要排查问题时开启。

良好实践

  1. 时长补偿逻辑闭环clips.py 中预先增加 transition_compensation 请求更长的素材,而在 unified_render_service.py 中通过 playback_speed 减速来兜底处理素材不足的情况,形成了良好的容错机制。
  2. 边界条件保护:在进行除法运算(如速度计算)前均检查了 actual_duration > 0,避免了除以零的风险;对计算出的速度进行了 max(0.25, ...) 下限钳制,防止速度过低导致渲染异常。
  3. 测试覆盖:新增了 test_duration_compensation.py 并更新了相关断言,确保了新逻辑的测试覆盖率。

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


🤖 由 AI 代码审查机器人自动生成 | 2026-09-01 11:56:32 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:否 - 阻塞级问题数量:0 个 ### 📊 审查概览 - 整体评价:通过 - 建议级问题数量:1 个 ### 🔴 阻塞级问题(必须修复) 无 ### 💡 改进建议(不阻塞合并) 1. **[apps/worker/video_processing/unified_render_service.py] 日志级别优化** - 具体内容:代码中新增了大量用于调试的 `logger.info` 日志(如第203-218行、1449-1456行、1514-1523行、1544-1552行、1762-1769行)。在生产环境中,如果视频片段较多或并发渲染量大,这些详细日志会产生巨大的 I/O 开销和磁盘占用,影响性能。建议将所有标记为 `[debug]` 的日志级别调整为 `logger.debug`,以便仅在需要排查问题时开启。 ### ✅ 良好实践 1. **时长补偿逻辑闭环**:`clips.py` 中预先增加 `transition_compensation` 请求更长的素材,而在 `unified_render_service.py` 中通过 `playback_speed` 减速来兜底处理素材不足的情况,形成了良好的容错机制。 2. **边界条件保护**:在进行除法运算(如速度计算)前均检查了 `actual_duration > 0`,避免了除以零的风险;对计算出的速度进行了 `max(0.25, ...)` 下限钳制,防止速度过低导致渲染异常。 3. **测试覆盖**:新增了 `test_duration_compensation.py` 并更新了相关断言,确保了新逻辑的测试覆盖率。 --- ✅ 格式检查通过 | ✅ 逻辑审查通过 | ⚠️ 建议关注性能 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-09-01 11:56:32 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
xiaoxia merged commit 4aeb1d5b66 into develop 2026-09-01 20:00:12 +08:00

🗑️ 预览环境已清理

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

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

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