feat(worker): 参考爆款视频风格分析模块 video_analyzer(#2051) #2100

Open
xiaoxia wants to merge 1 commits from feat/2051-video-analyzer into develop
Owner

背景

Gitea 工单 #2051【P0 后端】爆款视频 v1.3 参考爆款视频风格分析模块(video_analyzer)。

主要变更

  1. 新文件 apps/worker/viral_video/video_analyzer.py(~950 行),提供 6 步风格分析管线:

    • ① FFmpeg 抽关键帧(每 2s 1 帧 + 场景切换帧)
    • ② PySceneDetect ContentDetector(threshold=27) 镜头分割
    • ③ OpenCV Farneback 光流运镜检测(推/拉/摇/移/zoom/static,三档强度)
    • ④ librosa BPM 分析(>110 fast_cut / 80-110 medium / <80 slow_cinematic)
    • ⑤ OSS 上传关键帧 + 豆包 VLM 色调/构图/光线分析
    • ⑥ 豆包 LLM 整合输出完整 style_guide JSON(失败降级到规则合成)
  2. 渲染参数映射(clips → URS 可直接消费的参数):

    • map_camera_to_ken_burns:11 种运镜 → ken_burns zoom/pan 参数
    • map_transition_to_xfade:转场枚举 → TransitionEngine xfade 名称
    • map_color_to_video_filter:5 套色调滤镜预设 → FFmpeg eq + colorchannelmixer 参数
    • map_bgm_bpm:BPM ±5 选曲提示
    • build_render_params_for_clip(clip_index, style_guide):按镜头索引聚合所有渲染参数
  3. 降级链:

    • FFmpeg 抽帧失败 → VLM 路径;OpenCV 不可用 → static/low;librosa 不可用 → VLM 估计节奏;
    • LLM/VLM 不可用 → 规则合成 style_guide;任意子步骤异常不抛,best-effort 返回最小可用 style_guide。
  4. 资源约束:参考视频 ≤60s / ≤100MB,总超时 60s,临时帧 try/finally 清理。

  5. 依赖新增(requirements-worker.txt):scenedetect==0.6.4、librosa==0.10.2.post1、soundfile==0.12.1。

  6. 编排器适配:apps/worker/worker_app/tasks/viral_video.py 的 _step_video_analysis 改为从 apps.worker.viral_video.video_analyzer 导入,透传 style_strength 参数(light/medium/strict),占位 style_guide 字段与新 schema 对齐。

  7. 单元测试:tests/unit/test_video_analyzer.py 48 条,覆盖映射函数、光流运镜判定(合成光流场)、BPM 档位、规则合成、参数聚合、降级路径、临时文件清理。

注意

  • Prompt(VIDEO_STYLE_INTEGRATION_PROMPT / STYLE_CONSTRAINT_MODULE_PROMPT)由 #2040 提供;本模块通过豆包客户端直接构造 system prompt 调用,不依赖 #2040 的 prompt_store / prompts 模块,等 #2040 合入后可无缝切换到 run_prompt("video_style_integration", ...)。
  • ken_burns_direction_hint 与 storyboard 的 ken_burns 枚举保持一致(zoom_in_slow / pan_left_slow 等)。
  • 本 PR 依赖 #2039 框架合入(viral_video_jobs + orchestrator),若 #2039 未先合入,需在 #2039 合入后再合本 PR。

自检

  • 48 条新单测 + 40 条 viral_video 原有单测,合计 88 条通过
  • ruff check / ruff format 通过
  • 不触碰 #2040 的 prompt 系统
## 背景 Gitea 工单 #2051【P0 后端】爆款视频 v1.3 参考爆款视频风格分析模块(video_analyzer)。 ## 主要变更 1. **新文件 `apps/worker/viral_video/video_analyzer.py`**(~950 行),提供 6 步风格分析管线: - ① FFmpeg 抽关键帧(每 2s 1 帧 + 场景切换帧) - ② PySceneDetect ContentDetector(threshold=27) 镜头分割 - ③ OpenCV Farneback 光流运镜检测(推/拉/摇/移/zoom/static,三档强度) - ④ librosa BPM 分析(>110 fast_cut / 80-110 medium / <80 slow_cinematic) - ⑤ OSS 上传关键帧 + 豆包 VLM 色调/构图/光线分析 - ⑥ 豆包 LLM 整合输出完整 style_guide JSON(失败降级到规则合成) 2. **渲染参数映射**(clips → URS 可直接消费的参数): - `map_camera_to_ken_burns`:11 种运镜 → ken_burns zoom/pan 参数 - `map_transition_to_xfade`:转场枚举 → TransitionEngine xfade 名称 - `map_color_to_video_filter`:5 套色调滤镜预设 → FFmpeg eq + colorchannelmixer 参数 - `map_bgm_bpm`:BPM ±5 选曲提示 - `build_render_params_for_clip(clip_index, style_guide)`:按镜头索引聚合所有渲染参数 3. **降级链**: - FFmpeg 抽帧失败 → VLM 路径;OpenCV 不可用 → static/low;librosa 不可用 → VLM 估计节奏; - LLM/VLM 不可用 → 规则合成 style_guide;任意子步骤异常不抛,best-effort 返回最小可用 style_guide。 4. **资源约束**:参考视频 ≤60s / ≤100MB,总超时 60s,临时帧 try/finally 清理。 5. **依赖新增**(requirements-worker.txt):scenedetect==0.6.4、librosa==0.10.2.post1、soundfile==0.12.1。 6. **编排器适配**:`apps/worker/worker_app/tasks/viral_video.py` 的 `_step_video_analysis` 改为从 `apps.worker.viral_video.video_analyzer` 导入,透传 `style_strength` 参数(light/medium/strict),占位 style_guide 字段与新 schema 对齐。 7. **单元测试**:`tests/unit/test_video_analyzer.py` 48 条,覆盖映射函数、光流运镜判定(合成光流场)、BPM 档位、规则合成、参数聚合、降级路径、临时文件清理。 ## 注意 - Prompt(VIDEO_STYLE_INTEGRATION_PROMPT / STYLE_CONSTRAINT_MODULE_PROMPT)由 #2040 提供;本模块通过豆包客户端直接构造 system prompt 调用,不依赖 #2040 的 prompt_store / prompts 模块,等 #2040 合入后可无缝切换到 `run_prompt("video_style_integration", ...)`。 - `ken_burns_direction_hint` 与 storyboard 的 ken_burns 枚举保持一致(zoom_in_slow / pan_left_slow 等)。 - 本 PR 依赖 #2039 框架合入(viral_video_jobs + orchestrator),若 #2039 未先合入,需在 #2039 合入后再合本 PR。 ## 自检 - [x] 48 条新单测 + 40 条 viral_video 原有单测,合计 88 条通过 - [x] ruff check / ruff format 通过 - [x] 不触碰 #2040 的 prompt 系统

🚀 预览环境已部署

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

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

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

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

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #2100 | | 预览链接 | [https://pr-2100.preview.xiaoxiajianji.com](https://pr-2100.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
xiaoxia force-pushed feat/2051-video-analyzer from b78437c2ff to 68293eb79b 2026-09-29 21:18:08 +08:00 Compare
xiaoxia added 1 commit 2026-09-30 00:06:06 +08:00
feat(worker): 参考爆款视频风格分析模块 video_analyzer(#2051)
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 2s
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 / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker 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 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 1m0s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m37s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m7s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m7s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 7m1s
AI Code Review / AI Code Review (pull_request) Successful in 7m10s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 7m21s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 7m35s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 8m6s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 12m46s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 17m59s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 2s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
9814fcdc22
- 新建 apps/worker/viral_video/video_analyzer.py:6 步管线
  ① FFmpeg 抽关键帧(每 2s + 场景切换帧)
  ② PySceneDetect ContentDetector 镜头分割
  ③ OpenCV Farneback 光流(径向分量法避免平移误判为zoom)
  ④ librosa BPM 分析 → pace 档位
  ⑤ OSS 上传关键帧 + 豆包 VLM 色调/构图/光线分析
  ⑥ 豆包 LLM 整合成 style_guide JSON
- style_guide schema:pace/camera_movements/transitions/color_palette/
  lighting/mood/ken_burns_params(per-shot)/transition_map/
  video_filter_eq_params
- 映射函数:运镜→ken_burns / 转场→xfade / 色调→eq+colorchannelmixer /
  BPM→BGM,并提供 build_render_params_for_clip 按 clip_index 聚合
- 全链路降级(ffmpeg/opencv/librosa/VLM/LLM 任一失败均 best-effort 填充)
- 资源约束:≤60s ≤100MB 超时≤60s try/finally 清理临时帧
- 新增 3 个 worker 依赖:scenedetect / librosa / soundfile
- 48 个单元测试覆盖映射/光流/BPM/降级/清理
- bandit B404/B603 加 nosec,B310 用 httpx 替换 urllib
xiaoxia force-pushed feat/2051-video-analyzer from 3ec9248f0b to 9814fcdc22 2026-09-30 00:06:06 +08:00 Compare
Some checks are pending
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 2s
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 / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker 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 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 1m0s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m37s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m7s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m7s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 7m1s
AI Code Review / AI Code Review (pull_request) Successful in 7m10s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 7m21s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 7m35s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 8m6s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 12m46s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 17m59s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 2s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feat/2051-video-analyzer:feat/2051-video-analyzer
git checkout feat/2051-video-analyzer
Sign in to join this conversation.