feat(#1970): 智能降重 PR2 - dedup_enabled 开关 + 6 维片段级微变换 (#1975)
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 1s
CI/CD Pipeline / Check push changed paths (push) Successful in 2s
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 52s
CI/CD Pipeline / Build Staging API Image (push) Successful in 1m4s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 1m25s
CI/CD Pipeline / Retag skipped Staging API Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 46s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 3m43s
CI/CD Pipeline / Integration Tests (push) Successful in 3m45s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 4m8s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 2m21s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 3m36s
CI/CD Pipeline / Validate - Style (push) Successful in 6m0s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 3m46s
CI/CD Pipeline / Validate - Security (push) Successful in 8m2s
CI/CD Pipeline / Unit Tests (push) Successful in 9m27s
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / CI Gate (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Canary Release to Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped

Co-authored-by: xiaoxia <dev@xiaoxiajianji.com>
Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
This commit was merged in pull request #1975.
This commit is contained in:
2026-09-18 05:44:59 +08:00
committed by auto-approve-bot
parent f1621ace9f
commit a59a6a588a
11 changed files with 801 additions and 32 deletions
@@ -171,6 +171,87 @@ class UnifiedRenderService:
self._speed_engine = SpeedEngine()
self._asr_timeline_cache: Any = None # ASR 字幕结果缓存,避免重复调用
self._asr_timeline_cached = False
# #1970 PR2:片段级微变换计划缓存(懒构建,dedup_enabled=False 时为 None)
self._micro_plan_cache: Any = None
self._micro_plan_loaded = False
# ── #1970 PR2 智能降重:片段级微变换 ───────────────────────────────────
def _dedup_enabled(self) -> bool:
"""读取 plan.config.dedup_enabled,缺省视为 True(向后兼容)。"""
cfg = self.plan.config or {}
return bool(cfg.get("dedup_enabled", True))
def _get_micro_transform_plan(self, clip_count: int) -> Any:
"""按 task_id+视频序号构建可复现的片段级微变换计划。
种子 hash(generation_task_id + video_index)%10000,同一任务重渲结果一致。
dedup_enabled=False 时返回 None,调用方不注入任何微变换。
P1 字幕检测:无可靠的片段文字轨道信息,hflip 一律关闭(宁可不翻转)。
"""
if self._micro_plan_loaded:
return self._micro_plan_cache
self._micro_plan_loaded = True
if not self._dedup_enabled() or clip_count <= 0:
self._micro_plan_cache = None
return None
try:
from video_processing.micro_transform_pure import build_micro_transform_plan
cfg = self.plan.config or {}
task_id = str(cfg.get("generation_task_id", "") or "")
video_index = int(cfg.get("video_index", 0) or 0)
self._micro_plan_cache = build_micro_transform_plan(
task_id,
video_index,
clip_count,
clip_has_text=None, # P1 保守策略:全部按有文字处理,不翻转
enable_bgm_offset=bool(cfg.get("bgm")),
)
except Exception as e:
logger.warning("[unified-render] 微变换计划构建失败,本次不注入: %s", e)
self._micro_plan_cache = None
return self._micro_plan_cache
@staticmethod
def _apply_micro_transform_video(filters: list[str], mt: Any) -> None:
"""把片段视频微变换就地追加到 filter 链(post-scale 阶段调用)。
顺序:hflip 在 pre-scale 阶段由 _apply_micro_hflip 处理,这里只加
eq 亮度/对比度/饱和度。速度 setpts 与既有 clip speed 相乘(见调用点),
避免出现两条 setpts 互相覆盖。
"""
if mt is None:
return
if abs(mt.brightness) > 1e-4 or abs(mt.contrast - 1.0) > 1e-4 or abs(mt.saturation - 1.0) > 1e-4:
filters.append(
f"eq=brightness={mt.brightness:+.4f}:" f"contrast={mt.contrast:.4f}:saturation={mt.saturation:.4f}"
)
@staticmethod
def _apply_micro_hflip(filters: list[str], mt: Any) -> None:
"""片段级水平翻转(pre-scale 阶段)。P1 有文字/无法判定时 mt.hflip=False。"""
if mt is not None and mt.hflip and not mt.has_text:
filters.append("hflip")
@staticmethod
def _micro_speed_factor(mt: Any) -> float:
"""片段微变换速度因子(0.97~1.03),无计划返回 1.0。"""
if mt is None:
return 1.0
return float(getattr(mt, "speed", 1.0) or 1.0)
def _get_micro_bgm_offset(self) -> float:
"""#1970 PR2:读取本视频 BGM 起始偏移(秒),无 BGM/禁用时为 0。"""
if not self.plan.config:
return 0.0
try:
count = len([c for c in (self.plan.clips or []) if getattr(c, "clip_type", "main") != "audio"])
plan = self._get_micro_transform_plan(count)
if plan:
return round(float(plan.bgm_start_offset or 0.0), 3)
except Exception:
logger.debug("微变换 BGM 偏移读取失败,按 0 处理: plan_id=%s", getattr(self.plan, "id", "?"))
return 0.0
def render(self) -> RenderResult:
"""执行渲染,返回 RenderResult.
@@ -316,6 +397,9 @@ class UnifiedRenderService:
ctx = RenderContext(work_dir=self.work_dir, plan_id=self.plan.id)
from video_processing.bgm_mixer import BGMConfig, mix_bgm_with_main
_bgm_off = self._get_micro_bgm_offset()
if _bgm_off and not (bgm_config or {}).get("audio_offset"):
bgm_config = {**bgm_config, "audio_offset": _bgm_off}
bgm_cfg = BGMConfig.from_config_dict(self.bgm_path, bgm_config)
# 从直通输出中提取音频
main_audio_path = self.work_dir / f"pass_through_audio_{self.plan.id}.aac"
@@ -365,6 +449,7 @@ class UnifiedRenderService:
bgm_path=self.bgm_path,
bgm_config=bgm_config,
audio_tracks_config=audio_tracks_config,
bgm_start_offset=self._get_micro_bgm_offset(),
)
t_audio_end = time.time()
audio_mix_ms = int((t_audio_end - t_audio_start) * 1000)
@@ -1112,6 +1197,28 @@ class UnifiedRenderService:
if ass_path is not None:
return False, "有字幕叠加"
# #1970 PR2:片段级微变换(变速/hflip/亮度/对比度/饱和度)需要重编码
try:
_video_sources = [c for c in (self.clips or []) if getattr(c, "clip_type", "main") != "audio"]
_ordinal = -1
for _i, _c in enumerate(_video_sources):
if getattr(_c, "id", None) == getattr(clip, "clip_id", None):
_ordinal = _i
break
_mt_plan = self._get_micro_transform_plan(len(_video_sources))
if _mt_plan and 0 <= _ordinal < len(_mt_plan.clips):
_mt = _mt_plan.clips[_ordinal]
if (
abs(UnifiedRenderService._micro_speed_factor(_mt) - 1.0) >= 1e-6
or (_mt.hflip and not _mt.has_text)
or abs(_mt.brightness) > 1e-4
or abs(_mt.contrast - 1.0) > 1e-4
or abs(_mt.saturation - 1.0) > 1e-4
):
return False, "启用了片段级微变换"
except Exception:
logger.debug("stream copy 微变换门控检查异常,按可 copy 处理", exc_info=True)
# 有调速 → 需要重编码 → 不能 copy
speed = UnifiedRenderService._clip_speed(clip)
if abs(speed - 1.0) >= 1e-6:
@@ -1318,11 +1425,16 @@ class UnifiedRenderService:
# 视觉扰动(plan 级别,直通模式同样适用)
vp = self._get_visual_perturbation()
# #1970 PR2:单片段直通;计划按源视频片段数构建,序号取 config._micro_index
_src_video_count = len([c for c in (self.clips or []) if getattr(c, "clip_type", "main") != "audio"])
mt_plan = self._get_micro_transform_plan(max(1, _src_video_count))
_mi = int(clip.config.get("_micro_index", 0)) if isinstance(clip.config, dict) else 0
mt = mt_plan.clips[_mi] if mt_plan and 0 <= _mi < len(mt_plan.clips) else None
# 调速 — 与 filter_complex 路径一致(叠加视觉扰动 speed_factor)
# 调速 — 与 filter_complex 路径一致(叠加视觉扰动 speed_factor 与 #1970 微变换速度)
speed = UnifiedRenderService._clip_speed(clip)
vp_speed = vp.get("speed_factor", 1.0) if vp else 1.0
effective_speed = speed * vp_speed
effective_speed = speed * vp_speed # 微变换速度已烘焙进 playback_speed
if abs(effective_speed - 1.0) >= 1e-6:
filters.append(f"setpts=PTS/{effective_speed:.4f}")
@@ -1336,6 +1448,8 @@ class UnifiedRenderService:
# 视觉扰动:hflip(在 scale 之前)
if vp:
self._apply_visual_perturbation_pre_scale(filters, vp)
# #1970 PR2:片段级 hflip(P1 保守:有文字/无法判定时不翻转)
UnifiedRenderService._apply_micro_hflip(filters, mt)
# scale + pad(等比缩放+留黑边)
if role in ("overlay", "corner_voice"):
@@ -1354,6 +1468,8 @@ class UnifiedRenderService:
# 视觉扰动:zoom + brightness(在 scale+pad 之后、调色之前)
if vp:
self._apply_visual_perturbation_post_scale(filters, vp)
# #1970 PR2:片段级亮度/对比度/饱和度微调
UnifiedRenderService._apply_micro_transform_video(filters, mt)
# 调色滤镜
color_grade = ColorGradeConfig.from_dict(clip.config.get("color_grade"))
@@ -1450,7 +1566,8 @@ class UnifiedRenderService:
# 音频调速(在降噪之后、音量之前,与 render_audio.py concat 路径保持一致)
# SpeedEngine.build_audio_filter 内部已实现多级 atempo 串联,
# 自动处理超出 [0.5, 2.0] 范围的速度(如 0.25x → atempo=0.5,atempo=0.5)。
speed = UnifiedRenderService._clip_speed(clip)
# #1970 PR2:叠加片段微变换速度因子,保持音画同步。
speed = UnifiedRenderService._clip_speed(clip) # 微变换速度已烘焙进 playback_speed
if abs(speed - 1.0) >= 1e-6:
try:
from video_processing.speed_engine import SpeedConfig, SpeedEngine
@@ -1522,11 +1639,20 @@ class UnifiedRenderService:
支持多段裁剪:一个 clip 配置了 trim_segments 时会展开为多个 ResolvedClip。
"""
resolved: list[ResolvedClip] = []
# #1970 PR2:预建片段级微变换计划,按源视频片段序号取速度因子,
# 烘焙进 playback_speed,保证视频 setpts 与音频 atempo 一致。
video_source_clips = [c for c in self.clips if getattr(c, "clip_type", "main") != "audio"]
mt_plan = self._get_micro_transform_plan(len(video_source_clips))
_video_ordinal = {id(c): i for i, c in enumerate(video_source_clips)}
for clip in self.clips:
asset_id = clip.asset_id
if not asset_id:
logger.warning("片段无素材: clip_id=%s", clip.id)
continue
_mt_idx = _video_ordinal.get(id(clip), -1)
_mt = mt_plan.clips[_mt_idx] if mt_plan and 0 <= _mt_idx < len(mt_plan.clips) else None
_micro_speed = UnifiedRenderService._micro_speed_factor(_mt)
local_path = self.asset_path_map.get(asset_id)
if local_path is None or not local_path.exists():
@@ -1555,7 +1681,7 @@ class UnifiedRenderService:
seg_duration = seg.trim.duration
# 多段裁剪:如果段的时长超过素材实际时长,减速补偿
seg_speed = configured_speed
seg_speed = configured_speed * _micro_speed
if actual_duration > 0 and seg_duration > actual_duration + 0.05:
seg_speed = max(0.25, round(configured_speed * actual_duration / seg_duration, 4))
logger.info(
@@ -1578,7 +1704,7 @@ class UnifiedRenderService:
transition_effect=clip.transition_effect or "cut",
transition_duration=getattr(clip, "transition_duration", 0.0) or 0.0,
playback_speed=seg_speed,
config={**clip_config, "_segment_id": seg.segment_id},
config={**clip_config, "_segment_id": seg.segment_id, "_micro_index": _mt_idx},
actual_duration=actual_duration,
trim_config=seg.trim,
)
@@ -1633,12 +1759,13 @@ class UnifiedRenderService:
avail_in_asset,
freeze_seconds,
)
final_speed = configured_speed
final_speed = configured_speed * _micro_speed
# freeze 标记写入 config,供视频 tpad / 音频 apad 读取
resolved_config = dict(clip_config)
if freeze_seconds > 0:
resolved_config["_freeze_seconds"] = freeze_seconds
resolved_config["_micro_index"] = _mt_idx
rc = ResolvedClip(
clip_id=clip.id,
@@ -1754,9 +1881,15 @@ class UnifiedRenderService:
preprocessed_labels: list[str] = []
# 视觉扰动(plan 级别,所有 clip 共享同一套扰动参数)
vp = self._get_visual_perturbation()
# #1970 PR2:片段级微变换(每片段独立参数,dedup_enabled=False 时为 None)
# 计划按源视频片段数构建,trim 多段展开时各段通过 config._micro_index 找参数
_src_video_count = len([c for c in (self.clips or []) if getattr(c, "clip_type", "main") != "audio"])
mt_plan = self._get_micro_transform_plan(_src_video_count)
for i, clip in enumerate(all_clips):
label = f"v{i}"
role = _resolve_layer_role(clip.clip_type, clip.config)
_mi = int(clip.config.get("_micro_index", i)) if isinstance(clip.config, dict) else i
mt = mt_plan.clips[_mi] if mt_plan and 0 <= _mi < len(mt_plan.clips) else None
filters: list[str] = []
@@ -1774,10 +1907,10 @@ class UnifiedRenderService:
filters.append(f"trim=duration={trim_dur:.3f}")
filters.append("setpts=PTS-STARTPTS")
# 调速 — 基于 setpts 改变播放速度(叠加视觉扰动 speed_factor)
# 调速 — 基于 setpts 改变播放速度(叠加视觉扰动 speed_factor 与 #1970 微变换速度)
speed = UnifiedRenderService._clip_speed(clip)
vp_speed = vp.get("speed_factor", 1.0) if vp else 1.0
effective_speed = speed * vp_speed
effective_speed = speed * vp_speed # 微变换速度已烘焙进 playback_speed
if abs(effective_speed - 1.0) >= 1e-6:
filters.append(f"setpts=PTS/{effective_speed:.4f}")
@@ -1791,6 +1924,8 @@ class UnifiedRenderService:
# 视觉扰动:hflip(在 scale 之前,翻转原始画面)
if vp:
self._apply_visual_perturbation_pre_scale(filters, vp)
# #1970 PR2:片段级 hflip(P1 保守:有文字/无法判定时不翻转)
UnifiedRenderService._apply_micro_hflip(filters, mt)
# scale
if role in ("overlay", "corner_voice"):
@@ -1809,6 +1944,8 @@ class UnifiedRenderService:
# 视觉扰动:zoom + brightness(在 scale+pad 之后、调色之前)
if vp:
self._apply_visual_perturbation_post_scale(filters, vp)
# #1970 PR2:片段级亮度/对比度/饱和度微调
UnifiedRenderService._apply_micro_transform_video(filters, mt)
# 调色滤镜(每个 clip 独立的 color grade 配置)
color_grade = ColorGradeConfig.from_dict(clip.config.get("color_grade"))