feat: 片段时长自动对齐配音时长 #1693

Merged
xiaoxia merged 1 commits from feature/clip-duration-voice-alignment into develop 2026-09-04 16:09:24 +08:00
Owner

需求

后端生成视频时,片段总时长自动对齐配音时长,最终视频时长 = 配音时长。

实现

核心逻辑(在渲染前执行)

  1. 探测配音音频时长 (_get_voice_audio_duration)

    • 使用 probe_duration 获取配音文件时长
    • 无配音或探测失败时返回 0.0
  2. 调整片段时长 (_align_clips_to_voice_duration)

    • 计算片段总时长与配音时长的比例 ratio = voice_duration / total_clips_duration
    • abs(ratio - 1.0) <= 0.05(±5% 以内)→ 不调整
    • ratio < 1.0(片段比配音长)→ 按比例裁剪每段末尾
    • ratio > 1.0(片段比配音短)→ 按比例慢放每段(下限 0.25x)
  3. 调用时机

    • render() 中,clips 分组为 layers 后、计算视频总时长前调用
    • 调整后重新计算 video_duration_final 用于后续渲染

调整范围

  • 调整:mainbrollbackground 视频图层
  • 不调整:audiooverlaypip 等非视频图层

边界处理

  • 速度下限 0.25x(避免过慢)
  • 裁剪下限 0.1s(避免过短)
  • trim_config 同步调整

测试

新增 tests/unit/test_voice_duration_alignment.py(11 个测试):

  • test_no_voice_audio_no_adjustment - 无配音不调整
  • test_ratio_within_5_percent_no_adjustment - ±5% 内不调整
  • test_ratio_less_than_1_trim_clips - ratio < 1 裁剪
  • test_ratio_greater_than_1_slowdown_clips - ratio > 1 减速
  • test_speed_lower_bound_025 - 速度下限 0.25x
  • test_only_video_layers_adjusted - 只调整视频图层
  • test_multiple_video_layers_all_adjusted - 多图层全部调整
  • test_trim_config_also_adjusted - trim_config 同步调整
  • test_no_voiceover_path_returns_zero - 无路径返回 0
  • test_nonexistent_file_returns_zero - 文件不存在返回 0
  • test_probes_duration_from_file - 正确探测时长

153 个相关测试全通过

未实现的边界场景

以下场景本次未实现,后续如有需要可补充:

  • ratio > 1.1 时最后一段末尾冻结定格(需要 FFmpeg tpad 滤镜)
  • ratio > 1.5 时最后一段循环播放(需要复制 clip 或 FFmpeg loop 滤镜)
## 需求 后端生成视频时,片段总时长自动对齐配音时长,最终视频时长 = 配音时长。 ## 实现 ### 核心逻辑(在渲染前执行) 1. **探测配音音频时长** (`_get_voice_audio_duration`) - 使用 `probe_duration` 获取配音文件时长 - 无配音或探测失败时返回 0.0 2. **调整片段时长** (`_align_clips_to_voice_duration`) - 计算片段总时长与配音时长的比例 `ratio = voice_duration / total_clips_duration` - `abs(ratio - 1.0) <= 0.05`(±5% 以内)→ 不调整 - `ratio < 1.0`(片段比配音长)→ 按比例裁剪每段末尾 - `ratio > 1.0`(片段比配音短)→ 按比例慢放每段(下限 0.25x) 3. **调用时机** - 在 `render()` 中,clips 分组为 layers 后、计算视频总时长前调用 - 调整后重新计算 `video_duration_final` 用于后续渲染 ### 调整范围 - ✅ 调整:`main`、`broll`、`background` 视频图层 - ❌ 不调整:`audio`、`overlay`、`pip` 等非视频图层 ### 边界处理 - 速度下限 0.25x(避免过慢) - 裁剪下限 0.1s(避免过短) - trim_config 同步调整 ## 测试 新增 `tests/unit/test_voice_duration_alignment.py`(11 个测试): - `test_no_voice_audio_no_adjustment` - 无配音不调整 - `test_ratio_within_5_percent_no_adjustment` - ±5% 内不调整 - `test_ratio_less_than_1_trim_clips` - ratio < 1 裁剪 - `test_ratio_greater_than_1_slowdown_clips` - ratio > 1 减速 - `test_speed_lower_bound_025` - 速度下限 0.25x - `test_only_video_layers_adjusted` - 只调整视频图层 - `test_multiple_video_layers_all_adjusted` - 多图层全部调整 - `test_trim_config_also_adjusted` - trim_config 同步调整 - `test_no_voiceover_path_returns_zero` - 无路径返回 0 - `test_nonexistent_file_returns_zero` - 文件不存在返回 0 - `test_probes_duration_from_file` - 正确探测时长 **153 个相关测试全通过** ## 未实现的边界场景 以下场景本次未实现,后续如有需要可补充: - `ratio > 1.1` 时最后一段末尾冻结定格(需要 FFmpeg `tpad` 滤镜) - `ratio > 1.5` 时最后一段循环播放(需要复制 clip 或 FFmpeg `loop` 滤镜)
xiaoxia added 1 commit 2026-09-04 16:05:51 +08:00
feat: 片段时长自动对齐配音时长
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 2s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 3s
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 / PR Build API Image (pull_request) Successful in 29s
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 24s
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 / 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 / Unit Tests (pull_request) Successful in 1m29s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m44s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m51s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 1m52s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 2m56s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 35s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 10s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 36s
AI Code Review / AI Code Review (pull_request) Successful in 6m23s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 7m3s
CI/CD Pipeline / Validate - Style (pull_request) Failing after 22m52s
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 / CI Gate (pull_request) Failing after 2s
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
2bf63c0662
核心逻辑(在渲染前执行):
- 探测配音音频文件时长
- 计算片段总时长与配音时长的比例
- ±5% 以内不调整
- ratio < 1(片段比配音长):按比例裁剪每段末尾
- ratio > 1(片段比配音短):按比例慢放每段(下限 0.25x)

实现:
- 新增 _get_voice_audio_duration() 方法探测配音音频时长
- 新增 _align_clips_to_voice_duration() 方法调整片段时长
- 在 render() 中 clips 分组后、时长计算前调用对齐逻辑
- 调整后重新计算视频总时长用于后续渲染

测试:
- 新增 11 个单元测试覆盖各种场景
- 153 个相关测试全通过

🚀 预览环境已部署

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

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

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

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

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1693 | | 预览链接 | [https://pr-1693.preview.xiaoxiajianji.com](https://pr-1693.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
xiaoxia reviewed 2026-09-04 16:09:18 +08:00
xiaoxia left a comment
Author
Owner

LGTM CI 全绿

LGTM ✅ CI 全绿
xiaoxia merged commit db244fe14c into develop 2026-09-04 16:09:24 +08:00

🗑️ 预览环境已清理

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

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

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