Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 15e429ffb1 |
@@ -1187,8 +1187,6 @@ jobs:
|
||||
COSYVOICE_API_KEY: ${{ secrets.COSYVOICE_API_KEY }}
|
||||
DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }}
|
||||
MEDIAKIT_API_KEY: ${{ secrets.MEDIAKIT_API_KEY }}
|
||||
WECHAT_APP_ID: ${{ secrets.WECHAT_APP_ID }}
|
||||
WECHAT_APP_SECRET: ${{ secrets.WECHAT_APP_SECRET }}
|
||||
run: |
|
||||
set -eu
|
||||
echo "Rendering .env from template + secrets..."
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
"""add client_upload_id to assets and asset_id to ingest_jobs
|
||||
|
||||
Issue #1714:上传 complete 幂等 + worker 转码回写关联。
|
||||
- assets.client_upload_id:客户端幂等 token(complete 去重)
|
||||
- ingest_jobs.asset_id:complete 阶段创建的占位 asset id(worker 回写关联,
|
||||
防止 HEVC 转码改写 storage_key 后找不到占位而兜底新建 READY 记录)
|
||||
|
||||
Revision ID: 066_upload_idempotency
|
||||
Revises: 065_dup_record_sim_match
|
||||
Create Date: 2026-09-05
|
||||
"""
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "066_upload_idempotency"
|
||||
down_revision = "065_dup_record_sim_match"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("assets", sa.Column("client_upload_id", sa.String(64), nullable=True))
|
||||
op.create_index("ix_assets_client_upload_id", "assets", ["client_upload_id"])
|
||||
op.add_column("ingest_jobs", sa.Column("asset_id", sa.String(36), nullable=False, server_default=""))
|
||||
op.create_index("ix_ingest_jobs_asset_id", "ingest_jobs", ["asset_id"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_ingest_jobs_asset_id", table_name="ingest_jobs")
|
||||
op.drop_column("ingest_jobs", "asset_id")
|
||||
op.drop_index("ix_assets_client_upload_id", table_name="assets")
|
||||
op.drop_column("assets", "client_upload_id")
|
||||
@@ -14,7 +14,6 @@ from app.core.task_enqueue import (
|
||||
USER_PENDING_LIMIT,
|
||||
GlobalQueueFull,
|
||||
UserPendingLimitExceeded,
|
||||
build_rate_limit_detail,
|
||||
safe_enqueue_generation_task,
|
||||
)
|
||||
from app.dependencies import (
|
||||
@@ -24,7 +23,6 @@ from app.dependencies import (
|
||||
get_generation_task_repository,
|
||||
)
|
||||
from app.schemas.generation_task import (
|
||||
BatchPreviewGenerationTaskResponse,
|
||||
CreatePreviewGenerationTaskRequest,
|
||||
PreviewGenerationTaskResponse,
|
||||
)
|
||||
@@ -195,19 +193,11 @@ def _to_preview_response(task, generated_videos: list | None = None) -> PreviewG
|
||||
if started_at and completed_at:
|
||||
generate_duration = (completed_at - started_at).total_seconds()
|
||||
|
||||
title_cfg = getattr(task, "title_config", None)
|
||||
title_cfg = title_cfg if isinstance(title_cfg, dict) else {}
|
||||
extra_meta = getattr(task, "extra_meta", None)
|
||||
extra_meta = extra_meta if isinstance(extra_meta, dict) else {}
|
||||
voice_library_id = getattr(task, "voice_library_id", "") or ""
|
||||
if not isinstance(voice_library_id, str):
|
||||
voice_library_id = str(voice_library_id) if voice_library_id else ""
|
||||
return PreviewGenerationTaskResponse(
|
||||
task_id=task.id,
|
||||
status=task.status.value if hasattr(task.status, "value") else str(task.status),
|
||||
progress=float(task.progress or 0.0),
|
||||
is_preview=bool(getattr(task, "is_preview", True)),
|
||||
variant_index=int(extra_meta.get("variant_index", 0) or 0),
|
||||
resolution=getattr(task, "resolution", "") or "",
|
||||
video_url=video_url,
|
||||
duration=duration,
|
||||
@@ -216,8 +206,6 @@ def _to_preview_response(task, generated_videos: list | None = None) -> PreviewG
|
||||
transition_count=transition_count,
|
||||
material_usage=material_usage,
|
||||
error_message=task.error_message or "",
|
||||
title_text=str(title_cfg.get("text", "") or ""),
|
||||
voice_library_id=voice_library_id,
|
||||
created_at=task.created_at,
|
||||
started_at=started_at,
|
||||
finished_at=completed_at,
|
||||
@@ -225,100 +213,50 @@ def _to_preview_response(task, generated_videos: list | None = None) -> PreviewG
|
||||
)
|
||||
|
||||
|
||||
def _resolve_preview_edit_plan_id(
|
||||
*,
|
||||
request: CreatePreviewGenerationTaskRequest,
|
||||
task,
|
||||
db: Session,
|
||||
user_id: str,
|
||||
) -> str:
|
||||
"""确定任务关联的编辑计划ID:优先前端传入,否则按 template_id+user 兜底查找。"""
|
||||
if task.source_edit_plan_id:
|
||||
return task.source_edit_plan_id
|
||||
if not request.template_id:
|
||||
return ""
|
||||
try:
|
||||
from packages.adapters.sqlalchemy_impl.edit_plan_repository import (
|
||||
SQLAlchemyEditPlanRepository,
|
||||
)
|
||||
|
||||
_plan_repo = SQLAlchemyEditPlanRepository(db)
|
||||
_plans = _plan_repo.list_by_template(request.template_id, limit=20)
|
||||
for _p in _plans:
|
||||
if (_p.created_by_user_id or "") == user_id:
|
||||
logger.info(
|
||||
"[预览生成] 自动关联编辑计划: task_id=%s plan_id=%s",
|
||||
task.id,
|
||||
_p.id,
|
||||
)
|
||||
return _p.id
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"[预览生成] 查找关联编辑计划失败(不影响主流程): task_id=%s",
|
||||
task.id,
|
||||
exc_info=True,
|
||||
)
|
||||
return ""
|
||||
|
||||
|
||||
def _variant_value(values: list[str], index: int, fallback: str = "") -> str:
|
||||
"""从变体数组中取值:长度1=共用,长度>N=按索引,空数组=回退 fallback。"""
|
||||
if not values:
|
||||
return fallback
|
||||
if len(values) == 1:
|
||||
return values[0]
|
||||
return values[index] if index < len(values) else fallback
|
||||
|
||||
|
||||
@router.post("/preview", response_model=BatchPreviewGenerationTaskResponse, status_code=201)
|
||||
@router.post("/preview", response_model=PreviewGenerationTaskResponse, status_code=201)
|
||||
def create_preview_generation_task(
|
||||
request: CreatePreviewGenerationTaskRequest,
|
||||
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
||||
generation_task_repository=Depends(get_generation_task_repository),
|
||||
db: Session = Depends(get_db_session),
|
||||
asset_repo=Depends(get_asset_repository),
|
||||
) -> BatchPreviewGenerationTaskResponse:
|
||||
"""创建预览生成任务(支持批量)。
|
||||
) -> PreviewGenerationTaskResponse:
|
||||
"""创建预览生成任务。
|
||||
|
||||
preview_count=1 时行为与旧版完全一致(创建 1 个任务);
|
||||
preview_count=N 时一次创建 N 个独立变体任务:
|
||||
- 每个变体克隆独立编辑计划(独立 clips、独立随机素材起点),N 个预览内容互不相同
|
||||
- 每个变体拥有独立 task_id / 状态 / 预览视频 URL,前端按 task_id 分别轮询
|
||||
- 标题样式(font/color/position 等)全局共用;标题文字/配音/封面可按变体独立
|
||||
(titles[] / voice_library_ids[] / cover_urls[],长度1=共用,长度N=独立)
|
||||
预览渲染品质与正式生成一致(1080p, CRF 23, medium preset),确认生成时可直接复用预览产物。
|
||||
|
||||
Args:
|
||||
request: 预览任务创建请求(template_id + asset_ids 等)
|
||||
|
||||
Returns:
|
||||
201 + 变体任务数组 {items: [...], total: N}
|
||||
201 + 预览任务详情
|
||||
"""
|
||||
user_id = authenticated_user.user.id
|
||||
count = max(1, request.preview_count)
|
||||
logger.info(
|
||||
"[预览生成] 接收请求: user_id=%s, template_id=%s, asset_count=%d, preview_count=%d",
|
||||
user_id,
|
||||
request.template_id,
|
||||
len(request.asset_ids),
|
||||
count,
|
||||
request.preview_count,
|
||||
)
|
||||
|
||||
# 预检查队列限流(按变体总数计)
|
||||
# 预检查队列限流
|
||||
try:
|
||||
user_pending = generation_task_repository.count_pending_by_user(user_id)
|
||||
global_pending = generation_task_repository.count_pending_total()
|
||||
if user_pending + count > USER_PENDING_LIMIT:
|
||||
raise UserPendingLimitExceeded(
|
||||
user_id=user_id, pending_count=user_pending + count, limit=USER_PENDING_LIMIT
|
||||
)
|
||||
if global_pending + count > GLOBAL_PENDING_LIMIT:
|
||||
raise GlobalQueueFull(pending_count=global_pending + count, limit=GLOBAL_PENDING_LIMIT)
|
||||
if user_pending + 1 > USER_PENDING_LIMIT:
|
||||
raise UserPendingLimitExceeded(user_id=user_id, pending_count=user_pending + 1, limit=USER_PENDING_LIMIT)
|
||||
if global_pending + 1 > GLOBAL_PENDING_LIMIT:
|
||||
raise GlobalQueueFull(pending_count=global_pending + 1, limit=GLOBAL_PENDING_LIMIT)
|
||||
except UserPendingLimitExceeded as e:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(e, generation_task_repository, scope="user"),
|
||||
detail=f"您的待处理任务过多(当前 {e.pending_count - 1}/{e.limit}),请等待后再提交",
|
||||
) from e
|
||||
except GlobalQueueFull as e:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(e, generation_task_repository, scope="global"),
|
||||
detail="系统繁忙,请稍后再试",
|
||||
) from e
|
||||
|
||||
# 确定视频比例:优先前端传入,否则从模板 mode 推断
|
||||
@@ -335,11 +273,14 @@ def create_preview_generation_task(
|
||||
w, h = int(parts[0]), int(parts[1])
|
||||
base = 1920
|
||||
if w < h:
|
||||
# 竖屏
|
||||
output_width = round(base * w / h)
|
||||
output_height = base
|
||||
else:
|
||||
# 横屏
|
||||
output_width = base
|
||||
output_height = round(base * h / w)
|
||||
# 对齐到偶数
|
||||
output_width = output_width - output_width % 2
|
||||
output_height = output_height - output_height % 2
|
||||
except (ValueError, ZeroDivisionError):
|
||||
@@ -348,71 +289,42 @@ def create_preview_generation_task(
|
||||
|
||||
logger.info(
|
||||
"[预览生成] 分辨率: video_ratio=%s → %s (%dx%d)",
|
||||
video_ratio,
|
||||
resolution,
|
||||
output_width,
|
||||
output_height,
|
||||
video_ratio, resolution, output_width, output_height,
|
||||
)
|
||||
|
||||
# 从模板读取 editing_mode / mode 作为 strategy_id(渲染 pipeline 的 mode 参数)
|
||||
strategy_id = _resolve_strategy_id_from_template(request.template_id, db, user_id)
|
||||
base_title_config = request.title_config or {}
|
||||
|
||||
title_config = request.title_config or {}
|
||||
|
||||
use_case = CreateGenerationTaskUseCase(generation_task_repository)
|
||||
|
||||
# ── 预创建第一个任务,仅用于解析源编辑计划(不落库为最终任务)──
|
||||
# 先创建一个临时任务拿到 task 对象上下文,实际 N 个任务在循环中统一创建;
|
||||
# 为保持与旧版一致的源 plan 解析逻辑,先创建任务0、解析源 plan,
|
||||
# 再预克隆 N 个变体 plan,最后重建任务关联。
|
||||
# 简化实现:直接创建全部任务,plan 关联在创建后、入队前完成。
|
||||
|
||||
created_tasks: list = []
|
||||
variant_plan_ids: list[str] = [] # 每个变体最终关联的 plan_id(按变体顺序)
|
||||
|
||||
try:
|
||||
for variant_index in range(count):
|
||||
# 变体独立标题文字:titles[] 覆盖 title_config.text
|
||||
variant_title_text = _variant_value(request.titles, variant_index, "")
|
||||
variant_title_config = dict(base_title_config)
|
||||
if variant_title_text.strip():
|
||||
variant_title_config["text"] = variant_title_text.strip()
|
||||
|
||||
# 变体独立配音
|
||||
variant_voice_library_id = _variant_value(
|
||||
request.voice_library_ids, variant_index, request.voice_library_id
|
||||
task = use_case.execute(
|
||||
CreateGenerationTaskCommand(
|
||||
project_id="",
|
||||
asset_library_id="",
|
||||
strategy_id=strategy_id,
|
||||
voice_library_id=request.voice_library_id,
|
||||
template_id=request.template_id,
|
||||
asset_ids=list(request.asset_ids),
|
||||
title_ids=list(request.title_ids),
|
||||
voice_ids=list(request.voice_ids),
|
||||
created_by_user_id=user_id,
|
||||
source_edit_plan_id=request.source_edit_plan_id,
|
||||
asset_select_mode="",
|
||||
batch_id="",
|
||||
video_title=request.video_title,
|
||||
resolution=resolution,
|
||||
bgm_config=request.bgm_config or {},
|
||||
auto_retry_enabled=False,
|
||||
auto_retry_max=0,
|
||||
is_preview=True,
|
||||
title_config=title_config,
|
||||
output_width=output_width,
|
||||
output_height=output_height,
|
||||
)
|
||||
|
||||
task = use_case.execute(
|
||||
CreateGenerationTaskCommand(
|
||||
project_id="",
|
||||
asset_library_id="",
|
||||
strategy_id=strategy_id,
|
||||
voice_library_id=variant_voice_library_id,
|
||||
template_id=request.template_id,
|
||||
asset_ids=list(request.asset_ids),
|
||||
title_ids=list(request.title_ids),
|
||||
voice_ids=list(request.voice_ids),
|
||||
created_by_user_id=user_id,
|
||||
source_edit_plan_id=request.source_edit_plan_id,
|
||||
asset_select_mode="",
|
||||
batch_id="",
|
||||
video_title=request.video_title,
|
||||
resolution=resolution,
|
||||
bgm_config=request.bgm_config or {},
|
||||
auto_retry_enabled=False,
|
||||
auto_retry_max=0,
|
||||
is_preview=True,
|
||||
title_config=variant_title_config,
|
||||
output_width=output_width,
|
||||
output_height=output_height,
|
||||
)
|
||||
)
|
||||
task.extra_meta["variant_index"] = variant_index
|
||||
|
||||
# 解析源编辑计划(前端传入或按模板兜底查找)
|
||||
source_plan_id = _resolve_preview_edit_plan_id(request=request, task=task, db=db, user_id=user_id)
|
||||
task.source_edit_plan_id = source_plan_id
|
||||
generation_task_repository.update(task)
|
||||
created_tasks.append(task)
|
||||
)
|
||||
except ValueError as e:
|
||||
logger.warning("[预览生成] 创建失败: %s", e)
|
||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||
@@ -420,128 +332,93 @@ def create_preview_generation_task(
|
||||
logger.error("[预览生成] 创建失败: %s", e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="创建预览生成任务失败,请稍后再试") from e
|
||||
|
||||
# ── 克隆独立变体 plan:N 个预览全部克隆(预览不污染源 plan)──
|
||||
# 源 plan 不存在(无编辑历史)时各任务走自身随机选片流程,不克隆。
|
||||
source_plan_id = created_tasks[0].source_edit_plan_id if created_tasks else ""
|
||||
if source_plan_id:
|
||||
# 关联编辑计划:如果前端未传 source_edit_plan_id,通过 template_id + user_id 查找
|
||||
if not task.source_edit_plan_id and request.template_id:
|
||||
try:
|
||||
from packages.adapters.sqlalchemy_impl.edit_plan_repository import (
|
||||
SQLAlchemyEditPlanRepository,
|
||||
)
|
||||
|
||||
_plan_repo = SQLAlchemyEditPlanRepository(db)
|
||||
_plans = _plan_repo.list_by_template(request.template_id, limit=20)
|
||||
for _p in _plans:
|
||||
if (_p.created_by_user_id or "") == user_id:
|
||||
task.source_edit_plan_id = _p.id
|
||||
generation_task_repository.update(task)
|
||||
logger.info(
|
||||
"[预览生成] 自动关联编辑计划: task_id=%s plan_id=%s",
|
||||
task.id,
|
||||
_p.id,
|
||||
)
|
||||
break
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"[预览生成] 查找关联编辑计划失败(不影响主流程): task_id=%s",
|
||||
task.id,
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
# 每条预览都关联独立克隆 plan:多预览前端为 N 次并发调用,若共用同一 plan
|
||||
# 则 N 条预览片段完全相同;克隆时片段起点按持久化历史区间重算(含受控复用),
|
||||
# 保证各预览版本内容不同
|
||||
if task.source_edit_plan_id:
|
||||
try:
|
||||
from app.services.edit_plan_service import EditPlanService
|
||||
|
||||
_plan_svc = EditPlanService(db)
|
||||
for variant_index in range(count):
|
||||
last_err: Exception | None = None
|
||||
variant_plan = None
|
||||
for _attempt in range(2): # 1 次重试,抗 DB 瞬时抖动
|
||||
try:
|
||||
variant_plan = _plan_svc.clone_plan_for_variant(
|
||||
source_plan_id,
|
||||
created_by_user_id=user_id,
|
||||
name_suffix=f"预览变体{variant_index + 1}" if count > 1 else "预览变体",
|
||||
)
|
||||
break
|
||||
except Exception as clone_err: # noqa: PERF203
|
||||
last_err = clone_err
|
||||
logger.warning(
|
||||
"[预览生成] 克隆变体 plan 失败(尝试%d/2): variant=%d error=%s",
|
||||
_attempt + 1,
|
||||
variant_index,
|
||||
clone_err,
|
||||
exc_info=True,
|
||||
)
|
||||
if variant_plan is None:
|
||||
logger.error(
|
||||
"[预览生成] 克隆预览变体 plan 重试仍失败: variant=%d source=%s",
|
||||
variant_index,
|
||||
source_plan_id,
|
||||
exc_info=last_err,
|
||||
)
|
||||
# 标记已创建任务失败
|
||||
for t in created_tasks:
|
||||
_mark_task_failed(generation_task_repository, t, "预览变体计划创建失败")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="创建预览任务失败:无法生成独立剪辑计划,请重试",
|
||||
) from last_err
|
||||
variant_plan_ids.append(variant_plan.id)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("[预览生成] 克隆变体 plan 异常: %s", e, exc_info=True)
|
||||
for t in created_tasks:
|
||||
_mark_task_failed(generation_task_repository, t, "预览变体计划创建失败")
|
||||
_preview_plan = _plan_svc.clone_plan_for_variant(
|
||||
task.source_edit_plan_id,
|
||||
created_by_user_id=user_id,
|
||||
name_suffix="预览变体",
|
||||
)
|
||||
task.source_edit_plan_id = _preview_plan.id
|
||||
generation_task_repository.update(task)
|
||||
logger.info(
|
||||
"[预览生成] 预览关联独立克隆 plan: task_id=%s clone_plan_id=%s",
|
||||
task.id,
|
||||
_preview_plan.id,
|
||||
)
|
||||
except Exception as clone_err:
|
||||
# 不退回共用原 plan(否则多条预览内容相同,违反去重诉求):
|
||||
# 标记任务失败并中断,前端可重新发起预览
|
||||
logger.error(
|
||||
"[预览生成] 克隆预览变体 plan 失败,任务标记失败: task_id=%s error=%s",
|
||||
task.id,
|
||||
clone_err,
|
||||
exc_info=True,
|
||||
)
|
||||
_mark_task_failed(generation_task_repository, task, "预览变体计划创建失败")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="创建预览任务失败:无法生成独立剪辑计划,请重试",
|
||||
) from e
|
||||
) from clone_err
|
||||
|
||||
# 关联变体 plan 并回写标题配置
|
||||
for variant_index, task in enumerate(created_tasks):
|
||||
if variant_plan_ids:
|
||||
task.source_edit_plan_id = variant_plan_ids[variant_index]
|
||||
generation_task_repository.update(task)
|
||||
# 回写变体标题到 plan config(worker 渲染时从 plan 读取 title 配置)
|
||||
if task.source_edit_plan_id and (task.title_config or {}).get("text", "").strip():
|
||||
try:
|
||||
from app.api.routes.generation_tasks import _writeback_edit_plan_config
|
||||
|
||||
_writeback_edit_plan_config(
|
||||
plan_id=task.source_edit_plan_id,
|
||||
task_id=task.id,
|
||||
title_config=task.title_config,
|
||||
db=db,
|
||||
)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"[预览生成] 回写标题配置失败(不影响主流程): task_id=%s",
|
||||
task.id,
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
# ── 入队 ──
|
||||
responses: list[PreviewGenerationTaskResponse] = []
|
||||
rate_limit_exc: Exception | None = None # 记录首个限流异常,全部失败时返回结构化提示
|
||||
for variant_index, task in enumerate(created_tasks):
|
||||
try:
|
||||
enqueued = safe_enqueue_generation_task(
|
||||
task,
|
||||
generation_task_repository,
|
||||
user_id=user_id,
|
||||
log_prefix=f"[预览生成][变体{variant_index + 1}]",
|
||||
log_task_status=True,
|
||||
)
|
||||
if not enqueued:
|
||||
logger.warning("[预览生成] 任务入队失败: task_id=%s", task.id)
|
||||
_mark_task_failed(generation_task_repository, task, "任务入队失败")
|
||||
except UserPendingLimitExceeded as e:
|
||||
_mark_task_failed(generation_task_repository, task, "待处理任务超限")
|
||||
rate_limit_exc = rate_limit_exc or e
|
||||
except GlobalQueueFull as e:
|
||||
_mark_task_failed(generation_task_repository, task, "系统队列已满")
|
||||
rate_limit_exc = rate_limit_exc or e
|
||||
except Exception:
|
||||
logger.exception("[预览生成] 入队异常: task_id=%s", task.id)
|
||||
_mark_task_failed(generation_task_repository, task, "任务入队异常")
|
||||
# enqueue 会原地更新 task 状态/进度,直接用 task 构造响应
|
||||
responses.append(_to_preview_response(task))
|
||||
|
||||
# 队列满/限流时若全部失败,返回结构化错误码(前端区分"排队"与"创建失败")
|
||||
if all(r.status == "failed" for r in responses) and rate_limit_exc is not None:
|
||||
if isinstance(rate_limit_exc, UserPendingLimitExceeded):
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(rate_limit_exc, generation_task_repository, scope="user"),
|
||||
)
|
||||
# 入队执行;若入队失败则标记任务为 failed 避免僵尸数据
|
||||
try:
|
||||
if not safe_enqueue_generation_task(
|
||||
task,
|
||||
generation_task_repository,
|
||||
user_id=user_id,
|
||||
log_prefix="[预览生成]",
|
||||
log_task_status=True,
|
||||
):
|
||||
logger.warning("[预览生成] 任务入队失败: task_id=%s", task.id)
|
||||
_mark_task_failed(generation_task_repository, task, "任务入队失败")
|
||||
raise HTTPException(status_code=500, detail="任务入队失败,请稍后重试")
|
||||
except UserPendingLimitExceeded as e:
|
||||
_mark_task_failed(generation_task_repository, task, "待处理任务超限")
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=f"您的待处理任务过多(当前 {e.pending_count - 1}/{e.limit}),请等待后再提交",
|
||||
) from None
|
||||
except GlobalQueueFull:
|
||||
_mark_task_failed(generation_task_repository, task, "系统队列已满")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(rate_limit_exc, generation_task_repository, scope="global"),
|
||||
)
|
||||
detail="系统繁忙,请稍后再试",
|
||||
) from None
|
||||
|
||||
logger.info(
|
||||
"[预览生成] 创建完成: %d 个变体任务, task_ids=%s",
|
||||
len(responses),
|
||||
[r.task_id for r in responses],
|
||||
)
|
||||
return BatchPreviewGenerationTaskResponse(items=responses, total=len(responses))
|
||||
return _to_preview_response(task)
|
||||
|
||||
|
||||
@router.get("/preview/{task_id}", response_model=PreviewGenerationTaskResponse)
|
||||
|
||||
@@ -10,7 +10,6 @@ from app.core.task_enqueue import (
|
||||
USER_PENDING_LIMIT,
|
||||
GlobalQueueFull,
|
||||
UserPendingLimitExceeded,
|
||||
build_rate_limit_detail,
|
||||
safe_enqueue_generation_task,
|
||||
)
|
||||
from app.dependencies import (
|
||||
@@ -48,15 +47,6 @@ logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _variant_value(values: list[str], index: int, fallback: str = "") -> str:
|
||||
"""从变体数组中取值:长度1=共用,长度>N=按索引,空数组=回退 fallback。"""
|
||||
if not values:
|
||||
return fallback
|
||||
if len(values) == 1:
|
||||
return values[0]
|
||||
return values[index] if index < len(values) else fallback
|
||||
|
||||
|
||||
def _to_generation_task_response(task) -> GenerationTaskResponse:
|
||||
return GenerationTaskResponse(
|
||||
id=task.id,
|
||||
@@ -418,12 +408,12 @@ def create_generation_task(
|
||||
except UserPendingLimitExceeded as e:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(e, generation_task_repository, scope="user"),
|
||||
detail=f"您的待处理任务过多(当前 {e.pending_count - count}/{e.limit},本次提交 {count} 个),请等待完成后再提交",
|
||||
) from e
|
||||
except GlobalQueueFull as e:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(e, generation_task_repository, scope="global"),
|
||||
detail="系统繁忙,请稍后再试",
|
||||
) from e
|
||||
|
||||
# 画中画已下线:strategy_id 中的 pip/voice_pip 统一映射为 one_take
|
||||
@@ -482,21 +472,12 @@ def create_generation_task(
|
||||
if task_index > 0 and variant_plan_ids:
|
||||
effective_plan_id = variant_plan_ids[task_index - 1]
|
||||
|
||||
# 变体级独立配置:titles[]/voice_library_ids[]/cover_urls[]
|
||||
# 长度1=所有变体共用,长度=count=每个变体独立,空数组=回退单值字段
|
||||
variant_title_text = _variant_value(request.titles, task_index, "")
|
||||
variant_title_config = dict(request.title_config or {})
|
||||
if variant_title_text.strip():
|
||||
variant_title_config["text"] = variant_title_text.strip()
|
||||
variant_voice_library_id = _variant_value(request.voice_library_ids, task_index, request.voice_library_id)
|
||||
variant_cover_url = _variant_value(request.cover_urls, task_index, request.cover_url)
|
||||
|
||||
task = use_case.execute(
|
||||
CreateGenerationTaskCommand(
|
||||
project_id=project_id,
|
||||
asset_library_id=asset_library_id,
|
||||
strategy_id=effective_strategy_id,
|
||||
voice_library_id=variant_voice_library_id,
|
||||
voice_library_id=request.voice_library_id,
|
||||
template_id=request.template_id,
|
||||
asset_ids=resolved_asset_ids,
|
||||
title_ids=request.title_ids,
|
||||
@@ -514,12 +495,10 @@ def create_generation_task(
|
||||
source_task_id=request.source_task_id,
|
||||
output_width=request.output_width,
|
||||
output_height=request.output_height,
|
||||
cover_url=variant_cover_url,
|
||||
title_config=variant_title_config,
|
||||
cover_url=request.cover_url,
|
||||
title_config=request.title_config or {},
|
||||
)
|
||||
)
|
||||
# 变体序号写入 extra_meta(响应/排查时可辨识)
|
||||
task.extra_meta["variant_index"] = task_index
|
||||
try:
|
||||
# 兜底关联编辑计划:前端未传 source_edit_plan_id 时,
|
||||
# 通过 template_id + user_id 在 DB 层直接查找最新的 plan。
|
||||
@@ -554,13 +533,13 @@ def create_generation_task(
|
||||
|
||||
# 回写 plan.config:必须在 enqueue 之前执行,
|
||||
# 确保 worker 读取 plan 时 config 中已包含 generation_task_id。
|
||||
# 批量场景下每个变体关联独立 plan,需各自回写自己的变体标题配置。
|
||||
# 只在首个任务时回写一次,避免批量生成时循环覆盖。
|
||||
_effective_plan_id = task.source_edit_plan_id
|
||||
if _effective_plan_id:
|
||||
if _effective_plan_id and len(created_tasks) == 0:
|
||||
_writeback_edit_plan_config(
|
||||
plan_id=_effective_plan_id,
|
||||
task_id=task.id,
|
||||
title_config=variant_title_config,
|
||||
title_config=request.title_config,
|
||||
db=db,
|
||||
)
|
||||
|
||||
@@ -580,7 +559,7 @@ def create_generation_task(
|
||||
if not created_tasks:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(_e, generation_task_repository, scope="user"),
|
||||
detail="您的待处理任务过多,请等待完成后再提交",
|
||||
) from _e
|
||||
break
|
||||
except GlobalQueueFull as _e:
|
||||
@@ -588,7 +567,7 @@ def create_generation_task(
|
||||
if not created_tasks:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(_e, generation_task_repository, scope="global"),
|
||||
detail="系统繁忙,请稍后再试",
|
||||
) from _e
|
||||
break
|
||||
except HTTPException:
|
||||
@@ -714,15 +693,15 @@ def confirm_generation(
|
||||
log_task_status=True,
|
||||
):
|
||||
logger.warning("[确认生成] 入队失败: task_id=%s", new_task.id)
|
||||
except UserPendingLimitExceeded as _e:
|
||||
except UserPendingLimitExceeded:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(_e, generation_task_repository, scope="user"),
|
||||
detail="您的待处理任务过多,请等待完成后再提交",
|
||||
) from None
|
||||
except GlobalQueueFull as _e:
|
||||
except GlobalQueueFull:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(_e, generation_task_repository, scope="global"),
|
||||
detail="系统繁忙,请稍后再试",
|
||||
) from None
|
||||
|
||||
return BatchGenerationTaskResponse(
|
||||
@@ -804,24 +783,12 @@ def retry_generation_task(
|
||||
if user_pending >= USER_PENDING_LIMIT:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(
|
||||
UserPendingLimitExceeded(
|
||||
user_id=user_id,
|
||||
pending_count=user_pending,
|
||||
limit=USER_PENDING_LIMIT,
|
||||
),
|
||||
generation_task_repository,
|
||||
scope="user",
|
||||
),
|
||||
detail=f"您的待处理任务过多(当前 {user_pending}/{USER_PENDING_LIMIT}),请等待完成后再提交",
|
||||
)
|
||||
if global_pending >= GLOBAL_PENDING_LIMIT:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(
|
||||
GlobalQueueFull(pending_count=global_pending, limit=GLOBAL_PENDING_LIMIT),
|
||||
generation_task_repository,
|
||||
scope="global",
|
||||
),
|
||||
detail="系统繁忙,请稍后再试",
|
||||
)
|
||||
|
||||
use_case = CreateGenerationTaskUseCase(generation_task_repository)
|
||||
@@ -856,15 +823,15 @@ def retry_generation_task(
|
||||
log_task_status=True,
|
||||
):
|
||||
logger.warning("[生成任务] 重试入队失败: task_id=%s", retried.id)
|
||||
except UserPendingLimitExceeded as _e:
|
||||
except UserPendingLimitExceeded:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=build_rate_limit_detail(_e, generation_task_repository, scope="user"),
|
||||
detail="您的待处理任务过多,请等待完成后再提交",
|
||||
) from None
|
||||
except GlobalQueueFull as _e:
|
||||
except GlobalQueueFull:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=build_rate_limit_detail(_e, generation_task_repository, scope="global"),
|
||||
detail="系统繁忙,请稍后再试",
|
||||
) from None
|
||||
return _to_generation_task_response(retried)
|
||||
|
||||
|
||||
@@ -85,22 +85,12 @@ def _infer_mime_type_from_storage_key(storage_key: str) -> str:
|
||||
"""从 storage_key 推断 MIME 类型(与 worker 端保持一致)。"""
|
||||
lower_filename = storage_key.rsplit("/", 1)[-1].lower()
|
||||
_MIME_MAP = {
|
||||
".mov": "video/quicktime",
|
||||
".mp4": "video/mp4",
|
||||
".avi": "video/x-msvideo",
|
||||
".mkv": "video/x-matroska",
|
||||
".webm": "video/webm",
|
||||
".png": "image/png",
|
||||
".gif": "image/gif",
|
||||
".bmp": "image/bmp",
|
||||
".svg": "image/svg+xml",
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".mp3": "audio/mpeg",
|
||||
".wav": "audio/wav",
|
||||
".ogg": "audio/ogg",
|
||||
".flac": "audio/flac",
|
||||
".m4a": "audio/x-m4a",
|
||||
".mov": "video/quicktime", ".mp4": "video/mp4", ".avi": "video/x-msvideo",
|
||||
".mkv": "video/x-matroska", ".webm": "video/webm",
|
||||
".png": "image/png", ".gif": "image/gif", ".bmp": "image/bmp",
|
||||
".svg": "image/svg+xml", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
|
||||
".mp3": "audio/mpeg", ".wav": "audio/wav", ".ogg": "audio/ogg",
|
||||
".flac": "audio/flac", ".m4a": "audio/x-m4a",
|
||||
}
|
||||
for ext, mime in _MIME_MAP.items():
|
||||
if lower_filename.endswith(ext):
|
||||
@@ -108,85 +98,8 @@ def _infer_mime_type_from_storage_key(storage_key: str) -> str:
|
||||
return "video/mp4" # default
|
||||
|
||||
|
||||
# 兜底去重:无 file_hash / client_upload_id 时,同库同名近期活动记录视为重复
|
||||
FALLBACK_DEDUP_WINDOW_MINUTES = 30
|
||||
ACTIVE_ASSET_STATUSES = (AssetStatus.UPLOADING, AssetStatus.PROCESSING)
|
||||
|
||||
|
||||
def _find_duplicate_asset(
|
||||
asset_repository: Any,
|
||||
*,
|
||||
library_id: str,
|
||||
file_hash: str,
|
||||
client_upload_id: str,
|
||||
filename: str,
|
||||
file_size: int = 0,
|
||||
) -> Any:
|
||||
"""complete/上传幂等去重,按优先级查找已存在的素材。
|
||||
|
||||
1. client_upload_id(客户端幂等 token,同一次上传的重试保持一致)
|
||||
2. file_hash(内容哈希,不同上传只要内容相同即去重)
|
||||
3. 兜底:同库 + 同文件名(+同大小)且 30 分钟内仍处 uploading/processing
|
||||
的记录——旧客户端不传 hash/token 时,防止 complete 超时重试反复建占位。
|
||||
|
||||
全部为鸭子类型调用:旧仓储无对应方法时静默跳过,不破坏既有实现。
|
||||
"""
|
||||
if client_upload_id:
|
||||
find = getattr(asset_repository, "find_by_library_and_client_upload_id", None)
|
||||
if callable(find):
|
||||
existing = find(library_id=library_id, client_upload_id=client_upload_id)
|
||||
if existing is not None:
|
||||
logger.info(
|
||||
"素材幂等命中(client_upload_id): library=%s token=%s asset=%s",
|
||||
library_id,
|
||||
client_upload_id,
|
||||
getattr(existing, "id", "?"),
|
||||
)
|
||||
return existing
|
||||
if file_hash:
|
||||
existing = asset_repository.find_by_library_and_file_hash(
|
||||
library_id=library_id,
|
||||
file_hash=file_hash,
|
||||
)
|
||||
if existing is not None:
|
||||
logger.info(
|
||||
"素材去重命中(file_hash): library=%s hash=%s asset=%s",
|
||||
library_id,
|
||||
file_hash,
|
||||
existing.id,
|
||||
)
|
||||
return existing
|
||||
if filename:
|
||||
find_recent = getattr(asset_repository, "find_recent_active_by_library_and_name", None)
|
||||
if callable(find_recent):
|
||||
existing = find_recent(
|
||||
library_id=library_id,
|
||||
name=filename,
|
||||
within_minutes=FALLBACK_DEDUP_WINDOW_MINUTES,
|
||||
file_size=file_size or 0,
|
||||
)
|
||||
if existing is not None and getattr(existing, "status", None) in ACTIVE_ASSET_STATUSES:
|
||||
logger.info(
|
||||
"素材幂等兜底命中(近期活动同名记录): library=%s name=%s asset=%s status=%s",
|
||||
library_id,
|
||||
filename,
|
||||
getattr(existing, "id", "?"),
|
||||
getattr(existing, "status", "?"),
|
||||
)
|
||||
return existing
|
||||
return None
|
||||
|
||||
|
||||
def _create_pending_asset(
|
||||
asset_repository,
|
||||
project_id,
|
||||
library_id,
|
||||
storage_key,
|
||||
filename,
|
||||
mime_type,
|
||||
user_id,
|
||||
file_hash="",
|
||||
client_upload_id="",
|
||||
asset_repository, project_id, library_id, storage_key, filename, mime_type, user_id, file_hash=""
|
||||
):
|
||||
"""立即创建一条 PROCESSING 状态的 Asset 记录,使前端能马上看到新素材。"""
|
||||
asset = Asset.create(
|
||||
@@ -198,7 +111,6 @@ def _create_pending_asset(
|
||||
status=AssetStatus.PROCESSING,
|
||||
uploaded_by_user_id=user_id,
|
||||
file_hash=file_hash,
|
||||
client_upload_id=client_upload_id,
|
||||
)
|
||||
return asset_repository.create(asset)
|
||||
|
||||
@@ -209,7 +121,6 @@ def _submit_ingest_job(
|
||||
storage_key: str,
|
||||
ingest_job_repository: Any,
|
||||
file_hash: str = "",
|
||||
asset_id: str = "",
|
||||
) -> Any:
|
||||
use_case = SubmitIngestJobUseCase(ingest_job_repository)
|
||||
job = use_case.execute(
|
||||
@@ -218,7 +129,6 @@ def _submit_ingest_job(
|
||||
library_id=library_id,
|
||||
storage_key=storage_key,
|
||||
file_hash=file_hash,
|
||||
asset_id=asset_id,
|
||||
)
|
||||
)
|
||||
celery_app.send_task("worker.ingest_asset", args=[job.id])
|
||||
@@ -292,7 +202,7 @@ async def complete_direct_upload(
|
||||
asset_repository: Any = Depends(get_asset_repository),
|
||||
storage_service: OSSStorageService = Depends(get_storage_service),
|
||||
) -> DirectUploadCompleteResponse:
|
||||
"""确认浏览器直传完成并创建导入任务(幂等:重复 complete 返回同一素材)。"""
|
||||
"""确认浏览器直传完成并创建导入任务。"""
|
||||
require_project_and_library(
|
||||
request.project_id,
|
||||
request.library_id,
|
||||
@@ -302,29 +212,6 @@ async def complete_direct_upload(
|
||||
normalized_key = storage_service._normalize_storage_key(request.storage_key)
|
||||
if not normalized_key.startswith("uploads/"):
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid upload key")
|
||||
|
||||
filename = normalized_key.rsplit("/", 1)[-1]
|
||||
|
||||
# ── 幂等去重(放在 OSS 检查之前):complete 超时后前端重试时,
|
||||
# 第一次 complete 可能已建好占位记录,此时即使 OSS 检查失败也必须返回
|
||||
# 已存在记录,绝不能再建第二条。─
|
||||
existing = _find_duplicate_asset(
|
||||
asset_repository,
|
||||
library_id=request.library_id,
|
||||
file_hash=request.file_hash,
|
||||
client_upload_id=request.client_upload_id,
|
||||
filename=filename,
|
||||
file_size=request.file_size,
|
||||
)
|
||||
if existing is not None:
|
||||
return DirectUploadCompleteResponse(
|
||||
storage_key=existing.storage_key,
|
||||
ingest_job_id="",
|
||||
duplicated=True,
|
||||
asset_id=existing.id,
|
||||
url=storage_service.get_url(existing.storage_key),
|
||||
)
|
||||
|
||||
try:
|
||||
file_exists = storage_service.file_exists(normalized_key)
|
||||
except Exception as error:
|
||||
@@ -336,7 +223,29 @@ async def complete_direct_upload(
|
||||
if not file_exists:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Uploaded file not found")
|
||||
|
||||
# ── 素材去重检测:同素材库 + 同 file_hash 视为重复 ──
|
||||
if request.file_hash:
|
||||
existing = asset_repository.find_by_library_and_file_hash(
|
||||
library_id=request.library_id,
|
||||
file_hash=request.file_hash,
|
||||
)
|
||||
if existing is not None:
|
||||
logger.info(
|
||||
"素材去重命中: library=%s hash=%s existing_asset=%s",
|
||||
request.library_id,
|
||||
request.file_hash,
|
||||
existing.id,
|
||||
)
|
||||
return DirectUploadCompleteResponse(
|
||||
storage_key=normalized_key,
|
||||
ingest_job_id="",
|
||||
duplicated=True,
|
||||
asset_id=existing.id,
|
||||
url=storage_service.get_url(normalized_key),
|
||||
)
|
||||
|
||||
# 立即创建 Asset 记录(PROCESSING 状态),使前端刷新后即可看到新素材
|
||||
filename = normalized_key.rsplit("/", 1)[-1]
|
||||
mime_type = _infer_mime_type_from_storage_key(normalized_key)
|
||||
pending_asset = _create_pending_asset(
|
||||
asset_repository=asset_repository,
|
||||
@@ -347,7 +256,6 @@ async def complete_direct_upload(
|
||||
mime_type=mime_type,
|
||||
user_id=authenticated_user.user.id,
|
||||
file_hash=request.file_hash,
|
||||
client_upload_id=request.client_upload_id,
|
||||
)
|
||||
|
||||
job = _submit_ingest_job(
|
||||
@@ -356,7 +264,6 @@ async def complete_direct_upload(
|
||||
storage_key=normalized_key,
|
||||
ingest_job_repository=ingest_job_repository,
|
||||
file_hash=request.file_hash,
|
||||
asset_id=pending_asset.id,
|
||||
)
|
||||
return DirectUploadCompleteResponse(
|
||||
storage_key=normalized_key,
|
||||
@@ -376,8 +283,7 @@ async def upload_asset(
|
||||
project_id: str = Form(..., min_length=1, description="项目 ID"),
|
||||
library_id: str = Form(..., min_length=1, description="素材库 ID"),
|
||||
file: UploadFile = File(..., description="要上传的文件(视频、音频、图片等)"),
|
||||
file_hash: str = Form(default="", description="文件哈希,用于去重检测"),
|
||||
client_upload_id: str = Form(default="", description="客户端幂等 token(同一次上传的重试保持一致)"),
|
||||
file_hash: str = Form(default="", description="文件 MD5 哈希,用于去重检测"),
|
||||
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
||||
ingest_job_repository: Any = Depends(get_ingest_job_repository),
|
||||
project_repository: Any = Depends(get_project_repository),
|
||||
@@ -388,31 +294,32 @@ async def upload_asset(
|
||||
"""上传素材文件并触发导入流水线。"""
|
||||
require_project_and_library(project_id, library_id, project_repository, asset_library_repository)
|
||||
|
||||
# P2-5: 服务端验证 MIME 类型(先验证,再幂等去重,避免非法类型绕过)
|
||||
# ── 素材去重检测:上传前检查同素材库 + 同 file_hash ──
|
||||
if file_hash:
|
||||
existing = asset_repository.find_by_library_and_file_hash(
|
||||
library_id=library_id,
|
||||
file_hash=file_hash,
|
||||
)
|
||||
if existing is not None:
|
||||
logger.info(
|
||||
"素材去重命中(multipart): library=%s hash=%s existing_asset=%s",
|
||||
library_id,
|
||||
file_hash,
|
||||
existing.id,
|
||||
)
|
||||
return UploadAssetResponse(
|
||||
storage_key=existing.storage_key,
|
||||
ingest_job_id="",
|
||||
url="",
|
||||
duplicated=True,
|
||||
asset_id=existing.id,
|
||||
)
|
||||
|
||||
# P2-5: 服务端验证 MIME 类型
|
||||
validated_content_type = _validate_mime_type(file.content_type)
|
||||
|
||||
safe_filename = file.filename.replace("/", "_").replace("\\", "_") if file.filename else "unknown"
|
||||
|
||||
# ── 幂等去重:client_upload_id → file_hash → 近期活动同名记录兜底 ──
|
||||
# 放在 OSS 上传之前:重复提交直接返回,不占 OSS 流量、不建新记录。
|
||||
existing = _find_duplicate_asset(
|
||||
asset_repository,
|
||||
library_id=library_id,
|
||||
file_hash=file_hash,
|
||||
client_upload_id=client_upload_id,
|
||||
filename=safe_filename,
|
||||
file_size=0,
|
||||
)
|
||||
if existing is not None:
|
||||
return UploadAssetResponse(
|
||||
storage_key=existing.storage_key,
|
||||
ingest_job_id="",
|
||||
url="",
|
||||
duplicated=True,
|
||||
asset_id=existing.id,
|
||||
)
|
||||
|
||||
file_id = uuid4().hex[:8]
|
||||
safe_filename = file.filename.replace("/", "_").replace("\\", "_") if file.filename else "unknown"
|
||||
storage_key = f"uploads/{file_id}/{safe_filename}"
|
||||
|
||||
try:
|
||||
@@ -441,7 +348,6 @@ async def upload_asset(
|
||||
mime_type=validated_content_type,
|
||||
user_id=authenticated_user.user.id,
|
||||
file_hash=file_hash,
|
||||
client_upload_id=client_upload_id,
|
||||
)
|
||||
|
||||
job = _submit_ingest_job(
|
||||
@@ -450,7 +356,6 @@ async def upload_asset(
|
||||
storage_key=storage_key,
|
||||
ingest_job_repository=ingest_job_repository,
|
||||
file_hash=file_hash,
|
||||
asset_id=pending_asset.id,
|
||||
)
|
||||
|
||||
return UploadAssetResponse(
|
||||
|
||||
@@ -252,10 +252,6 @@ class RecomputeDedupRequest(BaseModel):
|
||||
None,
|
||||
description="指定视频 ID 列表。为空则对当前用户所有缺少查重数据的视频重新计算。",
|
||||
)
|
||||
force: bool = Field(
|
||||
False,
|
||||
description="强制重算:即使视频已有查重数据也重新入队(#1702 查重算法升级后用于存量视频重算)。",
|
||||
)
|
||||
|
||||
|
||||
class RecomputeDedupResponse(BaseModel):
|
||||
@@ -295,15 +291,15 @@ def recompute_dedup(
|
||||
skipped = 0
|
||||
|
||||
for video in target_videos:
|
||||
# 已有完整查重数据的跳过(force=True 时强制重算,#1702 算法升级后存量视频需要重算指纹/分片)
|
||||
if not request.force and video.duplicate_rate is not None and video.video_fingerprint:
|
||||
# 已有完整查重数据的跳过
|
||||
if video.duplicate_rate is not None and video.video_fingerprint:
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
# 触发异步查重任务
|
||||
celery_app.send_task("worker.check_duplicate", args=[video.id])
|
||||
enqueued += 1
|
||||
logger.info("Enqueued re-dedup for video %s (user=%s, force=%s)", video.id, user_id, request.force)
|
||||
logger.info("Enqueued re-dedup for video %s (user=%s)", video.id, user_id)
|
||||
|
||||
return RecomputeDedupResponse(
|
||||
enqueued=enqueued,
|
||||
|
||||
@@ -8,145 +8,27 @@ logger = logging.getLogger(__name__)
|
||||
# ── 限流阈值常量(全系统统一管理,不要在业务代码里硬编码) ──
|
||||
USER_PENDING_LIMIT = 3 # 单用户 pending 上限
|
||||
GLOBAL_PENDING_LIMIT = 20 # 全局 pending 上限
|
||||
WORKER_CONCURRENCY = 4 # worker 渲染并发数(infra/docker/compose.yml WORKER_CONCURRENCY 默认值)
|
||||
|
||||
# 限流错误码:前端据此区分"排队等待"与"创建失败"
|
||||
ERROR_CODE_USER_QUEUE_FULL = "USER_QUEUE_FULL" # 429:用户自己的任务排队中
|
||||
ERROR_CODE_SYSTEM_QUEUE_FULL = "SYSTEM_QUEUE_FULL" # 503:系统整体繁忙
|
||||
|
||||
|
||||
class UserPendingLimitExceeded(Exception):
|
||||
"""用户 pending 任务数超限,返回 429。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user_id: str,
|
||||
pending_count: int,
|
||||
limit: int,
|
||||
*,
|
||||
running_count: int = 0,
|
||||
requested_count: int = 1,
|
||||
queue_ahead: int = 0,
|
||||
estimated_wait_seconds: int = 0,
|
||||
):
|
||||
def __init__(self, user_id: str, pending_count: int, limit: int):
|
||||
self.user_id = user_id
|
||||
self.pending_count = pending_count
|
||||
self.limit = limit
|
||||
# 排队上下文(用于 429 结构化提示,前端展示"排队中"而非"创建失败")
|
||||
self.running_count = running_count
|
||||
self.requested_count = requested_count
|
||||
self.queue_ahead = queue_ahead
|
||||
self.estimated_wait_seconds = estimated_wait_seconds
|
||||
super().__init__(f"用户 {user_id} pending 任务数 {pending_count} 超过上限 {limit}")
|
||||
|
||||
|
||||
class GlobalQueueFull(Exception):
|
||||
"""全局限流,返回 503。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
pending_count: int,
|
||||
limit: int,
|
||||
*,
|
||||
running_count: int = 0,
|
||||
queue_ahead: int = 0,
|
||||
estimated_wait_seconds: int = 0,
|
||||
):
|
||||
def __init__(self, pending_count: int, limit: int):
|
||||
self.pending_count = pending_count
|
||||
self.limit = limit
|
||||
self.running_count = running_count
|
||||
self.queue_ahead = queue_ahead
|
||||
self.estimated_wait_seconds = estimated_wait_seconds
|
||||
super().__init__(f"系统 pending 任务数 {pending_count} 超过上限 {limit}")
|
||||
|
||||
|
||||
def _estimate_wait_seconds(queue_ahead: int, generation_task_repository: Any) -> int:
|
||||
"""根据排队任务数 + worker 并发数 + 历史平均任务耗时估算等待秒数。
|
||||
|
||||
估算公式:ceil(排队任务数 / 并发数) × 平均单任务耗时。
|
||||
拿不到历史数据时仓储层返回默认 120 秒。
|
||||
"""
|
||||
import math
|
||||
|
||||
if queue_ahead <= 0:
|
||||
return 0
|
||||
try:
|
||||
estimator = getattr(generation_task_repository, "estimate_avg_duration_seconds", None)
|
||||
avg_seconds = estimator() if estimator is not None else 120.0
|
||||
except Exception:
|
||||
avg_seconds = 120.0
|
||||
return int(math.ceil(queue_ahead / WORKER_CONCURRENCY) * avg_seconds)
|
||||
|
||||
|
||||
def build_rate_limit_detail(
|
||||
exc: Exception,
|
||||
generation_task_repository: Any,
|
||||
*,
|
||||
scope: str = "user",
|
||||
) -> dict:
|
||||
"""构造结构化限流响应体(HTTPException 的 detail)。
|
||||
|
||||
前端按 detail.code 判断场景:
|
||||
- USER_QUEUE_FULL (429):用户自己的任务在排队,应提示"等待/继续排队",不是创建失败
|
||||
- SYSTEM_QUEUE_FULL (503):系统繁忙,稍后重试
|
||||
|
||||
detail 字段:
|
||||
- code: 错误码
|
||||
- message: 可读中文提示(可直接展示)
|
||||
- queued_count: 当前排队(pending)任务数
|
||||
- running_count: 当前渲染中(running)任务数
|
||||
- queue_ahead: 前方排队任务数(预计等待批次依据)
|
||||
- estimated_wait_seconds: 预计等待秒数
|
||||
- limit: 对应限流上限
|
||||
"""
|
||||
if scope == "user" and isinstance(exc, UserPendingLimitExceeded):
|
||||
running = exc.running_count
|
||||
if not running:
|
||||
try:
|
||||
counter = getattr(generation_task_repository, "count_running_by_user", None)
|
||||
running = counter(exc.user_id) if counter is not None else 0
|
||||
except Exception:
|
||||
running = 0
|
||||
queue_ahead = exc.queue_ahead or max(exc.pending_count, 0)
|
||||
wait = exc.estimated_wait_seconds or _estimate_wait_seconds(queue_ahead, generation_task_repository)
|
||||
wait_minutes = max(1, round(wait / 60))
|
||||
message = (
|
||||
f"您有 {exc.pending_count} 个任务正在排队、{running} 个正在渲染,"
|
||||
f"同一时间最多提交 {exc.limit} 个任务。请等待约 {wait_minutes} 分钟后再提交"
|
||||
)
|
||||
return {
|
||||
"code": ERROR_CODE_USER_QUEUE_FULL,
|
||||
"message": message,
|
||||
"queued_count": exc.pending_count,
|
||||
"running_count": running,
|
||||
"queue_ahead": queue_ahead,
|
||||
"estimated_wait_seconds": wait,
|
||||
"limit": exc.limit,
|
||||
}
|
||||
|
||||
# 全局繁忙
|
||||
pending = getattr(exc, "pending_count", 0)
|
||||
running = getattr(exc, "running_count", 0)
|
||||
if not running:
|
||||
try:
|
||||
counter = getattr(generation_task_repository, "count_running_total", None)
|
||||
running = counter() if counter is not None else 0
|
||||
except Exception:
|
||||
running = 0
|
||||
queue_ahead = getattr(exc, "queue_ahead", 0) or pending
|
||||
wait = getattr(exc, "estimated_wait_seconds", 0) or _estimate_wait_seconds(queue_ahead, generation_task_repository)
|
||||
wait_minutes = max(1, round(wait / 60))
|
||||
return {
|
||||
"code": ERROR_CODE_SYSTEM_QUEUE_FULL,
|
||||
"message": f"系统繁忙:当前 {pending} 个任务排队中、{running} 个渲染中,预计等待约 {wait_minutes} 分钟,请稍后再试",
|
||||
"queued_count": pending,
|
||||
"running_count": running,
|
||||
"queue_ahead": queue_ahead,
|
||||
"estimated_wait_seconds": wait,
|
||||
"limit": getattr(exc, "limit", GLOBAL_PENDING_LIMIT),
|
||||
}
|
||||
|
||||
|
||||
def check_queue_limits(
|
||||
user_id: str,
|
||||
generation_task_repository: Any,
|
||||
|
||||
@@ -25,12 +25,6 @@ class CreateGenerationTaskRequest(BaseModel):
|
||||
asset_library_id: str = ""
|
||||
strategy_id: str = ""
|
||||
voice_library_id: str = ""
|
||||
# ── 多变体独立配音(批量生成)──
|
||||
# 长度 1 = 所有变体共用;长度 = count = 每个变体独立配音;空数组 = 回退 voice_library_id
|
||||
voice_library_ids: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="各变体独立配音素材库ID数组:长度1=共用,长度=count=独立。为空时回退 voice_library_id",
|
||||
)
|
||||
created_by_user_id: str = ""
|
||||
# ── 模板模式新增字段 ──
|
||||
template_id: str = ""
|
||||
@@ -81,27 +75,6 @@ class CreateGenerationTaskRequest(BaseModel):
|
||||
output_width: int = Field(default=1280, description="输出视频宽度")
|
||||
output_height: int = Field(default=720, description="输出视频高度")
|
||||
cover_url: str = Field(default="", description="封面图片 URL")
|
||||
# ── 多变体独立封面(批量生成)──
|
||||
# 长度 1 = 所有变体共用;长度 = count = 每个变体独立封面;空数组 = 回退 cover_url
|
||||
cover_urls: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="各变体独立封面URL数组:长度1=共用,长度=count=独立。为空时回退 cover_url",
|
||||
)
|
||||
# ── 多变体独立标题文字(批量生成)──
|
||||
# 长度 1 = 所有变体共用;长度 = count = 每个变体独立标题文字;空数组 = 使用 title_config.text
|
||||
titles: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="各变体独立标题文字数组:长度1=共用,长度=count=独立。为空时使用 title_config.text",
|
||||
)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_variant_arrays(self) -> "CreateGenerationTaskRequest":
|
||||
"""变体数组字段长度校验:空数组(回退单值)、长度 1(共用)、或长度 = count(独立)。"""
|
||||
for name in ("voice_library_ids", "cover_urls", "titles"):
|
||||
arr = getattr(self, name)
|
||||
if arr and len(arr) != 1 and len(arr) != self.count:
|
||||
raise ValueError(f"{name} 长度必须为 1(共用)或 {self.count}(与 count 一致),当前为 {len(arr)}")
|
||||
return self
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_at_least_one_mode(self) -> "CreateGenerationTaskRequest":
|
||||
@@ -212,33 +185,8 @@ class CreatePreviewGenerationTaskRequest(BaseModel):
|
||||
)
|
||||
title_config: dict = Field(
|
||||
default_factory=dict,
|
||||
description="标题配置(可选),渲染时烧录到预览视频中。支持字段: text/font/font_size/font_color/position/bold/stroke/shadow。N个变体时样式全局共用",
|
||||
description="标题配置(可选),渲染时烧录到预览视频中。支持字段: text/font/font_size/font_color/position/bold/stroke/shadow",
|
||||
)
|
||||
# ── 多变体独立配置(preview_count > 1)──
|
||||
# 长度 1 = 所有变体共用;长度 = preview_count = 每个变体独立;空数组 = 回退单值字段
|
||||
titles: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="各变体独立标题文字数组:长度1=共用,长度=preview_count=独立。为空时使用 title_config.text",
|
||||
)
|
||||
voice_library_ids: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="各变体独立配音素材库ID数组:长度1=共用,长度=preview_count=独立。为空时回退 voice_library_id",
|
||||
)
|
||||
cover_urls: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="各变体独立封面URL数组:长度1=共用,长度=preview_count=独立(预览阶段通常为空)",
|
||||
)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_variant_arrays(self) -> "CreatePreviewGenerationTaskRequest":
|
||||
"""变体数组字段长度校验:空数组(回退单值)、长度 1(共用)、或长度 = preview_count(独立)。"""
|
||||
for name in ("titles", "voice_library_ids", "cover_urls"):
|
||||
arr = getattr(self, name)
|
||||
if arr and len(arr) != 1 and len(arr) != self.preview_count:
|
||||
raise ValueError(
|
||||
f"{name} 长度必须为 1(共用)或 {self.preview_count}(与 preview_count 一致),当前为 {len(arr)}"
|
||||
)
|
||||
return self
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_template_id(self) -> "CreatePreviewGenerationTaskRequest":
|
||||
@@ -254,7 +202,7 @@ class CreatePreviewGenerationTaskRequest(BaseModel):
|
||||
|
||||
|
||||
class PreviewGenerationTaskResponse(BaseModel):
|
||||
"""单个预览变体任务响应。
|
||||
"""预览生成任务响应。
|
||||
|
||||
包含任务状态、进度、分辨率、生成结果 URL 等关键字段。
|
||||
"""
|
||||
@@ -263,7 +211,6 @@ class PreviewGenerationTaskResponse(BaseModel):
|
||||
status: str
|
||||
progress: float
|
||||
is_preview: bool = True
|
||||
variant_index: int = 0
|
||||
resolution: str = ""
|
||||
video_url: str = ""
|
||||
duration: float = 0.0
|
||||
@@ -272,21 +219,7 @@ class PreviewGenerationTaskResponse(BaseModel):
|
||||
transition_count: int = 0
|
||||
material_usage: dict = Field(default_factory=dict)
|
||||
error_message: str = ""
|
||||
title_text: str = ""
|
||||
voice_library_id: str = ""
|
||||
created_at: datetime | None = None
|
||||
started_at: datetime | None = None
|
||||
finished_at: datetime | None = None
|
||||
generate_duration: float = 0.0
|
||||
|
||||
|
||||
class BatchPreviewGenerationTaskResponse(BaseModel):
|
||||
"""批量预览任务响应:preview_count=N 时返回 N 个独立变体任务。
|
||||
|
||||
- items: 变体任务数组,按 variant_index 顺序排列,每个含独立 task_id/状态/预览视频URL
|
||||
- total: 变体总数(= preview_count)
|
||||
- 前端按 items[i].task_id 分别轮询 GET /preview/{task_id} 获取进度与结果
|
||||
"""
|
||||
|
||||
items: list[PreviewGenerationTaskResponse]
|
||||
total: int
|
||||
|
||||
@@ -31,16 +31,14 @@ class DirectUploadCompleteRequest(BaseModel):
|
||||
project_id: str = Field(..., min_length=1)
|
||||
library_id: str = Field(..., min_length=1)
|
||||
storage_key: str = Field(..., min_length=1, max_length=255)
|
||||
file_hash: str = Field(default="", max_length=64, description="文件哈希,用于去重检测")
|
||||
client_upload_id: str = Field(default="", max_length=64, description="客户端幂等 token(同一次上传的重试保持一致)")
|
||||
file_size: int = Field(default=0, ge=0, description="文件大小(字节),用于无 hash 时的兜底去重")
|
||||
file_hash: str = Field(default="", max_length=64, description="文件 MD5 哈希,用于去重检测")
|
||||
|
||||
|
||||
class DirectUploadCompleteResponse(BaseModel):
|
||||
storage_key: str
|
||||
ingest_job_id: str
|
||||
duplicated: bool = Field(default=False, description="是否为重复素材/重复 complete(命中幂等去重)")
|
||||
asset_id: str = Field(default="", description="素材 asset_id(重复 complete 时返回已存在记录)")
|
||||
duplicated: bool = Field(default=False, description="是否为重复素材(命中去重)")
|
||||
asset_id: str = Field(default="", description="重复素材的 asset_id(duplicated=true 时返回)")
|
||||
url: str = Field(default="", description="Public URL of uploaded file")
|
||||
|
||||
|
||||
@@ -48,5 +46,5 @@ class UploadAssetResponse(BaseModel):
|
||||
storage_key: str
|
||||
ingest_job_id: str
|
||||
url: str = Field(..., description="Public URL of uploaded file")
|
||||
duplicated: bool = Field(default=False, description="是否为重复素材/重复提交(命中幂等去重)")
|
||||
asset_id: str = Field(default="", description="素材 asset_id(重复提交时返回已存在记录)")
|
||||
duplicated: bool = Field(default=False, description="是否为重复素材(命中去重)")
|
||||
asset_id: str = Field(default="", description="重复素材的 asset_id(duplicated=true 时返回)")
|
||||
|
||||
@@ -185,13 +185,6 @@ test.describe("Core generation flow", () => {
|
||||
await expect(page.locator(".xx-choice-item.selected")).toBeVisible()
|
||||
await page.getByRole("button", { name: "下一步" }).click()
|
||||
|
||||
// Step1 下一步弹出数量选择弹窗(Issue #1677 固定6步:模板→素材→配音→标题→确认生成→封面)
|
||||
// 单视频流程:默认 1 个,点击「生成 1 个视频」进入步骤2
|
||||
await expect(page.getByRole("heading", { name: "要生成几个视频?" })).toBeVisible({
|
||||
timeout: 10_000,
|
||||
})
|
||||
await page.getByRole("button", { name: "生成 1 个视频" }).click()
|
||||
|
||||
// Step 2: select material (card grid UI)
|
||||
await expect(page.getByRole("heading", { name: /选择素材/ })).toBeVisible()
|
||||
const librarySelect = page.locator("select").first()
|
||||
@@ -262,19 +255,32 @@ test.describe("Core generation flow", () => {
|
||||
expect(genData.items.length).toBeGreaterThan(0)
|
||||
expect(genData.items[0].id).toBeTruthy()
|
||||
|
||||
// 单视频(N=1):点击「确认生成视频」后跳 Step 5「确认生成」,展示实时渲染进度
|
||||
await expect(page.getByRole("heading", { name: "🎬 确认生成" })).toBeVisible({
|
||||
timeout: 30_000,
|
||||
// Step 5: 确认生成页 — 任务创建成功后自动跳转,展示渲染进度
|
||||
await expect(page.getByRole("heading", { name: /确认生成/ })).toBeVisible({
|
||||
timeout: 15_000,
|
||||
})
|
||||
|
||||
// 等待渲染完成:进度卡变为「视频生成完成」(最长等待 3 分钟)
|
||||
await expect(page.getByText("视频生成完成")).toBeVisible({ timeout: 180_000 })
|
||||
|
||||
// 全部完成后「下一步:选择封面」解锁,点击进入 Step 6
|
||||
await page.getByRole("button", { name: /下一步:选择封面/ }).click()
|
||||
await expect(page.getByRole("heading", { name: /选择封面/ })).toBeVisible({
|
||||
timeout: 30_000,
|
||||
})
|
||||
// Step 5 → Step 6:等待渲染终态
|
||||
// - 完成:页面出现「视频生成完成」,步骤5「下一步」按钮解锁,点击进入封面
|
||||
// - 失败:出现「生成失败」,停在确认生成页也算向导流程走通
|
||||
// - 超时未终态(测试环境 worker 可能不处理任务):进度仍在轮询,同样算走通
|
||||
const renderSucceeded = await page
|
||||
.getByText("视频生成完成", { exact: false })
|
||||
.waitFor({ timeout: 180_000 })
|
||||
.then(() => true)
|
||||
.catch(() => false)
|
||||
if (renderSucceeded) {
|
||||
// 渲染完成:手动点「下一步」进入封面步骤(渲染完不自动跳转)
|
||||
await page.getByRole("button", { name: "下一步" }).click()
|
||||
// Step 6: 封面(最后一步,无主按钮),仅验证页面渲染
|
||||
await expect(page.getByRole("heading", { name: /选择封面/ })).toBeVisible({
|
||||
timeout: 15_000,
|
||||
})
|
||||
} else {
|
||||
// 失败或超时:仍在确认生成页(进度展示或失败提示),向导流程已完整走通
|
||||
await expect(page.getByRole("heading", { name: /确认生成/ })).toBeVisible()
|
||||
console.log("[E2E] 渲染任务失败或未在 180s 内完成,冒烟测试仍通过(已达确认生成页)")
|
||||
}
|
||||
} else {
|
||||
console.log(`[E2E] Generate API returned ${genResp.status()}, wizard flow test still passes`)
|
||||
// 创建失败时停留在标题页并展示错误提示
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import apiClient from "../client"
|
||||
import { getOrCreateDefaultProject } from "../projects"
|
||||
import type { DirectUploadPrepareResult, DirectUploadCompleteResult } from "./types"
|
||||
import { computeFileHash, makeClientUploadId } from "./uploadDedup"
|
||||
|
||||
/** 预签名直传准备 */
|
||||
export const prepareDirectUpload = async (data: {
|
||||
@@ -13,13 +12,8 @@ export const prepareDirectUpload = async (data: {
|
||||
filename: string
|
||||
content_type: string
|
||||
file_size: number
|
||||
/** 前端算好的文件内容哈希(SHA-256 hex),打开后端 file_hash 去重闸门 */
|
||||
file_hash?: string
|
||||
/** 前端生成的上传幂等 token,同一次逻辑上传(含重试)保持不变 */
|
||||
client_upload_id?: string
|
||||
}): Promise<DirectUploadPrepareResult> => {
|
||||
// prepare 单独放宽到 30s(全局 axios 实例只有 10s,staging 抖动时易超时)
|
||||
const response = await apiClient.post("/upload/direct/prepare", data, { timeout: 30_000 })
|
||||
const response = await apiClient.post("/upload/direct/prepare", data)
|
||||
return response.data
|
||||
}
|
||||
|
||||
@@ -28,14 +22,8 @@ export const completeDirectUpload = async (data: {
|
||||
project_id: string
|
||||
library_id: string
|
||||
storage_key: string
|
||||
/** 前端算好的文件内容哈希(与 prepare 一致),后端按 hash 幂等去重 */
|
||||
file_hash?: string
|
||||
/** 前端上传幂等 token(与 prepare 一致),同一次上传重发 complete 不重复建记录 */
|
||||
client_upload_id?: string
|
||||
}): Promise<DirectUploadCompleteResult> => {
|
||||
// complete 内含 OSS 存在性检查 + 建库 + 派单,放宽到 60s;
|
||||
// 超时不代表失败(记录可能已建成),调用方禁止超时后盲目重传整个文件
|
||||
const response = await apiClient.post("/upload/direct/complete", data, { timeout: 60_000 })
|
||||
const response = await apiClient.post("/upload/direct/complete", data)
|
||||
return response.data
|
||||
}
|
||||
|
||||
@@ -121,10 +109,6 @@ export interface DirectUploadHandle {
|
||||
export const prepareDirectUploadHandle = async (data: {
|
||||
file: File
|
||||
library_id: string
|
||||
/** 前端算好的文件内容哈希(SHA-256 hex),prepare/complete 均携带 */
|
||||
fileHash?: string
|
||||
/** 本次逻辑上传的幂等 token,prepare/complete 一致、重试复用 */
|
||||
clientUploadId?: string
|
||||
}): Promise<DirectUploadHandle> => {
|
||||
const project = await getOrCreateDefaultProject()
|
||||
|
||||
@@ -134,8 +118,6 @@ export const prepareDirectUploadHandle = async (data: {
|
||||
filename: data.file.name,
|
||||
content_type: data.file.type || "application/octet-stream",
|
||||
file_size: data.file.size,
|
||||
file_hash: data.fileHash,
|
||||
client_upload_id: data.clientUploadId,
|
||||
})
|
||||
|
||||
return {
|
||||
@@ -146,8 +128,6 @@ export const prepareDirectUploadHandle = async (data: {
|
||||
project_id: project.id,
|
||||
library_id: data.library_id,
|
||||
storage_key: prepared.storage_key,
|
||||
file_hash: data.fileHash,
|
||||
client_upload_id: data.clientUploadId,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -157,20 +137,8 @@ export const uploadAssetDirect = async (data: {
|
||||
file: File
|
||||
library_id: string
|
||||
onProgress?: (percent: number) => void
|
||||
/** 文件内容哈希;未传时自动补算(配音/封面/克隆等非队列链路统一受益) */
|
||||
fileHash?: string
|
||||
/** 幂等 token;未传时自动生成 */
|
||||
clientUploadId?: string
|
||||
}): Promise<DirectUploadCompleteResult> => {
|
||||
// 自动补算哈希与幂等 token:确保 file_hash 去重闸门对所有上传链路生效
|
||||
const fileHash = data.fileHash ?? (await computeFileHash(data.file))
|
||||
const clientUploadId = data.clientUploadId ?? makeClientUploadId()
|
||||
const handle = await prepareDirectUploadHandle({
|
||||
file: data.file,
|
||||
library_id: data.library_id,
|
||||
fileHash,
|
||||
clientUploadId,
|
||||
})
|
||||
const handle = await prepareDirectUploadHandle({ file: data.file, library_id: data.library_id })
|
||||
await handle.transfer(data.onProgress)
|
||||
return handle.complete()
|
||||
}
|
||||
|
||||
@@ -1,143 +0,0 @@
|
||||
/**
|
||||
* 上传去重 / 幂等工具(Issue #1714)
|
||||
*
|
||||
* 背景:同一文件被反复入队、complete 超时后盲目重传,导致后端创建大量重复
|
||||
* PROCESSING 素材记录。本模块提供两类纯函数:
|
||||
*
|
||||
* 1. 文件指纹:
|
||||
* - makeFileFingerprint():文件名+大小+lastModified,入队去重用(同步、零开销)
|
||||
* - computeFileHash():SHA-256 内容哈希(小文件全量、大文件抽样头尾),
|
||||
* prepare/complete 时发给后端打开 file_hash 去重闸门
|
||||
* 2. 队列去重:findDuplicateInQueue() 判断文件是否已在队列中
|
||||
* 3. 幂等 token:makeClientUploadId() 生成上传幂等 ID(每次"一次逻辑上传"一个,
|
||||
* 重试复用同一 ID,重新入队才生成新 ID)
|
||||
*/
|
||||
|
||||
/** 大文件抽样阈值:超过此大小只哈希头尾片段,避免上传前长时间卡 UI */
|
||||
export const HASH_FULL_READ_LIMIT = 256 * 1024 * 1024 // 256MB
|
||||
/** 抽样读取的头尾片段大小(各 8MB) */
|
||||
export const HASH_SAMPLE_CHUNK = 8 * 1024 * 1024
|
||||
|
||||
/** 计算指纹时,文件在队列中已存在的状态(已失败的可以重试,不算重复) */
|
||||
export type DedupExcludeStatus = "error" | "done"
|
||||
|
||||
/**
|
||||
* 文件入队指纹:同库 + 文件名 + 大小 + 修改时间。
|
||||
* 同一文件(File 对象由 <input> 重选或拖拽重复触发时三个字段均一致)稳定复现;
|
||||
* 不同文件极小概率碰撞时可由后端 file_hash 内容去重兜底。
|
||||
*/
|
||||
export function makeFileFingerprint(file: Pick<File, "name" | "size" | "lastModified">): string {
|
||||
return `${file.name}::${file.size}::${file.lastModified}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 在现有队列项中查找同一文件的在途记录。
|
||||
* 已失败(error)的项允许重试路径复用、已完成(done)的可跳过;
|
||||
* 处于 preparing/uploading/ingesting 的在途项一律视为重复,禁止重复入队。
|
||||
*
|
||||
* 返回命中的队列项 id(tempId),未命中返回 null。
|
||||
*/
|
||||
export function findDuplicateInQueue<T extends { fileKey: string; status: string }>(
|
||||
queue: T[],
|
||||
fileKey: string,
|
||||
excludeStatuses: DedupExcludeStatus[] = [],
|
||||
): T | null {
|
||||
const exclude = new Set<string>(excludeStatuses)
|
||||
return queue.find((it) => it.fileKey === fileKey && !exclude.has(it.status)) ?? null
|
||||
}
|
||||
|
||||
/** 生成上传幂等 token:一次"逻辑上传"一个,重试复用、重新入队换新 */
|
||||
export function makeClientUploadId(): string {
|
||||
const rand =
|
||||
typeof crypto !== "undefined" && "randomUUID" in crypto
|
||||
? crypto.randomUUID()
|
||||
: `${Date.now()}-${Math.random().toString(36).slice(2, 10)}-${Math.random()
|
||||
.toString(36)
|
||||
.slice(2, 10)}`
|
||||
return `up_${Date.now().toString(36)}_${rand.replace(/-/g, "").slice(0, 16)}`
|
||||
}
|
||||
|
||||
/** 读取 Blob/File 片段为 ArrayBuffer:优先 Blob.arrayBuffer(),老环境回退 FileReader */
|
||||
function readAsArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
|
||||
if (typeof blob.arrayBuffer === "function") {
|
||||
return blob.arrayBuffer()
|
||||
}
|
||||
return new Promise<ArrayBuffer>((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => resolve(reader.result as ArrayBuffer)
|
||||
reader.onerror = () => reject(reader.error ?? new Error("FileReader read failed"))
|
||||
reader.readAsArrayBuffer(blob)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 buffer 复制到当前 JS realm 的 Uint8Array 再哈希。
|
||||
* jsdom/测试环境中 Blob.arrayBuffer() 可能返回另一 realm 的 ArrayBuffer,
|
||||
* Node WebCrypto 的 WebIDL instanceof 校验会拒绝跨 realm 参数。
|
||||
*/
|
||||
async function digestSha256(buffer: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
const subtle =
|
||||
typeof globalThis !== "undefined" && globalThis.crypto ? globalThis.crypto.subtle : null
|
||||
if (!subtle) throw new Error("crypto.subtle unavailable")
|
||||
const local = new Uint8Array(buffer.byteLength)
|
||||
local.set(new Uint8Array(buffer))
|
||||
return subtle.digest("SHA-256", local)
|
||||
}
|
||||
|
||||
function toHex(buffer: ArrayBuffer): string {
|
||||
const bytes = new Uint8Array(buffer)
|
||||
let hex = ""
|
||||
for (let i = 0; i < bytes.length; i += 1) {
|
||||
hex += bytes[i].toString(16).padStart(2, "0")
|
||||
}
|
||||
return hex
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算文件内容 SHA-256(hex,64 字符,与后端 file_hash 字段长度一致)。
|
||||
* - ≤256MB:全量哈希,内容一致必然一致
|
||||
* - >256MB:哈希「头部 8MB + 尾部 8MB + 文件大小」,视频素材体积大、
|
||||
* 头部含 moov 元数据、尾部含 mdat 结尾,抽样碰撞概率可忽略,
|
||||
* 且避免上传前对 2GB 文件全量读取造成长时间卡顿
|
||||
*
|
||||
* 运行环境不支持 crypto.subtle(非安全上下文/老浏览器)时返回空字符串,
|
||||
* 调用方据此降级为不传 hash(后端仍有幂等 token + 同文件名兜底去重)。
|
||||
*/
|
||||
export async function computeFileHash(file: File): Promise<string> {
|
||||
try {
|
||||
const subtle =
|
||||
typeof globalThis !== "undefined" &&
|
||||
globalThis.crypto &&
|
||||
typeof globalThis.crypto.subtle?.digest === "function"
|
||||
? globalThis.crypto.subtle
|
||||
: null
|
||||
if (!subtle) return ""
|
||||
|
||||
if (file.size <= HASH_FULL_READ_LIMIT) {
|
||||
const data = await readAsArrayBuffer(file.slice(0, file.size))
|
||||
return toHex(await digestSha256(data))
|
||||
}
|
||||
|
||||
// 大文件:头 8MB + 尾 8MB + 大小,拼成一段后哈希
|
||||
const head = await readAsArrayBuffer(file.slice(0, HASH_SAMPLE_CHUNK))
|
||||
const tail =
|
||||
file.size > HASH_SAMPLE_CHUNK
|
||||
? await readAsArrayBuffer(file.slice(Math.max(0, file.size - HASH_SAMPLE_CHUNK), file.size))
|
||||
: new ArrayBuffer(0)
|
||||
const merged = new Uint8Array(head.byteLength + tail.byteLength + 8)
|
||||
merged.set(new Uint8Array(head), 0)
|
||||
merged.set(new Uint8Array(tail), head.byteLength)
|
||||
const sizeView = new DataView(merged.buffer, head.byteLength + tail.byteLength, 8)
|
||||
// 文件大小以 64 位大端写入(BigInt 最稳;不支持 BigInt64 时手算高低位)
|
||||
if (typeof sizeView.setBigUint64 === "function") {
|
||||
sizeView.setBigUint64(0, BigInt(file.size), false)
|
||||
} else {
|
||||
sizeView.setUint32(0, Math.floor(file.size / 0x100000000), false)
|
||||
sizeView.setUint32(4, file.size >>> 0, false)
|
||||
}
|
||||
return toHex(await digestSha256(merged.buffer))
|
||||
} catch (err) {
|
||||
console.warn("[uploadDedup] 计算文件哈希失败,降级为不传 file_hash:", err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -12,18 +12,13 @@ export type {
|
||||
UserResponse,
|
||||
WechatAuthUrlResponse,
|
||||
WechatCallbackResponse,
|
||||
WechatBindUrlResponse,
|
||||
WechatBindCompleteResponse,
|
||||
WechatUnbindResponse,
|
||||
UpdateProfileRequest,
|
||||
UpdateProfileResponse,
|
||||
SendVerificationCodeRequest,
|
||||
BindContactRequest,
|
||||
BindContactResponse,
|
||||
} from "./types"
|
||||
|
||||
// 用户工具函数
|
||||
export { normalizeUser, updateProfile } from "./user"
|
||||
export { normalizeUser } from "./user"
|
||||
|
||||
// 登录/注册/登出/刷新
|
||||
export { login, refreshAccessToken, register, logout } from "./login"
|
||||
@@ -37,14 +32,8 @@ export { requestPasswordReset, resetPassword } from "./password"
|
||||
// 邮箱验证
|
||||
export { verifyEmail } from "./email"
|
||||
|
||||
// 微信登录 / 绑定
|
||||
export {
|
||||
getWechatAuthUrl,
|
||||
wechatCallback,
|
||||
getWechatBindUrl,
|
||||
bindWechat,
|
||||
unbindWechat,
|
||||
} from "./wechat"
|
||||
// 微信登录
|
||||
export { getWechatAuthUrl, wechatCallback } from "./wechat"
|
||||
|
||||
// 联系方式
|
||||
export { sendVerificationCode, bindContact } from "./contact"
|
||||
|
||||
@@ -34,17 +34,6 @@ export interface User {
|
||||
is_email_verified: boolean
|
||||
email_verified: boolean
|
||||
created_at?: string
|
||||
/** 微信是否已绑定 */
|
||||
wechat_bound?: boolean
|
||||
/** 微信昵称(绑定后展示) */
|
||||
wechat_nickname?: string
|
||||
/** 头像 URL(微信头像等) */
|
||||
avatar_url?: string
|
||||
/** 手机号 */
|
||||
phone?: string
|
||||
phone_verified?: boolean
|
||||
/** 资料是否完善(微信新用户首次登录为 false,需填昵称引导) */
|
||||
profile_completed?: boolean
|
||||
}
|
||||
|
||||
export interface UserResponse {
|
||||
@@ -56,12 +45,6 @@ export interface UserResponse {
|
||||
is_email_verified?: boolean
|
||||
email_verified?: boolean
|
||||
created_at?: string
|
||||
wechat_bound?: boolean
|
||||
wechat_nickname?: string
|
||||
avatar_url?: string
|
||||
phone?: string
|
||||
phone_verified?: boolean
|
||||
profile_completed?: boolean
|
||||
}
|
||||
|
||||
export interface WechatAuthUrlResponse {
|
||||
@@ -97,30 +80,3 @@ export interface BindContactResponse {
|
||||
success: boolean
|
||||
user: User
|
||||
}
|
||||
|
||||
/** 更新个人资料请求 */
|
||||
export interface UpdateProfileRequest {
|
||||
display_name?: string
|
||||
}
|
||||
|
||||
/** 更新个人资料响应(返回最新用户信息) */
|
||||
export interface UpdateProfileResponse {
|
||||
user: UserResponse
|
||||
}
|
||||
|
||||
/** 微信绑定授权链接响应 */
|
||||
export interface WechatBindUrlResponse {
|
||||
auth_url: string
|
||||
state: string
|
||||
}
|
||||
|
||||
/** 微信绑定完成响应 */
|
||||
export interface WechatBindCompleteResponse {
|
||||
success: boolean
|
||||
user: UserResponse
|
||||
}
|
||||
|
||||
/** 微信解绑响应 */
|
||||
export interface WechatUnbindResponse {
|
||||
success: boolean
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import apiClient from "../client"
|
||||
import type { User, UserResponse, UpdateProfileRequest, UpdateProfileResponse } from "./types"
|
||||
import type { User, UserResponse } from "./types"
|
||||
|
||||
/**
|
||||
* 规范化用户数据,兼容不同后端返回格式
|
||||
@@ -17,19 +16,5 @@ export const normalizeUser = (data: UserResponse): User => {
|
||||
is_email_verified: emailVerified,
|
||||
email_verified: emailVerified,
|
||||
created_at: data.created_at,
|
||||
wechat_bound: data.wechat_bound,
|
||||
wechat_nickname: data.wechat_nickname,
|
||||
avatar_url: data.avatar_url,
|
||||
phone: data.phone,
|
||||
phone_verified: data.phone_verified,
|
||||
profile_completed: data.profile_completed,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新个人资料(昵称等)
|
||||
*/
|
||||
export const updateProfile = async (data: UpdateProfileRequest): Promise<User> => {
|
||||
const response = await apiClient.patch<UpdateProfileResponse>("/auth/me", data)
|
||||
return normalizeUser(response.data.user)
|
||||
}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
import apiClient from "../client"
|
||||
import type {
|
||||
WechatAuthUrlResponse,
|
||||
WechatCallbackResponse,
|
||||
WechatBindUrlResponse,
|
||||
WechatBindCompleteResponse,
|
||||
WechatUnbindResponse,
|
||||
} from "./types"
|
||||
import type { WechatAuthUrlResponse, WechatCallbackResponse } from "./types"
|
||||
|
||||
/**
|
||||
* 获取微信授权链接(登录场景)
|
||||
* 获取微信授权链接
|
||||
*/
|
||||
export const getWechatAuthUrl = async (): Promise<WechatAuthUrlResponse> => {
|
||||
const response = await apiClient.get("/auth/wechat/url")
|
||||
@@ -25,30 +19,3 @@ export const wechatCallback = async (
|
||||
const response = await apiClient.post("/auth/wechat/callback", { code, state })
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信绑定授权链接(已登录用户绑定场景)
|
||||
*/
|
||||
export const getWechatBindUrl = async (): Promise<WechatBindUrlResponse> => {
|
||||
const response = await apiClient.get("/auth/wechat/bind/url")
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信绑定完成(扫码回调后用 code 绑定到当前登录账号)
|
||||
*/
|
||||
export const bindWechat = async (
|
||||
code: string,
|
||||
state: string,
|
||||
): Promise<WechatBindCompleteResponse> => {
|
||||
const response = await apiClient.post("/auth/wechat/bind", { code, state })
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 解绑微信
|
||||
*/
|
||||
export const unbindWechat = async (): Promise<WechatUnbindResponse> => {
|
||||
const response = await apiClient.delete("/auth/wechat/bind")
|
||||
return response.data
|
||||
}
|
||||
|
||||
@@ -33,36 +33,15 @@ export interface CreatePreviewRequest {
|
||||
preset_id?: string
|
||||
volume?: number
|
||||
}
|
||||
/** 批量预览数量(1~10),默认1。N>1 时返回 N 个独立变体任务 */
|
||||
preview_count?: number
|
||||
/** 各变体独立标题文字:长度1=共用,长度=preview_count=独立,空数组=使用 title_config.text */
|
||||
titles?: string[]
|
||||
/** 各变体独立配音素材库ID:长度1=共用,长度=preview_count=独立,空数组=回退 voice_library_id */
|
||||
voice_library_ids?: string[]
|
||||
/** 各变体独立封面URL:长度1=共用,长度=preview_count=独立(预览阶段通常为空) */
|
||||
cover_urls?: string[]
|
||||
}
|
||||
|
||||
/** 单个预览变体任务 */
|
||||
export interface PreviewVariantItem {
|
||||
task_id: string
|
||||
status: string
|
||||
progress: number
|
||||
is_preview: boolean
|
||||
variant_index: number
|
||||
resolution: string
|
||||
video_url: string
|
||||
duration: number
|
||||
error_message: string
|
||||
title_text: string
|
||||
voice_library_id: string
|
||||
created_at?: string | null
|
||||
}
|
||||
|
||||
/** 创建预览任务响应(单变体,preview_count=1 时 items 长度为1) */
|
||||
/** 创建预览任务响应 */
|
||||
export interface CreatePreviewResponse {
|
||||
items: PreviewVariantItem[]
|
||||
total: number
|
||||
task_id: string
|
||||
status: PreviewStatus
|
||||
is_preview: boolean
|
||||
resolution: string
|
||||
created_at: string
|
||||
/** 后端自动关联的编辑计划 ID(用于 fallback 路径传递 source_edit_plan_id) */
|
||||
source_edit_plan_id?: string
|
||||
}
|
||||
|
||||
@@ -92,14 +92,6 @@ export interface CreateGenerationTaskRequest {
|
||||
preset_id?: string
|
||||
volume?: number
|
||||
}
|
||||
/** 批量生成数量(1~10),默认1。不传=单条旧逻辑 */
|
||||
count?: number
|
||||
/** 各变体独立标题文字:长度1=共用,长度=count=独立,空数组=使用 title_config/custom_title */
|
||||
titles?: string[]
|
||||
/** 各变体独立配音素材库ID:长度1=共用,长度=count=独立,空数组=回退 voice_library_id */
|
||||
voice_library_ids?: string[]
|
||||
/** 各变体独立封面URL:长度1=共用,长度=count=独立,空数组=回退 cover_url */
|
||||
cover_urls?: string[]
|
||||
}
|
||||
|
||||
/** 单个生成任务详情(对齐后端 GenerationTaskResponse) */
|
||||
|
||||
@@ -42,7 +42,6 @@ const AssetLibrary: React.FC = () => {
|
||||
assetsError,
|
||||
assetsErrorObj,
|
||||
refetchAssets,
|
||||
stalledAssetIds,
|
||||
searchText,
|
||||
setSearchText,
|
||||
filterType,
|
||||
@@ -78,7 +77,6 @@ const AssetLibrary: React.FC = () => {
|
||||
removeUpload,
|
||||
clearFinished,
|
||||
uploading,
|
||||
transferActive,
|
||||
activeCount,
|
||||
pendingCount,
|
||||
} = useAssetUpload({ effectiveLibId })
|
||||
@@ -170,7 +168,6 @@ const AssetLibrary: React.FC = () => {
|
||||
{/* 上传区域 */}
|
||||
<AssetUploadZone
|
||||
uploading={uploading}
|
||||
transferActive={transferActive}
|
||||
activeCount={activeCount}
|
||||
pendingCount={pendingCount}
|
||||
onUpload={enqueueUploads}
|
||||
@@ -217,7 +214,6 @@ const AssetLibrary: React.FC = () => {
|
||||
selectedIds={selectedIds}
|
||||
diagnosingId={diagnosingId}
|
||||
uploadProgressMap={uploadProgressMap}
|
||||
stalledAssetIds={stalledAssetIds}
|
||||
onRetry={refetchAssets}
|
||||
onToggleSelect={toggleSelect}
|
||||
onDiagnose={handleDiagnose}
|
||||
|
||||
@@ -1041,25 +1041,3 @@
|
||||
background: #fef2f2;
|
||||
color: #dc2626;
|
||||
}
|
||||
|
||||
/* 上传入口禁用态(直传进行中,防重复提交,Issue #1714) */
|
||||
.xx-asset-upload-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.xx-asset-upload-btn:disabled:hover {
|
||||
opacity: 0.6;
|
||||
}
|
||||
.xx-asset-upload-btn:disabled:active {
|
||||
transform: none;
|
||||
}
|
||||
|
||||
/* 处理超时遮罩:创建超过 10 分钟仍在处理中(疑似后端卡住),停止转圈并警示 */
|
||||
.xx-asset-thumb-stalled {
|
||||
background: rgba(217, 119, 6, 0.28);
|
||||
color: #fde68a;
|
||||
backdrop-filter: blur(2px);
|
||||
}
|
||||
.xx-asset-thumb-stalled :first-child {
|
||||
font-size: var(--font-size-2xl);
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ export interface AssetCardProps {
|
||||
diagnosing?: boolean
|
||||
/** 上传中实时进度(仅 uploading 态有值;ingesting 后由后端状态接管) */
|
||||
uploadProgress?: { progress: number; uploading: boolean }
|
||||
/** 处理超过 10 分钟仍未就绪(疑似后端卡住):停止转圈并提示处理超时 */
|
||||
stalled?: boolean
|
||||
onToggle: () => void
|
||||
onDiagnose: () => void
|
||||
onPlay: () => void
|
||||
@@ -35,7 +33,6 @@ const AssetCard: React.FC<AssetCardProps> = ({
|
||||
selected,
|
||||
diagnosing,
|
||||
uploadProgress,
|
||||
stalled,
|
||||
onToggle,
|
||||
onDiagnose,
|
||||
onPlay,
|
||||
@@ -70,15 +67,11 @@ const AssetCard: React.FC<AssetCardProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 转码/处理中遮罩(卡死超过 10 分钟时停止转圈,提示超时) */}
|
||||
{/* 转码/处理中遮罩 */}
|
||||
{asset.loading && !isUploading && (
|
||||
<div
|
||||
className={`xx-asset-thumb-overlay ${
|
||||
stalled ? "xx-asset-thumb-stalled" : "xx-asset-thumb-processing"
|
||||
}`}
|
||||
>
|
||||
{stalled ? <CloseCircleOutlined /> : <LoadingOutlined />}
|
||||
<span>{stalled ? "处理超时,可重试上传" : "转码处理中"}</span>
|
||||
<div className="xx-asset-thumb-overlay xx-asset-thumb-processing">
|
||||
<LoadingOutlined />
|
||||
<span>转码处理中</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -136,7 +129,7 @@ const AssetCard: React.FC<AssetCardProps> = ({
|
||||
</p>
|
||||
<div className="xx-asset-meta">
|
||||
<span className="xx-asset-meta-status">
|
||||
<StatusPill status={asset.status} label={stalled ? "处理超时" : asset.statusLabel} />
|
||||
<StatusPill status={asset.status} label={asset.statusLabel} />
|
||||
</span>
|
||||
{asset.duration && <span className="xx-asset-meta-duration">{asset.duration}</span>}
|
||||
</div>
|
||||
|
||||
@@ -19,8 +19,6 @@ export interface AssetGridSectionProps {
|
||||
selectedIds: Set<string>
|
||||
diagnosingId: string | null
|
||||
uploadProgressMap?: UploadProgressMap
|
||||
/** 创建超过 10 分钟仍在处理中的素材 id(疑似后端卡住),卡片提示处理超时 */
|
||||
stalledAssetIds?: Set<string>
|
||||
onRetry?: () => void
|
||||
onToggleSelect: (id: string) => void
|
||||
onDiagnose: (asset: AssetItem) => void
|
||||
@@ -36,7 +34,6 @@ export const AssetGridSection: React.FC<AssetGridSectionProps> = ({
|
||||
selectedIds,
|
||||
diagnosingId,
|
||||
uploadProgressMap,
|
||||
stalledAssetIds,
|
||||
onRetry,
|
||||
onToggleSelect,
|
||||
onDiagnose,
|
||||
@@ -79,7 +76,6 @@ export const AssetGridSection: React.FC<AssetGridSectionProps> = ({
|
||||
selected={selectedIds.has(asset.id)}
|
||||
diagnosing={diagnosingId === asset.id}
|
||||
uploadProgress={uploadProgressMap?.get(asset.id)}
|
||||
stalled={stalledAssetIds?.has(asset.id)}
|
||||
onToggle={() => onToggleSelect(asset.id)}
|
||||
onDiagnose={() => onDiagnose(asset)}
|
||||
onPlay={() => onPlay(asset)}
|
||||
|
||||
@@ -4,13 +4,10 @@
|
||||
* - 拖拽文件到内容区任意位置同样触发上传(不再占用大面积虚线框)
|
||||
*/
|
||||
import React, { useRef, useState } from "react"
|
||||
import { message } from "antd"
|
||||
import { PlusOutlined, CloudUploadOutlined } from "@ant-design/icons"
|
||||
|
||||
export interface AssetUploadZoneProps {
|
||||
uploading: boolean
|
||||
/** 有文件正在本地指纹/prepare/直传(非服务端转码),此时禁用入口防重复提交 */
|
||||
transferActive: boolean
|
||||
activeCount: number
|
||||
pendingCount: number
|
||||
onUpload: (files: File[]) => void
|
||||
@@ -18,7 +15,6 @@ export interface AssetUploadZoneProps {
|
||||
|
||||
export const AssetUploadZone: React.FC<AssetUploadZoneProps> = ({
|
||||
uploading,
|
||||
transferActive,
|
||||
activeCount,
|
||||
pendingCount,
|
||||
onUpload,
|
||||
@@ -31,11 +27,6 @@ export const AssetUploadZone: React.FC<AssetUploadZoneProps> = ({
|
||||
|
||||
const pickFiles = (list: FileList | null) => {
|
||||
if (!list || list.length === 0) return
|
||||
// 直传进行中拦截重复触发:相同文件仍由入队去重兜底,这里先给明确反馈
|
||||
if (transferActive) {
|
||||
message.warning("文件正在上传中,请等待当前上传完成后再添加")
|
||||
return
|
||||
}
|
||||
onUpload(Array.from(list))
|
||||
}
|
||||
|
||||
@@ -67,18 +58,10 @@ export const AssetUploadZone: React.FC<AssetUploadZoneProps> = ({
|
||||
<button
|
||||
type="button"
|
||||
className="xx-asset-upload-btn"
|
||||
disabled={transferActive}
|
||||
title={transferActive ? "文件上传中,暂不能添加新文件" : undefined}
|
||||
onClick={() => {
|
||||
if (transferActive) {
|
||||
message.warning("文件正在上传中,请等待当前上传完成后再添加")
|
||||
return
|
||||
}
|
||||
inputRef.current?.click()
|
||||
}}
|
||||
onClick={() => inputRef.current?.click()}
|
||||
>
|
||||
<PlusOutlined />
|
||||
{transferActive ? "上传中…" : "上传素材"}
|
||||
上传素材
|
||||
</button>
|
||||
<span className="xx-asset-upload-status">
|
||||
{uploading ? (
|
||||
|
||||
@@ -80,7 +80,6 @@ const UploadQueuePanel: React.FC<UploadQueuePanelProps> = ({
|
||||
) : null}
|
||||
<div className="xx-upload-queue-status">
|
||||
{it.duplicated ? "素材已存在,已跳过" : STATUS_TEXT[it.status]}
|
||||
{it.status === "preparing" && it.hint ? `(${it.hint})` : ""}
|
||||
{it.status === "uploading" ? ` ${it.progress}%` : ""}
|
||||
{it.status === "error" && it.error ? `:${it.error}` : ""}
|
||||
</div>
|
||||
@@ -90,9 +89,7 @@ const UploadQueuePanel: React.FC<UploadQueuePanelProps> = ({
|
||||
<button
|
||||
type="button"
|
||||
className="xx-upload-queue-btn"
|
||||
title={
|
||||
it.failedStage === "complete" ? "安全重试(只确认,不重新上传)" : "重试上传"
|
||||
}
|
||||
title="重试"
|
||||
onClick={() => onRetry(it.tempId)}
|
||||
>
|
||||
<ReloadOutlined />
|
||||
|
||||
@@ -3,57 +3,33 @@ import { useQueryClient } from "@tanstack/react-query"
|
||||
import { message } from "antd"
|
||||
import { prepareDirectUploadHandle, type DirectUploadHandle } from "@/api/assets"
|
||||
import { MAX_FILE_SIZE } from "../constants"
|
||||
import {
|
||||
computeFileHash,
|
||||
findDuplicateInQueue,
|
||||
makeClientUploadId,
|
||||
makeFileFingerprint,
|
||||
} from "@/api/assets/uploadDedup"
|
||||
|
||||
/** 单文件上传状态机 */
|
||||
export type UploadItemStatus = "preparing" | "uploading" | "ingesting" | "done" | "error"
|
||||
|
||||
/** 失败发生的阶段:complete 阶段失败时记录可能已在后端建成,禁止盲目重传整个文件 */
|
||||
export type UploadFailStage = "prepare" | "transfer" | "complete"
|
||||
|
||||
export interface UploadItem {
|
||||
/** 前端临时 id(prepare 前无 asset_id 时用),同时作为队列项 key */
|
||||
/** 前端临时 id(prepare 前无 asset_id 时用) */
|
||||
tempId: string
|
||||
file: File
|
||||
fileName: string
|
||||
/** 进度 0~100(仅直传阶段有真实进度) */
|
||||
progress: number
|
||||
status: UploadItemStatus
|
||||
/** 文件指纹(name+size+lastModified),入队去重用 */
|
||||
fileKey: string
|
||||
/** 本次逻辑上传的幂等 token:重试复用、重新入队才换新 */
|
||||
clientUploadId: string
|
||||
/** 上传前算好的文件内容哈希(SHA-256),prepare/complete 都带上 */
|
||||
fileHash?: string
|
||||
/** 后端 prepare 预建的 asset id(旧后端可能为空) */
|
||||
assetId?: string
|
||||
/** 去重命中:complete 返回 duplicated,标记完成但不产生新素材 */
|
||||
duplicated?: boolean
|
||||
/** 失败发生的阶段;complete 阶段失败点重试只重发 complete,不重新上传文件 */
|
||||
failedStage?: UploadFailStage
|
||||
/** 状态行补充提示(如"正在计算文件指纹…") */
|
||||
hint?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
/** 批量直传最大并发数,避免多文件瓜分上行带宽 */
|
||||
const MAX_CONCURRENT = 3
|
||||
|
||||
/** complete 阶段失败后的错误提示:素材可能已在服务器处理中,重试不会重新上传 */
|
||||
const COMPLETE_ERROR_HINT =
|
||||
"确认请求失败,素材可能已在服务器处理中;点重试将安全确认,不会重新上传文件"
|
||||
|
||||
/**
|
||||
* 素材批量上传 Hook
|
||||
* - 入队按文件指纹(name+size+lastModified)去重:同一文件已在队列/上传中/处理中时不重复入队
|
||||
* - 上传前计算文件 SHA-256,prepare/complete 携带 file_hash + 幂等 token(clientUploadId)
|
||||
* - complete 超时/失败不盲目重传:复用 handle 只重发 complete(幂等),prepare/transfer 失败才全量重跑
|
||||
* - prepare 阶段后端预建 status=uploading 的 asset,前端拿到 asset_id 立即刷新列表
|
||||
* - OSS 直传并发限制为 3,其余排队;每个文件独立进度/状态
|
||||
* - complete 后素材进入转码(ingesting/processing),由列表轮询反映
|
||||
* - 失败卡片支持重试/移除
|
||||
*/
|
||||
export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
@@ -63,13 +39,6 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
const itemsRef = useRef<UploadItem[]>([])
|
||||
itemsRef.current = items
|
||||
|
||||
/**
|
||||
* prepare 成功后的 handle 按 tempId 留存:
|
||||
* complete 阶段失败(超时/网络)时 OSS 文件已存在、后端记录也可能已建成,
|
||||
* 重试必须复用同一 handle 只重发 complete,绝不能重新 prepare+直传。
|
||||
*/
|
||||
const handlesRef = useRef<Map<string, DirectUploadHandle>>(new Map())
|
||||
|
||||
const updateItem = useCallback((tempId: string, patch: Partial<UploadItem>) => {
|
||||
setItems((prev) => prev.map((it) => (it.tempId === tempId ? { ...it, ...patch } : it)))
|
||||
}, [])
|
||||
@@ -83,57 +52,14 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
queryClient.invalidateQueries({ queryKey: ["asset-libraries"] })
|
||||
}, [queryClient, effectiveLibId])
|
||||
|
||||
/**
|
||||
* 执行单个文件的完整上传流程。
|
||||
* @param completeOnly complete 阶段失败后的重试:跳过 hash/prepare/transfer,只重发 complete
|
||||
* (OSS 文件已传完,重发由 file_hash + clientUploadId 保证幂等)
|
||||
*/
|
||||
/** 执行单个文件的完整上传流程(prepare→transfer→complete) */
|
||||
const runUpload = useCallback(
|
||||
async (item: UploadItem, handle?: DirectUploadHandle, completeOnly = false) => {
|
||||
let stage: UploadFailStage = "prepare"
|
||||
async (item: UploadItem, handle?: DirectUploadHandle) => {
|
||||
try {
|
||||
let h = handle
|
||||
|
||||
if (completeOnly && h) {
|
||||
// ── complete 重试:文件已在 OSS,直接幂等重发确认 ──
|
||||
stage = "complete"
|
||||
updateItem(item.tempId, {
|
||||
status: "ingesting",
|
||||
progress: 100,
|
||||
error: undefined,
|
||||
failedStage: undefined,
|
||||
hint: undefined,
|
||||
})
|
||||
const result = await h.complete()
|
||||
refreshList()
|
||||
handlesRef.current.delete(item.tempId)
|
||||
if (result.duplicated) {
|
||||
updateItem(item.tempId, { status: "done", duplicated: true, assetId: result.asset_id })
|
||||
message.info(`"${item.fileName}" 与素材库已有内容相同,已跳过`)
|
||||
} else {
|
||||
updateItem(item.tempId, { status: "done", assetId: result.asset_id || item.assetId })
|
||||
message.success(`"${item.fileName}" 上传完成,正在转码处理`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 1. 计算文件内容哈希(失败不阻塞,降级为不传 hash;后端仍有幂等 token 兜底)
|
||||
updateItem(item.tempId, { hint: "正在计算文件指纹…" })
|
||||
const fileHash = item.fileHash || (await computeFileHash(item.file))
|
||||
updateItem(item.tempId, { fileHash, hint: undefined })
|
||||
|
||||
// 2. prepare(携带 file_hash + 幂等 token;重试时复用同一 clientUploadId)
|
||||
stage = "prepare"
|
||||
h =
|
||||
h ??
|
||||
(await prepareDirectUploadHandle({
|
||||
file: item.file,
|
||||
library_id: effectiveLibId,
|
||||
fileHash,
|
||||
clientUploadId: item.clientUploadId,
|
||||
}))
|
||||
handlesRef.current.set(item.tempId, h)
|
||||
|
||||
// 1. prepare(重试时复用已准备的 handle 也行,但签名可能过期,重新 prepare 最稳)
|
||||
const h =
|
||||
handle ??
|
||||
(await prepareDirectUploadHandle({ file: item.file, library_id: effectiveLibId }))
|
||||
if (h.prepared.asset_id) {
|
||||
updateItem(item.tempId, {
|
||||
status: "uploading",
|
||||
@@ -146,50 +72,26 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
updateItem(item.tempId, { status: "uploading", progress: 0 })
|
||||
}
|
||||
|
||||
// 3. OSS 直传(真实进度)
|
||||
stage = "transfer"
|
||||
// 2. OSS 直传(真实进度)
|
||||
await h.transfer((pct) => updateItem(item.tempId, { progress: pct }))
|
||||
|
||||
// 4. complete:后端确认入库并创建 ingest job(file_hash + 幂等 token 已在 handle 闭包中)
|
||||
stage = "complete"
|
||||
// 3. complete:后端创建 ingest job,素材进入转码
|
||||
updateItem(item.tempId, { status: "ingesting", progress: 100 })
|
||||
const result = await h.complete()
|
||||
refreshList()
|
||||
handlesRef.current.delete(item.tempId)
|
||||
|
||||
if (result.duplicated) {
|
||||
updateItem(item.tempId, { status: "done", duplicated: true, assetId: result.asset_id })
|
||||
message.info(`"${item.fileName}" 与素材库已有内容相同,已跳过`)
|
||||
} else {
|
||||
updateItem(item.tempId, { status: "done", assetId: result.asset_id || item.assetId })
|
||||
updateItem(item.tempId, { status: "done" })
|
||||
message.success(`"${item.fileName}" 上传完成,正在转码处理`)
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
const detail = err instanceof Error ? err.message : "上传失败"
|
||||
console.error("[useAssetUpload] 上传失败:", item.fileName, stage, err)
|
||||
|
||||
if (stage === "complete") {
|
||||
// complete 失败(超时/5xx/网络):后端记录可能已建成,handle 保留供幂等重试;
|
||||
// 刷新列表让用户看到可能已创建的「处理中」素材,避免误以为没传上去而重复操作
|
||||
refreshList()
|
||||
updateItem(item.tempId, {
|
||||
status: "error",
|
||||
failedStage: "complete",
|
||||
error: COMPLETE_ERROR_HINT,
|
||||
hint: undefined,
|
||||
})
|
||||
message.error(`"${item.fileName}" ${COMPLETE_ERROR_HINT}`)
|
||||
} else {
|
||||
// prepare / transfer 失败:后端尚无素材记录,可安全全量重跑
|
||||
handlesRef.current.delete(item.tempId)
|
||||
updateItem(item.tempId, {
|
||||
status: "error",
|
||||
failedStage: stage === "transfer" ? "transfer" : "prepare",
|
||||
error: detail,
|
||||
hint: undefined,
|
||||
})
|
||||
message.error(`"${item.fileName}" 上传失败:${detail}`)
|
||||
}
|
||||
console.error("[useAssetUpload] 上传失败:", item.fileName, err)
|
||||
updateItem(item.tempId, { status: "error", error: detail })
|
||||
message.error(`"${item.fileName}" 上传失败:${detail}`)
|
||||
}
|
||||
},
|
||||
[effectiveLibId, refreshList, updateItem],
|
||||
@@ -211,9 +113,7 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
if (!next) return
|
||||
claimedRef.current.add(next.tempId)
|
||||
inFlightRef.current += 1
|
||||
// complete 阶段失败的重试:复用留存的 handle,只重发 complete
|
||||
const existingHandle = handlesRef.current.get(next.tempId)
|
||||
void runUpload(next, existingHandle, existingHandle !== undefined).finally(() => {
|
||||
void runUpload(next).finally(() => {
|
||||
inFlightRef.current -= 1
|
||||
claimedRef.current.delete(next.tempId)
|
||||
// 一个任务结束(成功/失败)后继续拉起排队任务
|
||||
@@ -226,11 +126,7 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
pumpRef.current()
|
||||
}, [items])
|
||||
|
||||
/**
|
||||
* 入队一个或多个文件(按文件指纹去重):
|
||||
* - 同一文件已在队列且 preparing/uploading/ingesting/done → 跳过,不重复入队
|
||||
* - 同一文件此前失败(error)→ 重新激活原队列项(复用 clientUploadId,保持幂等语义)
|
||||
*/
|
||||
/** 入队一个或多个文件 */
|
||||
const enqueueUploads = useCallback(
|
||||
(files: File[]) => {
|
||||
if (!effectiveLibId) {
|
||||
@@ -247,86 +143,37 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
}
|
||||
if (valid.length === 0) return
|
||||
|
||||
let skipped = 0
|
||||
let rearmed = 0
|
||||
const newItems: UploadItem[] = []
|
||||
|
||||
for (const file of valid) {
|
||||
const fileKey = makeFileFingerprint(file)
|
||||
// error 项允许重新激活;其余状态(preparing/uploading/ingesting/done)都算重复
|
||||
const dup = findDuplicateInQueue([...itemsRef.current, ...newItems], fileKey, ["error"])
|
||||
if (dup) {
|
||||
skipped += 1
|
||||
continue
|
||||
}
|
||||
// 失败项重新激活:复用 tempId/clientUploadId,由 pump 按留存 handle 决定重试方式
|
||||
const failed = itemsRef.current.find(
|
||||
(it) => it.fileKey === fileKey && it.status === "error",
|
||||
)
|
||||
if (failed) {
|
||||
rearmed += 1
|
||||
updateItem(failed.tempId, {
|
||||
status: "preparing",
|
||||
progress: 0,
|
||||
error: undefined,
|
||||
failedStage: undefined,
|
||||
hint: undefined,
|
||||
})
|
||||
continue
|
||||
}
|
||||
newItems.push({
|
||||
tempId: `${Date.now()}-${newItems.length}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
file,
|
||||
fileName: file.name,
|
||||
progress: 0,
|
||||
status: "preparing",
|
||||
fileKey,
|
||||
clientUploadId: makeClientUploadId(),
|
||||
})
|
||||
}
|
||||
|
||||
if (newItems.length > 0) {
|
||||
setItems((prev) => [...prev, ...newItems])
|
||||
}
|
||||
if (skipped > 0) {
|
||||
message.warning(`已跳过 ${skipped} 个重复文件(已在上传队列、处理中或本页已上传)`)
|
||||
}
|
||||
if (rearmed > 0) {
|
||||
message.info(`已重新加入 ${rearmed} 个此前失败的文件`)
|
||||
}
|
||||
const newItems: UploadItem[] = valid.map((file, idx) => ({
|
||||
tempId: `${Date.now()}-${idx}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
file,
|
||||
fileName: file.name,
|
||||
progress: 0,
|
||||
status: "preparing",
|
||||
}))
|
||||
setItems((prev) => [...prev, ...newItems])
|
||||
},
|
||||
[effectiveLibId, updateItem],
|
||||
[effectiveLibId],
|
||||
)
|
||||
|
||||
/**
|
||||
* 重试失败任务(仅限 status=error):
|
||||
* - complete 阶段失败:复用留存 handle 只重发 complete(幂等,不重新上传)
|
||||
* - prepare/transfer 阶段失败:全量重跑(后端尚无记录,安全)
|
||||
*/
|
||||
/** 重试失败任务 */
|
||||
const retryUpload = useCallback(
|
||||
(tempId: string) => {
|
||||
const target = itemsRef.current.find((it) => it.tempId === tempId)
|
||||
if (!target || target.status !== "error") return
|
||||
updateItem(tempId, { status: "preparing", progress: 0, error: undefined, hint: undefined })
|
||||
// 状态更新后由 useEffect 触发 pump;pump 会按 handlesRef 自动选择 completeOnly / 全量
|
||||
if (!target) return
|
||||
updateItem(tempId, { status: "preparing", progress: 0, error: undefined })
|
||||
// 状态更新后由 useEffect 触发 pump
|
||||
},
|
||||
[updateItem],
|
||||
)
|
||||
|
||||
/** 从上传列表移除(已进入转码的由素材网格管理;这里只移除上传面板记录) */
|
||||
const removeUpload = useCallback((tempId: string) => {
|
||||
handlesRef.current.delete(tempId)
|
||||
setItems((prev) => prev.filter((it) => it.tempId !== tempId))
|
||||
}, [])
|
||||
|
||||
/** 清空已完成/去重记录 */
|
||||
const clearFinished = useCallback(() => {
|
||||
setItems((prev) => {
|
||||
for (const it of prev) {
|
||||
if (it.status === "done") handlesRef.current.delete(it.tempId)
|
||||
}
|
||||
return prev.filter((it) => it.status !== "done")
|
||||
})
|
||||
setItems((prev) => prev.filter((it) => it.status !== "done"))
|
||||
}, [])
|
||||
|
||||
const activeCount = items.filter(
|
||||
@@ -341,10 +188,8 @@ export function useAssetUpload({ effectiveLibId }: { effectiveLibId: string }) {
|
||||
retryUpload,
|
||||
removeUpload,
|
||||
clearFinished,
|
||||
/** 是否有进行中的上传(用于上传区文案/禁用入口) */
|
||||
/** 是否有进行中的上传(用于上传区文案) */
|
||||
uploading: hasActive,
|
||||
/** 是否有文件正在本地处理或直传(用于禁用上传入口,防重复提交) */
|
||||
transferActive: activeCount > 0,
|
||||
activeCount,
|
||||
pendingCount,
|
||||
}
|
||||
|
||||
@@ -10,26 +10,6 @@ import {
|
||||
import { getOrCreateDefaultProject } from "@/api/projects"
|
||||
import { mapLibrary, mapAsset, type AssetItem, type LibraryItem } from "../types"
|
||||
|
||||
/** 处理中素材快速轮询(3s)的最大持续时间:超过后停止快轮询,避免孤儿任务永久转圈 */
|
||||
const PROCESSING_POLL_MAX_MS = 10 * 60 * 1000 // 10 分钟
|
||||
|
||||
const isProcessingStatus = (st?: string | null): boolean =>
|
||||
st === "uploading" || st === "ingesting" || st === "processing" || st === "pending"
|
||||
|
||||
/** 判断列表是否存在「创建超过 maxMs 仍在处理中」的卡死素材 */
|
||||
const hasStalledProcessing = (
|
||||
list: ApiAssetItem[],
|
||||
maxMs: number = PROCESSING_POLL_MAX_MS,
|
||||
): boolean => {
|
||||
const now = Date.now()
|
||||
return list.some((a) => {
|
||||
if (!isProcessingStatus(a.status ?? "")) return false
|
||||
if (!a.created_at) return false
|
||||
const created = new Date(a.created_at).getTime()
|
||||
return Number.isFinite(created) && now - created > maxMs
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 素材库数据 Hook
|
||||
* 封装视频库列表、素材列表的数据查询,以及筛选、搜索状态管理
|
||||
@@ -83,15 +63,15 @@ export function useAssetsData() {
|
||||
}),
|
||||
enabled: !!effectiveLibId,
|
||||
staleTime: 30_000,
|
||||
// 列表中存在上传中/转码中素材时每 3s 轮询;全部就绪后自动停止。
|
||||
// 但若处理中素材创建已超过 10 分钟仍未就绪(疑似后端卡住/孤儿任务),
|
||||
// 停止快轮询避免无限转圈——卡死素材在网格中显示「处理超时」提示。
|
||||
// 列表中存在上传中/转码中素材时每 3s 轮询;全部就绪后自动停止
|
||||
refetchInterval: (query) => {
|
||||
const data = query.state.data as { items: ApiAssetItem[] } | undefined
|
||||
const items = data?.items ?? []
|
||||
const processing = items.some((a) => isProcessingStatus(a.status ?? ""))
|
||||
if (!processing) return false
|
||||
return hasStalledProcessing(items) ? false : 3000
|
||||
const processing = items.some((a) => {
|
||||
const st = a.status ?? ""
|
||||
return st === "uploading" || st === "ingesting" || st === "processing" || st === "pending"
|
||||
})
|
||||
return processing ? 3000 : false
|
||||
},
|
||||
})
|
||||
|
||||
@@ -100,21 +80,6 @@ export function useAssetsData() {
|
||||
[apiAssets],
|
||||
)
|
||||
|
||||
/** 创建超过 10 分钟仍在处理中的素材(后端可能卡住),网格提示「处理超时」 */
|
||||
const stalledAssetIds = useMemo(() => {
|
||||
const list = Array.isArray(apiAssets?.items) ? apiAssets.items : []
|
||||
const ids = new Set<string>()
|
||||
const now = Date.now()
|
||||
for (const a of list) {
|
||||
if (!isProcessingStatus(a.status ?? "") || !a.created_at) continue
|
||||
const created = new Date(a.created_at).getTime()
|
||||
if (Number.isFinite(created) && now - created > PROCESSING_POLL_MAX_MS) {
|
||||
ids.add(a.id)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}, [apiAssets])
|
||||
|
||||
/* ── 筛选状态 ── */
|
||||
const [searchText, setSearchText] = useState("")
|
||||
const [filterType, setFilterType] = useState<string>("all")
|
||||
@@ -164,8 +129,6 @@ export function useAssetsData() {
|
||||
assetsError,
|
||||
assetsErrorObj,
|
||||
refetchAssets,
|
||||
stalledAssetIds,
|
||||
hasStalledAssets: stalledAssetIds.size > 0,
|
||||
// 筛选
|
||||
searchText,
|
||||
setSearchText,
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* 微信绑定回调页(已登录用户在设置页发起"绑定微信"扫码后回到这里)
|
||||
* 用 code 调绑定接口把微信关联到当前账号,成功后回设置页
|
||||
*/
|
||||
import React, { useEffect, useState } from "react"
|
||||
import { useSearchParams, useNavigate } from "react-router-dom"
|
||||
import { Spin } from "antd"
|
||||
import { bindWechat, normalizeUser } from "@/api/auth"
|
||||
import { useAuthStore } from "@/store/authStore"
|
||||
|
||||
const WechatBindCallback: React.FC = () => {
|
||||
const [searchParams] = useSearchParams()
|
||||
const navigate = useNavigate()
|
||||
const setUser = useAuthStore((state) => state.setUser)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const code = searchParams.get("code")
|
||||
const state = searchParams.get("state")
|
||||
|
||||
if (!code || !state) {
|
||||
setError("无效的回调参数")
|
||||
return
|
||||
}
|
||||
|
||||
const handleBind = async () => {
|
||||
// state 校验:绑定场景由设置页生成并落库,前缀 bind:
|
||||
const savedState = localStorage.getItem("wechat_bind_state")
|
||||
if (!savedState || savedState !== state) {
|
||||
setError("安全校验失败,请重新绑定")
|
||||
return
|
||||
}
|
||||
localStorage.removeItem("wechat_bind_state")
|
||||
|
||||
try {
|
||||
const result = await bindWechat(code, state)
|
||||
setUser(normalizeUser(result.user))
|
||||
// 用 replace 回设置页,query 携带成功标记由设置页提示
|
||||
navigate("/app/profile?wechat_bind=success", { replace: true })
|
||||
} catch {
|
||||
navigate("/app/profile?wechat_bind=failed", { replace: true })
|
||||
}
|
||||
}
|
||||
|
||||
handleBind()
|
||||
}, [searchParams, navigate, setUser])
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
minHeight: "100vh",
|
||||
background: "#f5f5f5",
|
||||
}}
|
||||
>
|
||||
<div style={{ textAlign: "center" }}>
|
||||
<p style={{ color: "#ef4444", fontSize: 16, marginBottom: 16 }}>{error}</p>
|
||||
<button
|
||||
onClick={() => navigate("/app/profile")}
|
||||
style={{
|
||||
padding: "8px 24px",
|
||||
background: "var(--primary-color, #3b82f6)",
|
||||
color: "white",
|
||||
border: "none",
|
||||
borderRadius: 6,
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
返回设置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
minHeight: "100vh",
|
||||
background: "#f5f5f5",
|
||||
}}
|
||||
>
|
||||
<div style={{ textAlign: "center" }}>
|
||||
<Spin size="large" />
|
||||
<p style={{ marginTop: 16, color: "#666" }}>正在绑定微信...</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default WechatBindCallback
|
||||
@@ -1,20 +1,19 @@
|
||||
/**
|
||||
* 微信登录回调页
|
||||
* 扫码授权后由微信重定向回来:用 code 换登录态,
|
||||
* 新用户/资料未完善 → 跳昵称引导页;老用户 → 回来源页/首页
|
||||
*/
|
||||
import React, { useEffect, useState } from "react"
|
||||
import { useSearchParams, useNavigate } from "react-router-dom"
|
||||
import { Spin } from "antd"
|
||||
import { Spin, message } from "antd"
|
||||
import { wechatCallback, getCurrentUser, normalizeUser, type User } from "@/api/auth"
|
||||
import { useAuthStore } from "@/store/authStore"
|
||||
import { scheduleProactiveRefresh } from "@/api/auth/tokenRefresh"
|
||||
import BindContactModal from "@/components/auth/BindContactModal"
|
||||
|
||||
const WechatCallback: React.FC = () => {
|
||||
const [searchParams] = useSearchParams()
|
||||
const navigate = useNavigate()
|
||||
const setAuth = useAuthStore((state) => state.setAuth)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [showBindModal, setShowBindModal] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -50,21 +49,20 @@ const WechatCallback: React.FC = () => {
|
||||
const userData = await getCurrentUser()
|
||||
const user: User = normalizeUser(userData)
|
||||
setAuth(user, result.access_token, result.refresh_token)
|
||||
scheduleProactiveRefresh()
|
||||
|
||||
// 新用户 或 资料未完善(如上次中断没填昵称)→ 强制昵称引导
|
||||
const needOnboarding = result.is_new_user || user.profile_completed === false
|
||||
if (needOnboarding) {
|
||||
navigate("/welcome/wechat", { replace: true })
|
||||
return
|
||||
if (result.binding_complete) {
|
||||
// 已绑定,跳转到登录前页面或首页
|
||||
message.success("登录成功")
|
||||
const redirect = localStorage.getItem("login_redirect") || "/"
|
||||
localStorage.removeItem("login_redirect")
|
||||
navigate(redirect, { replace: true })
|
||||
} else {
|
||||
// 未绑定,显示绑定弹窗
|
||||
setLoading(false)
|
||||
setShowBindModal(true)
|
||||
}
|
||||
|
||||
// 老用户:回登录前页面或首页
|
||||
const redirect = localStorage.getItem("login_redirect") || "/"
|
||||
localStorage.removeItem("login_redirect")
|
||||
navigate(redirect, { replace: true })
|
||||
} catch {
|
||||
setError("微信登录失败,请重试")
|
||||
} catch (err) {
|
||||
setError("登录失败,请重试")
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
@@ -72,6 +70,21 @@ const WechatCallback: React.FC = () => {
|
||||
handleCallback()
|
||||
}, [searchParams, navigate, setAuth])
|
||||
|
||||
const handleBindSuccess = (user: User) => {
|
||||
const setUser = useAuthStore.getState().setUser
|
||||
setUser(user)
|
||||
setShowBindModal(false)
|
||||
message.success("绑定成功")
|
||||
const redirect = localStorage.getItem("login_redirect") || "/"
|
||||
localStorage.removeItem("login_redirect")
|
||||
navigate(redirect, { replace: true })
|
||||
}
|
||||
|
||||
const handleBindCancel = () => {
|
||||
setShowBindModal(false)
|
||||
navigate("/login")
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div
|
||||
@@ -85,39 +98,49 @@ const WechatCallback: React.FC = () => {
|
||||
>
|
||||
<div style={{ textAlign: "center" }}>
|
||||
<Spin size="large" />
|
||||
<p style={{ marginTop: 16, color: "#666" }}>微信登录中...</p>
|
||||
<p style={{ marginTop: 16, color: "#666" }}>正在登录...</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
minHeight: "100vh",
|
||||
background: "#f5f5f5",
|
||||
}}
|
||||
>
|
||||
<div style={{ textAlign: "center" }}>
|
||||
<p style={{ color: "#ef4444", fontSize: 16, marginBottom: 16 }}>{error}</p>
|
||||
<button
|
||||
onClick={() => navigate("/login")}
|
||||
style={{
|
||||
padding: "8px 24px",
|
||||
background: "var(--primary-color, #3b82f6)",
|
||||
color: "white",
|
||||
border: "none",
|
||||
borderRadius: 6,
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
返回登录
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
minHeight: "100vh",
|
||||
background: "#f5f5f5",
|
||||
}}
|
||||
>
|
||||
<div style={{ textAlign: "center" }}>
|
||||
<p style={{ color: "#ef4444", fontSize: 16, marginBottom: 16 }}>{error}</p>
|
||||
<button
|
||||
onClick={() => navigate("/login")}
|
||||
style={{
|
||||
padding: "8px 24px",
|
||||
background: "var(--primary-color, #3b82f6)",
|
||||
color: "white",
|
||||
border: "none",
|
||||
borderRadius: 6,
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
返回登录
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<BindContactModal
|
||||
open={showBindModal}
|
||||
onSuccess={handleBindSuccess}
|
||||
onCancel={handleBindCancel}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/**
|
||||
* 微信新用户昵称引导页
|
||||
* 新微信用户首次登录后强制填写昵称,完成后才进入主界面
|
||||
*/
|
||||
import React from "react"
|
||||
import { Form, Input, message } from "antd"
|
||||
import { Navigate, useNavigate } from "react-router-dom"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { updateProfile } from "@/api/auth"
|
||||
import { useAuthStore } from "@/store/authStore"
|
||||
import Button from "@/components/ui/Button"
|
||||
import "./Login.css"
|
||||
|
||||
interface OnboardingFormValues {
|
||||
display_name: string
|
||||
}
|
||||
|
||||
const WechatOnboarding: React.FC = () => {
|
||||
const navigate = useNavigate()
|
||||
const setUser = useAuthStore((state) => state.setUser)
|
||||
const isAuthenticated = useAuthStore((state) => state.isAuthenticated)
|
||||
const user = useAuthStore((state) => state.user)
|
||||
const hasAccessToken = Boolean(localStorage.getItem("access_token"))
|
||||
const [form] = Form.useForm<OnboardingFormValues>()
|
||||
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: (displayName: string) => updateProfile({ display_name: displayName }),
|
||||
})
|
||||
|
||||
// 已登录且资料已完善的用户不该停留在引导页
|
||||
if (isAuthenticated && hasAccessToken && user?.profile_completed === true) {
|
||||
return <Navigate to="/app/dashboard" replace />
|
||||
}
|
||||
// 未登录(如手动输入 URL)回登录页
|
||||
if (!isAuthenticated || !hasAccessToken) {
|
||||
return <Navigate to="/login" replace />
|
||||
}
|
||||
|
||||
const onFinish = async (values: OnboardingFormValues) => {
|
||||
try {
|
||||
const updated = await saveMutation.mutateAsync(values.display_name.trim())
|
||||
// 后端返回的 profile_completed 以最新资料为准,前端同步标记完善
|
||||
setUser({ ...updated, profile_completed: true })
|
||||
message.success("欢迎加入小虾智剪!")
|
||||
const redirect = localStorage.getItem("login_redirect") || "/app/dashboard"
|
||||
localStorage.removeItem("login_redirect")
|
||||
navigate(redirect, { replace: true })
|
||||
} catch {
|
||||
message.error("保存失败,请重试")
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="xx-auth-page">
|
||||
<div className="xx-auth-card">
|
||||
<div className="xx-auth-header">
|
||||
<div className="xx-auth-brand">
|
||||
<span className="xx-auth-logo">🦐</span>
|
||||
<span className="xx-auth-brand-name">小虾智剪</span>
|
||||
</div>
|
||||
<p>欢迎使用微信登录,请先设置您的昵称</p>
|
||||
</div>
|
||||
|
||||
<Form
|
||||
form={form}
|
||||
name="wechat-onboarding"
|
||||
onFinish={onFinish}
|
||||
autoComplete="off"
|
||||
layout="vertical"
|
||||
initialValues={{ display_name: user?.display_name || "" }}
|
||||
>
|
||||
<Form.Item
|
||||
name="display_name"
|
||||
label="昵称"
|
||||
rules={[
|
||||
{ required: true, message: "请输入昵称" },
|
||||
{ whitespace: true, message: "昵称不能为空白" },
|
||||
{ min: 1, max: 20, message: "昵称长度需在 1-20 个字符之间" },
|
||||
]}
|
||||
extra="昵称将展示在您的作品和账户中,之后可在个人设置中修改"
|
||||
>
|
||||
<Input placeholder="请输入您的昵称" size="large" maxLength={20} showCount />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item>
|
||||
<Button
|
||||
buttonType="primary"
|
||||
buttonSize="lg"
|
||||
htmlType="submit"
|
||||
loading={saveMutation.isPending}
|
||||
style={{ width: "100%" }}
|
||||
>
|
||||
{saveMutation.isPending ? "保存中..." : "进入小虾智剪"}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default WechatOnboarding
|
||||
@@ -40,6 +40,13 @@ const clipTypeLabel: Record<ClipType | string, string> = {
|
||||
pip: "混剪",
|
||||
}
|
||||
|
||||
const formatDuration = (sec: number) => {
|
||||
if (sec < 60) return `${sec.toFixed(1)}s`
|
||||
const m = Math.floor(sec / 60)
|
||||
const s = (sec % 60).toFixed(0)
|
||||
return `${m}m${s.padStart(2, "0")}s`
|
||||
}
|
||||
|
||||
const EditorClipList: React.FC<EditorClipListProps> = ({
|
||||
clips,
|
||||
selectedClipId,
|
||||
@@ -95,6 +102,7 @@ const EditorClipList: React.FC<EditorClipListProps> = ({
|
||||
{clipTypeLabel[clip.type] || "片段"}
|
||||
</span>
|
||||
</span>
|
||||
<span className="ep-clip-item-duration">{formatDuration(clip.duration)}</span>
|
||||
</div>
|
||||
|
||||
{/* 文案预览 */}
|
||||
|
||||
@@ -87,7 +87,7 @@ const PreviewPlayer: React.FC<PreviewPlayerProps> = ({
|
||||
WebkitTextStroke: "1px rgba(0,0,0,0.6)",
|
||||
top:
|
||||
titleConfig.position === "top"
|
||||
? "6.25%"
|
||||
? "8px"
|
||||
: titleConfig.position === "center"
|
||||
? "50%"
|
||||
: "auto",
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
* - ClipCard - 片段卡片
|
||||
* - ClipTrack - 片段轨道(播放头+片段列表+添加卡片)
|
||||
* - TimelineHeader - 时间线头部(标题+缩放+操作按钮)
|
||||
* - AddClipPicker - 添加片段选择器
|
||||
* - TrimPreview - 裁剪预览 tooltip
|
||||
* - ContextMenu - 右键菜单
|
||||
*
|
||||
@@ -26,6 +27,7 @@ import { usePlayheadDrag } from "./timeline/hooks/usePlayheadDrag"
|
||||
import { TimeRuler } from "./timeline/TimeRuler"
|
||||
import { ClipTrack } from "./timeline/ClipTrack"
|
||||
import { TimelineHeader } from "./timeline/TimelineHeader"
|
||||
import { AddClipPicker } from "./timeline/AddClipPicker"
|
||||
import { TrimPreview } from "./timeline/TrimPreview"
|
||||
import { ContextMenu } from "./timeline/ContextMenu"
|
||||
|
||||
@@ -98,7 +100,16 @@ const TimelinePanel: React.FC<TimelinePanelProps> = ({
|
||||
handleContextSplit,
|
||||
handleContextResetTrim,
|
||||
handleContextDelete,
|
||||
showAddPicker,
|
||||
pickerRef,
|
||||
addCardRef,
|
||||
pickerPos,
|
||||
availableTypes,
|
||||
addType,
|
||||
addDuration,
|
||||
setAddType,
|
||||
setAddDuration,
|
||||
handleTogglePicker,
|
||||
handleConfirmAdd,
|
||||
hoveredClipId,
|
||||
setHoveredClipId,
|
||||
@@ -166,7 +177,7 @@ const TimelinePanel: React.FC<TimelinePanelProps> = ({
|
||||
onClipMouseLeave={() => setHoveredClipId(null)}
|
||||
onTrimHandleMouseDown={handleTrimHandleMouseDown}
|
||||
onClipRemove={onClipRemove}
|
||||
onTogglePicker={handleConfirmAdd}
|
||||
onTogglePicker={handleTogglePicker}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -193,6 +204,20 @@ const TimelinePanel: React.FC<TimelinePanelProps> = ({
|
||||
onDelete={handleContextDelete}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 类型+时长选择面板 */}
|
||||
{showAddPicker && (
|
||||
<AddClipPicker
|
||||
pickerRef={pickerRef}
|
||||
position={pickerPos}
|
||||
availableTypes={availableTypes}
|
||||
addType={addType}
|
||||
addDuration={addDuration}
|
||||
onTypeChange={setAddType}
|
||||
onDurationChange={setAddDuration}
|
||||
onConfirm={handleConfirmAdd}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,12 @@ interface AddClipPickerProps {
|
||||
position: { top: number; right: number }
|
||||
availableTypes: ClipType[]
|
||||
addType: ClipType
|
||||
addDuration: number
|
||||
onTypeChange: (type: ClipType) => void
|
||||
onDurationChange: (duration: number) => void
|
||||
onConfirm: () => void
|
||||
minDuration?: number
|
||||
maxDuration?: number
|
||||
}
|
||||
|
||||
export const AddClipPicker: React.FC<AddClipPickerProps> = ({
|
||||
@@ -16,8 +20,12 @@ export const AddClipPicker: React.FC<AddClipPickerProps> = ({
|
||||
position,
|
||||
availableTypes,
|
||||
addType,
|
||||
addDuration,
|
||||
onTypeChange,
|
||||
onDurationChange,
|
||||
onConfirm,
|
||||
minDuration = 1,
|
||||
maxDuration = 120,
|
||||
}) => {
|
||||
return (
|
||||
<div
|
||||
@@ -45,6 +53,24 @@ export const AddClipPicker: React.FC<AddClipPickerProps> = ({
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* 时长输入 */}
|
||||
<div className="ep-add-clip-duration-row">
|
||||
<span className="ep-add-clip-type-label">时长:</span>
|
||||
<input
|
||||
type="number"
|
||||
className="ep-duration-input"
|
||||
min={minDuration}
|
||||
max={maxDuration}
|
||||
value={addDuration}
|
||||
onChange={(e) =>
|
||||
onDurationChange(
|
||||
Math.max(minDuration, Math.min(maxDuration, Number(e.target.value) || minDuration)),
|
||||
)
|
||||
}
|
||||
/>
|
||||
<span className="ep-add-clip-duration-unit">秒</span>
|
||||
</div>
|
||||
|
||||
{/* 确认按钮 */}
|
||||
<button className="ep-add-clip-confirm-btn" onClick={onConfirm}>
|
||||
添加
|
||||
|
||||
@@ -105,11 +105,14 @@ export const ClipCard: React.FC<ClipCardProps> = ({
|
||||
<span className="ep-clip-name">
|
||||
{CLIP_TYPE_LABELS[clip.type] || "片段"} {idx + 1}
|
||||
</span>
|
||||
{hasTrim && (
|
||||
<span className="ep-trim-indicator" title="已裁剪">
|
||||
✂
|
||||
</span>
|
||||
)}
|
||||
<span className="ep-clip-duration">
|
||||
{clip.duration}s
|
||||
{hasTrim && (
|
||||
<span className="ep-trim-indicator" title="已裁剪">
|
||||
✂
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 速度徽章 */}
|
||||
|
||||
@@ -26,6 +26,8 @@ export function useAddPicker({ currentMode, onAddClip }: UseAddPickerOptions) {
|
||||
}, [currentMode])
|
||||
|
||||
const [addType, setAddType] = useState<ClipType>(defaultAddType)
|
||||
const [addDuration, setAddDuration] = useState<number>(DEFAULT_ADD_DURATION)
|
||||
|
||||
useEffect(() => {
|
||||
if (!availableTypes.includes(addType)) {
|
||||
setAddType(defaultAddType)
|
||||
@@ -93,9 +95,9 @@ export function useAddPicker({ currentMode, onAddClip }: UseAddPickerOptions) {
|
||||
}, [showAddPicker])
|
||||
|
||||
const handleConfirmAdd = useCallback(() => {
|
||||
onAddClip(addType, DEFAULT_ADD_DURATION)
|
||||
onAddClip(addType, addDuration)
|
||||
setShowAddPicker(false)
|
||||
}, [onAddClip, addType])
|
||||
}, [onAddClip, addType, addDuration])
|
||||
|
||||
return {
|
||||
showAddPicker,
|
||||
@@ -105,8 +107,9 @@ export function useAddPicker({ currentMode, onAddClip }: UseAddPickerOptions) {
|
||||
pickerPos,
|
||||
availableTypes,
|
||||
addType,
|
||||
addDuration: DEFAULT_ADD_DURATION,
|
||||
addDuration,
|
||||
setAddType,
|
||||
setAddDuration,
|
||||
handleTogglePicker,
|
||||
handleConfirmAdd,
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ export const useTimelineMenus = (
|
||||
addType,
|
||||
addDuration,
|
||||
setAddType,
|
||||
setAddDuration,
|
||||
handleTogglePicker,
|
||||
handleConfirmAdd,
|
||||
} = useAddPicker({ currentMode, onAddClip })
|
||||
@@ -56,6 +57,7 @@ export const useTimelineMenus = (
|
||||
addType,
|
||||
addDuration,
|
||||
setAddType,
|
||||
setAddDuration,
|
||||
handleTogglePicker,
|
||||
handleConfirmAdd,
|
||||
// 悬停状态
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* 智能剪辑页面(Issue #1677 多视频批量生成,修正版)
|
||||
* 固定 6 步向导:模板(弹数量) → 素材 → 配音 → 标题 → 确认生成 → 封面,单视频与批量完全一致
|
||||
* 智能剪辑页面 — 前端实时预览架构
|
||||
* 6 步向导:选择模板 → 素材 → 配音 → 标题(含预览) → 确认生成 → 选择封面
|
||||
*
|
||||
* 架构:
|
||||
* - 预览全部为纯前端 Canvas 实时播放(FrontendPreviewPlayer),不调任何后端渲染接口:
|
||||
* N=1 单播放器;N>1 CanvasPreviewGrid(variantSeed 让素材排布/起始点不同,画面有差异)
|
||||
* - 步骤5确认生成:正式生成接口(count + titles[]/voice_library_ids[]/cover_urls[]),
|
||||
* 批量时逐任务独立进度/失败重试(BatchGenerationGrid)
|
||||
* - 步骤 4 右侧显示 FrontendPreviewPlayer 实时预览
|
||||
* - 步骤 5 右侧内联播放生成中的/最终视频
|
||||
* - 步骤 6 封面从最终成片中智能选帧(MediaKit)
|
||||
* - 点"确认生成"时调用 createGenerationTask 创建一次服务器渲染任务
|
||||
*/
|
||||
import React, { useMemo, useState, useEffect, useRef, useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
@@ -16,16 +16,17 @@ import { useQuery } from "@tanstack/react-query"
|
||||
import { useCloneProgress } from "@/hooks/useCloneProgress"
|
||||
import CloneModal from "@/components/voice/CloneModal"
|
||||
import GenerateHeader from "./components/GenerateHeader"
|
||||
import {
|
||||
calculateTotalVideoDuration,
|
||||
estimateTotalVideoDuration,
|
||||
} from "./utils/calculateTotalVideoDuration"
|
||||
import FrontendPreviewPlayer from "./components/FrontendPreviewPlayer"
|
||||
import CanvasPreviewGrid from "./components/CanvasPreviewGrid"
|
||||
import PreviewCountModal from "./components/PreviewCountModal"
|
||||
import GenerateStepsBar from "./components/GenerateStepsBar"
|
||||
import GenerateStepContent from "./components/GenerateStepContent"
|
||||
import GenerateStepActions from "./components/GenerateStepActions"
|
||||
import { useGenerateFormState } from "./hooks/useGenerateFormState"
|
||||
import { useStepNavigation } from "./hooks/useStepNavigation"
|
||||
import { useGenerateVideo } from "./hooks/useGenerateVideo"
|
||||
|
||||
import { usePreviewAssets } from "./hooks/usePreviewAssets"
|
||||
import { useTitleStyleUpdaters } from "./hooks/useStep4Title/useTitleStyleUpdaters"
|
||||
import { getAssetsByKind } from "@/api/assets"
|
||||
@@ -56,8 +57,10 @@ const GeneratePage: React.FC = () => {
|
||||
selectedVoice,
|
||||
setSelectedVoice,
|
||||
voiceMode,
|
||||
setVoiceMode,
|
||||
selectedClonedVoice,
|
||||
|
||||
setSelectedClonedVoice,
|
||||
presetVoices,
|
||||
cloneModalOpen,
|
||||
setCloneModalOpen,
|
||||
videoRatio,
|
||||
@@ -73,51 +76,8 @@ const GeneratePage: React.FC = () => {
|
||||
setStoredSourceEditPlanId,
|
||||
serverClips,
|
||||
setServerClips,
|
||||
previewCount,
|
||||
setPreviewCount,
|
||||
previewTitles,
|
||||
setPreviewTitles,
|
||||
voiceModePerVideo,
|
||||
setVoiceModePerVideo,
|
||||
voiceLibraryIds,
|
||||
setVoiceLibraryIds,
|
||||
previewCovers,
|
||||
setPreviewCovers,
|
||||
selectedVariantIds,
|
||||
setSelectedVariantIds,
|
||||
} = formState
|
||||
|
||||
const isBatch = previewCount > 1
|
||||
|
||||
/* ── 配音选择同步:共用配音 ↔ 变体数组 ── */
|
||||
// 触发场景:①共用配音变化 ②批量模式进入/退出 ③独立→共用切换(需把所有变体刷成共用配音)
|
||||
// 独立模式下:仅同步变体[0](其选择器绑定共用配音),用户单独选择的其他变体不覆盖
|
||||
const prevVoiceSyncRef = useRef({
|
||||
voice: selectedVoice,
|
||||
batch: isBatch,
|
||||
perVideo: voiceModePerVideo,
|
||||
})
|
||||
useEffect(() => {
|
||||
const prev = prevVoiceSyncRef.current
|
||||
const voiceChanged = prev.voice !== selectedVoice
|
||||
const modeChanged = prev.batch !== isBatch || prev.perVideo !== voiceModePerVideo
|
||||
prevVoiceSyncRef.current = { voice: selectedVoice, batch: isBatch, perVideo: voiceModePerVideo }
|
||||
if (!voiceChanged && !modeChanged) return
|
||||
if (!isBatch) return
|
||||
if (!voiceModePerVideo) {
|
||||
// 共用模式(含刚从独立切回):所有变体跟随共用配音,未选择的补默认值
|
||||
setVoiceLibraryIds((prevIds) => (prevIds || []).map((id) => id || selectedVoice))
|
||||
} else if (voiceChanged) {
|
||||
// 独立模式下共用配音变化:仅同步变体[0](与共用选择器绑定),其余不覆盖
|
||||
setVoiceLibraryIds((prevIds) =>
|
||||
(prevIds || []).map((id, i) => (i === 0 ? selectedVoice : id)),
|
||||
)
|
||||
}
|
||||
}, [selectedVoice, isBatch, voiceModePerVideo, setVoiceLibraryIds])
|
||||
|
||||
/* ── 数量选择弹窗 ── */
|
||||
const [countModalOpen, setCountModalOpen] = useState(false)
|
||||
|
||||
/* ── 标题样式回调 ── */
|
||||
const styleUpdaters = useTitleStyleUpdaters({
|
||||
titleSettings,
|
||||
@@ -132,8 +92,6 @@ const GeneratePage: React.FC = () => {
|
||||
|
||||
const [previewVoiceAudioUrl, setPreviewVoiceAudioUrl] = useState<string | null>(null)
|
||||
const ttsAbortRef = useRef<AbortController | null>(null)
|
||||
// TTS 试听文案:批量跟随变体0标题(仅取首项,避免编辑其他变体标题触发多余 TTS 请求)
|
||||
const variant0Title = isBatch ? previewTitles?.[0] || "" : ""
|
||||
|
||||
useEffect(() => {
|
||||
const voiceAsset = voiceMaterials.find((m) => m.id === selectedVoice)
|
||||
@@ -142,10 +100,8 @@ const GeneratePage: React.FC = () => {
|
||||
return
|
||||
}
|
||||
|
||||
// 批量模式下 TTS 文案跟随变体0标题;单视频跟随主标题
|
||||
const ttsTitle = isBatch ? variant0Title || "" : titleSettings.title
|
||||
const voiceId = selectedClonedVoice || selectedVoice
|
||||
if (!voiceId || !ttsTitle) {
|
||||
if (!voiceId || !titleSettings.title) {
|
||||
setPreviewVoiceAudioUrl(null)
|
||||
return
|
||||
}
|
||||
@@ -155,7 +111,7 @@ const GeneratePage: React.FC = () => {
|
||||
ttsAbortRef.current = controller
|
||||
let cancelled = false
|
||||
|
||||
previewTts({ text: ttsTitle, voice_id: voiceId })
|
||||
previewTts({ text: titleSettings.title, voice_id: voiceId })
|
||||
.then((res) => {
|
||||
if (!cancelled && res.audio_url) {
|
||||
setPreviewVoiceAudioUrl(res.audio_url)
|
||||
@@ -172,17 +128,11 @@ const GeneratePage: React.FC = () => {
|
||||
cancelled = true
|
||||
controller.abort()
|
||||
}
|
||||
}, [
|
||||
selectedVoice,
|
||||
selectedClonedVoice,
|
||||
titleSettings.title,
|
||||
variant0Title,
|
||||
isBatch,
|
||||
voiceMaterials,
|
||||
])
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedVoice, selectedClonedVoice, titleSettings.title, voiceMaterials])
|
||||
|
||||
/* ── 克隆声音 ── */
|
||||
const { addClone } = useCloneProgress()
|
||||
const { clones: clonedVoices, addClone, hasProcessing } = useCloneProgress()
|
||||
|
||||
const handleCloneSuccess = (voice: VoiceClone) => {
|
||||
addClone(voice)
|
||||
@@ -211,27 +161,26 @@ const GeneratePage: React.FC = () => {
|
||||
[bgm, currentTemplate],
|
||||
)
|
||||
|
||||
/* ── 加载素材详情(供前端预览播放器使用) ── */
|
||||
/* ── 加载素材详情(供前端预览播放器使用 + 配音时长校验) ── */
|
||||
const previewAssetsEnabled = previewAssetIds.length > 0
|
||||
const { assets: previewAssets, ready: previewAssetsReady } = usePreviewAssets(
|
||||
previewAssetIds,
|
||||
previewAssetsEnabled,
|
||||
)
|
||||
|
||||
/* ── 预览就绪:纯前端 Canvas 预览,素材详情加载完即可秒开(单视频/批量一致) ── */
|
||||
const previewReady = previewAssetsReady && !!currentTemplate
|
||||
|
||||
/* ── 勾选变体 ── */
|
||||
const toggleVariantSelect = useCallback(
|
||||
(index: number) => {
|
||||
setSelectedVariantIds((prev) => {
|
||||
const list = prev || []
|
||||
return list.includes(index) ? list.filter((i) => i !== index) : [...list, index].sort()
|
||||
})
|
||||
},
|
||||
[setSelectedVariantIds],
|
||||
/* ── 预览就绪:素材已加载,且有模板 ── */
|
||||
const previewReady = useMemo(
|
||||
() => previewAssetsReady && !!currentTemplate,
|
||||
[previewAssetsReady, currentTemplate],
|
||||
)
|
||||
|
||||
/* ── 视频总时长计算 ── */
|
||||
const totalVideoDuration = useMemo(() => {
|
||||
const exact = calculateTotalVideoDuration(previewAssets, currentTemplate ?? undefined)
|
||||
if (exact > 0) return exact
|
||||
return estimateTotalVideoDuration(currentTemplate ?? undefined)
|
||||
}, [previewAssets, currentTemplate])
|
||||
|
||||
/* ── 视频生成核心逻辑 ── */
|
||||
const {
|
||||
generating,
|
||||
@@ -239,10 +188,8 @@ const GeneratePage: React.FC = () => {
|
||||
generated,
|
||||
generateError,
|
||||
generatedVideos,
|
||||
batchTasks,
|
||||
generate: handleGenerate,
|
||||
retry: handleRetryGenerate,
|
||||
retryBatchTask: handleRetryBatchTask,
|
||||
dismissError: handleDismissError,
|
||||
download: handleDownload,
|
||||
share: handleShare,
|
||||
@@ -264,92 +211,27 @@ const GeneratePage: React.FC = () => {
|
||||
sourceEditPlanId: storedSourceEditPlanId || sourceEditPlanId,
|
||||
previewTaskId,
|
||||
bgmConfig,
|
||||
previewCount,
|
||||
variantTitles: previewTitles,
|
||||
variantVoiceLibraryIds: voiceLibraryIds,
|
||||
voiceModePerVideo,
|
||||
variantCoverUrls: previewCovers,
|
||||
selectedVariantIndexes: isBatch ? selectedVariantIds : undefined,
|
||||
onGenerationSuccess: () => {
|
||||
setPreviewTaskId(null)
|
||||
setStoredSourceEditPlanId(null)
|
||||
},
|
||||
})
|
||||
|
||||
/* ── 数量弹窗确认:设置数量 + 同步批量数组长度 + 进入步骤2 ── */
|
||||
const handleCountConfirm = useCallback(
|
||||
(count: number) => {
|
||||
setPreviewCount(count)
|
||||
setCountModalOpen(false)
|
||||
// 同步批量数组长度
|
||||
setPreviewTitles((prev) => {
|
||||
const list = prev || []
|
||||
const base = list[0] || titleSettings.title || ""
|
||||
return Array.from({ length: count }, (_, i) => list[i] ?? (i === 0 ? base : ""))
|
||||
})
|
||||
setVoiceLibraryIds((prev) => {
|
||||
const list = prev || []
|
||||
return Array.from({ length: count }, (_, i) => list[i] ?? selectedVoice ?? "")
|
||||
})
|
||||
setPreviewCovers((prev) => {
|
||||
const list = prev || []
|
||||
return Array.from({ length: count }, (_, i) => list[i] ?? "")
|
||||
})
|
||||
setSelectedVariantIds(Array.from({ length: count }, (_, i) => i))
|
||||
setCurrentStep(2)
|
||||
},
|
||||
[
|
||||
setPreviewCount,
|
||||
setPreviewTitles,
|
||||
setVoiceLibraryIds,
|
||||
setPreviewCovers,
|
||||
setSelectedVariantIds,
|
||||
setCurrentStep,
|
||||
titleSettings.title,
|
||||
selectedVoice,
|
||||
],
|
||||
)
|
||||
|
||||
/* ── 步骤4「确认生成视频」:校验通过 → 创建正式生成任务 → 跳步骤5看实时进展 ── */
|
||||
/* ── 步骤4「确认生成视频」:校验标题/预览 → 创建最终渲染任务 → 成功后进入步骤5 ── */
|
||||
const handleConfirmGenerate = useCallback(async () => {
|
||||
// 标题校验:批量只校验已勾选的变体;单视频校验主标题
|
||||
if (isBatch) {
|
||||
if (selectedVariantIds.length === 0) {
|
||||
message.warning("请至少勾选一个视频")
|
||||
return
|
||||
}
|
||||
const missing = selectedVariantIds.some((i) => !previewTitles[i]?.trim())
|
||||
if (missing) {
|
||||
message.warning("请为每个勾选的视频输入标题")
|
||||
return
|
||||
}
|
||||
} else if (!titleSettings.title?.trim()) {
|
||||
// 与 buildPayload.validateGenerateInputs 一致:AI 自动选标题模式(aiAutoSelect)
|
||||
// 允许空标题由后端生成;手动模式必须填写,避免提交空标题
|
||||
if (!titleSettings.aiAutoSelect) {
|
||||
message.warning("请先选择或输入标题")
|
||||
return
|
||||
}
|
||||
if (!titleSettings.title.trim()) {
|
||||
message.warning("请选择或输入标题")
|
||||
return
|
||||
}
|
||||
if (!previewReady) {
|
||||
message.warning("预览素材正在加载,请稍候")
|
||||
message.warning("预览视频正在加载,请稍候")
|
||||
return
|
||||
}
|
||||
const ok = await handleGenerate()
|
||||
if (ok) {
|
||||
// 单视频与批量一致:任务创建成功后进入步骤5「确认生成」看实时渲染进展
|
||||
setCurrentStep(5)
|
||||
}
|
||||
}, [
|
||||
isBatch,
|
||||
selectedVariantIds,
|
||||
previewTitles,
|
||||
titleSettings.aiAutoSelect,
|
||||
titleSettings.title,
|
||||
previewReady,
|
||||
handleGenerate,
|
||||
setCurrentStep,
|
||||
])
|
||||
}, [titleSettings.title, previewReady, handleGenerate, setCurrentStep])
|
||||
|
||||
/* ── 步骤导航 ── */
|
||||
const { goNext, goPrev } = useStepNavigation({
|
||||
@@ -360,21 +242,13 @@ const GeneratePage: React.FC = () => {
|
||||
selectedMaterials,
|
||||
smartSelectedIds,
|
||||
titleSettings,
|
||||
previewReady,
|
||||
generated,
|
||||
onOpenCountModal: () => setCountModalOpen(true),
|
||||
})
|
||||
|
||||
/* ── 最终成片(单视频右侧播放) ── */
|
||||
/* ── 最终成片(步骤5/6 右侧播放) ── */
|
||||
const finalVideo = generatedVideos[0]
|
||||
|
||||
/* ── 布局 class:步骤4标题页=预览+标题侧栏;步骤5/6批量=整行宽;步骤1~3=整行宽 ── */
|
||||
const layoutClassName = useMemo(() => {
|
||||
if (currentStep < 4) return "xx-generate-layout full-width"
|
||||
if (currentStep === 4) return "xx-generate-layout step4-layout"
|
||||
// 步骤5/6:批量网格需要整行宽度;单视频保持 表单+右侧成片 两栏
|
||||
return isBatch ? "xx-generate-layout full-width" : "xx-generate-layout"
|
||||
}, [currentStep, isBatch])
|
||||
|
||||
/* ================================================================
|
||||
渲染
|
||||
================================================================ */
|
||||
@@ -385,61 +259,8 @@ const GeneratePage: React.FC = () => {
|
||||
|
||||
<GenerateStepsBar currentStep={currentStep} onStepClick={setCurrentStep} />
|
||||
|
||||
<div className={layoutClassName}>
|
||||
{/* ════ 步骤4:左侧预览大区域(纯前端 Canvas 实时预览) ════ */}
|
||||
{currentStep === 4 && !!currentTemplate && (
|
||||
<div className="xx-generate-preview-col">
|
||||
{!isBatch ? (
|
||||
/* 单视频:前端 Canvas 实时预览(与旧版一致,零回归) */
|
||||
<FrontendPreviewPlayer
|
||||
assets={previewAssets}
|
||||
template={currentTemplate}
|
||||
videoRatio={videoRatio}
|
||||
ready={previewAssets.length > 0}
|
||||
serverClips={serverClips}
|
||||
voiceAudioUrl={previewVoiceAudioUrl || undefined}
|
||||
titleSettings={{
|
||||
title: titleSettings.title,
|
||||
size: titleSettings.size,
|
||||
font: titleSettings.font,
|
||||
color: titleSettings.color,
|
||||
position: titleSettings.position as "top" | "center" | "bottom" | "custom",
|
||||
bold: titleSettings.bold,
|
||||
italic: titleSettings.italic,
|
||||
stroke: titleSettings.stroke,
|
||||
shadow: titleSettings.shadow,
|
||||
posX: titleSettings.posX,
|
||||
posY: titleSettings.posY,
|
||||
}}
|
||||
onTitlePositionChange={styleUpdaters.updateTitlePosition}
|
||||
/>
|
||||
) : (
|
||||
/* 批量:N 个前端 Canvas 预览网格(不调任何后端渲染接口,秒开) */
|
||||
<div className="xx-form-section">
|
||||
<div className="xx-preview-header">
|
||||
<h3>🎬 {previewCount} 个视频预览</h3>
|
||||
<span style={{ fontSize: 13, color: "var(--text-secondary, #666)" }}>
|
||||
实时预览,勾选要生成的视频
|
||||
</span>
|
||||
</div>
|
||||
<CanvasPreviewGrid
|
||||
count={previewCount}
|
||||
assets={previewAssets}
|
||||
template={currentTemplate}
|
||||
videoRatio={videoRatio}
|
||||
titles={previewTitles}
|
||||
titleSettings={titleSettings}
|
||||
voiceAudioUrl={previewVoiceAudioUrl || undefined}
|
||||
selectedIds={selectedVariantIds}
|
||||
onToggleSelect={toggleVariantSelect}
|
||||
selectable={!generating}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* ════ 右侧:步骤1~3 表单 / 步骤4 标题边栏 / 步骤5 确认生成进度 / 步骤6 封面 ════ */}
|
||||
<div className={`xx-generate-layout${currentStep < 4 ? " full-width" : ""}`}>
|
||||
{/* ════ 左侧:表单区 ════ */}
|
||||
<div className="xx-generate-form">
|
||||
<GenerateStepContent
|
||||
currentStep={currentStep}
|
||||
@@ -470,26 +291,25 @@ const GeneratePage: React.FC = () => {
|
||||
onCoverSettingsChange={setCoverSettings}
|
||||
selectedVoice={selectedVoice}
|
||||
onSelectedVoiceChange={setSelectedVoice}
|
||||
totalVideoDuration={totalVideoDuration}
|
||||
onServerClipsChange={setServerClips}
|
||||
voiceMode={voiceMode}
|
||||
onVoiceModeChange={setVoiceMode}
|
||||
selectedClonedVoice={selectedClonedVoice}
|
||||
onSelectedClonedVoiceChange={setSelectedClonedVoice}
|
||||
clonedVoices={clonedVoices}
|
||||
addClone={addClone}
|
||||
hasProcessing={hasProcessing}
|
||||
cloneModalOpen={cloneModalOpen}
|
||||
onCloneModalOpenChange={setCloneModalOpen}
|
||||
generating={generating}
|
||||
generated={generated}
|
||||
generateError={generateError}
|
||||
progress={progress}
|
||||
generatedVideos={generatedVideos}
|
||||
onRetry={handleRetryGenerate}
|
||||
onRetryBatchTask={handleRetryBatchTask}
|
||||
onDismissError={handleDismissError}
|
||||
batchTasks={batchTasks}
|
||||
previewCount={previewCount}
|
||||
previewTitles={previewTitles}
|
||||
onPreviewTitlesChange={setPreviewTitles}
|
||||
voiceModePerVideo={voiceModePerVideo}
|
||||
onVoiceModePerVideoChange={setVoiceModePerVideo}
|
||||
voiceLibraryIds={voiceLibraryIds}
|
||||
onVoiceLibraryIdsChange={setVoiceLibraryIds}
|
||||
previewCovers={previewCovers}
|
||||
onPreviewCoversChange={setPreviewCovers}
|
||||
selectedVariantIds={selectedVariantIds}
|
||||
presetVoices={presetVoices}
|
||||
/>
|
||||
|
||||
<GenerateStepActions
|
||||
@@ -500,13 +320,36 @@ const GeneratePage: React.FC = () => {
|
||||
generating={generating}
|
||||
generated={generated}
|
||||
generateError={generateError}
|
||||
selectedCount={isBatch ? selectedVariantIds.length : 1}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* ════ 步骤5/6(单视频):右侧成片播放器 ════ */}
|
||||
{currentStep >= 5 && !isBatch && generated && finalVideo && (
|
||||
<div className="xx-generate-right-col">
|
||||
{/* ════ 右侧:步骤4实时预览,步骤5/6最终视频 ════ */}
|
||||
<div className="xx-generate-right-col">
|
||||
{currentStep === 4 && !!currentTemplate && (
|
||||
<FrontendPreviewPlayer
|
||||
assets={previewAssets}
|
||||
template={currentTemplate}
|
||||
videoRatio={videoRatio}
|
||||
ready={previewAssets.length > 0}
|
||||
serverClips={serverClips}
|
||||
voiceAudioUrl={previewVoiceAudioUrl || undefined}
|
||||
titleSettings={{
|
||||
title: titleSettings.title,
|
||||
size: titleSettings.size,
|
||||
font: titleSettings.font,
|
||||
color: titleSettings.color,
|
||||
position: titleSettings.position as "top" | "center" | "bottom" | "custom",
|
||||
bold: titleSettings.bold,
|
||||
italic: titleSettings.italic,
|
||||
stroke: titleSettings.stroke,
|
||||
shadow: titleSettings.shadow,
|
||||
posX: titleSettings.posX,
|
||||
posY: titleSettings.posY,
|
||||
}}
|
||||
onTitlePositionChange={styleUpdaters.updateTitlePosition}
|
||||
/>
|
||||
)}
|
||||
{currentStep >= 5 && generated && finalVideo && (
|
||||
<div className="xx-inline-video-player">
|
||||
<video
|
||||
src={finalVideo.download_url || finalVideo.file_url}
|
||||
@@ -530,18 +373,10 @@ const GeneratePage: React.FC = () => {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 数量选择弹窗 */}
|
||||
<PreviewCountModal
|
||||
open={countModalOpen}
|
||||
defaultCount={1}
|
||||
onConfirm={handleCountConfirm}
|
||||
onCancel={() => setCountModalOpen(false)}
|
||||
/>
|
||||
|
||||
{/* 音色克隆弹窗 */}
|
||||
<CloneModal
|
||||
open={cloneModalOpen}
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
/**
|
||||
* 第5步「确认生成」— 批量渲染进度网格(Issue #1677)
|
||||
*
|
||||
* N 个正式生成任务各自独立卡片:进度条 / 成功成片播放 / 失败原因 + 单独重试。
|
||||
* 数据来自 useGenerateVideo 的 batchTasks(useGenerationPolling 实时回传)。
|
||||
*/
|
||||
import React from "react"
|
||||
import { LoadingOutlined, CheckCircleFilled, CloseCircleOutlined } from "@ant-design/icons"
|
||||
import type { BatchTaskState } from "../hooks/generate-video/useGenerationPolling"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
|
||||
interface BatchGenerationGridProps {
|
||||
tasks: BatchTaskState[]
|
||||
/** 变体标题(按变体序号取) */
|
||||
titles: string[]
|
||||
/** 失败任务重试 */
|
||||
onRetryTask: (taskId: string) => void
|
||||
}
|
||||
|
||||
const BatchGenerationGrid: React.FC<BatchGenerationGridProps> = ({
|
||||
tasks,
|
||||
titles,
|
||||
onRetryTask,
|
||||
}) => {
|
||||
const sorted = [...tasks].sort(
|
||||
(a, b) => Number(a.variantIndex || 0) - Number(b.variantIndex || 0),
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<div className="xx-preview-header">
|
||||
<h3>🎬 正在生成 {tasks.length} 个视频</h3>
|
||||
<span style={{ fontSize: 13, color: "var(--text-secondary, #666)" }}>
|
||||
完成 {tasks.filter((t) => t.status === "completed").length} / {tasks.length}
|
||||
</span>
|
||||
</div>
|
||||
<div className="xx-batch-gen-grid">
|
||||
{sorted.map((task) => {
|
||||
const title = titles[task.variantIndex] || `视频 ${task.variantIndex + 1}`
|
||||
const video = (task.videos?.[0] || null) as GeneratedVideo | null
|
||||
return (
|
||||
<div key={task.taskId} className={`xx-batch-gen-card status-${task.status}`}>
|
||||
<div className="xx-batch-gen-card-head">
|
||||
<span className="xx-batch-gen-card-title" title={title}>
|
||||
{task.status === "completed" ? (
|
||||
<CheckCircleFilled style={{ color: "#52c41a", marginRight: 6 }} />
|
||||
) : task.status === "failed" ? (
|
||||
<CloseCircleOutlined style={{ color: "#ef4444", marginRight: 6 }} />
|
||||
) : (
|
||||
<LoadingOutlined style={{ color: "#1677ff", marginRight: 6 }} />
|
||||
)}
|
||||
视频 {task.variantIndex + 1}:{title}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="xx-batch-gen-card-body">
|
||||
{task.status === "running" && (
|
||||
<>
|
||||
<div className="xx-gen-progress-bar">
|
||||
<div
|
||||
className="xx-gen-progress-bar-fill"
|
||||
style={{ width: `${Math.min(task.progress, 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
<div className="xx-batch-gen-card-pct">{Math.round(task.progress)}%</div>
|
||||
</>
|
||||
)}
|
||||
{task.status === "completed" && video && (
|
||||
<video
|
||||
src={video.download_url || video.file_url}
|
||||
controls
|
||||
style={{ width: "100%", borderRadius: 8, background: "#000", maxHeight: 280 }}
|
||||
poster={video.thumbnail_url}
|
||||
/>
|
||||
)}
|
||||
{task.status === "completed" && !video && (
|
||||
<div className="xx-batch-gen-card-done">✅ 已完成(成片可在下一步选择封面)</div>
|
||||
)}
|
||||
{task.status === "failed" && (
|
||||
<div className="xx-batch-gen-card-failed">
|
||||
<div className="xx-batch-gen-card-err">{task.error || "生成失败"}</div>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary xx-btn-sm"
|
||||
onClick={() => onRetryTask(task.taskId)}
|
||||
>
|
||||
🔄 重试此视频
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default BatchGenerationGrid
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* 批量前端 Canvas 实时预览网格(Issue #1677 修正方案)
|
||||
*
|
||||
* N 个 FrontendPreviewPlayer 网格排列:
|
||||
* - 纯前端 Canvas + video 元素实时播放素材片段,不调任何后端渲染接口
|
||||
* - variantSeed 让每个变体素材排布/起始点不同,画面有可见差异
|
||||
* - 各自叠加独立标题浮层(variantTitle),标题样式全局共用
|
||||
* - 勾选框决定提交时生成哪些变体
|
||||
*/
|
||||
import React from "react"
|
||||
import type { AssetItem } from "@/api/assets"
|
||||
import type { EditingTemplate } from "@/api/editing-planner"
|
||||
import type { TitleSettings } from "../types"
|
||||
import FrontendPreviewPlayer from "./FrontendPreviewPlayer"
|
||||
|
||||
interface CanvasPreviewGridProps {
|
||||
count: number
|
||||
assets: AssetItem[]
|
||||
template: EditingTemplate | null
|
||||
videoRatio: string
|
||||
titles: string[]
|
||||
titleSettings: TitleSettings
|
||||
/** 共用配音预览音频(仅第 1 个变体播放,避免多路音频重叠) */
|
||||
voiceAudioUrl?: string
|
||||
/** 勾选的变体序号 */
|
||||
selectedIds: number[]
|
||||
onToggleSelect: (index: number) => void
|
||||
/** 生成中禁止勾选 */
|
||||
selectable?: boolean
|
||||
}
|
||||
|
||||
const CanvasPreviewGrid: React.FC<CanvasPreviewGridProps> = ({
|
||||
count,
|
||||
assets,
|
||||
template,
|
||||
videoRatio,
|
||||
titles,
|
||||
titleSettings,
|
||||
voiceAudioUrl,
|
||||
selectedIds,
|
||||
onToggleSelect,
|
||||
selectable = true,
|
||||
}) => {
|
||||
// count 上限已在源头 PreviewCountModal 的数量选择(1~MAX_PREVIEW_COUNT=10)clamp,
|
||||
// 这里完整渲染所有变体,保证每个变体都有勾选/预览入口,UI 与数据不脱节
|
||||
return (
|
||||
<div className="xx-canvas-grid">
|
||||
{Array.from({ length: count }, (_, i) => {
|
||||
const checked = selectedIds.includes(i)
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className={`xx-canvas-grid-card${checked ? " selected" : ""}`}
|
||||
data-variant={i}
|
||||
>
|
||||
<div className="xx-canvas-grid-card-bar">
|
||||
<label className="xx-canvas-grid-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={checked}
|
||||
disabled={!selectable}
|
||||
onChange={() => onToggleSelect(i)}
|
||||
/>
|
||||
<span>视频 {i + 1}</span>
|
||||
</label>
|
||||
</div>
|
||||
<FrontendPreviewPlayer
|
||||
assets={assets}
|
||||
template={template}
|
||||
videoRatio={videoRatio}
|
||||
ready={assets.length > 0}
|
||||
variantSeed={i + 1}
|
||||
variantTitle={titles[i] || ""}
|
||||
voiceAudioUrl={i === 0 ? voiceAudioUrl : undefined}
|
||||
compact
|
||||
titleSettings={{
|
||||
title: titles[i] || "",
|
||||
size: titleSettings.size,
|
||||
font: titleSettings.font,
|
||||
color: titleSettings.color,
|
||||
position: titleSettings.position as "top" | "center" | "bottom" | "custom",
|
||||
bold: titleSettings.bold,
|
||||
italic: titleSettings.italic,
|
||||
stroke: titleSettings.stroke,
|
||||
shadow: titleSettings.shadow,
|
||||
posX: titleSettings.posX,
|
||||
posY: titleSettings.posY,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CanvasPreviewGrid
|
||||
@@ -41,16 +41,6 @@ interface FrontendPreviewPlayerProps {
|
||||
posY?: number | null
|
||||
}
|
||||
onTitlePositionChange?: (posX: number, posY: number) => void
|
||||
/**
|
||||
* 变体种子(批量生成 #1677):同一批素材在不同变体中采用不同的素材顺序与
|
||||
* 片段起始点,让 N 个 Canvas 预览画面有差异(纯前端随机剪辑模拟,不调后端)。
|
||||
* 0 / 不传 = 单视频,排布与旧版完全一致(零回归)。
|
||||
*/
|
||||
variantSeed?: number
|
||||
/** 变体标题文字(批量时每个预览独立标题,叠加在画面上);不传用 titleSettings.title */
|
||||
variantTitle?: string
|
||||
/** 紧凑模式(批量网格中使用,缩小内边距/标题尺寸) */
|
||||
compact?: boolean
|
||||
}
|
||||
|
||||
function formatTime(seconds: number): string {
|
||||
@@ -62,23 +52,10 @@ function formatTime(seconds: number): string {
|
||||
/**
|
||||
* 将素材映射为播放片段(复用原逻辑)
|
||||
*/
|
||||
/** 简单可复现随机数(mulberry32),同一种子产出稳定排布,避免每次渲染抖动 */
|
||||
function seededRandom(seed: number): () => number {
|
||||
let a = seed >>> 0
|
||||
return () => {
|
||||
a |= 0
|
||||
a = (a + 0x6d2b79f5) | 0
|
||||
let t = Math.imul(a ^ (a >>> 15), 1 | a)
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
||||
}
|
||||
}
|
||||
|
||||
function buildPlaybackSegments(
|
||||
assets: AssetItem[],
|
||||
template: EditingTemplate | null,
|
||||
serverClips?: EditPlanClip[],
|
||||
variantSeed = 0,
|
||||
): PlaybackSegment[] {
|
||||
if (!assets.length) return []
|
||||
|
||||
@@ -102,40 +79,18 @@ function buildPlaybackSegments(
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: 本地构建片段
|
||||
// variantSeed=0(单视频):与旧行为完全一致(素材原序、起始点 0),零回归
|
||||
// variantSeed>0(批量变体):素材顺序按种子轮换 + 片段起始点在素材内偏移,
|
||||
// 模拟后端"AI 随机剪辑出不同版本",让 N 个预览画面有可见差异
|
||||
// Fallback: 本地构建片段(与旧行为一致)
|
||||
const templateSegments = template?.segments || []
|
||||
const segments: PlaybackSegment[] = []
|
||||
const orderedAssets = variantSeed > 0 ? [...assets] : assets
|
||||
if (variantSeed > 0 && orderedAssets.length > 1) {
|
||||
const rand = seededRandom(variantSeed * 7919 + 13)
|
||||
// 素材轮换:把数组旋转 (seed % n) 位,再对后半段做一次稳定交换
|
||||
const n = orderedAssets.length
|
||||
const rotate = variantSeed % n
|
||||
orderedAssets.push(...orderedAssets.splice(0, rotate))
|
||||
const swapA = Math.floor(rand() * n)
|
||||
const swapB = Math.floor(rand() * n)
|
||||
if (swapA !== swapB) {
|
||||
;[orderedAssets[swapA], orderedAssets[swapB]] = [orderedAssets[swapB], orderedAssets[swapA]]
|
||||
}
|
||||
}
|
||||
|
||||
orderedAssets.forEach((asset, i) => {
|
||||
assets.forEach((asset, i) => {
|
||||
const assetDuration = asset.duration || asset.metadata?.duration || 30
|
||||
const tplSeg = templateSegments[i] || templateSegments[templateSegments.length - 1]
|
||||
const segDuration = tplSeg
|
||||
? Math.min(tplSeg.duration_max, Math.max(tplSeg.duration_min, assetDuration))
|
||||
: Math.min(assetDuration, 10)
|
||||
|
||||
let startTime = 0
|
||||
if (variantSeed > 0 && assetDuration - segDuration > 1) {
|
||||
const rand = seededRandom(variantSeed * 104729 + i * 31 + 7)
|
||||
// 起始点在素材可用区间内随机偏移(至少留 0.5s 余量)
|
||||
const maxStart = Math.max(0, assetDuration - segDuration - 0.5)
|
||||
startTime = Math.round(rand() * maxStart * 10) / 10
|
||||
}
|
||||
const startTime = 0
|
||||
const endTime = Math.min(startTime + segDuration, assetDuration)
|
||||
const videoUrl = asset.file_url || asset.storage_key
|
||||
|
||||
@@ -154,16 +109,11 @@ const FrontendPreviewPlayer: React.FC<FrontendPreviewPlayerProps> = ({
|
||||
voiceAudioUrl,
|
||||
titleSettings,
|
||||
onTitlePositionChange,
|
||||
variantSeed = 0,
|
||||
variantTitle,
|
||||
compact = false,
|
||||
}) => {
|
||||
const segments = useMemo(
|
||||
() => buildPlaybackSegments(assets, template, serverClips, variantSeed),
|
||||
[assets, template, serverClips, variantSeed],
|
||||
() => buildPlaybackSegments(assets, template, serverClips),
|
||||
[assets, template, serverClips],
|
||||
)
|
||||
// 批量变体:标题文字取 variantTitle,样式仍由全局 titleSettings 控制
|
||||
const effectiveTitle = variantTitle ?? titleSettings?.title
|
||||
|
||||
// ── ASS 坐标系参数(与后端 ass_subtitle_builder.py 一致) ──
|
||||
const TITLE_MARGIN_TOP = 120
|
||||
@@ -284,7 +234,7 @@ const FrontendPreviewPlayer: React.FC<FrontendPreviewPlayerProps> = ({
|
||||
// ── Canvas 播放器(WebCodecs 路径) ──
|
||||
const canvasTitle = titleSettings
|
||||
? {
|
||||
text: effectiveTitle || "标题预览",
|
||||
text: titleSettings.title || "标题预览",
|
||||
fontSize: titleSettings.size,
|
||||
fontFamily: titleSettings.font || "思源黑体",
|
||||
color: titleSettings.color || "#ffffff",
|
||||
@@ -570,15 +520,13 @@ const FrontendPreviewPlayer: React.FC<FrontendPreviewPlayerProps> = ({
|
||||
style={{
|
||||
position: "relative",
|
||||
width: "100%",
|
||||
maxWidth: compact ? "100%" : 280,
|
||||
margin: compact ? 0 : "0 auto",
|
||||
maxWidth: 280,
|
||||
aspectRatio: "9 / 16",
|
||||
background: "#0a0a0a",
|
||||
borderRadius: compact ? 10 : 24,
|
||||
borderRadius: 24,
|
||||
overflow: "hidden",
|
||||
boxShadow: compact
|
||||
? "inset 0 0 0 1px rgba(255,255,255,0.06)"
|
||||
: "0 4px 6px -1px rgba(0,0,0,0.3), 0 20px 50px -12px rgba(0,0,0,0.5), inset 0 0 0 1px rgba(255,255,255,0.06)",
|
||||
boxShadow:
|
||||
"0 4px 6px -1px rgba(0,0,0,0.3), 0 20px 50px -12px rgba(0,0,0,0.5), inset 0 0 0 1px rgba(255,255,255,0.06)",
|
||||
}}
|
||||
>
|
||||
{/* ── Canvas 渲染层(WebCodecs 路径) ── */}
|
||||
@@ -643,8 +591,6 @@ const FrontendPreviewPlayer: React.FC<FrontendPreviewPlayerProps> = ({
|
||||
<div
|
||||
style={{
|
||||
position: "absolute",
|
||||
width: `${100 - 2 * titleSidePct}%`,
|
||||
maxWidth: `${100 - 2 * titleSidePct}%`,
|
||||
...(customTitleXPct != null && customTitleYPct != null
|
||||
? {
|
||||
left: `${customTitleXPct}%`,
|
||||
@@ -653,17 +599,17 @@ const FrontendPreviewPlayer: React.FC<FrontendPreviewPlayerProps> = ({
|
||||
textAlign: "center" as const,
|
||||
}
|
||||
: {
|
||||
left: "50%",
|
||||
transform: "translateX(-50%)",
|
||||
left: `${titleSidePct}%`,
|
||||
right: `${titleSidePct}%`,
|
||||
textAlign: "center" as const,
|
||||
...(titleSettings.position === "top"
|
||||
? { top: `${titleTopPct}%` }
|
||||
: titleSettings.position === "center"
|
||||
? { top: "50%", transform: "translate(-50%, -50%)" }
|
||||
? { top: "50%", transform: "translateY(-50%)" }
|
||||
: { bottom: `${titleBottomPct}%` }),
|
||||
}),
|
||||
pointerEvents: onTitlePositionChange && variantSeed === 0 ? "auto" : "none",
|
||||
cursor: onTitlePositionChange && variantSeed === 0 ? "grab" : "default",
|
||||
pointerEvents: "auto",
|
||||
cursor: onTitlePositionChange ? "grab" : "default",
|
||||
touchAction: "none",
|
||||
userSelect: "none",
|
||||
WebkitUserSelect: "none",
|
||||
@@ -693,7 +639,7 @@ const FrontendPreviewPlayer: React.FC<FrontendPreviewPlayerProps> = ({
|
||||
: undefined,
|
||||
}}
|
||||
>
|
||||
{(effectiveTitle || "").split(/[//]/).map((part, i) => (
|
||||
{titleSettings.title.split(/[//]/).map((part, i) => (
|
||||
<span key={i}>
|
||||
{i > 0 && <br />}
|
||||
{part}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* GeneratePage 步骤底部操作按钮(Issue #1677 修正:固定 6 步)
|
||||
* GeneratePage 步骤底部操作按钮
|
||||
*
|
||||
* 步骤 1~3:上一步 / 下一步
|
||||
* 步骤 4(选择标题):「✨ 确认生成视频 / 确认生成 N 个视频」→ 创建正式生成任务,成功后跳步骤5
|
||||
* 步骤 5(确认生成):渲染进度页,全部完成后「下一步:选择封面」;仅上一步
|
||||
* 步骤 4(标题+预览):上一步 / 确认生成视频(点击后直接创建最终渲染任务,成功后跳转步骤5)
|
||||
* 步骤 5(确认生成):上一步 / 下一步(渲染中禁用,渲染完成后可进入封面)
|
||||
* 步骤 6(选择封面):仅上一步
|
||||
*/
|
||||
import React from "react"
|
||||
@@ -12,16 +12,14 @@ export interface GenerateStepActionsProps {
|
||||
currentStep: number
|
||||
onPrev: () => void
|
||||
onNext: () => void
|
||||
/** 步骤4:确认生成视频(校验 + 创建渲染任务) */
|
||||
/** 步骤4:确认生成视频(校验 + 创建渲染任务 + 成功后进入步骤5) */
|
||||
onConfirmGenerate: () => void | Promise<void>
|
||||
generating: boolean
|
||||
generated: boolean
|
||||
generateError: string | null
|
||||
/** 批量模式下勾选的视频数量(N=1 时为1) */
|
||||
selectedCount?: number
|
||||
}
|
||||
|
||||
const GenerateStepActions: React.FC<GenerateStepActionsProps> = ({
|
||||
export const GenerateStepActions: React.FC<GenerateStepActionsProps> = ({
|
||||
currentStep,
|
||||
onPrev,
|
||||
onNext,
|
||||
@@ -29,10 +27,9 @@ const GenerateStepActions: React.FC<GenerateStepActionsProps> = ({
|
||||
generating,
|
||||
generated,
|
||||
generateError,
|
||||
selectedCount = 1,
|
||||
}) => {
|
||||
const renderPrimaryButton = () => {
|
||||
/* 步骤 1~3:上一步 / 下一步 */
|
||||
/* 步骤 1~3:上一步 / 下一步(必填校验由 useStepNavigation.goNext 统一处理) */
|
||||
if (currentStep < 4) {
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" onClick={onNext}>
|
||||
@@ -41,46 +38,50 @@ const GenerateStepActions: React.FC<GenerateStepActionsProps> = ({
|
||||
)
|
||||
}
|
||||
|
||||
/* 步骤 4:选择标题 — 确认生成 */
|
||||
/* 步骤 4:确认生成视频(触发按钮在标题页) */
|
||||
if (currentStep === 4) {
|
||||
if (generating) {
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" disabled>
|
||||
⏳ 正在提交生成任务…
|
||||
⏳ 视频生成中…
|
||||
</button>
|
||||
)
|
||||
}
|
||||
if (generateError) {
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" onClick={onConfirmGenerate}>
|
||||
🔄 重新生成
|
||||
🔄 重新生成视频
|
||||
</button>
|
||||
)
|
||||
}
|
||||
if (generated) {
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" onClick={onNext}>
|
||||
下一步 →
|
||||
</button>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" onClick={onConfirmGenerate}>
|
||||
{selectedCount > 1 ? `✨ 确认生成 ${selectedCount} 个视频` : "✨ 确认生成视频"}
|
||||
✨ 确认生成视频
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
/* 步骤 5:确认生成进度页 — 全部完成后下一步进封面 */
|
||||
/* 步骤 5:渲染中禁用,完成后下一步进入封面 */
|
||||
if (currentStep === 5) {
|
||||
if (generated) {
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" onClick={onNext}>
|
||||
下一步:选择封面 →
|
||||
</button>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<button className="xx-btn xx-btn-primary" disabled>
|
||||
⏳ 视频渲染中…
|
||||
<button
|
||||
className="xx-btn xx-btn-primary"
|
||||
onClick={onNext}
|
||||
disabled={generating || !generated}
|
||||
>
|
||||
{generating ? "视频生成中…" : "下一步 →"}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
/* 步骤 6(封面,最后一步):无主按钮 */
|
||||
/* 步骤 6(最后一步):无主按钮 */
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
/**
|
||||
* GeneratePage 步骤内容渲染
|
||||
* 步骤顺序(6步,Issue #1677 修正):模板(1) → 素材(2) → 配音(3) → 标题(4) → 确认生成(5) → 封面(6)
|
||||
* 步骤4预览(Canvas 网格)与步骤5进度(批量渲染网格)由 GeneratePage 直接渲染在左侧大区域。
|
||||
* 步骤顺序(6步):模板(1) → 素材(2) → 配音(3) → 标题(4) → 确认生成(5) → 封面(6)
|
||||
*/
|
||||
import React from "react"
|
||||
import type { EditingTemplate } from "@/api/editing-planner"
|
||||
import type { EditPlanClip } from "@/api/template-editor"
|
||||
import type { PresetVoiceItem } from "@/api/voices"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
import type { CoverConfig } from "../types/cover"
|
||||
import type { TitleSettings } from "../types"
|
||||
import Step1TemplateSelect from "../components/Step1TemplateSelect"
|
||||
import Step2MaterialSelect from "../components/Step2MaterialSelect"
|
||||
import Step3VoiceWithMode from "./Step3VoiceWithMode"
|
||||
import Step3VoiceSelect from "../components/Step5VoiceSelect"
|
||||
import Step4TitleSettings from "../components/Step4TitleSettings"
|
||||
import Step5ConfirmGenerate from "../components/Step7ConfirmGenerate"
|
||||
import Step6CoverSettings from "../components/Step6CoverSettings"
|
||||
import BatchGenerationGrid from "./BatchGenerationGrid"
|
||||
import type { BatchTaskState } from "../hooks/generate-video/useGenerationPolling"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
|
||||
export interface GenerateStepContentProps {
|
||||
@@ -49,7 +49,17 @@ export interface GenerateStepContentProps {
|
||||
/* 配音 */
|
||||
selectedVoice: string
|
||||
onSelectedVoiceChange: (id: string) => void
|
||||
totalVideoDuration?: number
|
||||
onServerClipsChange: (clips: EditPlanClip[]) => void
|
||||
voiceMode: "preset" | "custom" | "clone"
|
||||
onVoiceModeChange: (mode: "preset" | "custom" | "clone") => void
|
||||
selectedClonedVoice: string
|
||||
onSelectedClonedVoiceChange: (id: string) => void
|
||||
clonedVoices: VoiceClone[]
|
||||
addClone: (voice: VoiceClone) => void
|
||||
hasProcessing: boolean
|
||||
cloneModalOpen: boolean
|
||||
onCloneModalOpenChange: (open: boolean) => void
|
||||
/* 生成 */
|
||||
generating: boolean
|
||||
generated: boolean
|
||||
@@ -57,26 +67,13 @@ export interface GenerateStepContentProps {
|
||||
progress: number
|
||||
generatedVideos: GeneratedVideo[]
|
||||
onRetry: () => void
|
||||
onRetryBatchTask: (taskId: string) => void
|
||||
onDismissError: () => void
|
||||
/** 批量:每个正式生成任务的独立状态(步骤5进度网格) */
|
||||
batchTasks: BatchTaskState[]
|
||||
/* 其他 */
|
||||
presetVoices: PresetVoiceItem[]
|
||||
/** BGM 开关 */
|
||||
bgm: boolean
|
||||
/** BGM 配置(来自模板) */
|
||||
bgmConfig?: { enabled: boolean; music_id?: string }
|
||||
/* ── 批量生成(#1677)── */
|
||||
previewCount: number
|
||||
previewTitles: string[]
|
||||
onPreviewTitlesChange: (titles: string[]) => void
|
||||
voiceModePerVideo: boolean
|
||||
onVoiceModePerVideoChange: (v: boolean) => void
|
||||
voiceLibraryIds: string[]
|
||||
onVoiceLibraryIdsChange: (ids: string[]) => void
|
||||
previewCovers: string[]
|
||||
onPreviewCoversChange: (urls: string[]) => void
|
||||
/** 批量模式勾选的变体索引(封面卡片按勾选顺序展示) */
|
||||
selectedVariantIds?: number[]
|
||||
}
|
||||
|
||||
export const GenerateStepContent: React.FC<GenerateStepContentProps> = (props) => {
|
||||
@@ -107,25 +104,19 @@ export const GenerateStepContent: React.FC<GenerateStepContentProps> = (props) =
|
||||
onCoverSettingsChange,
|
||||
selectedVoice,
|
||||
onSelectedVoiceChange,
|
||||
totalVideoDuration,
|
||||
onServerClipsChange,
|
||||
voiceMode,
|
||||
selectedClonedVoice,
|
||||
clonedVoices,
|
||||
generating,
|
||||
generated,
|
||||
generateError,
|
||||
progress,
|
||||
onRetry,
|
||||
generatedVideos,
|
||||
batchTasks,
|
||||
onRetryBatchTask,
|
||||
previewCount,
|
||||
previewTitles,
|
||||
onPreviewTitlesChange,
|
||||
voiceModePerVideo,
|
||||
onVoiceModePerVideoChange,
|
||||
voiceLibraryIds,
|
||||
onVoiceLibraryIdsChange,
|
||||
previewCovers,
|
||||
onPreviewCoversChange,
|
||||
selectedVariantIds,
|
||||
onRetry,
|
||||
onDismissError,
|
||||
presetVoices,
|
||||
} = props
|
||||
|
||||
/* 当前模板的 segments,传给 Step2 构建 clips */
|
||||
@@ -157,14 +148,10 @@ export const GenerateStepContent: React.FC<GenerateStepContentProps> = (props) =
|
||||
)
|
||||
case 3:
|
||||
return (
|
||||
<Step3VoiceWithMode
|
||||
previewCount={previewCount}
|
||||
<Step3VoiceSelect
|
||||
selectedVoice={selectedVoice}
|
||||
onSelectedVoiceChange={onSelectedVoiceChange}
|
||||
voiceModePerVideo={voiceModePerVideo}
|
||||
onVoiceModePerVideoChange={onVoiceModePerVideoChange}
|
||||
voiceLibraryIds={voiceLibraryIds}
|
||||
onVoiceLibraryIdsChange={onVoiceLibraryIdsChange}
|
||||
totalVideoDuration={totalVideoDuration}
|
||||
/>
|
||||
)
|
||||
case 4:
|
||||
@@ -183,66 +170,31 @@ export const GenerateStepContent: React.FC<GenerateStepContentProps> = (props) =
|
||||
onApplyPreset={onApplyPreset}
|
||||
activePreset={activePreset}
|
||||
titlePresets={titlePresets}
|
||||
previewCount={previewCount}
|
||||
previewTitles={previewTitles}
|
||||
onPreviewTitlesChange={onPreviewTitlesChange}
|
||||
/>
|
||||
)
|
||||
case 5:
|
||||
/* 确认生成页:批量=逐任务进度网格;单视频=进度状态卡(成片播放器在左侧大区域) */
|
||||
if (previewCount > 1) {
|
||||
return (
|
||||
<BatchGenerationGrid
|
||||
tasks={batchTasks}
|
||||
titles={previewTitles}
|
||||
onRetryTask={onRetryBatchTask}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/* 单视频:渲染进度 / 失败重试 / 完成提示(成片播放器在右侧栏) */
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<h3>🎬 确认生成</h3>
|
||||
{generating && (
|
||||
<div className="xx-gen-progress-card">
|
||||
<div className="xx-gen-progress-header">
|
||||
<div className="xx-gen-progress-info">
|
||||
<div className="xx-gen-progress-phase">
|
||||
⏳ 视频渲染中… {Math.round(progress)}%
|
||||
</div>
|
||||
<div className="xx-gen-progress-sub">
|
||||
生成过程中可以切换到其他页面,完成后可在任务历史查看
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-gen-progress-bar">
|
||||
<div
|
||||
className="xx-gen-progress-bar-fill"
|
||||
style={{ width: `${Math.min(Math.round(progress), 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{generateError && !generating && (
|
||||
<div className="xx-gen-error-card">
|
||||
<div className="xx-gen-error-info">
|
||||
<div className="xx-gen-error-title">生成失败</div>
|
||||
<div className="xx-gen-error-msg">{generateError}</div>
|
||||
</div>
|
||||
<button type="button" className="xx-btn xx-btn-primary xx-btn-sm" onClick={onRetry}>
|
||||
🔄 重试
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{generated && !generating && (
|
||||
<div className="xx-gen-success-card">
|
||||
<div className="xx-gen-success-info">
|
||||
<div className="xx-gen-success-title">✅ 视频生成完成!</div>
|
||||
<div className="xx-gen-success-sub">右侧可预览成片,点击「下一步」选择封面</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Step5ConfirmGenerate
|
||||
templates={userTemplates}
|
||||
selectedTemplate={selectedTemplate}
|
||||
materialMode={materialMode}
|
||||
selectedMaterials={selectedMaterials}
|
||||
smartSelectedIds={smartSelectedIds}
|
||||
title={titleSettings.title}
|
||||
voiceMode={voiceMode}
|
||||
selectedVoice={selectedVoice}
|
||||
selectedClonedVoice={selectedClonedVoice}
|
||||
presetVoices={presetVoices}
|
||||
clonedVoices={clonedVoices}
|
||||
coverSettings={coverSettings}
|
||||
generating={generating}
|
||||
generated={generated}
|
||||
generateError={generateError}
|
||||
progress={progress}
|
||||
generatedVideos={generatedVideos}
|
||||
onRetry={onRetry}
|
||||
onDismissError={onDismissError}
|
||||
/>
|
||||
)
|
||||
case 6:
|
||||
return (
|
||||
@@ -252,11 +204,6 @@ export const GenerateStepContent: React.FC<GenerateStepContentProps> = (props) =
|
||||
selectedTemplate={selectedTemplate}
|
||||
titleSettings={titleSettings}
|
||||
generatedVideos={generatedVideos}
|
||||
previewCount={previewCount}
|
||||
previewTitles={previewTitles}
|
||||
previewCovers={previewCovers}
|
||||
onPreviewCoversChange={onPreviewCoversChange}
|
||||
selectedVariantIndexes={selectedVariantIds}
|
||||
/>
|
||||
)
|
||||
default:
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
/**
|
||||
* 生成数量选择弹窗(Issue #1677)
|
||||
* Step1 选完模板点「下一步」时弹出:要生成几个视频?(1~10)
|
||||
* 默认 1,回车 = 1(零额外操作)
|
||||
*/
|
||||
import React, { useState, useEffect, useRef } from "react"
|
||||
import { MAX_PREVIEW_COUNT } from "../constants"
|
||||
|
||||
interface PreviewCountModalProps {
|
||||
open: boolean
|
||||
/** 默认值(上次选择,默认1) */
|
||||
defaultCount?: number
|
||||
onConfirm: (count: number) => void
|
||||
onCancel: () => void
|
||||
}
|
||||
|
||||
const PreviewCountModal: React.FC<PreviewCountModalProps> = ({
|
||||
open,
|
||||
defaultCount = 1,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}) => {
|
||||
const [count, setCount] = useState(defaultCount)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setCount(defaultCount)
|
||||
// 弹窗打开后聚焦并选中,方便直接回车=默认1
|
||||
setTimeout(() => inputRef.current?.focus(), 50)
|
||||
}
|
||||
}, [open, defaultCount])
|
||||
|
||||
const clamp = (n: number) => Math.max(1, Math.min(MAX_PREVIEW_COUNT, n || 1))
|
||||
|
||||
const handleConfirm = () => {
|
||||
onConfirm(clamp(count))
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault()
|
||||
handleConfirm()
|
||||
}
|
||||
if (e.key === "Escape") {
|
||||
onCancel()
|
||||
}
|
||||
}
|
||||
|
||||
if (!open) return null
|
||||
|
||||
return (
|
||||
<div className="xx-modal-mask" onClick={onCancel}>
|
||||
<div className="xx-modal-box xx-count-modal" onClick={(e) => e.stopPropagation()}>
|
||||
<h3 style={{ margin: "0 0 8px", fontSize: 18 }}>要生成几个视频?</h3>
|
||||
<p style={{ margin: "0 0 20px", fontSize: 13, color: "var(--text-secondary, #666)" }}>
|
||||
素材共用,AI 随机剪辑出不同版本,每个视频可独立设置标题、配音和封面
|
||||
</p>
|
||||
|
||||
<div className="xx-count-selector">
|
||||
<button
|
||||
type="button"
|
||||
className="xx-count-btn"
|
||||
onClick={() => setCount((c) => clamp(c - 1))}
|
||||
disabled={count <= 1}
|
||||
aria-label="减少"
|
||||
>
|
||||
−
|
||||
</button>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="number"
|
||||
min={1}
|
||||
max={MAX_PREVIEW_COUNT}
|
||||
value={count}
|
||||
onChange={(e) => setCount(clamp(parseInt(e.target.value, 10) || 1))}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="xx-count-input"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-count-btn"
|
||||
onClick={() => setCount((c) => clamp(c + 1))}
|
||||
disabled={count >= MAX_PREVIEW_COUNT}
|
||||
aria-label="增加"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="xx-count-quick">
|
||||
{[1, 3, 5, 10].map((n) => (
|
||||
<button
|
||||
key={n}
|
||||
type="button"
|
||||
className={`xx-count-chip ${count === n ? "active" : ""}`}
|
||||
onClick={() => setCount(n)}
|
||||
>
|
||||
{n} 个
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="xx-count-actions">
|
||||
<button type="button" className="xx-btn xx-btn-ghost" onClick={onCancel}>
|
||||
取消
|
||||
</button>
|
||||
<button type="button" className="xx-btn xx-btn-primary" onClick={handleConfirm}>
|
||||
{count === 1 ? "生成 1 个视频" : `生成 ${count} 个视频`}
|
||||
</button>
|
||||
</div>
|
||||
<p
|
||||
style={{
|
||||
margin: "12px 0 0",
|
||||
fontSize: 12,
|
||||
color: "var(--text-tertiary, #999)",
|
||||
textAlign: "center",
|
||||
}}
|
||||
>
|
||||
直接按回车 = 生成 1 个
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PreviewCountModal
|
||||
@@ -47,7 +47,9 @@ const Step1TemplateSelect: React.FC<Step1TemplateSelectProps> = (props) => {
|
||||
🎬
|
||||
</div>
|
||||
<h4>{tpl.name}</h4>
|
||||
<p>{tpl.segments.length}片段</p>
|
||||
<p>
|
||||
{tpl.estimated_duration}s · {tpl.segments.length}片段
|
||||
</p>
|
||||
{tpl.tags.length > 0 && (
|
||||
<div
|
||||
style={{
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/**
|
||||
* Step3 配音选择(Issue #1677 批量生成)
|
||||
* - 单视频 / 共用模式:与原配音选择完全一致
|
||||
* - 独立模式(开关开启):N 个配音选择器,每个视频独立选择
|
||||
*/
|
||||
import React from "react"
|
||||
import Step3VoiceSelect from "./Step5VoiceSelect"
|
||||
|
||||
interface Step3VoiceWithModeProps {
|
||||
previewCount: number
|
||||
/** 共用配音ID */
|
||||
selectedVoice: string
|
||||
onSelectedVoiceChange: (id: string) => void
|
||||
/** 是否独立配音 */
|
||||
voiceModePerVideo: boolean
|
||||
onVoiceModePerVideoChange: (v: boolean) => void
|
||||
/** 各变体独立配音ID */
|
||||
voiceLibraryIds: string[]
|
||||
onVoiceLibraryIdsChange: (ids: string[]) => void
|
||||
}
|
||||
|
||||
const Step3VoiceWithMode: React.FC<Step3VoiceWithModeProps> = ({
|
||||
previewCount,
|
||||
selectedVoice,
|
||||
onSelectedVoiceChange,
|
||||
voiceModePerVideo,
|
||||
onVoiceModePerVideoChange,
|
||||
voiceLibraryIds,
|
||||
onVoiceLibraryIdsChange,
|
||||
}) => {
|
||||
const isBatch = previewCount > 1
|
||||
|
||||
if (!isBatch) {
|
||||
return (
|
||||
<Step3VoiceSelect
|
||||
selectedVoice={selectedVoice}
|
||||
onSelectedVoiceChange={onSelectedVoiceChange}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
{/* 共用/独立切换 */}
|
||||
<div className="xx-title-ai-toggle" style={{ marginBottom: 16 }}>
|
||||
<div>
|
||||
<div style={{ fontWeight: 600, fontSize: 15 }}>
|
||||
🎙️ 配音方式:{voiceModePerVideo ? "每个视频独立配音" : "所有视频共用配音"}
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: "var(--text-tertiary, #999)", marginTop: 2 }}>
|
||||
{voiceModePerVideo
|
||||
? `为 ${previewCount} 个视频分别选择不同配音`
|
||||
: "所有视频使用同一个配音(默认)"}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
className={`xx-switch ${voiceModePerVideo ? "active" : ""}`}
|
||||
onClick={() => onVoiceModePerVideoChange(!voiceModePerVideo)}
|
||||
role="switch"
|
||||
aria-checked={voiceModePerVideo}
|
||||
tabIndex={0}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault()
|
||||
onVoiceModePerVideoChange(!voiceModePerVideo)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="xx-switch-knob" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!voiceModePerVideo ? (
|
||||
<Step3VoiceSelect
|
||||
heading="🎙️ 共用配音"
|
||||
description={`所有 ${previewCount} 个视频使用同一个配音,点击卡片可预览播放`}
|
||||
selectedVoice={selectedVoice}
|
||||
onSelectedVoiceChange={onSelectedVoiceChange}
|
||||
/>
|
||||
) : (
|
||||
<div className="xx-per-voice-list">
|
||||
{Array.from({ length: previewCount }, (_, i) => (
|
||||
<Step3VoiceSelect
|
||||
key={i}
|
||||
heading={`🎙️ 视频 ${i + 1} 的配音`}
|
||||
description="为这个视频单独选择配音"
|
||||
compact
|
||||
selectedVoice={voiceLibraryIds[i] || ""}
|
||||
onSelectedVoiceChange={(id) => {
|
||||
const next = [...voiceLibraryIds]
|
||||
next[i] = id
|
||||
onVoiceLibraryIdsChange(next)
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Step3VoiceWithMode
|
||||
@@ -1,22 +1,17 @@
|
||||
/**
|
||||
* Step 4 选择标题(Issue #1677 批量生成)
|
||||
* Step 4 选择标题(合并原 Step4 标题输入 + Step5 标题样式面板)
|
||||
*
|
||||
* 布局(由 GeneratePage 编排):左侧大区域实时预览(单=大播放器,批量=Canvas 网格),
|
||||
* 右侧边栏标题设置。本组件渲染在右侧边栏:
|
||||
* - 单视频:AI 标题生成器 + AutoComplete 标题库(与旧版完全一致,零回归)
|
||||
* - 批量:N 个独立标题输入框(AutoComplete 支持标题库选择)+ 批量 AI 生成
|
||||
* (一次生成 N 个标题,分别填入各变体,可单独换一个)
|
||||
* - 标题样式(字体/颜色/位置/大小/粗斜描边/预设):全局统一
|
||||
* 左侧:标题文字输入 + AI生成标题 + 样式设置(位置/字号/字体/颜色/样式/预设)
|
||||
* 右侧:FrontendPreviewPlayer 实时预览(由 GeneratePage 统一渲染)
|
||||
*/
|
||||
import React, { useMemo, useState } from "react"
|
||||
import { AutoComplete, Input, message } from "antd"
|
||||
import { LoadingOutlined } from "@ant-design/icons"
|
||||
import React from "react"
|
||||
import { AutoComplete } from "antd"
|
||||
import { PlayCircleOutlined } from "@ant-design/icons"
|
||||
import type { TitleSettings } from "../types"
|
||||
import { POSITION_OPTIONS, FONT_OPTIONS } from "../constants"
|
||||
import { useStep4Title } from "../hooks/useStep4Title"
|
||||
import AiTitleGenerator from "./title/AiTitleGenerator"
|
||||
import TitleStylePanel from "./title/TitleStylePanel"
|
||||
import { AI_TITLE_TEMPLATES } from "../constants"
|
||||
|
||||
interface Step4TitleSettingsProps {
|
||||
titleSettings: TitleSettings
|
||||
@@ -34,42 +29,6 @@ interface Step4TitleSettingsProps {
|
||||
onApplyPreset: (presetKey: string) => void
|
||||
activePreset: string | null
|
||||
titlePresets: { key: string; label: string; previewStyle: React.CSSProperties }[]
|
||||
/* ── 批量生成(#1677)── */
|
||||
/** 生成数量 */
|
||||
previewCount?: number
|
||||
/** 每个变体的标题文字(长度=previewCount) */
|
||||
previewTitles?: string[]
|
||||
onPreviewTitlesChange?: (titles: string[]) => void
|
||||
}
|
||||
|
||||
/** 从本地 AI 标题模板池按主题词生成 N 个不同标题(与单视频 AI 生成同源) */
|
||||
function buildBatchAiTitles(topic: string, count: number): string[] {
|
||||
const styles: Array<"catchy" | "emotional" | "informative"> = [
|
||||
"catchy",
|
||||
"emotional",
|
||||
"informative",
|
||||
]
|
||||
const pool: string[] = []
|
||||
styles.forEach((style) => {
|
||||
const templates = AI_TITLE_TEMPLATES[style] || []
|
||||
templates.forEach((tpl) => pool.push(tpl.replace(/\{topic\}/g, topic)))
|
||||
})
|
||||
// 洗牌后取前 count 个;不足则轮转补齐
|
||||
const shuffled = [...pool].sort(() => Math.random() - 0.5)
|
||||
const out: string[] = []
|
||||
for (let i = 0; i < count; i++) {
|
||||
out.push(shuffled[i % shuffled.length] || "")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function extractTopic(text: string): string {
|
||||
const keywords = text
|
||||
.replace(/[,。!?、,.!?]/g, " ")
|
||||
.split(/\s+/)
|
||||
.filter(Boolean)
|
||||
if (keywords.length === 0) return "这个话题"
|
||||
return keywords.slice(0, 3).join("")
|
||||
}
|
||||
|
||||
const Step4TitleSettings: React.FC<Step4TitleSettingsProps> = (props) => {
|
||||
@@ -85,208 +44,122 @@ const Step4TitleSettings: React.FC<Step4TitleSettingsProps> = (props) => {
|
||||
onApplyPreset,
|
||||
activePreset,
|
||||
titlePresets,
|
||||
previewCount = 1,
|
||||
previewTitles,
|
||||
onPreviewTitlesChange,
|
||||
} = props
|
||||
|
||||
const isBatch = previewCount > 1
|
||||
const [batchAiLoading, setBatchAiLoading] = useState(false)
|
||||
const [batchAiTopic, setBatchAiTopic] = useState("")
|
||||
|
||||
/** 更新单个变体标题;变体0同步写回 titleSettings.title(全局样式面板/草稿/TTS 链路依赖) */
|
||||
const updateVariantTitle = (index: number, val: string) => {
|
||||
if (!previewTitles || !onPreviewTitlesChange) return
|
||||
const next = [...previewTitles]
|
||||
next[index] = val
|
||||
onPreviewTitlesChange(next)
|
||||
if (index === 0) {
|
||||
t.updateTitle(val)
|
||||
}
|
||||
}
|
||||
|
||||
/** 批量 AI 生成:按主题词生成标题,分别填入 N 个变体 */
|
||||
const handleBatchAiGenerate = async (onlyEmpty = false) => {
|
||||
if (!onPreviewTitlesChange || !previewTitles) return
|
||||
const topic = (batchAiTopic || t.aiTitleInput || "").trim()
|
||||
if (!topic) {
|
||||
message.warning("请先输入主题词,例如:萌宠日常、旅行vlog")
|
||||
return
|
||||
}
|
||||
setBatchAiLoading(true)
|
||||
try {
|
||||
// 与单视频一致:本地模板模拟 AI 生成(1200ms 体验延迟)
|
||||
await new Promise((resolve) => setTimeout(resolve, 800))
|
||||
const picked = buildBatchAiTitles(extractTopic(topic), previewCount)
|
||||
const next = [...previewTitles]
|
||||
for (let i = 0; i < previewCount; i++) {
|
||||
if (onlyEmpty && next[i]?.trim()) continue
|
||||
if (picked[i]) next[i] = picked[i]
|
||||
}
|
||||
onPreviewTitlesChange(next)
|
||||
if (next[0]) t.updateTitle(next[0])
|
||||
message.success(`已为 ${previewCount} 个视频生成标题,可单独修改`)
|
||||
} finally {
|
||||
setBatchAiLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const titleOptions = useMemo(
|
||||
() => t.userTitles.map((ut) => ({ label: ut.content, value: ut.content })),
|
||||
[t.userTitles],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="xx-form-section xx-title-sidebar">
|
||||
<div className="xx-form-section">
|
||||
<h3>📝 选择标题</h3>
|
||||
|
||||
{!isBatch ? (
|
||||
/* ── 单视频:原有 AI 标题 + 输入框(保持不变,零回归) ── */
|
||||
{/* AI 自动选择模式 */}
|
||||
{t.titleSettings.aiAutoSelect && (
|
||||
<>
|
||||
{t.titleSettings.aiAutoSelect ? (
|
||||
<>
|
||||
<div className="xx-title-ai-toggle">
|
||||
<span className="xx-toggle-label">AI 自动选择标题</span>
|
||||
<div className="xx-switch active" onClick={t.toggleAiAutoSelect}>
|
||||
<div className="xx-switch-knob" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-form-field">
|
||||
<label>当前 AI 选定标题</label>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 10,
|
||||
padding: "8px 12px",
|
||||
background: "var(--bg-secondary, rgba(0,0,0,0.04))",
|
||||
borderRadius: 8,
|
||||
fontSize: 14,
|
||||
color: "var(--text-primary, #333)",
|
||||
}}
|
||||
>
|
||||
<span style={{ flex: 1 }}>
|
||||
{(previewTitles?.[0] ?? t.titleSettings.title) || "AI 将自动为你选择标题"}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary"
|
||||
style={{ flexShrink: 0, fontSize: 13, padding: "4px 12px" }}
|
||||
onClick={t.autoGenerateTitle}
|
||||
>
|
||||
🔄 换一个
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<AiTitleGenerator
|
||||
inputValue={t.aiTitleInput}
|
||||
onInputChange={t.setAiTitleInput}
|
||||
generating={t.aiTitleGenerating}
|
||||
onGenerate={t.handleGenerateAiTitles}
|
||||
results={t.aiTitleResults}
|
||||
hasGenerated={t.hasGeneratedTitles}
|
||||
onSelect={t.handleSelectAiTitle}
|
||||
selectedTitle={t.titleSettings.title}
|
||||
onRefresh={t.handleRefreshAiTitles}
|
||||
/>
|
||||
|
||||
<div className="xx-title-ai-toggle">
|
||||
<span className="xx-toggle-label">AI 自动选择标题</span>
|
||||
<div className="xx-switch" onClick={t.toggleAiAutoSelect}>
|
||||
<div className="xx-switch-knob" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-form-field">
|
||||
<label>标题</label>
|
||||
<AutoComplete
|
||||
placeholder="输入标题文字…"
|
||||
allowClear
|
||||
maxLength={50}
|
||||
style={{ width: "100%" }}
|
||||
value={(previewTitles?.[0] ?? t.titleSettings.title) || undefined}
|
||||
onChange={(val) => {
|
||||
t.updateTitle(val || "")
|
||||
onPreviewTitlesChange?.([val || ""])
|
||||
}}
|
||||
options={titleOptions}
|
||||
filterOption={(inputValue, option) => {
|
||||
const title = (option?.label || option?.value || "") as string
|
||||
return title.toLowerCase().includes((inputValue || "").toLowerCase())
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
/* ── 批量:AI 批量生成 + N 个独立标题输入框(AutoComplete 支持标题库) ── */
|
||||
<div className="xx-batch-titles">
|
||||
<div
|
||||
style={{
|
||||
fontSize: 12,
|
||||
color: "var(--text-secondary, #666)",
|
||||
marginBottom: 10,
|
||||
lineHeight: 1.6,
|
||||
}}
|
||||
>
|
||||
为每个视频输入独立标题,修改会实时叠加到左侧对应视频上。标题样式(字体/颜色/位置)全局统一。
|
||||
</div>
|
||||
|
||||
{/* 批量 AI 标题 */}
|
||||
<div className="xx-batch-ai-row">
|
||||
<Input
|
||||
placeholder="主题词,如:萌宠日常、旅行vlog"
|
||||
value={batchAiTopic || t.aiTitleInput}
|
||||
onChange={(e) => {
|
||||
setBatchAiTopic(e.target.value)
|
||||
t.setAiTitleInput(e.target.value)
|
||||
}}
|
||||
maxLength={30}
|
||||
size="small"
|
||||
style={{ flex: 1 }}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary xx-btn-sm"
|
||||
disabled={batchAiLoading}
|
||||
onClick={() => handleBatchAiGenerate(false)}
|
||||
>
|
||||
{batchAiLoading ? <LoadingOutlined /> : "✨"} 一键生成 {previewCount} 个标题
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-ghost xx-btn-sm"
|
||||
disabled={batchAiLoading}
|
||||
onClick={() => handleBatchAiGenerate(true)}
|
||||
>
|
||||
补填空标题
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{Array.from({ length: previewCount }, (_, i) => (
|
||||
<div className="xx-form-field" key={i}>
|
||||
<label>视频 {i + 1} 标题</label>
|
||||
<AutoComplete
|
||||
placeholder={`视频 ${i + 1} 的标题…`}
|
||||
maxLength={50}
|
||||
style={{ width: "100%" }}
|
||||
value={previewTitles?.[i] || undefined}
|
||||
onChange={(val) => updateVariantTitle(i, val || "")}
|
||||
options={titleOptions}
|
||||
filterOption={(inputValue, option) => {
|
||||
const title = (option?.label || option?.value || "") as string
|
||||
return title.toLowerCase().includes((inputValue || "").toLowerCase())
|
||||
}}
|
||||
/>
|
||||
<div className="xx-title-ai-toggle">
|
||||
<span className="xx-toggle-label">AI 自动选择标题</span>
|
||||
<div className="xx-switch active" onClick={t.toggleAiAutoSelect}>
|
||||
<div className="xx-switch-knob" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 显示当前 AI 选中的标题(只读)+ 换一个按钮 */}
|
||||
<div className="xx-form-field">
|
||||
<label>当前 AI 选定标题</label>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 10,
|
||||
padding: "8px 12px",
|
||||
background: "var(--bg-secondary, rgba(0,0,0,0.04))",
|
||||
borderRadius: 8,
|
||||
fontSize: 14,
|
||||
color: "var(--text-primary, #333)",
|
||||
}}
|
||||
>
|
||||
<span style={{ flex: 1 }}>{t.titleSettings.title || "AI 将自动为你选择标题"}</span>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary"
|
||||
style={{ flexShrink: 0, fontSize: 13, padding: "4px 12px" }}
|
||||
onClick={t.autoGenerateTitle}
|
||||
>
|
||||
🔄 换一个
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 标题样式面板(全局共用) */}
|
||||
{/* 手动选择模式 */}
|
||||
{!t.titleSettings.aiAutoSelect && (
|
||||
<>
|
||||
<AiTitleGenerator
|
||||
inputValue={t.aiTitleInput}
|
||||
onInputChange={t.setAiTitleInput}
|
||||
generating={t.aiTitleGenerating}
|
||||
onGenerate={t.handleGenerateAiTitles}
|
||||
results={t.aiTitleResults}
|
||||
hasGenerated={t.hasGeneratedTitles}
|
||||
onSelect={t.handleSelectAiTitle}
|
||||
selectedTitle={t.titleSettings.title}
|
||||
onRefresh={t.handleRefreshAiTitles}
|
||||
/>
|
||||
|
||||
<div className="xx-title-ai-toggle">
|
||||
<span className="xx-toggle-label">AI 自动选择标题</span>
|
||||
<div className="xx-switch" onClick={t.toggleAiAutoSelect}>
|
||||
<div className="xx-switch-knob" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="xx-form-field">
|
||||
<label>标题</label>
|
||||
<AutoComplete
|
||||
placeholder="输入或从标题库选择…"
|
||||
allowClear
|
||||
maxLength={50}
|
||||
style={{ width: "100%" }}
|
||||
value={t.titleSettings.title || undefined}
|
||||
onChange={(val) => t.updateTitle(val || "")}
|
||||
options={t.userTitles.map((ut) => ({
|
||||
label: ut.content,
|
||||
value: ut.content,
|
||||
}))}
|
||||
filterOption={(inputValue, option) => {
|
||||
const title = (option?.label || option?.value || "") as string
|
||||
return title.toLowerCase().includes((inputValue || "").toLowerCase())
|
||||
}}
|
||||
notFoundContent={
|
||||
t.userTitles.length === 0 ? (
|
||||
<span style={{ color: "var(--text-tertiary)", fontSize: 13 }}>
|
||||
标题库为空,请前往「标题管理」添加
|
||||
</span>
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 标题样式面板(原 Step5) */}
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 8,
|
||||
padding: "10px 14px",
|
||||
background: "rgba(59, 130, 246, 0.08)",
|
||||
borderRadius: 8,
|
||||
marginTop: 16,
|
||||
marginBottom: 12,
|
||||
border: "1px solid rgba(59, 130, 246, 0.15)",
|
||||
}}
|
||||
>
|
||||
<PlayCircleOutlined style={{ fontSize: 16, color: "#3b82f6" }} />
|
||||
<span style={{ fontSize: 12, color: "var(--text-secondary, #666)" }}>
|
||||
右侧为实时预览,调整样式即时生效
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<TitleStylePanel
|
||||
settings={t.titleSettings}
|
||||
onUpdatePosition={onUpdatePosition}
|
||||
|
||||
@@ -5,21 +5,18 @@
|
||||
import React, { useState, useRef, useCallback } from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { AudioOutlined, SoundOutlined } from "@ant-design/icons"
|
||||
import { AudioOutlined, SoundOutlined, WarningOutlined } from "@ant-design/icons"
|
||||
import { Modal } from "antd"
|
||||
import { getAssetsByKind } from "@/api/assets"
|
||||
import type { AssetItem } from "@/api/assets"
|
||||
|
||||
interface Step5VoiceSelectProps {
|
||||
selectedVoice: string
|
||||
onSelectedVoiceChange: (id: string) => void
|
||||
/** 卡片标题(独立配音模式下显示"视频 N 的配音"),默认"选择配音" */
|
||||
heading?: string
|
||||
/** 描述文案 */
|
||||
description?: string
|
||||
/** 是否使用紧凑卡片样式(独立配音模式下 N 个并排) */
|
||||
compact?: boolean
|
||||
totalVideoDuration?: number
|
||||
}
|
||||
|
||||
/** 格式化时长 mm:ss */
|
||||
/** 获取素材实际时长(优先顶层 duration,fallback 到 metadata.duration) */
|
||||
const getDuration = (item: AssetItem): number => {
|
||||
return item.duration ?? (item.metadata?.duration as number) ?? 0
|
||||
@@ -37,6 +34,13 @@ const isAiVoice = (item: AssetItem): boolean => {
|
||||
return (!duration || duration <= 0) && (!size || size <= 0)
|
||||
}
|
||||
|
||||
const formatDuration = (seconds?: number): string => {
|
||||
if (!seconds || seconds <= 0) return "00:00"
|
||||
const m = Math.floor(seconds / 60)
|
||||
const s = Math.floor(seconds % 60)
|
||||
return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`
|
||||
}
|
||||
|
||||
/** 格式化文件大小 */
|
||||
const formatFileSize = (bytes?: number): string => {
|
||||
if (!bytes || bytes <= 0) return "未知"
|
||||
@@ -49,13 +53,13 @@ const formatFileSize = (bytes?: number): string => {
|
||||
const Step5VoiceSelect: React.FC<Step5VoiceSelectProps> = ({
|
||||
selectedVoice,
|
||||
onSelectedVoiceChange,
|
||||
heading = "🎙️ 选择配音",
|
||||
description = "从配音库中选择已上传的素材,点击卡片可预览播放",
|
||||
compact = false,
|
||||
totalVideoDuration = 0,
|
||||
}) => {
|
||||
const navigate = useNavigate()
|
||||
const [playingId, setPlayingId] = useState<string | null>(null)
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null)
|
||||
const [durationWarningOpen, setDurationWarningOpen] = useState(false)
|
||||
const [pendingVoiceId, setPendingVoiceId] = useState<string | null>(null)
|
||||
|
||||
// 获取用户上传的配音素材
|
||||
const { data: materials = [], isLoading } = useQuery({
|
||||
@@ -96,14 +100,38 @@ const Step5VoiceSelect: React.FC<Step5VoiceSelectProps> = ({
|
||||
[playingId],
|
||||
)
|
||||
|
||||
/** 选中素材(直接选中,不再做时长校验弹窗) */
|
||||
/** 选中素材(含时长校验) */
|
||||
const handleSelect = useCallback(
|
||||
(id: string) => {
|
||||
// 如果启用了时长校验,且配音时长不足(AI 音色按脚本实时合成,不参与时长校验)
|
||||
if (totalVideoDuration > 0) {
|
||||
const material = materials.find((m) => m.id === id)
|
||||
if (material && !isAiVoice(material) && getDuration(material) < totalVideoDuration) {
|
||||
setPendingVoiceId(id)
|
||||
setDurationWarningOpen(true)
|
||||
return
|
||||
}
|
||||
}
|
||||
onSelectedVoiceChange(id)
|
||||
},
|
||||
[onSelectedVoiceChange],
|
||||
[onSelectedVoiceChange, totalVideoDuration, materials],
|
||||
)
|
||||
|
||||
/** 确认使用时长不足的配音 */
|
||||
const handleConfirmUseAnyway = useCallback(() => {
|
||||
if (pendingVoiceId) {
|
||||
onSelectedVoiceChange(pendingVoiceId)
|
||||
}
|
||||
setDurationWarningOpen(false)
|
||||
setPendingVoiceId(null)
|
||||
}, [pendingVoiceId, onSelectedVoiceChange])
|
||||
|
||||
/** 取消选择 */
|
||||
const handleCancelSelection = useCallback(() => {
|
||||
setDurationWarningOpen(false)
|
||||
setPendingVoiceId(null)
|
||||
}, [])
|
||||
|
||||
/** 跳转到配音库上传 */
|
||||
const handleGoToUpload = useCallback(() => {
|
||||
navigate("/app/voices?tab=material&upload=1")
|
||||
@@ -157,14 +185,14 @@ const Step5VoiceSelect: React.FC<Step5VoiceSelectProps> = ({
|
||||
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<h3>{heading}</h3>
|
||||
<p style={{ color: "#666", marginBottom: 16, fontSize: 14 }}>{description}</p>
|
||||
<h3>🎙️ 选择配音</h3>
|
||||
<p style={{ color: "#666", marginBottom: 16, fontSize: 14 }}>
|
||||
从配音库中选择已上传的素材,点击卡片可预览播放
|
||||
</p>
|
||||
<div
|
||||
style={{
|
||||
display: "grid",
|
||||
gridTemplateColumns: compact
|
||||
? "repeat(auto-fill, minmax(160px, 1fr))"
|
||||
: "repeat(auto-fill, minmax(220px, 1fr))",
|
||||
gridTemplateColumns: "repeat(auto-fill, minmax(220px, 1fr))",
|
||||
gap: 12,
|
||||
}}
|
||||
>
|
||||
@@ -249,7 +277,7 @@ const Step5VoiceSelect: React.FC<Step5VoiceSelectProps> = ({
|
||||
{item.name}
|
||||
</div>
|
||||
|
||||
{/* 文件大小 */}
|
||||
{/* 时长 + 大小 */}
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
@@ -261,13 +289,65 @@ const Step5VoiceSelect: React.FC<Step5VoiceSelectProps> = ({
|
||||
>
|
||||
{isAiVoice(item) ? (
|
||||
<span style={{ color: "#1677ff", fontWeight: 500 }}>AI 音色</span>
|
||||
) : null}
|
||||
) : (
|
||||
<span style={{ display: "flex", alignItems: "center", gap: 4 }}>
|
||||
{formatDuration(getDuration(item))}
|
||||
{totalVideoDuration > 0 && getDuration(item) < Number(totalVideoDuration) && (
|
||||
<span
|
||||
style={{
|
||||
color: "#ff4d4f",
|
||||
fontSize: 11,
|
||||
fontWeight: 500,
|
||||
display: "inline-flex",
|
||||
alignItems: "center",
|
||||
gap: 2,
|
||||
}}
|
||||
>
|
||||
<WarningOutlined />
|
||||
时长不足
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
<span>{isAiVoice(item) ? "按文本合成" : formatFileSize(getFileSize(item))}</span>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* 时长不足警告弹窗 */}
|
||||
<Modal
|
||||
title={
|
||||
<span style={{ display: "flex", alignItems: "center", gap: 8 }}>
|
||||
<WarningOutlined style={{ color: "#faad14" }} />
|
||||
配音时长不足
|
||||
</span>
|
||||
}
|
||||
open={durationWarningOpen}
|
||||
onOk={handleConfirmUseAnyway}
|
||||
onCancel={handleCancelSelection}
|
||||
okText="仍要使用"
|
||||
cancelText="重新选择"
|
||||
okButtonProps={{ danger: true }}
|
||||
>
|
||||
{(() => {
|
||||
const pendingMaterial = pendingVoiceId
|
||||
? materials.find((m) => m.id === pendingVoiceId)
|
||||
: null
|
||||
return (
|
||||
<p>
|
||||
该配音时长(
|
||||
<strong>
|
||||
{pendingMaterial ? formatDuration(getDuration(pendingMaterial)) : "--"}
|
||||
</strong>
|
||||
)短于视频总时长(
|
||||
<strong>{formatDuration(totalVideoDuration)}</strong>
|
||||
),播放时配音可能提前结束,建议选择更长的配音素材。
|
||||
</p>
|
||||
)
|
||||
})()}
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
/**
|
||||
* Step 5 选择封面(Issue #1677 批量生成改造)
|
||||
* - 单视频:保留原封面流程(自动生成/封面设置模板/封面预览)
|
||||
* - N 个视频:N 张封面卡片,每张带对应视频标题,可逐个自动生成或上传
|
||||
*/
|
||||
import React, { useRef } from "react"
|
||||
import React from "react"
|
||||
import { Modal, Spin } from "antd"
|
||||
import { LoadingOutlined } from "@ant-design/icons"
|
||||
import type { CoverConfig } from "../types/cover"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
import type { TitleSettings } from "../types"
|
||||
import { useStep6Cover } from "../hooks/useStep6Cover"
|
||||
import { useBatchCovers } from "../hooks/useBatchCovers"
|
||||
import Button from "@/components/ui/Button"
|
||||
import CoverSettingsModal from "./cover-settings/CoverSettingsModal"
|
||||
import CoverEditorModal from "./cover-settings/CoverEditorModal"
|
||||
@@ -24,15 +17,6 @@ interface Step6CoverSettingsProps {
|
||||
titleSettings?: TitleSettings
|
||||
/** 确认生成步骤产出的最终视频列表 */
|
||||
generatedVideos: GeneratedVideo[]
|
||||
/* ── 批量生成(#1677)── */
|
||||
previewCount?: number
|
||||
/** 每个变体的标题文字 */
|
||||
previewTitles?: string[]
|
||||
/** 每个变体的封面URL(按变体索引) */
|
||||
previewCovers?: string[]
|
||||
onPreviewCoversChange?: (urls: string[]) => void
|
||||
/** 勾选的变体索引(批量封面按此顺序展示,与最终成片顺序一致) */
|
||||
selectedVariantIndexes?: number[]
|
||||
}
|
||||
|
||||
const Step6CoverSettings: React.FC<Step6CoverSettingsProps> = (props) => {
|
||||
@@ -62,173 +46,13 @@ const Step6CoverSettings: React.FC<Step6CoverSettingsProps> = (props) => {
|
||||
generatedVideos: props.generatedVideos,
|
||||
})
|
||||
|
||||
const previewCount = props.previewCount || 1
|
||||
const isBatch = previewCount > 1
|
||||
const previewTitles = props.previewTitles || []
|
||||
const previewCovers = props.previewCovers || []
|
||||
/** 卡片展示的变体索引顺序:批量=勾选顺序(与成片顺序一致),单视频=[0] */
|
||||
const cardIndexes =
|
||||
isBatch && props.selectedVariantIndexes?.length
|
||||
? props.selectedVariantIndexes
|
||||
: Array.from({ length: previewCount }, (_, i) => i)
|
||||
const uploadInputRef = useRef<HTMLInputElement>(null)
|
||||
const uploadTargetRef = useRef<number>(0)
|
||||
|
||||
const completedVideos = props.generatedVideos.filter((v) => v.status === "completed")
|
||||
const batchTitles = cardIndexes.map((vi) => previewTitles[vi] || "")
|
||||
const batchCoversList = cardIndexes.map((vi) => previewCovers[vi] || "")
|
||||
const batchCovers = useBatchCovers({
|
||||
selectedTemplate: props.selectedTemplate || "",
|
||||
generatedVideos: props.generatedVideos,
|
||||
titles: batchTitles,
|
||||
titleStyle: {
|
||||
font: props.titleSettings?.font || "思源黑体",
|
||||
size: props.titleSettings?.size || 28,
|
||||
color: props.titleSettings?.color || "#ffffff",
|
||||
position: props.titleSettings?.position || "top",
|
||||
bold: props.titleSettings?.bold ?? true,
|
||||
stroke: props.titleSettings?.stroke ?? true,
|
||||
shadow: props.titleSettings?.shadow ?? false,
|
||||
},
|
||||
covers: batchCoversList,
|
||||
onCoversChange: (urls) => {
|
||||
// 按卡片顺序写回对应变体索引
|
||||
const next = [...(props.previewCovers || [])]
|
||||
cardIndexes.forEach((vi, cardPos) => {
|
||||
next[vi] = urls[cardPos] || ""
|
||||
})
|
||||
props.onPreviewCoversChange?.(next)
|
||||
},
|
||||
})
|
||||
const handleAutoGenerate = () => {
|
||||
generateAutoCover()
|
||||
}
|
||||
|
||||
// 预览图:优先 thumbnail_url,其次 upload_url
|
||||
const previewUrl = coverSettings.thumbnail_url || coverSettings.upload_url
|
||||
|
||||
const handleUploadClick = (variantIndex: number) => {
|
||||
uploadTargetRef.current = variantIndex
|
||||
uploadInputRef.current?.click()
|
||||
}
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
e.target.value = ""
|
||||
if (file) {
|
||||
const variantIndex = uploadTargetRef.current
|
||||
const cardPos = cardIndexes.indexOf(variantIndex)
|
||||
if (cardPos >= 0) void batchCovers.uploadOne(cardPos, file)
|
||||
}
|
||||
}
|
||||
|
||||
/* ── 批量封面 ── */
|
||||
if (isBatch) {
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<h3>🖼️ 选择封面</h3>
|
||||
|
||||
<div
|
||||
style={{
|
||||
padding: "10px 14px",
|
||||
background: "rgba(16, 185, 129, 0.08)",
|
||||
borderRadius: 8,
|
||||
marginBottom: 16,
|
||||
border: "1px solid rgba(16, 185, 129, 0.15)",
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary, #666)",
|
||||
}}
|
||||
>
|
||||
🎬 共 {completedVideos.length} 个成片,封面将从对应成片中智能选帧并叠加该视频的标题
|
||||
</div>
|
||||
|
||||
<div style={{ display: "flex", gap: 8, marginBottom: 16 }}>
|
||||
<Button
|
||||
buttonType="primary"
|
||||
onClick={() => void batchCovers.generateAll()}
|
||||
disabled={completedVideos.length === 0 || batchCovers.loadingIndex !== null}
|
||||
>
|
||||
✨ 一键全部自动生成
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="xx-cover-grid">
|
||||
{cardIndexes.map((variantIndex, cardPos) => {
|
||||
const url = batchCoversList[cardPos]
|
||||
const isLoading = batchCovers.loadingIndex === cardPos
|
||||
const isUploading = batchCovers.uploadingIndex === cardPos
|
||||
const title = batchTitles[cardPos]
|
||||
return (
|
||||
<div className="xx-cover-card" key={variantIndex}>
|
||||
<div className="xx-cover-card-title">视频 {variantIndex + 1}</div>
|
||||
<div className="xx-cover-card-box">
|
||||
{isLoading || isUploading ? (
|
||||
<div className="xx-cover-card-loading">
|
||||
<Spin indicator={<LoadingOutlined style={{ fontSize: 24 }} spin />} />
|
||||
<span>{isLoading ? "AI 选帧中…" : "上传中…"}</span>
|
||||
</div>
|
||||
) : url ? (
|
||||
<img
|
||||
src={url}
|
||||
alt={`视频${variantIndex + 1}封面`}
|
||||
className="xx-cover-card-img"
|
||||
/>
|
||||
) : (
|
||||
<div className="xx-cover-card-placeholder">
|
||||
<span style={{ fontSize: 26 }}>🖼️</span>
|
||||
<span style={{ fontSize: 12 }}>未设置封面</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="xx-cover-card-ratio">9:16</div>
|
||||
</div>
|
||||
{title && (
|
||||
<div
|
||||
style={{
|
||||
fontSize: 12,
|
||||
color: "var(--text-secondary, #666)",
|
||||
marginTop: 6,
|
||||
overflow: "hidden",
|
||||
textOverflow: "ellipsis",
|
||||
whiteSpace: "nowrap",
|
||||
}}
|
||||
title={title}
|
||||
>
|
||||
标题:{title}
|
||||
</div>
|
||||
)}
|
||||
<div style={{ display: "flex", gap: 6, marginTop: 8 }}>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-primary xx-btn-sm"
|
||||
style={{ flex: 1, fontSize: 12, padding: "4px 8px" }}
|
||||
onClick={() => void batchCovers.generateOne(cardPos)}
|
||||
disabled={isLoading || isUploading}
|
||||
>
|
||||
{url ? "🔄 重新生成" : "✨ 自动生成"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="xx-btn xx-btn-ghost xx-btn-sm"
|
||||
style={{ flex: 1, fontSize: 12, padding: "4px 8px" }}
|
||||
onClick={() => handleUploadClick(variantIndex)}
|
||||
disabled={isLoading || isUploading}
|
||||
>
|
||||
📤 上传
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
<input
|
||||
ref={uploadInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
style={{ display: "none" }}
|
||||
onChange={handleFileChange}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/* ── 单视频:原有流程保持不变 ── */
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<h3>🖼️ 选择封面</h3>
|
||||
@@ -251,7 +75,7 @@ const Step6CoverSettings: React.FC<Step6CoverSettingsProps> = (props) => {
|
||||
)}
|
||||
|
||||
<div className="xx-cover-actions">
|
||||
<Button buttonType="primary" onClick={generateAutoCover} disabled={!finalVideo}>
|
||||
<Button buttonType="primary" onClick={handleAutoGenerate} disabled={!finalVideo}>
|
||||
✨ 自动生成封面
|
||||
</Button>
|
||||
<Button buttonType="ghost" onClick={() => setShowCoverSettings(true)}>
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Step 7 确认生成组件
|
||||
*/
|
||||
import React from "react"
|
||||
import type { EditingTemplate } from "@/api/editing-planner"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
import type { CoverConfig } from "../types/cover"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
import type { PresetVoiceItem } from "@/api/voices"
|
||||
import { useStep7Generate } from "../hooks/useStep7Generate"
|
||||
import SummaryCard from "./step7-confirm/SummaryCard"
|
||||
import GenerationStatus from "./step7-confirm/GenerationStatus"
|
||||
|
||||
interface Step7ConfirmGenerateProps {
|
||||
templates: EditingTemplate[]
|
||||
selectedTemplate: string
|
||||
materialMode: "manual" | "auto"
|
||||
selectedMaterials: string[]
|
||||
smartSelectedIds: string[]
|
||||
title: string
|
||||
voiceMode: "preset" | "custom" | "clone"
|
||||
selectedVoice: string
|
||||
selectedClonedVoice: string
|
||||
presetVoices: PresetVoiceItem[]
|
||||
clonedVoices: VoiceClone[]
|
||||
coverSettings: CoverConfig
|
||||
generating: boolean
|
||||
generated: boolean
|
||||
generateError: string | null
|
||||
progress: number
|
||||
generatedVideos: GeneratedVideo[]
|
||||
onRetry: () => void
|
||||
onDismissError: () => void
|
||||
}
|
||||
|
||||
const Step7ConfirmGenerate: React.FC<Step7ConfirmGenerateProps> = (props) => {
|
||||
const {
|
||||
templateName,
|
||||
materialSummary,
|
||||
title,
|
||||
voiceName,
|
||||
coverSummary,
|
||||
generating,
|
||||
generated,
|
||||
generateError,
|
||||
progress,
|
||||
generatedVideos,
|
||||
getGenerationPhase,
|
||||
handleScrollToPreview,
|
||||
} = useStep7Generate(props)
|
||||
|
||||
const { onRetry, onDismissError } = props
|
||||
|
||||
return (
|
||||
<div className="xx-form-section">
|
||||
<h3>✨ 确认生成</h3>
|
||||
<SummaryCard
|
||||
templateName={templateName}
|
||||
materialSummary={materialSummary}
|
||||
title={title}
|
||||
voiceName={voiceName}
|
||||
coverSummary={coverSummary}
|
||||
/>
|
||||
<GenerationStatus
|
||||
generating={generating}
|
||||
generated={generated}
|
||||
generateError={generateError}
|
||||
progress={progress}
|
||||
generatedVideos={generatedVideos}
|
||||
getGenerationPhase={getGenerationPhase}
|
||||
onScrollToPreview={handleScrollToPreview}
|
||||
onRetry={onRetry}
|
||||
onDismissError={onDismissError}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Step7ConfirmGenerate
|
||||
@@ -0,0 +1,44 @@
|
||||
import React from "react"
|
||||
|
||||
interface SummaryCardProps {
|
||||
templateName: string
|
||||
materialSummary: string
|
||||
title: string
|
||||
voiceName: string
|
||||
coverSummary: string
|
||||
}
|
||||
|
||||
const SummaryCard: React.FC<SummaryCardProps> = ({
|
||||
templateName,
|
||||
materialSummary,
|
||||
title,
|
||||
voiceName,
|
||||
coverSummary,
|
||||
}) => {
|
||||
return (
|
||||
<div className="xx-summary-card">
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">模板</span>
|
||||
<span className="xx-summary-value">{templateName}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">素材</span>
|
||||
<span className="xx-summary-value">{materialSummary}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">标题</span>
|
||||
<span className="xx-summary-value">{title || "未选择"}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">配音</span>
|
||||
<span className="xx-summary-value">{voiceName}</span>
|
||||
</div>
|
||||
<div className="xx-summary-row">
|
||||
<span className="xx-summary-label">封面</span>
|
||||
<span className="xx-summary-value">{coverSummary}</span>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SummaryCard
|
||||
@@ -37,10 +37,6 @@ export const STEPS = [
|
||||
{ key: 6, label: "选择封面" },
|
||||
]
|
||||
|
||||
/* ── 批量生成限制 ── */
|
||||
export const MAX_PREVIEW_COUNT = 10
|
||||
export const MIN_PREVIEW_COUNT = 1
|
||||
|
||||
/* ── 标题位置选项 ── */
|
||||
export const POSITION_OPTIONS = [
|
||||
{ value: "top", label: "顶部" },
|
||||
|
||||
@@ -2900,545 +2900,3 @@
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
Issue #1677 多视频批量生成
|
||||
================================================================ */
|
||||
|
||||
/* ── Step4 布局对调:左侧预览大区域,右侧标题边栏 ── */
|
||||
.xx-generate-layout.step4-layout {
|
||||
grid-template-columns: 1fr 380px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.xx-generate-preview-col {
|
||||
min-width: 0;
|
||||
position: sticky;
|
||||
top: 16px;
|
||||
}
|
||||
|
||||
.xx-generate-preview-col .xx-form-section {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.xx-title-sidebar {
|
||||
max-height: calc(100vh - 140px);
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
/* ── 数量选择弹窗 ── */
|
||||
.xx-modal-mask {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.45);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.xx-modal-box {
|
||||
background: var(--bg-primary, #fff);
|
||||
border-radius: 16px;
|
||||
padding: 28px;
|
||||
width: 420px;
|
||||
max-width: calc(100vw - 32px);
|
||||
box-shadow: 0 12px 48px rgba(0, 0, 0, 0.18);
|
||||
}
|
||||
|
||||
.xx-count-selector {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 16px;
|
||||
margin: 8px 0 16px;
|
||||
}
|
||||
|
||||
.xx-count-btn {
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border-radius: 50%;
|
||||
border: 1px solid var(--border-primary, #d9d9d9);
|
||||
background: var(--bg-secondary, #f5f5f5);
|
||||
font-size: 22px;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
color: var(--text-primary, #333);
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.xx-count-btn:hover:not(:disabled) {
|
||||
border-color: #1677ff;
|
||||
color: #1677ff;
|
||||
}
|
||||
|
||||
.xx-count-btn:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.xx-count-input {
|
||||
width: 88px;
|
||||
height: 52px;
|
||||
text-align: center;
|
||||
font-size: 26px;
|
||||
font-weight: 700;
|
||||
border: 2px solid var(--border-primary, #d9d9d9);
|
||||
border-radius: 12px;
|
||||
color: var(--text-primary, #333);
|
||||
background: var(--bg-primary, #fff);
|
||||
}
|
||||
|
||||
.xx-count-input:focus {
|
||||
outline: none;
|
||||
border-color: #1677ff;
|
||||
}
|
||||
|
||||
/* 隐藏 number input 上下箭头 */
|
||||
.xx-count-input::-webkit-outer-spin-button,
|
||||
.xx-count-input::-webkit-inner-spin-button {
|
||||
-webkit-appearance: none;
|
||||
margin: 0;
|
||||
}
|
||||
.xx-count-input {
|
||||
-moz-appearance: textfield;
|
||||
appearance: textfield;
|
||||
}
|
||||
|
||||
.xx-count-quick {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
justify-content: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.xx-count-chip {
|
||||
padding: 6px 16px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid var(--border-primary, #d9d9d9);
|
||||
background: var(--bg-primary, #fff);
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
color: var(--text-secondary, #666);
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.xx-count-chip:hover {
|
||||
border-color: #1677ff;
|
||||
color: #1677ff;
|
||||
}
|
||||
|
||||
.xx-count-chip.active {
|
||||
background: #1677ff;
|
||||
border-color: #1677ff;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.xx-count-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.xx-count-actions .xx-btn {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
/* ── 批量预览网格 ── */
|
||||
.xx-variant-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.xx-variant-card {
|
||||
position: relative;
|
||||
border: 2px solid var(--border-primary, #e8e8e8);
|
||||
border-radius: 12px;
|
||||
padding: 10px;
|
||||
background: var(--bg-primary, #fff);
|
||||
cursor: pointer;
|
||||
transition: all 0.18s;
|
||||
}
|
||||
|
||||
.xx-variant-card:hover {
|
||||
border-color: #91caff;
|
||||
}
|
||||
|
||||
.xx-variant-card.selected {
|
||||
border-color: #1677ff;
|
||||
box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.12);
|
||||
}
|
||||
|
||||
.xx-variant-card.failed {
|
||||
border-color: #ffccc7;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.xx-variant-check {
|
||||
position: absolute;
|
||||
top: 14px;
|
||||
left: 14px;
|
||||
z-index: 3;
|
||||
width: 26px;
|
||||
height: 26px;
|
||||
border-radius: 50%;
|
||||
border: 2px solid #fff;
|
||||
background: rgba(0, 0, 0, 0.35);
|
||||
color: #fff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.xx-variant-check.checked {
|
||||
background: #1677ff;
|
||||
border-color: #1677ff;
|
||||
}
|
||||
|
||||
.xx-variant-index {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary, #666);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.xx-variant-video-wrap {
|
||||
position: relative;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
background: #000;
|
||||
aspect-ratio: 9 / 16;
|
||||
max-height: 420px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.xx-variant-loading,
|
||||
.xx-variant-failed {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
color: var(--text-secondary, #999);
|
||||
font-size: 12px;
|
||||
padding: 16px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.xx-variant-progress {
|
||||
width: 120px;
|
||||
height: 4px;
|
||||
border-radius: 2px;
|
||||
background: rgba(255, 255, 255, 0.25);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.xx-variant-progress-bar {
|
||||
height: 100%;
|
||||
background: #1677ff;
|
||||
border-radius: 2px;
|
||||
transition: width 0.4s;
|
||||
}
|
||||
|
||||
.xx-variant-progress-text {
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.xx-variant-title-overlay {
|
||||
position: absolute;
|
||||
left: 8%;
|
||||
right: 8%;
|
||||
text-align: center;
|
||||
font-weight: 700;
|
||||
line-height: 1.3;
|
||||
pointer-events: none;
|
||||
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7);
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.xx-variant-title-overlay.pos-top {
|
||||
top: 8%;
|
||||
}
|
||||
|
||||
.xx-variant-title-overlay.pos-center {
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
}
|
||||
|
||||
.xx-variant-title-overlay.pos-bottom,
|
||||
.xx-variant-title-overlay.pos-custom {
|
||||
bottom: 10%;
|
||||
}
|
||||
|
||||
.xx-variant-footer {
|
||||
min-height: 22px;
|
||||
margin-top: 8px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.xx-variant-ready-tag {
|
||||
color: #52c41a;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.xx-variant-skip-tag {
|
||||
color: var(--text-tertiary, #999);
|
||||
}
|
||||
|
||||
/* ── 批量配音列表 ── */
|
||||
.xx-per-voice-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.xx-per-voice-list .xx-form-section {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* ── 批量封面网格 ── */
|
||||
.xx-cover-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.xx-cover-card {
|
||||
border: 1px solid var(--border-primary, #e8e8e8);
|
||||
border-radius: 12px;
|
||||
padding: 10px;
|
||||
background: var(--bg-primary, #fff);
|
||||
}
|
||||
|
||||
.xx-cover-card-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary, #666);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.xx-cover-card-box {
|
||||
position: relative;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
background: #000;
|
||||
aspect-ratio: 9 / 16;
|
||||
max-height: 300px;
|
||||
}
|
||||
|
||||
.xx-cover-card-img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.xx-cover-card-placeholder,
|
||||
.xx-cover-card-loading {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
color: var(--text-tertiary, #999);
|
||||
background: var(--bg-secondary, #f7f7f7);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.xx-cover-card-ratio {
|
||||
position: absolute;
|
||||
right: 6px;
|
||||
bottom: 6px;
|
||||
background: rgba(0, 0, 0, 0.55);
|
||||
color: #fff;
|
||||
font-size: 10px;
|
||||
padding: 1px 6px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
/* ── 响应式:窄屏 Step4 回退单列 ── */
|
||||
@media (max-width: 960px) {
|
||||
.xx-generate-layout.step4-layout {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.xx-generate-preview-col {
|
||||
position: static;
|
||||
}
|
||||
|
||||
.xx-title-sidebar {
|
||||
max-height: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
批量前端 Canvas 预览网格(Issue #1677 修正:纯前端实时预览)
|
||||
============================================================ */
|
||||
.xx-canvas-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.xx-canvas-grid-card {
|
||||
border: 2px solid var(--border-primary, #e2e8f0);
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
background: #000;
|
||||
transition: border-color 0.2s ease;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.xx-canvas-grid-card.selected {
|
||||
border-color: var(--primary-color, #1677ff);
|
||||
box-shadow: 0 0 0 2px rgba(22, 119, 255, 0.15);
|
||||
}
|
||||
|
||||
.xx-canvas-grid-card-bar {
|
||||
position: relative;
|
||||
z-index: 2;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 6px 10px;
|
||||
background: var(--bg-surface, #fff);
|
||||
border-bottom: 1px solid var(--border-primary, #e2e8f0);
|
||||
}
|
||||
|
||||
.xx-canvas-grid-check {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: var(--text-primary, #1a1a1a);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.xx-canvas-grid-check input[type="checkbox"] {
|
||||
width: 15px;
|
||||
height: 15px;
|
||||
cursor: pointer;
|
||||
accent-color: var(--primary-color, #1677ff);
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
批量标题:AI 一键生成行(Issue #1677)
|
||||
============================================================ */
|
||||
.xx-batch-ai-row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 10px 12px;
|
||||
margin-bottom: 12px;
|
||||
background: var(--bg-secondary, #f7f8fa);
|
||||
border: 1px dashed var(--border-primary, #d9d9d9);
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.xx-batch-ai-row .xx-form-field {
|
||||
margin: 0;
|
||||
flex: 1;
|
||||
min-width: 140px;
|
||||
}
|
||||
|
||||
.xx-batch-titles {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
第5步确认生成:批量渲染进度网格(Issue #1677)
|
||||
============================================================ */
|
||||
.xx-batch-gen-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card {
|
||||
border: 1px solid var(--border-primary, #e2e8f0);
|
||||
border-radius: 12px;
|
||||
padding: 14px;
|
||||
background: var(--bg-surface, #fff);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card.status-completed {
|
||||
border-color: rgba(82, 196, 26, 0.4);
|
||||
background: rgba(82, 196, 26, 0.04);
|
||||
}
|
||||
|
||||
.xx-batch-gen-card.status-failed {
|
||||
border-color: rgba(239, 68, 68, 0.4);
|
||||
background: rgba(239, 68, 68, 0.04);
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-title {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary, #1a1a1a);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-pct {
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary, #666);
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-done {
|
||||
font-size: 13px;
|
||||
color: var(--success-color, #52c41a);
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-failed {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.xx-batch-gen-card-err {
|
||||
font-size: 13px;
|
||||
color: var(--error-color, #ef4444);
|
||||
line-height: 1.5;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
/* ── 响应式:窄屏批量网格回退单列 ── */
|
||||
@media (max-width: 960px) {
|
||||
.xx-canvas-grid,
|
||||
.xx-batch-gen-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,19 +29,6 @@ export interface UseGenerateVideoProps {
|
||||
}
|
||||
/** 生成成功后的回调(用于清除持久化的 previewTaskId 等状态) */
|
||||
onGenerationSuccess?: () => void
|
||||
/* ── 批量生成(#1677)── */
|
||||
/** 生成数量(1=单条旧逻辑,>1=批量) */
|
||||
previewCount?: number
|
||||
/** 每个变体的标题文字(长度=count 时各自独立) */
|
||||
variantTitles?: string[]
|
||||
/** 独立配音模式下每个变体的配音ID(空数组=共用 selectedVoice) */
|
||||
variantVoiceLibraryIds?: string[]
|
||||
/** 是否独立配音 */
|
||||
voiceModePerVideo?: boolean
|
||||
/** 每个变体的封面URL(空数组=回退 coverSettings) */
|
||||
variantCoverUrls?: string[]
|
||||
/** 勾选要生成的变体索引(批量模式) */
|
||||
selectedVariantIndexes?: number[]
|
||||
}
|
||||
|
||||
/** 生成阶段 */
|
||||
@@ -57,7 +44,7 @@ export interface UseGenerateVideoResult {
|
||||
generated: boolean
|
||||
generateError: string | null
|
||||
generatedVideos: GeneratedVideo[]
|
||||
generate: () => Promise<boolean>
|
||||
generate: () => Promise<void>
|
||||
retry: () => void
|
||||
dismissError: () => void
|
||||
download: () => Promise<void>
|
||||
|
||||
@@ -1,28 +1,14 @@
|
||||
import { useRef, useCallback, useState } from "react"
|
||||
import { useRef, useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
import axios from "axios"
|
||||
import { getGenerationTask, retryTask as retryGenerationTaskApi } from "@/api/tasks/tasks"
|
||||
import { getGenerationTask } from "@/api/tasks/tasks"
|
||||
import { getGenerationTaskResults } from "@/api/template-editor"
|
||||
import { safeExtractError } from "./errorUtils"
|
||||
|
||||
/** 批量生成时单个任务的实时状态(Issue #1677 第5步确认生成页) */
|
||||
export interface BatchTaskState {
|
||||
taskId: string
|
||||
/** 变体序号(0-based,与标题/封面数组对齐) */
|
||||
variantIndex: number
|
||||
status: "running" | "completed" | "failed"
|
||||
progress: number
|
||||
error: string | null
|
||||
/** 完成后的成片视频 */
|
||||
videos: unknown[]
|
||||
}
|
||||
|
||||
interface UseGenerationPollingOptions {
|
||||
onProgress: (progress: number) => void
|
||||
onComplete: (videos: unknown[]) => void
|
||||
onFailed: (errorMsg: string) => void
|
||||
/** 批量:单任务状态变化(第5步逐卡片展示) */
|
||||
onBatchTaskUpdate?: (taskId: string, patch: Partial<BatchTaskState>) => void
|
||||
}
|
||||
|
||||
/** 最大连续错误次数(仅对可重试错误),超过后终止轮询 */
|
||||
@@ -31,30 +17,31 @@ const MAX_RETRYABLE_ERRORS = 10
|
||||
const MAX_RESULTS_RETRIES = 3
|
||||
|
||||
/**
|
||||
* 生成状态轮询 Hook(v4 — 批量任务独立状态 + 单任务重试)
|
||||
* 生成状态轮询 Hook(v2 — 改用 /generation/tasks/{task_id})
|
||||
*
|
||||
* startPolling(taskId) 轮询单个任务;
|
||||
* startPollingBatch(tasks) 并行轮询 N 个任务:
|
||||
* - 每个任务独立进度/状态/失败,通过 onBatchTaskUpdate 实时回传
|
||||
* - 全部成功才 onComplete(聚合视频按变体顺序);任一失败不影响其他任务继续
|
||||
* - retryTask(taskId) 单独重试失败任务(重新轮询,后端任务仍在跑则直接接续)
|
||||
* 旧版轮询 GET /templates/{id}/editor/generation-status 依赖 plan 维度状态,
|
||||
* 在编辑流程数据链路断裂时拿不到 task_id。新版直接使用 POST /generation/tasks
|
||||
* 返回的 task_id 轮询任务详情,不再依赖 plan。
|
||||
*
|
||||
* 错误处理:
|
||||
* - 4xx(尤其 404)视为不可恢复,立即 onFailed,不再重试
|
||||
* - 5xx / 网络错误重试,最多连续 MAX_RETRYABLE_ERRORS 次
|
||||
* - 任务完成后获取结果失败会重试 MAX_RESULTS_RETRIES 次,仍失败则 onFailed
|
||||
*/
|
||||
export function useGenerationPolling({
|
||||
export const useGenerationPolling = ({
|
||||
onProgress,
|
||||
onComplete,
|
||||
onFailed,
|
||||
onBatchTaskUpdate,
|
||||
}: UseGenerationPollingOptions) {
|
||||
const progressTimer = useRef<ReturnType<typeof setTimeout>[]>([])
|
||||
}: UseGenerationPollingOptions) => {
|
||||
const progressTimer = useRef<ReturnType<typeof setTimeout>>()
|
||||
const cancelledRef = useRef(false)
|
||||
/** 批量任务上下文:taskId → 变体序号 */
|
||||
const batchContextRef = useRef<Map<string, number>>(new Map())
|
||||
const [, forceTick] = useState(0)
|
||||
|
||||
const clearTimer = useCallback(() => {
|
||||
cancelledRef.current = true
|
||||
progressTimer.current.forEach((t) => clearTimeout(t))
|
||||
progressTimer.current = []
|
||||
if (progressTimer.current) {
|
||||
clearTimeout(progressTimer.current)
|
||||
progressTimer.current = undefined
|
||||
}
|
||||
}, [])
|
||||
|
||||
/** 任务完成后拉取结果列表,带重试 */
|
||||
@@ -75,237 +62,85 @@ export function useGenerationPolling({
|
||||
[],
|
||||
)
|
||||
|
||||
const extractErrorMessage = (pollErr: unknown, status: number): string => {
|
||||
const msg =
|
||||
(axios.isAxiosError(pollErr) &&
|
||||
(pollErr.response?.data as { detail?: string; message?: string } | undefined)?.detail) ||
|
||||
(axios.isAxiosError(pollErr) &&
|
||||
(pollErr.response?.data as { detail?: string; message?: string } | undefined)?.message) ||
|
||||
`查询任务失败 (${status})`
|
||||
return safeExtractError(msg)
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询单个任务。
|
||||
* - isBatch=true:状态变化通过 onBatchTaskUpdate 回传,不触发整体 onProgress/onComplete
|
||||
* - resolve(videos) 成功;reject(Error) 失败
|
||||
*/
|
||||
const pollSingleTask = useCallback(
|
||||
(
|
||||
taskId: string,
|
||||
runId: number,
|
||||
callbacks?: {
|
||||
onTaskProgress?: (pct: number) => void
|
||||
onTaskCompleted?: (videos: unknown[]) => void
|
||||
onTaskFailed?: (msg: string) => void
|
||||
},
|
||||
): Promise<unknown[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let consecutiveErrors = 0
|
||||
let done = false
|
||||
|
||||
const poll = async () => {
|
||||
if (cancelledRef.current || done) return
|
||||
try {
|
||||
const task = await getGenerationTask(taskId)
|
||||
if (cancelledRef.current || done) return
|
||||
consecutiveErrors = 0
|
||||
|
||||
if (task.status === "completed") {
|
||||
done = true
|
||||
const videos = await fetchResultsWithRetry(taskId)
|
||||
if (cancelledRef.current) return
|
||||
if (videos === null) {
|
||||
const msg = "视频已生成,但获取结果列表失败,请稍后在任务列表查看"
|
||||
callbacks?.onTaskFailed?.(msg)
|
||||
reject(new Error(msg))
|
||||
return
|
||||
}
|
||||
callbacks?.onTaskCompleted?.(videos)
|
||||
resolve(videos)
|
||||
return
|
||||
}
|
||||
|
||||
if (task.status === "failed" || task.status === "cancelled") {
|
||||
done = true
|
||||
const rawMsg =
|
||||
task.error_info?.error_message ||
|
||||
task.error_message ||
|
||||
(task.status === "cancelled" ? "任务已取消" : "视频生成失败,请联系管理员或重试")
|
||||
const msg = safeExtractError(rawMsg)
|
||||
callbacks?.onTaskFailed?.(msg)
|
||||
reject(new Error(msg))
|
||||
return
|
||||
}
|
||||
|
||||
const pct = Math.max(0, Math.min(99, Math.round(Number(task.progress) || 0)))
|
||||
callbacks?.onTaskProgress?.(pct)
|
||||
if (!callbacks && runId === 0) {
|
||||
onProgress(pct)
|
||||
}
|
||||
const timer = setTimeout(poll, 2000)
|
||||
progressTimer.current.push(timer)
|
||||
} catch (pollErr) {
|
||||
if (cancelledRef.current || done) return
|
||||
console.error("[轮询出错] taskId:", taskId, pollErr)
|
||||
const status = axios.isAxiosError(pollErr) ? pollErr.response?.status : undefined
|
||||
if (status && status >= 400 && status < 500) {
|
||||
done = true
|
||||
const msg = extractErrorMessage(pollErr, status)
|
||||
callbacks?.onTaskFailed?.(msg)
|
||||
reject(new Error(msg))
|
||||
return
|
||||
}
|
||||
consecutiveErrors += 1
|
||||
if (consecutiveErrors >= MAX_RETRYABLE_ERRORS) {
|
||||
done = true
|
||||
const msg = "任务状态查询连续失败,请稍后在任务列表查看结果"
|
||||
callbacks?.onTaskFailed?.(msg)
|
||||
reject(new Error(msg))
|
||||
return
|
||||
}
|
||||
const timer = setTimeout(poll, 3000)
|
||||
progressTimer.current.push(timer)
|
||||
}
|
||||
}
|
||||
|
||||
const timer = setTimeout(poll, 1500)
|
||||
progressTimer.current.push(timer)
|
||||
})
|
||||
},
|
||||
[onProgress, fetchResultsWithRetry],
|
||||
)
|
||||
|
||||
/** 单任务轮询(单视频,兼容旧调用) */
|
||||
const startPolling = useCallback(
|
||||
(taskId: string) => {
|
||||
cancelledRef.current = false
|
||||
batchContextRef.current.clear()
|
||||
pollSingleTask(taskId, 0)
|
||||
.then((videos) => {
|
||||
if (cancelledRef.current) return
|
||||
onProgress(100)
|
||||
onComplete(videos)
|
||||
message.success("视频生成完成!")
|
||||
})
|
||||
.catch((err: Error) => {
|
||||
if (cancelledRef.current) return
|
||||
console.error("[生成失败] taskId:", taskId, err.message)
|
||||
onFailed(err.message)
|
||||
message.error(err.message)
|
||||
})
|
||||
},
|
||||
[pollSingleTask, onProgress, onComplete, onFailed],
|
||||
)
|
||||
let consecutiveErrors = 0
|
||||
|
||||
/**
|
||||
* 批量多任务轮询:
|
||||
* - 每个任务独立进度/状态回传 onBatchTaskUpdate
|
||||
* * 全部完成后按变体顺序聚合视频 onComplete
|
||||
* - 部分失败:整体不 onFailed(第5步逐卡片展示失败+重试按钮);全部失败才 onFailed
|
||||
*/
|
||||
const startPollingBatch = useCallback(
|
||||
(tasks: { taskId: string; variantIndex: number }[]) => {
|
||||
cancelledRef.current = false
|
||||
const runId = Date.now()
|
||||
const progressMap = new Map<string, number>()
|
||||
const resultMap = new Map<string, unknown[]>()
|
||||
const failureMap = new Map<string, string>()
|
||||
batchContextRef.current = new Map(tasks.map((t) => [t.taskId, t.variantIndex]))
|
||||
|
||||
const reportAggregateProgress = () => {
|
||||
const poll = async () => {
|
||||
if (cancelledRef.current) return
|
||||
const values = tasks.map((t) => progressMap.get(t.taskId) ?? 0)
|
||||
const avg = Math.round(values.reduce((a, b) => a + b, 0) / Math.max(values.length, 1))
|
||||
onProgress(Math.min(avg, 99))
|
||||
}
|
||||
try {
|
||||
const task = await getGenerationTask(taskId)
|
||||
consecutiveErrors = 0
|
||||
|
||||
const checkAllSettled = () => {
|
||||
if (resultMap.size + failureMap.size < tasks.length) return
|
||||
if (resultMap.size === tasks.length) {
|
||||
onProgress(100)
|
||||
const ordered = tasks.map((t) => resultMap.get(t.taskId) || []).flat()
|
||||
onComplete(ordered)
|
||||
message.success(`全部 ${tasks.length} 个视频生成完成!`)
|
||||
} else if (resultMap.size > 0) {
|
||||
// 部分失败:成功的视频聚合进成片列表(可进封面),失败卡片带重试按钮
|
||||
onProgress(100)
|
||||
const ordered = tasks
|
||||
.filter((t) => resultMap.has(t.taskId))
|
||||
.map((t) => resultMap.get(t.taskId) || [])
|
||||
.flat()
|
||||
onComplete(ordered)
|
||||
message.warning(
|
||||
`${failureMap.size} 个视频生成失败,可点击卡片上的「重试此视频」,成功的视频可先进入下一步`,
|
||||
)
|
||||
} else {
|
||||
const firstMsg = failureMap.get(tasks[0].taskId) || "全部视频生成失败"
|
||||
onFailed(firstMsg)
|
||||
if (task.status === "completed") {
|
||||
onProgress(100)
|
||||
const videos = await fetchResultsWithRetry(taskId)
|
||||
if (cancelledRef.current) return
|
||||
if (videos === null) {
|
||||
const errorMsg = "视频已生成,但获取结果列表失败,请稍后在任务列表查看"
|
||||
console.error("[生成结果获取失败] taskId:", taskId)
|
||||
onFailed(errorMsg)
|
||||
message.error(errorMsg)
|
||||
return
|
||||
}
|
||||
onComplete(videos)
|
||||
message.success("视频生成完成!")
|
||||
return
|
||||
}
|
||||
|
||||
if (task.status === "failed" || task.status === "cancelled") {
|
||||
const rawMsg =
|
||||
task.error_info?.error_message ||
|
||||
task.error_message ||
|
||||
(task.status === "cancelled" ? "任务已取消" : "视频生成失败,请联系管理员或重试")
|
||||
const errorMsg = safeExtractError(rawMsg)
|
||||
console.error("[生成失败] taskId:", taskId, "响应:", task)
|
||||
onFailed(errorMsg)
|
||||
message.error(errorMsg)
|
||||
return
|
||||
}
|
||||
|
||||
// pending / waiting / running — 继续轮询
|
||||
const pct = Math.max(0, Math.min(99, Math.round(Number(task.progress) || 0)))
|
||||
onProgress(pct)
|
||||
progressTimer.current = setTimeout(poll, 2000)
|
||||
} catch (pollErr) {
|
||||
if (cancelledRef.current) return
|
||||
console.error("[轮询出错] taskId:", taskId, pollErr)
|
||||
|
||||
// 4xx 不可恢复,立即失败
|
||||
const status = axios.isAxiosError(pollErr) ? pollErr.response?.status : undefined
|
||||
if (status && status >= 400 && status < 500) {
|
||||
const msg =
|
||||
(axios.isAxiosError(pollErr) &&
|
||||
(pollErr.response?.data as { detail?: string; message?: string } | undefined)
|
||||
?.detail) ||
|
||||
(axios.isAxiosError(pollErr) &&
|
||||
(pollErr.response?.data as { detail?: string; message?: string } | undefined)
|
||||
?.message) ||
|
||||
`查询任务失败 (${status})`
|
||||
const errorMsg = safeExtractError(msg)
|
||||
onFailed(errorMsg)
|
||||
message.error(errorMsg)
|
||||
return
|
||||
}
|
||||
|
||||
consecutiveErrors += 1
|
||||
if (consecutiveErrors >= MAX_RETRYABLE_ERRORS) {
|
||||
const errorMsg = "任务状态查询连续失败,请稍后在任务列表查看结果"
|
||||
onFailed(errorMsg)
|
||||
message.error(errorMsg)
|
||||
return
|
||||
}
|
||||
progressTimer.current = setTimeout(poll, 3000)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.forEach(({ taskId, variantIndex }) => {
|
||||
onBatchTaskUpdate?.(taskId, {
|
||||
taskId,
|
||||
variantIndex,
|
||||
status: "running",
|
||||
progress: 0,
|
||||
error: null,
|
||||
videos: [],
|
||||
})
|
||||
pollSingleTask(taskId, runId, {
|
||||
onTaskProgress: (pct) => {
|
||||
progressMap.set(taskId, pct)
|
||||
onBatchTaskUpdate?.(taskId, { status: "running", progress: pct })
|
||||
reportAggregateProgress()
|
||||
},
|
||||
onTaskCompleted: (videos) => {
|
||||
progressMap.set(taskId, 100)
|
||||
resultMap.set(taskId, videos)
|
||||
onBatchTaskUpdate?.(taskId, { status: "completed", progress: 100, videos })
|
||||
reportAggregateProgress()
|
||||
checkAllSettled()
|
||||
},
|
||||
onTaskFailed: (msg) => {
|
||||
failureMap.set(taskId, msg)
|
||||
onBatchTaskUpdate?.(taskId, { status: "failed", error: msg })
|
||||
checkAllSettled()
|
||||
},
|
||||
}).catch(() => {
|
||||
// 失败已在 onTaskFailed 处理,这里吞掉 Promise rejection
|
||||
})
|
||||
})
|
||||
progressTimer.current = setTimeout(poll, 1500)
|
||||
},
|
||||
[pollSingleTask, onProgress, onComplete, onFailed, onBatchTaskUpdate],
|
||||
[onProgress, onComplete, onFailed, fetchResultsWithRetry],
|
||||
)
|
||||
|
||||
/** 单独重试失败任务(第5步卡片「重试此视频」):先调后端重试接口,再轮询 */
|
||||
const retryTask = useCallback(
|
||||
async (taskId: string) => {
|
||||
if (cancelledRef.current) cancelledRef.current = false
|
||||
const variantIndex = batchContextRef.current.get(taskId) ?? 0
|
||||
onBatchTaskUpdate?.(taskId, { status: "running", progress: 0, error: null, videos: [] })
|
||||
try {
|
||||
await retryGenerationTaskApi(taskId)
|
||||
} catch (err) {
|
||||
// 后端不支持重试或任务不可重试:直接重新轮询(任务可能已被自动恢复)
|
||||
console.warn("[重试任务接口调用失败,改为直接轮询]", err)
|
||||
}
|
||||
pollSingleTask(taskId, Date.now(), {
|
||||
onTaskProgress: (pct) => onBatchTaskUpdate?.(taskId, { status: "running", progress: pct }),
|
||||
onTaskCompleted: (videos) => {
|
||||
onBatchTaskUpdate?.(taskId, { status: "completed", progress: 100, videos })
|
||||
message.success(`视频 ${variantIndex + 1} 重试成功`)
|
||||
},
|
||||
onTaskFailed: (msg) => onBatchTaskUpdate?.(taskId, { status: "failed", error: msg }),
|
||||
}).catch(() => {
|
||||
/* 失败已在回调处理 */
|
||||
})
|
||||
forceTick((n) => n + 1)
|
||||
return variantIndex
|
||||
},
|
||||
[pollSingleTask, onBatchTaskUpdate],
|
||||
)
|
||||
|
||||
return { startPolling, startPollingBatch, retryTask, clearTimer }
|
||||
return { startPolling, clearTimer }
|
||||
}
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
/**
|
||||
* 批量封面 Hook(Issue #1677)
|
||||
* N 个视频时:逐个自动生成封面(从对应成片抽帧 + 叠加对应标题)或上传自定义封面
|
||||
*/
|
||||
import { useCallback, useState } from "react"
|
||||
import { message } from "antd"
|
||||
import { generateCover } from "@/api/generation"
|
||||
import { uploadAssetDirect, getAssetLibraries } from "@/api/assets"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
|
||||
interface UseBatchCoversOptions {
|
||||
selectedTemplate: string
|
||||
generatedVideos: GeneratedVideo[]
|
||||
/** 每个变体的标题文字 */
|
||||
titles: string[]
|
||||
/** 标题样式(全局共用) */
|
||||
titleStyle: {
|
||||
font: string
|
||||
size: number
|
||||
color: string
|
||||
position: string
|
||||
bold: boolean
|
||||
stroke: boolean
|
||||
shadow: boolean
|
||||
}
|
||||
covers: string[]
|
||||
onCoversChange: (urls: string[]) => void
|
||||
}
|
||||
|
||||
export function useBatchCovers({
|
||||
selectedTemplate,
|
||||
generatedVideos,
|
||||
titles,
|
||||
titleStyle,
|
||||
covers,
|
||||
onCoversChange,
|
||||
}: UseBatchCoversOptions) {
|
||||
const [loadingIndex, setLoadingIndex] = useState<number | null>(null)
|
||||
const [uploadingIndex, setUploadingIndex] = useState<number | null>(null)
|
||||
|
||||
const patchCover = useCallback(
|
||||
(index: number, url: string) => {
|
||||
const next = [...covers]
|
||||
next[index] = url
|
||||
onCoversChange(next)
|
||||
},
|
||||
[covers, onCoversChange],
|
||||
)
|
||||
|
||||
/** 为第 index 个视频自动生成封面 */
|
||||
const generateOne = useCallback(
|
||||
async (index: number) => {
|
||||
const finalVideos = generatedVideos.filter((v) => v.status === "completed")
|
||||
const target = finalVideos[index] || generatedVideos[index]
|
||||
if (!target) {
|
||||
message.warning("该视频尚未生成完成")
|
||||
return
|
||||
}
|
||||
setLoadingIndex(index)
|
||||
try {
|
||||
const titleText = titles[index] || ""
|
||||
const response = await generateCover(selectedTemplate, {
|
||||
generated_video_id: target.id,
|
||||
video_url: target.file_url || target.download_url || "",
|
||||
cover_type: "ai_frame",
|
||||
...(titleText
|
||||
? {
|
||||
title_config: {
|
||||
text: titleText,
|
||||
font: titleStyle.font,
|
||||
font_size: titleStyle.size,
|
||||
font_color: titleStyle.color,
|
||||
position: titleStyle.position,
|
||||
bold: titleStyle.bold,
|
||||
stroke: titleStyle.stroke,
|
||||
shadow: titleStyle.shadow,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
})
|
||||
const url = response.cover?.image_url || response.cover?.thumbnail_url || ""
|
||||
if (url) {
|
||||
patchCover(index, url)
|
||||
message.success(`视频 ${index + 1} 封面生成成功`)
|
||||
} else {
|
||||
message.warning(`视频 ${index + 1} 封面生成未返回图片,请重试`)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[封面] 视频 ${index + 1} 生成失败:`, err)
|
||||
message.error(`视频 ${index + 1} 封面生成失败,请重试`)
|
||||
} finally {
|
||||
setLoadingIndex(null)
|
||||
}
|
||||
},
|
||||
[generatedVideos, titles, titleStyle, selectedTemplate, patchCover],
|
||||
)
|
||||
|
||||
/** 为第 index 个视频上传自定义封面 */
|
||||
const uploadOne = useCallback(
|
||||
async (index: number, file: File) => {
|
||||
setUploadingIndex(index)
|
||||
try {
|
||||
const libs = await getAssetLibraries()
|
||||
const imageLib = libs.find((l) => l.kind === "image") || libs[0]
|
||||
if (!imageLib) {
|
||||
message.error("未找到素材库,请先创建")
|
||||
return
|
||||
}
|
||||
const result = await uploadAssetDirect({
|
||||
file,
|
||||
library_id: imageLib.id,
|
||||
})
|
||||
const url = result?.url || ""
|
||||
if (url) {
|
||||
patchCover(index, url)
|
||||
message.success(`视频 ${index + 1} 封面已上传`)
|
||||
} else {
|
||||
message.warning("上传完成但未获取到图片URL,请重试")
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[封面] 视频 ${index + 1} 上传失败:`, err)
|
||||
message.error("封面上传失败,请重试")
|
||||
} finally {
|
||||
setUploadingIndex(null)
|
||||
}
|
||||
},
|
||||
[patchCover],
|
||||
)
|
||||
|
||||
/** 一键全部自动生成(串行,避免队列限流) */
|
||||
const generateAll = useCallback(async () => {
|
||||
const finalVideos = generatedVideos.filter((v) => v.status === "completed")
|
||||
for (let i = 0; i < finalVideos.length; i++) {
|
||||
if (covers[i]) continue // 已有封面跳过
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await generateOne(i)
|
||||
}
|
||||
message.success("全部封面已生成")
|
||||
}, [generatedVideos, covers, generateOne])
|
||||
|
||||
return {
|
||||
loadingIndex,
|
||||
uploadingIndex,
|
||||
generateOne,
|
||||
uploadOne,
|
||||
generateAll,
|
||||
}
|
||||
}
|
||||
|
||||
export default useBatchCovers
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* GeneratePage 表单状态管理
|
||||
* 集中管理 5 步向导的所有共享状态、API 加载、URL 参数解析
|
||||
* 集中管理 7 步向导的所有共享状态、API 加载、URL 参数解析
|
||||
*/
|
||||
import { useState } from "react"
|
||||
import { useSearchParams } from "react-router-dom"
|
||||
@@ -100,26 +100,6 @@ export interface GenerateFormState {
|
||||
/** 从预览响应中提取的 source_edit_plan_id(供 fallback 路径使用) */
|
||||
storedSourceEditPlanId: string | null
|
||||
setStoredSourceEditPlanId: (planId: string | null) => void
|
||||
|
||||
/* ── 批量生成(Issue #1677)── */
|
||||
/** 生成数量(1~10),1=单条旧逻辑 */
|
||||
previewCount: number
|
||||
setPreviewCount: (n: number) => void
|
||||
/** 每个变体的标题文字,长度=previewCount;[0] 与 titleSettings.title 保持同步 */
|
||||
previewTitles: string[]
|
||||
setPreviewTitles: (titles: string[] | ((prev: string[]) => string[])) => void
|
||||
/** false=所有视频共用一个配音;true=每个视频独立配音 */
|
||||
voiceModePerVideo: boolean
|
||||
setVoiceModePerVideo: (v: boolean) => void
|
||||
/** 独立配音模式下每个变体的配音素材ID,长度=previewCount */
|
||||
voiceLibraryIds: string[]
|
||||
setVoiceLibraryIds: (ids: string[] | ((prev: string[]) => string[])) => void
|
||||
/** 每个变体的封面URL(自动生成或上传),长度=previewCount,空串=未设置 */
|
||||
previewCovers: string[]
|
||||
setPreviewCovers: (urls: string[] | ((prev: string[]) => string[])) => void
|
||||
/** 确认生成时勾选的变体索引 */
|
||||
selectedVariantIds: number[]
|
||||
setSelectedVariantIds: (ids: number[] | ((prev: number[]) => number[])) => void
|
||||
}
|
||||
|
||||
export const useGenerateFormState = (): GenerateFormState => {
|
||||
@@ -205,14 +185,6 @@ export const useGenerateFormState = (): GenerateFormState => {
|
||||
null,
|
||||
)
|
||||
|
||||
/* ── 批量生成状态(Issue #1677)── */
|
||||
const [previewCount, setPreviewCount] = useState(1)
|
||||
const [previewTitles, setPreviewTitles] = useState<string[]>([""])
|
||||
const [voiceModePerVideo, setVoiceModePerVideo] = useState(false)
|
||||
const [voiceLibraryIds, setVoiceLibraryIds] = useState<string[]>([""])
|
||||
const [previewCovers, setPreviewCovers] = useState<string[]>([""])
|
||||
const [selectedVariantIds, setSelectedVariantIds] = useState<number[]>([0])
|
||||
|
||||
/* ── 从 URL / 编辑计划加载配置 ── */
|
||||
usePlanConfigLoader({
|
||||
editPlanId,
|
||||
@@ -261,17 +233,5 @@ export const useGenerateFormState = (): GenerateFormState => {
|
||||
setPreviewTaskId,
|
||||
storedSourceEditPlanId,
|
||||
setStoredSourceEditPlanId,
|
||||
previewCount,
|
||||
setPreviewCount,
|
||||
previewTitles,
|
||||
setPreviewTitles,
|
||||
voiceModePerVideo,
|
||||
setVoiceModePerVideo,
|
||||
voiceLibraryIds,
|
||||
setVoiceLibraryIds,
|
||||
previewCovers,
|
||||
setPreviewCovers,
|
||||
selectedVariantIds,
|
||||
setSelectedVariantIds,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
* 视频生成 Hook
|
||||
* 封装视频生成的核心逻辑、状态管理、轮询等
|
||||
*/
|
||||
import { useState, useCallback, useEffect } from "react"
|
||||
import { useState, useCallback } from "react"
|
||||
import { message } from "antd"
|
||||
import { type GeneratedVideo, getEditPlanClips, createClipsFromAssets } from "@/api/template-editor"
|
||||
import { createGenerationTask } from "@/api/tasks/tasks"
|
||||
import type { UseGenerateVideoProps } from "./generate-video/types"
|
||||
import { getGenerationPhase } from "./generate-video/phase"
|
||||
import { useGenerationPolling, type BatchTaskState } from "./generate-video/useGenerationPolling"
|
||||
import { useGenerationPolling } from "./generate-video/useGenerationPolling"
|
||||
import { validateGenerateInputs } from "./generate-video/buildPayload"
|
||||
import { calculateResolution } from "../utils/calculateResolution"
|
||||
import { extractBackendError, translateError } from "./generate-video/errorUtils"
|
||||
@@ -22,32 +22,6 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
const [generated, setGenerated] = useState(false)
|
||||
const [generateError, setGenerateError] = useState<string | null>(null)
|
||||
const [generatedVideos, setGeneratedVideos] = useState<GeneratedVideo[]>([])
|
||||
/** 批量模式:每个正式生成任务的独立状态(第5步逐卡片展示) */
|
||||
const [batchTasks, setBatchTasks] = useState<BatchTaskState[]>([])
|
||||
|
||||
const handleBatchTaskUpdate = useCallback((taskId: string, patch: Partial<BatchTaskState>) => {
|
||||
setBatchTasks((prev) => {
|
||||
const list = prev || []
|
||||
const idx = list.findIndex((t) => t.taskId === taskId)
|
||||
if (idx === -1) {
|
||||
return [
|
||||
...list,
|
||||
{
|
||||
taskId,
|
||||
variantIndex: patch.variantIndex ?? 0,
|
||||
status: "running",
|
||||
progress: 0,
|
||||
error: null,
|
||||
videos: [],
|
||||
...patch,
|
||||
},
|
||||
]
|
||||
}
|
||||
const next = [...list]
|
||||
next[idx] = { ...next[idx], ...patch }
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
|
||||
const handleProgress = useCallback((p: number) => setProgress(p), [])
|
||||
const handleComplete = useCallback(
|
||||
@@ -55,19 +29,6 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
setGenerating(false)
|
||||
setGenerated(true)
|
||||
setGeneratedVideos(videos as GeneratedVideo[])
|
||||
// 批量:成功任务的 videos 已通过 onBatchTaskUpdate 写入,这里同步兜底
|
||||
setBatchTasks((prev) =>
|
||||
(prev || []).map((t) =>
|
||||
t.status === "completed" && t.videos.length === 0
|
||||
? {
|
||||
...t,
|
||||
videos: (videos as GeneratedVideo[]).filter(
|
||||
(v) => v.generation_task_id === t.taskId,
|
||||
),
|
||||
}
|
||||
: t,
|
||||
),
|
||||
)
|
||||
onGenerationSuccess?.()
|
||||
},
|
||||
[onGenerationSuccess],
|
||||
@@ -77,30 +38,10 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
setGenerateError(errorMsg)
|
||||
}, [])
|
||||
|
||||
/* 批量:任务状态变化时聚合已完成成片(含失败重试成功后补入),
|
||||
按变体索引排序,供步骤6封面按勾选顺序逐个取视频 */
|
||||
useEffect(() => {
|
||||
if (batchTasks.length === 0) return
|
||||
const byVariant = new Map<number, GeneratedVideo>()
|
||||
batchTasks.forEach((t) => {
|
||||
if (t.status === "completed" && t.videos && t.videos.length > 0) {
|
||||
byVariant.set(t.variantIndex, t.videos[0] as GeneratedVideo)
|
||||
}
|
||||
})
|
||||
const ordered = [...byVariant.entries()].sort((a, b) => a[0] - b[0]).map(([, v]) => v)
|
||||
setGeneratedVideos((prev) => {
|
||||
if (prev.length === ordered.length && prev.every((v, i) => v.id === ordered[i].id)) {
|
||||
return prev
|
||||
}
|
||||
return ordered
|
||||
})
|
||||
}, [batchTasks])
|
||||
|
||||
const { startPolling, startPollingBatch, retryTask, clearTimer } = useGenerationPolling({
|
||||
const { startPolling, clearTimer } = useGenerationPolling({
|
||||
onProgress: handleProgress,
|
||||
onComplete: handleComplete,
|
||||
onFailed: handleFailed,
|
||||
onBatchTaskUpdate: handleBatchTaskUpdate,
|
||||
})
|
||||
|
||||
/* ── 生成视频 ──
|
||||
@@ -116,7 +57,6 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
setProgress(0)
|
||||
setGenerated(false)
|
||||
setGenerateError(null)
|
||||
setBatchTasks([])
|
||||
clearTimer()
|
||||
|
||||
try {
|
||||
@@ -143,11 +83,7 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
}
|
||||
}
|
||||
|
||||
const isBatch = (props.previewCount || 1) > 1
|
||||
const hide = message.loading(
|
||||
isBatch ? `正在生成 ${props.previewCount} 个视频...` : "正在生成预览视频...",
|
||||
0,
|
||||
)
|
||||
const hide = message.loading("正在生成预览视频...", 0)
|
||||
|
||||
const coverUrl = props.coverSettings?.thumbnail_url || props.coverSettings?.upload_url || ""
|
||||
|
||||
@@ -156,29 +92,6 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
? props.selectedClonedVoice || props.selectedVoice || ""
|
||||
: props.selectedVoice || ""
|
||||
|
||||
/* ── 批量变体数组(长度1=共用,长度=count=独立,空=回退单值) ── */
|
||||
const indexes =
|
||||
isBatch && props.selectedVariantIndexes?.length
|
||||
? props.selectedVariantIndexes
|
||||
: Array.from({ length: props.previewCount || 1 }, (_, i) => i)
|
||||
const batchCount = isBatch ? indexes.length : 1
|
||||
|
||||
// 标题文字数组:批量时按勾选顺序
|
||||
const titlesArr =
|
||||
isBatch && (props.variantTitles?.length || 0) >= batchCount
|
||||
? indexes.map((i) => props.variantTitles![i] || props.titleSettings?.title || "")
|
||||
: []
|
||||
// 配音数组:独立配音模式按勾选顺序;否则不传(回退共用 voice_library_id)
|
||||
const voiceArr =
|
||||
isBatch && props.voiceModePerVideo && props.variantVoiceLibraryIds?.length
|
||||
? indexes.map((i) => props.variantVoiceLibraryIds![i] || voiceLibraryId)
|
||||
: []
|
||||
// 封面数组:批量时按勾选顺序(未设置封面的变体传空串,后端回退智能封面)
|
||||
const coversArr =
|
||||
isBatch && props.variantCoverUrls?.length
|
||||
? indexes.map((i) => props.variantCoverUrls![i] || "")
|
||||
: []
|
||||
|
||||
try {
|
||||
const taskResp = await createGenerationTask({
|
||||
template_id: selectedTemplate,
|
||||
@@ -196,10 +109,6 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
...(props.bgmConfig?.music_id ? { preset_id: props.bgmConfig.music_id } : {}),
|
||||
},
|
||||
...(props.sourceEditPlanId ? { source_edit_plan_id: props.sourceEditPlanId } : {}),
|
||||
...(isBatch ? { count: batchCount } : {}),
|
||||
...(titlesArr.length ? { titles: titlesArr } : {}),
|
||||
...(voiceArr.length ? { voice_library_ids: voiceArr } : {}),
|
||||
...(coversArr.length ? { cover_urls: coversArr } : {}),
|
||||
...(props.titleSettings?.title
|
||||
? {
|
||||
title_config: {
|
||||
@@ -224,17 +133,12 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
: {}),
|
||||
})
|
||||
hide()
|
||||
const taskIds = (taskResp.items || []).map((it) => it.id).filter(Boolean)
|
||||
const taskId = taskResp.items?.[0]?.id
|
||||
|
||||
if (taskIds.length === 0) {
|
||||
if (!taskId) {
|
||||
throw new Error("创建任务成功但未返回任务 ID,请稍后在任务列表查看")
|
||||
}
|
||||
if (taskIds.length > 1) {
|
||||
// 批量:任务按创建顺序与勾选变体一一对应(后端按 count 顺序创建)
|
||||
startPollingBatch(taskIds.map((taskId, i) => ({ taskId, variantIndex: indexes[i] ?? i })))
|
||||
} else {
|
||||
startPolling(taskIds[0])
|
||||
}
|
||||
startPolling(taskId)
|
||||
} catch (err) {
|
||||
hide()
|
||||
throw err
|
||||
@@ -250,21 +154,13 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}, [props, clearTimer, startPolling, startPollingBatch, selectedTemplate])
|
||||
}, [props, clearTimer, startPolling, selectedTemplate])
|
||||
|
||||
const retry = useCallback(() => {
|
||||
setGenerateError(null)
|
||||
generate()
|
||||
}, [generate])
|
||||
|
||||
/** 第5步:单独重试某个失败任务 */
|
||||
const retryBatchTask = useCallback(
|
||||
(taskId: string) => {
|
||||
retryTask(taskId)
|
||||
},
|
||||
[retryTask],
|
||||
)
|
||||
|
||||
const dismissError = useCallback(() => {
|
||||
setGenerateError(null)
|
||||
}, [])
|
||||
@@ -309,8 +205,6 @@ export function useGenerateVideo(props: UseGenerateVideoProps) {
|
||||
generatedVideos,
|
||||
generate,
|
||||
retry,
|
||||
retryBatchTask,
|
||||
batchTasks,
|
||||
dismissError,
|
||||
download,
|
||||
share,
|
||||
|
||||
@@ -114,11 +114,8 @@ export function useServerPreview({
|
||||
const resp = await createPreview(request)
|
||||
if (seq !== requestSeqRef.current || !mountedRef.current) return
|
||||
|
||||
// 兼容批量响应 {items, total}:取第一个变体
|
||||
const firstTask = resp.items?.[0]
|
||||
const taskId = firstTask?.task_id || ""
|
||||
setTaskId(taskId)
|
||||
onCreatedRef.current?.(taskId, resp.source_edit_plan_id)
|
||||
setTaskId(resp.task_id)
|
||||
onCreatedRef.current?.(resp.task_id, resp.source_edit_plan_id)
|
||||
|
||||
let completed = false
|
||||
|
||||
@@ -135,7 +132,7 @@ export function useServerPreview({
|
||||
if (completed || seq !== requestSeqRef.current || !mountedRef.current) return
|
||||
|
||||
try {
|
||||
const st = await getPreviewStatus(taskId)
|
||||
const st = await getPreviewStatus(resp.task_id)
|
||||
if (completed || seq !== requestSeqRef.current || !mountedRef.current) return
|
||||
|
||||
if (st.status === "completed" && st.video_url) {
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Step 7 确认生成 Hook
|
||||
* 封装生成确认页的展示逻辑
|
||||
*/
|
||||
import { useMemo } from "react"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import type { EditingTemplate } from "@/api/editing-planner"
|
||||
import type { GeneratedVideo } from "@/api/template-editor"
|
||||
import type { CoverConfig } from "../types/cover"
|
||||
import type { VoiceClone } from "@/api/voice-clone"
|
||||
import type { PresetVoiceItem } from "@/api/voices"
|
||||
import { getAssetsByKind } from "@/api/assets"
|
||||
import { COVER_MODE_LABELS } from "../constants"
|
||||
|
||||
interface UseStep7GenerateProps {
|
||||
templates: EditingTemplate[]
|
||||
selectedTemplate: string
|
||||
materialMode: "manual" | "auto"
|
||||
selectedMaterials: string[]
|
||||
smartSelectedIds: string[]
|
||||
title: string
|
||||
voiceMode: "preset" | "custom" | "clone"
|
||||
selectedVoice: string
|
||||
selectedClonedVoice: string
|
||||
presetVoices: PresetVoiceItem[]
|
||||
clonedVoices: VoiceClone[]
|
||||
coverSettings: CoverConfig
|
||||
generating: boolean
|
||||
generated: boolean
|
||||
generateError: string | null
|
||||
progress: number
|
||||
generatedVideos: GeneratedVideo[]
|
||||
}
|
||||
|
||||
export function useStep7Generate({
|
||||
templates,
|
||||
selectedTemplate,
|
||||
materialMode,
|
||||
selectedMaterials,
|
||||
smartSelectedIds,
|
||||
title,
|
||||
voiceMode: _voiceMode,
|
||||
selectedVoice,
|
||||
selectedClonedVoice: _selectedClonedVoice,
|
||||
presetVoices: _presetVoices,
|
||||
clonedVoices: _clonedVoices,
|
||||
coverSettings,
|
||||
generating,
|
||||
generated,
|
||||
generateError,
|
||||
progress,
|
||||
generatedVideos,
|
||||
}: UseStep7GenerateProps) {
|
||||
const templateName = useMemo(
|
||||
() => templates.find((t) => t.id === selectedTemplate)?.name ?? "未选择",
|
||||
[templates, selectedTemplate],
|
||||
)
|
||||
|
||||
const materialSummary = useMemo(() => {
|
||||
if (materialMode === "auto") {
|
||||
return `${smartSelectedIds.length} 个素材(智能匹配)`
|
||||
}
|
||||
return `${selectedMaterials.length} 个素材`
|
||||
}, [materialMode, selectedMaterials.length, smartSelectedIds.length])
|
||||
|
||||
// 从配音素材库中查找 voiceName
|
||||
const { data: voiceMaterials = [] } = useQuery({
|
||||
queryKey: ["assets", "voice"],
|
||||
queryFn: () => getAssetsByKind("voice", { limit: 50 }),
|
||||
})
|
||||
|
||||
const voiceName = useMemo(() => {
|
||||
const asset = voiceMaterials.find((v) => v.id === selectedVoice)
|
||||
return asset ? asset.name : "未选择"
|
||||
}, [voiceMaterials, selectedVoice])
|
||||
|
||||
const coverSummary = useMemo(() => {
|
||||
if (!coverSettings.enabled) return "不使用"
|
||||
return COVER_MODE_LABELS[coverSettings.mode] || "智能封面"
|
||||
}, [coverSettings])
|
||||
|
||||
const getGenerationPhase = (p: number) => {
|
||||
if (p < 20) return { label: "分析素材与配置", icon: "🔍" }
|
||||
if (p < 50) return { label: "智能剪辑合成", icon: "🎬" }
|
||||
if (p < 80) return { label: "渲染视频中", icon: "⚡" }
|
||||
return { label: "即将完成", icon: "✨" }
|
||||
}
|
||||
|
||||
const handleScrollToPreview = () => {
|
||||
const el =
|
||||
document.querySelector(".xx-inline-video-player") ||
|
||||
document.querySelector(".xx-preview-section")
|
||||
el?.scrollIntoView({ behavior: "smooth", block: "start" })
|
||||
}
|
||||
|
||||
return {
|
||||
templateName,
|
||||
materialSummary,
|
||||
title,
|
||||
voiceName,
|
||||
coverSummary,
|
||||
generating,
|
||||
generated,
|
||||
generateError,
|
||||
progress,
|
||||
generatedVideos,
|
||||
getGenerationPhase,
|
||||
handleScrollToPreview,
|
||||
}
|
||||
}
|
||||
|
||||
export default useStep7Generate
|
||||
@@ -1,11 +1,6 @@
|
||||
/**
|
||||
* GeneratePage 步骤导航(Issue #1677 修正:固定 6 步,单视频与批量一致)
|
||||
* 步骤:模板(1) → 素材(2) → 配音(3) → 标题(4) → 确认生成(5) → 封面(6)
|
||||
*
|
||||
* - 步骤4底部按钮是「确认生成视频/确认生成 N 个视频」(由 GenerateStepActions 调
|
||||
* onConfirmGenerate),创建成功后跳转步骤5;本 hook 的 goNext 只负责 1→2→3→4
|
||||
* 和 5→6 的「下一步」。
|
||||
* - 步骤5(确认生成进度页):渲染全部完成(generated)后「下一步」解锁进封面。
|
||||
* GeneratePage 步骤导航
|
||||
* 步骤顺序(6步):模板(1) → 素材(2) → 配音(3) → 标题(4) → 确认生成(5) → 封面(6)
|
||||
*/
|
||||
import { message } from "antd"
|
||||
import type { TitleSettings } from "../types"
|
||||
@@ -18,10 +13,10 @@ export interface UseStepNavigationOptions {
|
||||
selectedMaterials: string[]
|
||||
smartSelectedIds: string[]
|
||||
titleSettings: TitleSettings
|
||||
/** 是否已完成视频生成(步骤5全部渲染完成后才能进入封面) */
|
||||
/** 预览是否已就绪(素材已加载,可播放) */
|
||||
previewReady: boolean
|
||||
/** 是否已完成视频生成(步骤5确认生成后才能进入封面) */
|
||||
generated: boolean
|
||||
/** Step1 点下一步时弹出数量选择弹窗 */
|
||||
onOpenCountModal: () => void
|
||||
}
|
||||
|
||||
export interface UseStepNavigationReturn {
|
||||
@@ -37,18 +32,14 @@ export const useStepNavigation = (options: UseStepNavigationOptions): UseStepNav
|
||||
materialMode,
|
||||
selectedMaterials,
|
||||
smartSelectedIds,
|
||||
titleSettings,
|
||||
previewReady,
|
||||
generated,
|
||||
onOpenCountModal,
|
||||
} = options
|
||||
|
||||
const goNext = () => {
|
||||
if (currentStep === 1) {
|
||||
if (!selectedTemplate) {
|
||||
message.warning("请先选择一个模板")
|
||||
return
|
||||
}
|
||||
// 选完模板弹数量选择弹窗(每次都弹,不记忆)
|
||||
onOpenCountModal()
|
||||
if (currentStep === 1 && !selectedTemplate) {
|
||||
message.warning("请先选择一个模板")
|
||||
return
|
||||
}
|
||||
if (currentStep === 2 && materialMode === "manual" && selectedMaterials.length === 0) {
|
||||
@@ -59,12 +50,21 @@ export const useStepNavigation = (options: UseStepNavigationOptions): UseStepNav
|
||||
message.warning("请先进行智能匹配并选择素材")
|
||||
return
|
||||
}
|
||||
// 步骤5(确认生成):全部渲染完成后才能下一步进封面
|
||||
if (currentStep === 5) {
|
||||
if (!generated) {
|
||||
message.warning("视频还在渲染中,请等待生成完成")
|
||||
// Step4(标题+预览):标题必填 + 预览必须已加载
|
||||
if (currentStep === 4) {
|
||||
if (!titleSettings.title.trim()) {
|
||||
message.warning("请选择或输入标题")
|
||||
return
|
||||
}
|
||||
if (!previewReady) {
|
||||
message.warning("预览视频正在加载,请稍候")
|
||||
return
|
||||
}
|
||||
}
|
||||
// Step5(确认生成):必须已完成生成才能进入封面
|
||||
if (currentStep === 5 && !generated) {
|
||||
message.warning("请先生成视频")
|
||||
return
|
||||
}
|
||||
if (currentStep < 6) {
|
||||
setCurrentStep((s) => s + 1)
|
||||
|
||||
@@ -35,13 +35,7 @@ export const useTaskHistory = () => {
|
||||
} = useQuery<TaskItem[], Error>({
|
||||
queryKey: ["tasks"],
|
||||
queryFn: getUserTasks,
|
||||
staleTime: 5_000,
|
||||
// 有进行中任务时每 3 秒自动刷新,全部结束后停止轮询
|
||||
refetchInterval: (query) => {
|
||||
const list = query.state.data ?? []
|
||||
const hasActive = list.some((t) => ["pending", "waiting", "running"].includes(t.status))
|
||||
return hasActive ? 3_000 : false
|
||||
},
|
||||
staleTime: 30_000,
|
||||
})
|
||||
|
||||
// 重试 mutation
|
||||
|
||||
@@ -178,35 +178,3 @@
|
||||
border-color: var(--border-color);
|
||||
margin: var(--space-lg) 0;
|
||||
}
|
||||
|
||||
/* 微信账号绑定卡片 */
|
||||
.xx-settings-wechat {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-lg);
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.xx-settings-wechat-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-md);
|
||||
}
|
||||
|
||||
.xx-settings-wechat-info .xx-wechat-icon {
|
||||
font-size: 28px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.xx-settings-wechat-info strong {
|
||||
display: block;
|
||||
color: var(--text-primary);
|
||||
font-size: var(--font-size-md);
|
||||
}
|
||||
|
||||
.xx-settings-wechat-info p {
|
||||
margin: 2px 0 0;
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
|
||||
@@ -1,111 +1,39 @@
|
||||
/**
|
||||
* 个人设置页面
|
||||
* - 个人资料(昵称)保存
|
||||
* - 微信账号绑定状态 / 绑定 / 解绑
|
||||
* P1-2: 添加 PageHead
|
||||
* P1-3: antd Form/Input/Button/Alert → 自定义 UI 组件
|
||||
*/
|
||||
import React, { useEffect, useRef, useState } from "react"
|
||||
import { useSearchParams } from "react-router-dom"
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
||||
import { message } from "antd"
|
||||
import React, { useState } from "react"
|
||||
import { Button, Input, Modal } from "@/components/ui"
|
||||
import { getCurrentUser, updateProfile, getWechatBindUrl, unbindWechat } from "@/api/auth"
|
||||
import { useAuthStore } from "@/store/authStore"
|
||||
import PageHead from "@/components/layout/PageHead"
|
||||
import "./ProfileSettings.css"
|
||||
|
||||
const Settings: React.FC = () => {
|
||||
const user = useAuthStore((state) => state.user)
|
||||
const setUser = useAuthStore((state) => state.setUser)
|
||||
const queryClient = useQueryClient()
|
||||
const [searchParams, setSearchParams] = useSearchParams()
|
||||
const [displayName, setDisplayName] = useState(user?.display_name || "")
|
||||
const bindTipShownRef = useRef(false)
|
||||
|
||||
// 拉取最新用户信息(微信绑定状态以后端为准)
|
||||
const { data: freshUser } = useQuery({
|
||||
queryKey: ["currentUser"],
|
||||
queryFn: getCurrentUser,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (freshUser) {
|
||||
setUser(freshUser)
|
||||
setDisplayName((prev) => prev || freshUser.display_name || "")
|
||||
}
|
||||
}, [freshUser, setUser])
|
||||
|
||||
// 绑定回调结果提示(?wechat_bind=success|failed)
|
||||
useEffect(() => {
|
||||
if (bindTipShownRef.current) return
|
||||
const result = searchParams.get("wechat_bind")
|
||||
if (!result) return
|
||||
bindTipShownRef.current = true
|
||||
if (result === "success") {
|
||||
message.success("微信绑定成功")
|
||||
} else if (result === "failed") {
|
||||
message.error("微信绑定失败,请重试")
|
||||
}
|
||||
searchParams.delete("wechat_bind")
|
||||
setSearchParams(searchParams, { replace: true })
|
||||
}, [searchParams, setSearchParams])
|
||||
|
||||
const wechatBound = user?.wechat_bound === true
|
||||
|
||||
const saveProfileMutation = useMutation({
|
||||
mutationFn: () => updateProfile({ display_name: displayName.trim() }),
|
||||
onSuccess: (updated) => {
|
||||
setUser(updated)
|
||||
message.success("资料已保存")
|
||||
},
|
||||
onError: () => {
|
||||
message.error("保存失败,请重试")
|
||||
},
|
||||
})
|
||||
|
||||
const handleBindWechat = async () => {
|
||||
try {
|
||||
const result = await getWechatBindUrl()
|
||||
localStorage.setItem("wechat_bind_state", result.state)
|
||||
window.location.href = result.auth_url
|
||||
} catch {
|
||||
message.error("微信绑定暂不可用,请稍后重试")
|
||||
}
|
||||
}
|
||||
|
||||
const unbindMutation = useMutation({
|
||||
mutationFn: unbindWechat,
|
||||
onSuccess: () => {
|
||||
message.success("已解绑微信")
|
||||
queryClient.invalidateQueries({ queryKey: ["currentUser"] })
|
||||
// 本地立即更新,避免等待刷新
|
||||
if (user) {
|
||||
setUser({ ...user, wechat_bound: false, wechat_nickname: "" })
|
||||
}
|
||||
},
|
||||
onError: () => {
|
||||
message.error("解绑失败,请重试")
|
||||
},
|
||||
})
|
||||
|
||||
const handleUnbind = () => {
|
||||
Modal.confirm({
|
||||
title: "解绑微信",
|
||||
content: "解绑后将无法使用微信登录该账号,确定要解绑吗?",
|
||||
okText: "确定解绑",
|
||||
cancelText: "取消",
|
||||
okButtonProps: { danger: true },
|
||||
onOk: () => unbindMutation.mutateAsync(),
|
||||
const handleSave = () => {
|
||||
Modal.info({
|
||||
title: "提示",
|
||||
content: "个人资料修改接口暂未开放,保存功能即将上线。",
|
||||
})
|
||||
}
|
||||
|
||||
const displayNameDirty = displayName.trim() !== (user?.display_name || "")
|
||||
|
||||
return (
|
||||
<div className="xx-settings-page">
|
||||
<PageHead title="个人设置" description="管理您的账户信息" />
|
||||
|
||||
<div className="xx-settings-card">
|
||||
<h3>个人信息</h3>
|
||||
<div className="xx-settings-notice">
|
||||
<span className="xx-settings-notice-icon">ℹ️</span>
|
||||
<div>
|
||||
<strong>个人资料编辑暂未开放</strong>
|
||||
<p>当前仅展示登录用户信息,资料修改接口接入后再开放保存。</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="xx-settings-form">
|
||||
<div className="xx-settings-field">
|
||||
<label className="xx-settings-label">用户名</label>
|
||||
@@ -114,76 +42,25 @@ const Settings: React.FC = () => {
|
||||
|
||||
<div className="xx-settings-field">
|
||||
<label className="xx-settings-label">邮箱</label>
|
||||
<Input
|
||||
value={user?.email && !user.email.endsWith("@wechat.local") ? user.email : ""}
|
||||
disabled
|
||||
placeholder={user?.email?.endsWith("@wechat.local") ? "微信账号暂未绑定邮箱" : "邮箱"}
|
||||
/>
|
||||
<Input value={user?.email || ""} disabled placeholder="邮箱" />
|
||||
</div>
|
||||
|
||||
<div className="xx-settings-field">
|
||||
<label className="xx-settings-label">昵称</label>
|
||||
<label className="xx-settings-label">显示名称</label>
|
||||
<Input
|
||||
value={displayName}
|
||||
onChange={(e) => setDisplayName(e.target.value)}
|
||||
placeholder="请输入昵称"
|
||||
maxLength={20}
|
||||
placeholder="请输入显示名称"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="xx-settings-field">
|
||||
<Button
|
||||
buttonType="primary"
|
||||
buttonSize="md"
|
||||
onClick={() => saveProfileMutation.mutate()}
|
||||
loading={saveProfileMutation.isPending}
|
||||
disabled={!displayName.trim() || !displayNameDirty}
|
||||
>
|
||||
保存
|
||||
<Button buttonType="primary" buttonSize="md" onClick={handleSave} disabled>
|
||||
保存暂未开放
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="xx-settings-card">
|
||||
<h3>微信账号</h3>
|
||||
<div className="xx-settings-wechat">
|
||||
<div className="xx-settings-wechat-info">
|
||||
<span className="xx-wechat-icon">💬</span>
|
||||
<div>
|
||||
{wechatBound ? (
|
||||
<>
|
||||
<strong>
|
||||
已绑定微信{user?.wechat_nickname ? `(${user.wechat_nickname})` : ""}
|
||||
</strong>
|
||||
<p>可使用微信扫码登录本账号</p>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<strong>未绑定微信</strong>
|
||||
<p>绑定后可使用微信扫码快速登录</p>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="xx-settings-wechat-actions">
|
||||
{wechatBound ? (
|
||||
<Button
|
||||
buttonType="ghost"
|
||||
buttonSize="md"
|
||||
onClick={handleUnbind}
|
||||
loading={unbindMutation.isPending}
|
||||
>
|
||||
解绑
|
||||
</Button>
|
||||
) : (
|
||||
<Button buttonType="primary" buttonSize="md" onClick={handleBindWechat}>
|
||||
绑定微信
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,16 +6,10 @@ import { useAuthStore } from "@/store/authStore"
|
||||
export const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
|
||||
const isAuthenticated = useAuthStore((state) => state.isAuthenticated)
|
||||
const hasAccessToken = Boolean(localStorage.getItem("access_token"))
|
||||
const profileCompleted = useAuthStore((state) => state.user?.profile_completed !== false)
|
||||
|
||||
if (!isAuthenticated || !hasAccessToken) {
|
||||
return <Navigate to="/login" replace />
|
||||
}
|
||||
|
||||
// 微信新用户未完成昵称引导时,禁止进入主界面
|
||||
if (!profileCompleted) {
|
||||
return <Navigate to="/welcome/wechat" replace />
|
||||
}
|
||||
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ import Register from "@/pages/auth/Register"
|
||||
import ForgotPassword from "@/pages/auth/ForgotPassword"
|
||||
import ResetPassword from "@/pages/auth/ResetPassword"
|
||||
import WechatCallback from "@/pages/auth/WechatCallback"
|
||||
import WechatOnboarding from "@/pages/auth/WechatOnboarding"
|
||||
import WechatBindCallback from "@/pages/auth/WechatBindCallback"
|
||||
import { useAuthStore } from "@/store/authStore"
|
||||
|
||||
/** 首页路由组件:已登录跳 dashboard,未登录显示落地页 */
|
||||
@@ -47,12 +45,4 @@ export const publicRoutes: RouteObject[] = [
|
||||
path: "/auth/wechat/callback",
|
||||
element: <WechatCallback />,
|
||||
},
|
||||
{
|
||||
path: "/auth/wechat/bind/callback",
|
||||
element: <WechatBindCallback />,
|
||||
},
|
||||
{
|
||||
path: "/welcome/wechat",
|
||||
element: <WechatOnboarding />,
|
||||
},
|
||||
]
|
||||
|
||||
@@ -13,12 +13,6 @@ interface User {
|
||||
display_name: string
|
||||
is_email_verified: boolean
|
||||
email_verified: boolean
|
||||
wechat_bound?: boolean
|
||||
wechat_nickname?: string
|
||||
avatar_url?: string
|
||||
phone?: string
|
||||
phone_verified?: boolean
|
||||
profile_completed?: boolean
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* 上传去重/幂等工具单测(Issue #1714)
|
||||
*/
|
||||
import { describe, it, expect } from "vitest"
|
||||
import {
|
||||
computeFileHash,
|
||||
findDuplicateInQueue,
|
||||
makeClientUploadId,
|
||||
makeFileFingerprint,
|
||||
} from "@/api/assets/uploadDedup"
|
||||
|
||||
const makeFile = (name: string, size = 100, lastModified = 1_700_000_000_000) =>
|
||||
new File([new Uint8Array(size)], name, { type: "video/mp4", lastModified })
|
||||
|
||||
describe("makeFileFingerprint", () => {
|
||||
it("同一文件(name+size+lastModified 相同)指纹一致", () => {
|
||||
const a = makeFile("a.mp4", 1000, 12345)
|
||||
const b = makeFile("a.mp4", 1000, 12345)
|
||||
expect(makeFileFingerprint(a)).toBe(makeFileFingerprint(b))
|
||||
})
|
||||
|
||||
it("文件名/大小/修改时间任一不同指纹即不同", () => {
|
||||
const base = makeFile("a.mp4", 1000, 100)
|
||||
expect(makeFileFingerprint(base)).not.toBe(makeFileFingerprint(makeFile("b.mp4", 1000, 100)))
|
||||
expect(makeFileFingerprint(base)).not.toBe(makeFileFingerprint(makeFile("a.mp4", 1001, 100)))
|
||||
expect(makeFileFingerprint(base)).not.toBe(makeFileFingerprint(makeFile("a.mp4", 1000, 101)))
|
||||
})
|
||||
})
|
||||
|
||||
describe("findDuplicateInQueue", () => {
|
||||
const queue = [
|
||||
{ fileKey: "k1", status: "preparing" },
|
||||
{ fileKey: "k2", status: "uploading" },
|
||||
{ fileKey: "k3", status: "ingesting" },
|
||||
{ fileKey: "k4", status: "done" },
|
||||
{ fileKey: "k5", status: "error" },
|
||||
]
|
||||
|
||||
it("在途状态(preparing/uploading/ingesting/done)命中重复", () => {
|
||||
expect(findDuplicateInQueue(queue, "k1")?.status).toBe("preparing")
|
||||
expect(findDuplicateInQueue(queue, "k2")?.status).toBe("uploading")
|
||||
expect(findDuplicateInQueue(queue, "k3")?.status).toBe("ingesting")
|
||||
expect(findDuplicateInQueue(queue, "k4")?.status).toBe("done")
|
||||
})
|
||||
|
||||
it("未命中返回 null", () => {
|
||||
expect(findDuplicateInQueue(queue, "missing")).toBeNull()
|
||||
})
|
||||
|
||||
it("排除 error 状态后,失败项不算重复(允许重新激活)", () => {
|
||||
expect(findDuplicateInQueue(queue, "k5", ["error"])).toBeNull()
|
||||
})
|
||||
|
||||
it("同时排除 done 后,已完成项也不算重复", () => {
|
||||
expect(findDuplicateInQueue(queue, "k4", ["error", "done"])).toBeNull()
|
||||
// 但在途的仍然命中
|
||||
expect(findDuplicateInQueue(queue, "k1", ["error", "done"])).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe("makeClientUploadId", () => {
|
||||
it("生成带前缀且互不相同的幂等 token", () => {
|
||||
const ids = new Set(Array.from({ length: 20 }, () => makeClientUploadId()))
|
||||
expect(ids.size).toBe(20)
|
||||
for (const id of ids) expect(id.startsWith("up_")).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("computeFileHash", () => {
|
||||
it("相同内容 hash 一致、不同内容 hash 不同", async () => {
|
||||
const f1 = makeFile("a.mp4", 4096)
|
||||
const f2 = makeFile("b.mp4", 4096)
|
||||
// 两个文件都是 0 填充,内容相同 → hash 一致
|
||||
expect(await computeFileHash(f1)).toBe(await computeFileHash(f2))
|
||||
|
||||
const f3 = new File([new Uint8Array(4096).fill(7)], "c.mp4", { type: "video/mp4" })
|
||||
expect(await computeFileHash(f1)).not.toBe(await computeFileHash(f3))
|
||||
})
|
||||
|
||||
it("返回 64 位十六进制(SHA-256,与后端 file_hash 长度一致)", async () => {
|
||||
const hash = await computeFileHash(makeFile("a.mp4", 1024))
|
||||
expect(hash).toMatch(/^[0-9a-f]{64}$/)
|
||||
})
|
||||
})
|
||||
|
||||
describe("computeFileHash 大文件抽样(>256MB)", () => {
|
||||
it("抽样路径正常返回 64 位 hex,且大小不同则 hash 不同", async () => {
|
||||
// mock 一个「声称」300MB 的 File:slice 返回小 buffer 即可,不真分配 300MB
|
||||
const makeBig = (declaredSize: number, head: number) => {
|
||||
const f = new File([new Uint8Array([head, 2, 3])], "big.mov", { type: "video/quicktime" })
|
||||
Object.defineProperty(f, "size", { value: declaredSize, configurable: true })
|
||||
// slice 仍按真实内容返回小片段(头尾片段内容由底层小 buffer 决定)
|
||||
return f
|
||||
}
|
||||
const h1 = await computeFileHash(makeBig(300 * 1024 * 1024, 1))
|
||||
const h2 = await computeFileHash(makeBig(301 * 1024 * 1024, 1))
|
||||
expect(h1).toMatch(/^[0-9a-f]{64}$/)
|
||||
// 声明大小不同 → 写入的 64 位 size 字段不同 → hash 必须不同(锁定 setBigUint64 路径)
|
||||
expect(h1).not.toBe(h2)
|
||||
})
|
||||
})
|
||||
@@ -1,8 +1,8 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest"
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"
|
||||
import { describe, expect, it, vi } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import { MemoryRouter } from "react-router-dom"
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
|
||||
|
||||
// mock PageHead 简单mock
|
||||
vi.mock("@/components/layout/PageHead", () => ({
|
||||
default: ({ title, description }: { title: string; description?: string }) => (
|
||||
<div data-testid="page-head">
|
||||
@@ -12,142 +12,51 @@ vi.mock("@/components/layout/PageHead", () => ({
|
||||
),
|
||||
}))
|
||||
|
||||
const mockSetUser = vi.fn()
|
||||
const mockInvalidate = vi.fn()
|
||||
let authState: Record<string, unknown> = {
|
||||
user: {
|
||||
id: "1",
|
||||
user_id: "1",
|
||||
username: "testuser",
|
||||
email: "test@example.com",
|
||||
display_name: "Test User",
|
||||
wechat_bound: false,
|
||||
},
|
||||
isAuthenticated: true,
|
||||
setUser: mockSetUser,
|
||||
}
|
||||
|
||||
vi.mock("@/store/authStore", () => ({
|
||||
useAuthStore: (selector: (state: unknown) => unknown) => selector(authState),
|
||||
}))
|
||||
|
||||
const getCurrentUserMock = vi.fn(async () => authState.user as Record<string, unknown>)
|
||||
const updateProfileMock = vi.fn()
|
||||
const getWechatBindUrlMock = vi.fn(async () => ({
|
||||
auth_url: "https://wx.example/auth",
|
||||
state: "s1",
|
||||
}))
|
||||
const unbindWechatMock = vi.fn(async () => ({ success: true }))
|
||||
|
||||
vi.mock("@/api/auth", () => ({
|
||||
getCurrentUser: () => getCurrentUserMock(),
|
||||
updateProfile: (d: unknown) => updateProfileMock(d),
|
||||
getWechatBindUrl: () => getWechatBindUrlMock(),
|
||||
unbindWechat: () => unbindWechatMock(),
|
||||
}))
|
||||
|
||||
vi.mock("antd", async () => {
|
||||
const actual = await vi.importActual("antd")
|
||||
return { ...actual, message: { success: vi.fn(), error: vi.fn() } }
|
||||
})
|
||||
|
||||
import Settings from "@/pages/profile/Settings"
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
})
|
||||
|
||||
const renderPage = () =>
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MemoryRouter>
|
||||
<Settings />
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
describe("Settings Page", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
authState = {
|
||||
useAuthStore: (selector: (state: any) => any) =>
|
||||
selector({
|
||||
user: {
|
||||
id: "1",
|
||||
user_id: "1",
|
||||
username: "testuser",
|
||||
email: "test@example.com",
|
||||
display_name: "Test User",
|
||||
wechat_bound: false,
|
||||
is_email_verified: true,
|
||||
email_verified: true,
|
||||
},
|
||||
isAuthenticated: true,
|
||||
setUser: mockSetUser,
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
import Settings from "@/pages/profile/Settings"
|
||||
|
||||
describe("Settings Page", () => {
|
||||
it("should render without crashing", () => {
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<Settings />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
expect(screen.getByText("个人设置")).toBeTruthy()
|
||||
})
|
||||
|
||||
it("渲染个人设置与用户信息", () => {
|
||||
renderPage()
|
||||
expect(screen.getByText("个人设置")).toBeTruthy()
|
||||
it("should display user info", () => {
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<Settings />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
expect(screen.getByDisplayValue("testuser")).toBeTruthy()
|
||||
expect(screen.getByDisplayValue("test@example.com")).toBeTruthy()
|
||||
})
|
||||
|
||||
it("未绑定时显示绑定微信按钮,点击跳转微信授权", async () => {
|
||||
renderPage()
|
||||
expect(screen.getByText("未绑定微信")).toBeTruthy()
|
||||
const btn = screen.getByText("绑定微信")
|
||||
fireEvent.click(btn)
|
||||
await waitFor(() => {
|
||||
expect(getWechatBindUrlMock).toHaveBeenCalled()
|
||||
expect(localStorage.getItem("wechat_bind_state")).toBe("s1")
|
||||
})
|
||||
})
|
||||
|
||||
it("已绑定时显示状态与解绑按钮,确认后调解绑接口", async () => {
|
||||
authState.user = {
|
||||
...(authState.user as object),
|
||||
wechat_bound: true,
|
||||
wechat_nickname: "微信昵称",
|
||||
} as never
|
||||
renderPage()
|
||||
expect(screen.getByText(/已绑定微信/)).toBeTruthy()
|
||||
fireEvent.click(
|
||||
screen.getByText(
|
||||
(_, el) => el?.tagName === "BUTTON" && (el.textContent ?? "").replace(/\s/g, "") === "解绑",
|
||||
),
|
||||
it("should show save button is disabled", () => {
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<Settings />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
// antd Modal.confirm 弹确认框(标题+内容均含"解绑微信",用 role=dialog 内的确认按钮)
|
||||
await waitFor(() => {
|
||||
expect(document.querySelector(".ant-modal-confirm")).toBeTruthy()
|
||||
})
|
||||
fireEvent.click(
|
||||
screen.getByText(
|
||||
(_, el) => el?.tagName === "BUTTON" && (el.textContent ?? "").includes("确定解绑"),
|
||||
),
|
||||
)
|
||||
await waitFor(() => {
|
||||
expect(unbindWechatMock).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
it("修改昵称后保存按钮可用,点击调用更新接口", async () => {
|
||||
renderPage()
|
||||
const saveBtn = screen.getByText(
|
||||
(_, el) => el?.tagName === "BUTTON" && (el.textContent ?? "").replace(/\s/g, "") === "保存",
|
||||
)
|
||||
expect(saveBtn.closest("button")?.disabled).toBe(true)
|
||||
fireEvent.change(screen.getByDisplayValue("Test User"), {
|
||||
target: { value: "新昵称" },
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(saveBtn.closest("button")?.disabled).toBe(false)
|
||||
})
|
||||
updateProfileMock.mockResolvedValueOnce({
|
||||
id: "1",
|
||||
display_name: "新昵称",
|
||||
wechat_bound: false,
|
||||
})
|
||||
fireEvent.click(saveBtn)
|
||||
await waitFor(() => {
|
||||
expect(updateProfileMock).toHaveBeenCalledWith({ display_name: "新昵称" })
|
||||
})
|
||||
const button = screen.getByText("保存暂未开放")
|
||||
expect(button).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -104,9 +104,6 @@ vi.mock("@ant-design/icons", () => ({
|
||||
SoundOutlined: () => <span />,
|
||||
UploadOutlined: () => <span />,
|
||||
UserOutlined: () => <span />,
|
||||
VideoCameraOutlined: () => <span />,
|
||||
InboxOutlined: () => <span />,
|
||||
CloseOutlined: () => <span />,
|
||||
}))
|
||||
|
||||
vi.mock("@/store/authStore", () => ({
|
||||
|
||||
@@ -31,23 +31,17 @@ interface FakeHandle {
|
||||
complete: ReturnType<typeof vi.fn>
|
||||
/** 手动结束传输(transfer 被调用后挂载);finish(true) 以失败结束 */
|
||||
finish: (fail?: boolean) => void
|
||||
/** complete 已被调用的次数 */
|
||||
completeCalls: { resolve: () => void; reject: (err: unknown) => void }[]
|
||||
}
|
||||
|
||||
let activeTransfers = 0
|
||||
let maxConcurrent = 0
|
||||
|
||||
/**
|
||||
* 创建一个假 handle:
|
||||
* - transfer 返回挂起的 promise,finish()/finish(true) 控制成败
|
||||
* - complete 每次调用返回独立的挂起 promise,由 completeCalls 记录控制,
|
||||
* 成功调 resolve(idx) / 失败调 reject(idx)(模拟超时)
|
||||
* 创建一个假 handle:transfer 返回挂起的 promise,
|
||||
* finish 槽位在 transfer executor 同步执行时挂载,测试中调用 finish() 控制成败
|
||||
*/
|
||||
const makeFakeHandle = (opts: {
|
||||
id: string
|
||||
duplicated?: boolean
|
||||
failTransfer?: boolean
|
||||
completeAuto?: boolean
|
||||
}) => {
|
||||
const h: FakeHandle = {
|
||||
const makeFakeHandle = (opts: { id: string; duplicated?: boolean; failTransfer?: boolean }) => {
|
||||
const h = {
|
||||
prepared: {
|
||||
upload_url: "https://oss.example.com/u",
|
||||
method: "POST",
|
||||
@@ -58,39 +52,15 @@ const makeFakeHandle = (opts: {
|
||||
asset_id: opts.id,
|
||||
},
|
||||
transfer: vi.fn(),
|
||||
complete: vi.fn(),
|
||||
finish: () => {},
|
||||
completeCalls: [],
|
||||
complete: vi.fn().mockResolvedValue({
|
||||
storage_key: "uploads/x/y.mp4",
|
||||
ingest_job_id: opts.duplicated ? "" : "job-1",
|
||||
url: "https://oss.example.com/u",
|
||||
duplicated: opts.duplicated,
|
||||
asset_id: opts.id,
|
||||
}),
|
||||
finish: (() => {}) as (fail?: boolean) => void,
|
||||
}
|
||||
|
||||
h.complete.mockImplementation(
|
||||
() =>
|
||||
new Promise<{
|
||||
storage_key: string
|
||||
ingest_job_id: string
|
||||
url: string
|
||||
duplicated: boolean
|
||||
asset_id: string
|
||||
}>((resolve, reject) => {
|
||||
h.completeCalls.push({
|
||||
resolve: () =>
|
||||
resolve({
|
||||
storage_key: "uploads/x/y.mp4",
|
||||
ingest_job_id: opts.duplicated ? "" : `job-${opts.id}`,
|
||||
url: "https://oss.example.com/u",
|
||||
duplicated: !!opts.duplicated,
|
||||
asset_id: opts.id,
|
||||
}),
|
||||
reject,
|
||||
})
|
||||
// 默认立即成功,保持旧用例简单
|
||||
if (opts.completeAuto !== false) {
|
||||
const idx = h.completeCalls.length - 1
|
||||
Promise.resolve().then(() => h.completeCalls[idx]?.resolve())
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
h.transfer.mockImplementation(
|
||||
() =>
|
||||
new Promise<void>((_resolve, reject) => {
|
||||
@@ -108,24 +78,17 @@ const makeFakeHandle = (opts: {
|
||||
|
||||
type FakeHandleLike = ReturnType<typeof makeFakeHandle>
|
||||
|
||||
let activeTransfers = 0
|
||||
let maxConcurrent = 0
|
||||
|
||||
/** prepare mock:调用序号生成稳定 id,立即把 handle(含 finish 槽位)推入数组 */
|
||||
const installPrepareMock = (
|
||||
handles: FakeHandleLike[],
|
||||
optOverrides?: (id: string) => {
|
||||
duplicated?: boolean
|
||||
failTransfer?: boolean
|
||||
completeAuto?: boolean
|
||||
},
|
||||
optOverrides?: (id: string) => { duplicated?: boolean; failTransfer?: boolean },
|
||||
) => {
|
||||
let callNo = 0
|
||||
;(prepareDirectUploadHandle as unknown as ReturnType<typeof vi.fn>).mockImplementation(
|
||||
async () => {
|
||||
const id = `asset-${callNo++}`
|
||||
const overrides = optOverrides?.(id) ?? {}
|
||||
const h = makeFakeHandle({ id, completeAuto: true, ...overrides })
|
||||
const h = makeFakeHandle({ id, ...overrides })
|
||||
handles.push(h)
|
||||
await new Promise((r) => setTimeout(r, 10))
|
||||
return h
|
||||
@@ -260,96 +223,4 @@ describe("useAssetUpload", () => {
|
||||
expect(result.current.uploadItems[0].status).toBe("done")
|
||||
})
|
||||
})
|
||||
it("同一文件多次选择不重复入队(指纹去重)", async () => {
|
||||
const handles: FakeHandleLike[] = []
|
||||
installPrepareMock(handles)
|
||||
|
||||
const { result } = renderHook(() => useAssetUpload({ effectiveLibId: "lib-1" }), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
|
||||
// 同一文件(name+size+lastModified 完全一致)第一次入队
|
||||
const sameFile = mp4("same.mp4")
|
||||
await act(async () => {
|
||||
result.current.enqueueUploads([sameFile])
|
||||
})
|
||||
await waitFor(() => expect(handles.length).toBe(1))
|
||||
expect(result.current.uploadItems).toHaveLength(1)
|
||||
|
||||
// transfer 挂起期间,再次选择同一文件(模拟用户反复点选/拖拽)
|
||||
await act(async () => {
|
||||
result.current.enqueueUploads([sameFile])
|
||||
})
|
||||
await act(async () => {
|
||||
result.current.enqueueUploads([sameFile])
|
||||
})
|
||||
// 队列表只有 1 项、prepare 只有 1 次
|
||||
expect(result.current.uploadItems).toHaveLength(1)
|
||||
expect(handles.length).toBe(1)
|
||||
|
||||
// 完成后再次重复选择(已 done):仍然不新增
|
||||
await act(async () => {
|
||||
handles[0].finish()
|
||||
})
|
||||
await waitFor(() => expect(result.current.uploadItems[0].status).toBe("done"))
|
||||
await act(async () => {
|
||||
result.current.enqueueUploads([sameFile])
|
||||
})
|
||||
expect(result.current.uploadItems).toHaveLength(1)
|
||||
expect(handles.length).toBe(1)
|
||||
|
||||
// 不同文件正常入队
|
||||
await act(async () => {
|
||||
result.current.enqueueUploads([mp4("other.mp4")])
|
||||
})
|
||||
await waitFor(() => expect(handles.length).toBe(2))
|
||||
expect(result.current.uploadItems).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("complete 失败(超时)后重试:只重发 complete,不重新 prepare/直传", async () => {
|
||||
const handles: FakeHandleLike[] = []
|
||||
installPrepareMock(handles, () => ({ completeAuto: false }))
|
||||
|
||||
const { result } = renderHook(() => useAssetUpload({ effectiveLibId: "lib-1" }), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
|
||||
await act(async () => {
|
||||
result.current.enqueueUploads([mp4("slow.mp4")])
|
||||
})
|
||||
await waitFor(() => expect(handles.length).toBe(1))
|
||||
await waitFor(() => expect(handles[0].transfer).toHaveBeenCalled())
|
||||
await act(async () => {
|
||||
handles[0].finish()
|
||||
})
|
||||
// complete 被调用但挂起
|
||||
await waitFor(() => expect(handles[0].complete).toHaveBeenCalledTimes(1))
|
||||
|
||||
// 模拟 complete 超时(后端记录可能已建成)
|
||||
await act(async () => {
|
||||
handles[0].completeCalls[0]?.reject(new Error("complete timeout (ECONNABORTED)"))
|
||||
})
|
||||
const tempId = result.current.uploadItems[0].tempId
|
||||
await waitFor(() => {
|
||||
const it = result.current.uploadItems.find((x) => x.tempId === tempId)
|
||||
expect(it?.status).toBe("error")
|
||||
expect(it?.failedStage).toBe("complete")
|
||||
})
|
||||
|
||||
// 点重试:pump 复用 handle,只再调一次 complete(transfer/prepare 不重复)
|
||||
await act(async () => {
|
||||
result.current.retryUpload(tempId)
|
||||
})
|
||||
await waitFor(() => expect(handles[0].complete).toHaveBeenCalledTimes(2))
|
||||
expect(handles.length).toBe(1) // 没有重新 prepare
|
||||
expect(handles[0].transfer).toHaveBeenCalledTimes(1) // 没有重新直传
|
||||
|
||||
// 第二次 complete 成功
|
||||
await act(async () => {
|
||||
handles[0].completeCalls[1]?.resolve()
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(result.current.uploadItems.find((x) => x.tempId === tempId)?.status).toBe("done")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,127 +1,79 @@
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||
import { render, screen, waitFor, cleanup } from "@testing-library/react"
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import { MemoryRouter } from "react-router-dom"
|
||||
import WechatCallback from "@/pages/auth/WechatCallback"
|
||||
|
||||
const mockNavigate = vi.fn()
|
||||
const mockSetAuth = vi.fn()
|
||||
const mockSearchParams = [new URLSearchParams({ code: "test_code", state: "test_state" })] as const
|
||||
const mockAuthState = { setAuth: mockSetAuth }
|
||||
|
||||
// 文件级 localStorage mock(避免每个用例重复 spy 导致链式污染)
|
||||
const localStorageStore: Record<string, string> = {}
|
||||
vi.spyOn(Storage.prototype, "getItem").mockImplementation((key) => localStorageStore[key] || null)
|
||||
vi.spyOn(Storage.prototype, "setItem").mockImplementation((key, val) => {
|
||||
localStorageStore[key] = val
|
||||
})
|
||||
vi.spyOn(Storage.prototype, "removeItem").mockImplementation((key) => {
|
||||
delete localStorageStore[key]
|
||||
})
|
||||
|
||||
let mockCallbackResult: Record<string, unknown> = {}
|
||||
let mockCurrentUser: Record<string, unknown> = {}
|
||||
let callbackShouldFail = false
|
||||
|
||||
vi.mock("react-router-dom", async () => {
|
||||
const actual = await vi.importActual("react-router-dom")
|
||||
return {
|
||||
...actual,
|
||||
useNavigate: () => mockNavigate,
|
||||
useSearchParams: () => mockSearchParams,
|
||||
useNavigate: () => vi.fn(),
|
||||
useSearchParams: () => [new URLSearchParams({ code: "test_code", state: "test_state" })],
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock("@/api/auth", () => ({
|
||||
wechatCallback: vi.fn(async () => {
|
||||
if (callbackShouldFail) throw new Error("fail")
|
||||
return mockCallbackResult
|
||||
}),
|
||||
getCurrentUser: vi.fn(async () => mockCurrentUser),
|
||||
wechatCallback: vi.fn(() => new Promise(() => {})), // pending promise,保持loading
|
||||
getCurrentUser: vi.fn(),
|
||||
normalizeUser: (u: unknown) => u,
|
||||
}))
|
||||
|
||||
vi.mock("@/api/auth/tokenRefresh", () => ({
|
||||
scheduleProactiveRefresh: vi.fn(),
|
||||
cancelProactiveRefresh: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("@/store/authStore", () => ({
|
||||
useAuthStore: (selector: (state: unknown) => unknown) => selector({ setAuth: mockSetAuth }),
|
||||
useAuthStore: () => ({
|
||||
setAuth: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
const renderPage = () =>
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<WechatCallback />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
vi.mock("@/components/auth/BindContactModal", () => ({
|
||||
default: ({ open }: { open: boolean }) => (
|
||||
<div data-testid="bind-contact-modal" style={{ display: open ? "block" : "none" }}>
|
||||
BindContactModal
|
||||
</div>
|
||||
),
|
||||
}))
|
||||
|
||||
vi.mock("antd", async () => {
|
||||
const actual = await vi.importActual("antd")
|
||||
return {
|
||||
...actual,
|
||||
message: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
describe("WechatCallback Page", () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
callbackShouldFail = false
|
||||
localStorageStore.wechat_state = "test_state"
|
||||
mockCallbackResult = {
|
||||
access_token: "at",
|
||||
refresh_token: "rt",
|
||||
is_new_user: false,
|
||||
// mock localStorage,设置wechat_state匹配,让校验通过
|
||||
const store: Record<string, string> = {
|
||||
wechat_state: "test_state",
|
||||
}
|
||||
mockCurrentUser = {
|
||||
id: "u1",
|
||||
display_name: "老用户",
|
||||
profile_completed: true,
|
||||
}
|
||||
})
|
||||
|
||||
it("老用户登录成功跳转首页/来源页", async () => {
|
||||
renderPage()
|
||||
await waitFor(() => {
|
||||
expect(mockNavigate).toHaveBeenCalledWith("/", { replace: true })
|
||||
vi.spyOn(Storage.prototype, "getItem").mockImplementation((key) => store[key] || null)
|
||||
vi.spyOn(Storage.prototype, "setItem").mockImplementation((key, val) => {
|
||||
store[key] = val
|
||||
})
|
||||
expect(mockSetAuth).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("新用户(is_new_user)跳转昵称引导页", async () => {
|
||||
mockCallbackResult = { access_token: "at", refresh_token: "rt", is_new_user: true }
|
||||
mockCurrentUser = { id: "u2", display_name: "微信用户", profile_completed: false }
|
||||
renderPage()
|
||||
await waitFor(() => {
|
||||
expect(mockNavigate).toHaveBeenCalledWith("/welcome/wechat", { replace: true })
|
||||
vi.spyOn(Storage.prototype, "removeItem").mockImplementation((key) => {
|
||||
delete store[key]
|
||||
})
|
||||
})
|
||||
|
||||
it("is_new_user=false 但 profile_completed=false(上次中断)也跳引导页", async () => {
|
||||
mockCallbackResult = { access_token: "at", refresh_token: "rt", is_new_user: false }
|
||||
mockCurrentUser = { id: "u3", display_name: "微信用户", profile_completed: false }
|
||||
renderPage()
|
||||
await waitFor(() => {
|
||||
expect(mockNavigate).toHaveBeenCalledWith("/welcome/wechat", { replace: true })
|
||||
})
|
||||
it("should render without crashing", () => {
|
||||
const { container } = render(
|
||||
<MemoryRouter>
|
||||
<WechatCallback />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
expect(container).toBeTruthy()
|
||||
})
|
||||
|
||||
it("state 不匹配显示安全错误", async () => {
|
||||
localStorageStore.wechat_state = "other_state"
|
||||
renderPage()
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("安全校验失败,请重新登录")).toBeTruthy()
|
||||
})
|
||||
expect(mockNavigate).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("接口失败显示错误提示", async () => {
|
||||
callbackShouldFail = true
|
||||
renderPage()
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("微信登录失败,请重试")).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
it("处理中显示 loading", () => {
|
||||
renderPage()
|
||||
expect(screen.getByText("微信登录中...")).toBeTruthy()
|
||||
it("should show loading state while processing", () => {
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<WechatCallback />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
// wechatCallback 返回 pending promise,所以应该显示 loading
|
||||
expect(screen.getByText("正在登录...")).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||
import { render, screen, fireEvent, waitFor, cleanup } from "@testing-library/react"
|
||||
import { MemoryRouter } from "react-router-dom"
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
|
||||
import WechatOnboarding from "@/pages/auth/WechatOnboarding"
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
})
|
||||
|
||||
const mockNavigate = vi.fn()
|
||||
const mockSetUser = vi.fn()
|
||||
let updateProfileMock = vi.fn()
|
||||
|
||||
vi.mock("react-router-dom", async () => {
|
||||
const actual = await vi.importActual("react-router-dom")
|
||||
return { ...actual, useNavigate: () => mockNavigate }
|
||||
})
|
||||
|
||||
let authState: Record<string, unknown> = {}
|
||||
vi.mock("@/store/authStore", () => ({
|
||||
useAuthStore: (selector: (state: unknown) => unknown) => selector(authState),
|
||||
}))
|
||||
|
||||
vi.mock("@/api/auth", () => ({
|
||||
updateProfile: (data: { display_name: string }) => updateProfileMock(data),
|
||||
}))
|
||||
|
||||
vi.mock("antd", async () => {
|
||||
const actual = await vi.importActual("antd")
|
||||
return { ...actual, message: { success: vi.fn(), error: vi.fn() } }
|
||||
})
|
||||
|
||||
const renderPage = () =>
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MemoryRouter>
|
||||
<WechatOnboarding />
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
describe("WechatOnboarding 昵称引导页", () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
authState = {
|
||||
isAuthenticated: true,
|
||||
user: { id: "u1", display_name: "", profile_completed: false },
|
||||
setUser: mockSetUser,
|
||||
}
|
||||
localStorage.setItem("access_token", "at")
|
||||
updateProfileMock = vi.fn(async (data: { display_name: string }) => ({
|
||||
id: "u1",
|
||||
display_name: data.display_name,
|
||||
profile_completed: true,
|
||||
}))
|
||||
})
|
||||
|
||||
it("未登录时跳转登录页", () => {
|
||||
authState = {
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
setUser: mockSetUser,
|
||||
}
|
||||
localStorage.removeItem("access_token")
|
||||
renderPage()
|
||||
expect(mockNavigate).not.toHaveBeenCalled()
|
||||
// Navigate 组件渲染即生效;这里断言页面不含昵称表单
|
||||
expect(screen.queryByText("进入小虾智剪")).toBeNull()
|
||||
})
|
||||
|
||||
it("资料已完善的用户跳 dashboard", () => {
|
||||
authState = {
|
||||
isAuthenticated: true,
|
||||
user: { id: "u1", display_name: "已起名", profile_completed: true },
|
||||
setUser: mockSetUser,
|
||||
}
|
||||
renderPage()
|
||||
expect(screen.queryByText("进入小虾智剪")).toBeNull()
|
||||
})
|
||||
|
||||
it("新用户可见昵称表单并能提交", async () => {
|
||||
renderPage()
|
||||
expect(screen.getByText("欢迎使用微信登录,请先设置您的昵称")).toBeTruthy()
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText("请输入您的昵称"), {
|
||||
target: { value: "小虾用户" },
|
||||
})
|
||||
fireEvent.click(screen.getByText("进入小虾智剪"))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(updateProfileMock).toHaveBeenCalledWith({ display_name: "小虾用户" })
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(mockSetUser).toHaveBeenCalled()
|
||||
expect(mockNavigate).toHaveBeenCalledWith("/app/dashboard", { replace: true })
|
||||
})
|
||||
})
|
||||
|
||||
it("昵称为空时不允许提交(表单校验)", async () => {
|
||||
renderPage()
|
||||
fireEvent.change(screen.getByPlaceholderText("请输入您的昵称"), {
|
||||
target: { value: " " },
|
||||
})
|
||||
fireEvent.click(screen.getByText("进入小虾智剪"))
|
||||
// 等待表单校验
|
||||
await waitFor(
|
||||
() => {
|
||||
expect(updateProfileMock).not.toHaveBeenCalled()
|
||||
},
|
||||
{ timeout: 1000 },
|
||||
)
|
||||
})
|
||||
|
||||
it("提交失败显示错误且不跳转", async () => {
|
||||
updateProfileMock = vi.fn(async () => {
|
||||
throw new Error("500")
|
||||
})
|
||||
renderPage()
|
||||
fireEvent.change(screen.getByPlaceholderText("请输入您的昵称"), {
|
||||
target: { value: "小虾用户" },
|
||||
})
|
||||
fireEvent.click(screen.getByText("进入小虾智剪"))
|
||||
await waitFor(() => {
|
||||
expect(mockNavigate).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -19,10 +19,6 @@ import "@/pages/generate/GeneratePage"
|
||||
import "@/pages/generate/components/Step2MaterialSelect"
|
||||
import "@/pages/generate/components/Step4TitleSettings"
|
||||
import "@/pages/generate/components/Step5VoiceSelect"
|
||||
import "@/pages/generate/components/Step3VoiceWithMode"
|
||||
import "@/pages/generate/components/CanvasPreviewGrid"
|
||||
import "@/pages/generate/components/BatchGenerationGrid"
|
||||
import "@/pages/generate/components/PreviewCountModal"
|
||||
import "@/pages/generate/components/PreviewVideoPanel"
|
||||
import "@/pages/generate/components/GenerateResultPanel"
|
||||
import "@/pages/generate/components/GenerateStepContent"
|
||||
@@ -49,7 +45,6 @@ describe("GeneratePage module smoke test", () => {
|
||||
})
|
||||
})
|
||||
import "@/pages/generate/hooks/useGenerateVideo"
|
||||
import "@/pages/generate/hooks/useBatchCovers"
|
||||
import "@/pages/generate/hooks/usePreviewAssets"
|
||||
import "@/pages/generate/hooks/useSegmentScheduler"
|
||||
import "@/pages/generate/hooks/generate-video/useGenerationPolling"
|
||||
|
||||
@@ -31,65 +31,25 @@ SCENE_CHANGE_THRESHOLD = 30 # 灰度差异阈值
|
||||
MIN_KEYFRAME_INTERVAL_SEC = 1.0 # 最小关键帧间隔(秒)
|
||||
MAX_KEYFRAMES = 30 # 最大关键帧数
|
||||
MIN_KEYFRAMES = 5 # 最小关键帧数
|
||||
FINGERPRINT_SAMPLE_INTERVAL_SEC = 1.0 # 指纹采样间隔(秒):密集均匀采样,保证两视频时序可对齐
|
||||
FINGERPRINT_MAX_SAMPLES = 30 # 长视频采样数上限(超过后采样间隔自动放宽)
|
||||
LONG_VIDEO_SEGMENT_SEC = 30 # 长视频每段秒数
|
||||
LONG_VIDEO_DURATION_THRESHOLD_SEC = 180 # 3 分钟阈值
|
||||
MIN_FRAMES_PER_SEGMENT = 2 # 长视频每段最少帧数
|
||||
|
||||
# ── 滑动窗口匹配常量(Issue #1702 二次校准) ─────────────────────
|
||||
# 阈值经 staging 真实数据两轮回归校准(worker 容器内离线实验):
|
||||
# 第一轮(2026-09-05):同源对 <=12 命中 4/11,异源最小距离 24 → 定 12;
|
||||
# 第二轮(2026-09-05,证据视频 B->A 仍漏检):扩大样本到该用户全部
|
||||
# 15 个真实成片(13 个异源候选)实测:
|
||||
# - 同源成片对(A 20s / B、C 各 11.75s,1s 密集采样):
|
||||
# B->A 中位数距离 14,<=16 命中 8/11=0.73;C->A 8/11=0.73
|
||||
# - 异源成片对(13 个真实视频):每帧全局最近邻最小距离 18,
|
||||
# <=16 命中帧数全部为 0(最近邻 18 仅个别帧,中位数 22~28)
|
||||
# 12 漏掉同源降重对(降重滤镜/字幕/画面扰动把距离从 ~8 推到 14~16);
|
||||
# 16 对同源命中 0.73+ 且与异源分布(最近邻 >=18)仍有 >=2bit 安全裕度,
|
||||
# 异源 <=16 命中 0 帧,无误报空间。
|
||||
PHASH_THRESHOLD = 16
|
||||
SEGMENT_MATCH_THRESHOLD = PHASH_THRESHOLD # 片段匹配阈值与帧匹配统一(#1702:阈值常量统一来源)
|
||||
MIN_CONSECUTIVE_MATCHES = 5 # 连续匹配默认门槛;短视频自适应 min(5, max(2, 分片数//2))
|
||||
# ── 滑动窗口匹配常量 ────────────────────────────────────────────
|
||||
SEGMENT_MATCH_THRESHOLD = 8 # 帧匹配汉明距离阈值
|
||||
MIN_CONSECUTIVE_MATCHES = 5 # 最少连续匹配帧数
|
||||
MAX_GAP = 2 # 允许的最大间隙帧数
|
||||
NEIGHBOR_WINDOW = 1 # 分片时序对齐:允许 ±1 邻接偏移(1s 密集采样下即 ±1s,缓解切点不一致)
|
||||
|
||||
# ── 融合判定常量 ────────────────────────────────────────────────
|
||||
PHASH_WEIGHT = 0.7 # pHash 权重
|
||||
HISTOGRAM_WEIGHT = 0.3 # 直方图权重
|
||||
MATCH_RATIO_THRESHOLD = 0.7 # 全片重复(is_duplicate)至少 70% 帧匹配
|
||||
PARTIAL_COVERAGE_THRESHOLD = 0.5 # 局部复用覆盖率 >=50% 也判全片重复
|
||||
MATCH_RATIO_THRESHOLD = 0.7 # 至少 70% 帧匹配
|
||||
DUPLICATE_THRESHOLD = 0.70 # 融合后相似度阈值
|
||||
|
||||
# ── 降重裁剪规避常量(Issue #1702) ─────────────────────────────
|
||||
# 成片强制 2-5% random_edge_crop 降重只服务外部平台;自查重指纹取中心 90%
|
||||
# 区域,使两次不同裁剪的同源画面 pHash 距离回到同分布。
|
||||
FINGERPRINT_CENTER_CROP_RATIO = 0.90
|
||||
|
||||
|
||||
# ── 感知哈希 & 颜色直方图工具函数 ────────────────────────────────
|
||||
|
||||
|
||||
def center_crop_frame(image: np.ndarray, ratio: float = FINGERPRINT_CENTER_CROP_RATIO) -> np.ndarray:
|
||||
"""取画面中心 ratio 比例区域(裁除四边边缘)。
|
||||
|
||||
查重指纹用:random_edge_crop 降重(2-5% 四边随机裁剪)会让同源画面 pHash
|
||||
位翻转 12-16,污染自查重(Issue #1702)。算 pHash/颜色直方图前先居中裁除
|
||||
边缘 10%,两次不同裁剪的同源画面中心区域基本重合,指纹不再被降重污染。
|
||||
降重只服务外部平台,不影响内部查重。
|
||||
"""
|
||||
if image is None or image.size == 0:
|
||||
return image
|
||||
h, w = image.shape[:2]
|
||||
ch, cw = int(h * ratio), int(w * ratio)
|
||||
if ch <= 0 or cw <= 0 or (ch >= h and cw >= w):
|
||||
return image
|
||||
y0 = (h - ch) // 2
|
||||
x0 = (w - cw) // 2
|
||||
return image[y0 : y0 + ch, x0 : x0 + cw]
|
||||
|
||||
|
||||
def compute_phash(image: np.ndarray, hash_size: int = 8) -> str:
|
||||
"""计算图像的感知哈希(pHash),基于 DCT(离散余弦变换)。
|
||||
|
||||
@@ -141,17 +101,11 @@ def hamming_distance(hash1: str, hash2: str) -> int:
|
||||
|
||||
|
||||
def compute_color_histogram(image: np.ndarray, bins: int = 32) -> list[float]:
|
||||
"""Compute BGR color histogram for an image.
|
||||
|
||||
Issue #1702: 每个通道独立做 NORM_L1 归一化(通道内 Σ=1,是概率分布),
|
||||
三通道拼接存储。Bhattacharyya 系数对拼接向量直接 Σ√(a*b) 会得到
|
||||
3 通道之和(范围 [0,3],实测 ~14.9 是旧 L2 归一化的错误结果),
|
||||
消费方 _bhattacharyya_coefficient 按通道数平均归一到 [0,1]。
|
||||
"""
|
||||
"""Compute color histogram for an image."""
|
||||
hist = []
|
||||
for i in range(3):
|
||||
h = cv2.calcHist([image], [i], None, [bins], [0, 256])
|
||||
h = cv2.normalize(h, h, norm_type=cv2.NORM_L1).flatten()
|
||||
h = cv2.normalize(h, h).flatten()
|
||||
hist.extend(h)
|
||||
return hist
|
||||
|
||||
@@ -256,30 +210,6 @@ def detect_keyframe_timestamps(
|
||||
return keyframe_times
|
||||
|
||||
|
||||
def sample_fingerprint_timestamps(
|
||||
duration: float,
|
||||
*,
|
||||
interval_sec: float = FINGERPRINT_SAMPLE_INTERVAL_SEC,
|
||||
max_samples: int = FINGERPRINT_MAX_SAMPLES,
|
||||
) -> list[float]:
|
||||
"""指纹采样时间戳:固定间隔密集均匀采样(Issue #1702)。
|
||||
|
||||
动态场景检测抽帧(#1659)在两个同源视频上会各自取到不同时刻,切点/取帧
|
||||
错位让对齐帧的 pHash 距离都很大(实测同源对最小距离 12 且配对时序错乱)。
|
||||
改为固定 1s 间隔均匀采样后,复用片段的帧时刻天然对齐,配合 ±1 邻接窗口
|
||||
即可检出同源/局部复用。长视频(>max_samples*interval)自动放宽间隔到
|
||||
duration/max_samples,保证分片数有上限。
|
||||
"""
|
||||
if duration <= 0:
|
||||
return []
|
||||
step = interval_sec
|
||||
n_uniform = int(duration / step)
|
||||
if n_uniform > max_samples:
|
||||
step = duration / max_samples
|
||||
count = max(1, int(duration / step))
|
||||
return [step * (i + 0.5) for i in range(count)]
|
||||
|
||||
|
||||
# ── 数据类 ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -367,33 +297,23 @@ def find_duplicate_segments(
|
||||
target_chunks: list,
|
||||
*,
|
||||
match_threshold: int = SEGMENT_MATCH_THRESHOLD,
|
||||
min_consecutive: Optional[int] = None,
|
||||
min_consecutive: int = MIN_CONSECUTIVE_MATCHES,
|
||||
max_gap: int = MAX_GAP,
|
||||
neighbor_window: int = NEIGHBOR_WINDOW,
|
||||
) -> list[DuplicateSegment]:
|
||||
"""滑动窗口时序匹配:找出两组分片之间的重复片段(Issue #1702 重构)。
|
||||
"""滑动窗口时序匹配:找出两组分片之间的重复片段。
|
||||
|
||||
算法:
|
||||
1. 构建 query×target 全量汉明距离矩阵;每个 query chunk 保留所有
|
||||
距离 <= match_threshold 的候选 target 分片(与帧匹配判定同一阈值)。
|
||||
2. 时序一致贪心对齐:沿 query 时序推进,run 内优先选择与上一匹配帧
|
||||
目标序号连贯(|delta| <= neighbor_window+1,允许 ±1 邻接/时序偏移
|
||||
对齐——1s 密集采样下相邻帧 pHash 接近,最近邻在目标相邻帧间
|
||||
正/反向跳变均属正常,缓解场景切割切点、取帧错位、局部倒退)的
|
||||
候选;同距时偏好小索引(最早对齐位置)。
|
||||
3. 连贯匹配中允许 <= max_gap 帧间隙桥接;断裂后另起新 run——天然
|
||||
支持局部片段复用(复用片段可出现在任意时序位置,各成独立片段)。
|
||||
4. 连续匹配帧数 >= min_consecutive 的 run 报为重复片段。短视频自适应:
|
||||
min_consecutive = min(5, max(2, len(query_chunks)//2));n=1 时
|
||||
不形成片段,由调用方匹配帧回退兜底。
|
||||
1. 对每个 query chunk,找到 target 中汉明距离最小的 chunk
|
||||
2. 距离 <= match_threshold 视为匹配
|
||||
3. 找连续匹配的 run(允许 max_gap 帧间隙)
|
||||
4. 连续匹配数 >= min_consecutive 的 run 报告为重复片段
|
||||
|
||||
Args:
|
||||
query_chunks: 查询视频的分片列表(FingerprintChunk 或 dict)
|
||||
target_chunks: 目标视频的分片列表
|
||||
match_threshold: 汉明距离匹配阈值(统一常量 PHASH_THRESHOLD)
|
||||
min_consecutive: 最少连续匹配帧数;None 时按短视频自适应
|
||||
match_threshold: 汉明距离匹配阈值
|
||||
min_consecutive: 最少连续匹配帧数
|
||||
max_gap: 允许的最大间隙帧数
|
||||
neighbor_window: 时序对齐允许的目标分片序号邻接窗口(正/反向均允许)
|
||||
|
||||
Returns:
|
||||
DuplicateSegment 列表
|
||||
@@ -401,94 +321,95 @@ def find_duplicate_segments(
|
||||
if not query_chunks or not target_chunks:
|
||||
return []
|
||||
|
||||
def _get(chunk, key):
|
||||
def _get_phash(chunk) -> str:
|
||||
if isinstance(chunk, dict):
|
||||
return chunk[key]
|
||||
return getattr(chunk, key)
|
||||
return chunk["phash_binary"]
|
||||
return chunk.phash_binary
|
||||
|
||||
n, m = len(query_chunks), len(target_chunks)
|
||||
q_ph = [_get(c, "phash_binary") for c in query_chunks]
|
||||
t_ph = [_get(c, "phash_binary") for c in target_chunks]
|
||||
def _get_start(chunk) -> int:
|
||||
if isinstance(chunk, dict):
|
||||
return chunk["start_time_ms"]
|
||||
return chunk.start_time_ms
|
||||
|
||||
# Step 1: 全量距离矩阵。每个 query chunk 保留所有 <= 阈值的候选 target,
|
||||
# 按距离升序;同距时小索引优先(取最早的对齐位置,贪心连贯推进时最保守,
|
||||
# 不会越过复用片段末端;重复 hash 的连续帧由 Step 2 的连贯性窗口约束)。
|
||||
candidates: list[list[tuple[int, int]]] = [] # 每 query 帧: [(target_idx, dist), ...]
|
||||
for i in range(n):
|
||||
dists = [hamming_distance(q_ph[i], t_ph[j]) for j in range(m)]
|
||||
cand = [(j, d) for j, d in enumerate(dists) if d <= match_threshold]
|
||||
cand.sort(key=lambda x: (x[1], x[0]))
|
||||
candidates.append(cand)
|
||||
def _get_end(chunk) -> int:
|
||||
if isinstance(chunk, dict):
|
||||
return chunk["end_time_ms"]
|
||||
return chunk.end_time_ms
|
||||
|
||||
# 短视频自适应连续匹配门槛(Issue #1702 工单公式):
|
||||
# MIN_CONSECUTIVE_MATCHES = min(5, max(2, 分片数//2))。
|
||||
# n=1 时门槛为 2 不形成片段,由 _evaluate_candidate 的匹配帧回退
|
||||
# (temporal_coverage 按匹配帧占比估计)兜底检出,不回归。
|
||||
if min_consecutive is None:
|
||||
min_consecutive = min(MIN_CONSECUTIVE_MATCHES, max(2, n // 2))
|
||||
# Step 1: 逐帧匹配
|
||||
frame_matches: list[tuple[bool, int, int]] = [] # (is_match, min_dist, best_target_idx)
|
||||
for qc in query_chunks:
|
||||
qc_phash = _get_phash(qc)
|
||||
best_dist = 64
|
||||
best_idx = 0
|
||||
for j, tc in enumerate(target_chunks):
|
||||
d = hamming_distance(qc_phash, _get_phash(tc))
|
||||
if d < best_dist:
|
||||
best_dist = d
|
||||
best_idx = j
|
||||
frame_matches.append((best_dist <= match_threshold, best_dist, best_idx))
|
||||
|
||||
# Step 2: 时序一致贪心对齐。
|
||||
# run 内偏好与上一匹配帧目标序号连贯(|delta| <= neighbor_window+1,
|
||||
# 支持 ±1 邻接窗口/时序偏移对齐,正反向抖动均允许)的候选;
|
||||
# 无连贯候选时关闭旧 run。
|
||||
# 这天然支持局部片段复用:同一 query 视频中多个复用片段各自形成独立 run。
|
||||
frame_matches: list[tuple[bool, int, int]] = []
|
||||
runs: list[tuple[int, int]] = []
|
||||
run_start: Optional[int] = None
|
||||
run_last_t: Optional[int] = None
|
||||
# Step 2: 找连续匹配的 runs
|
||||
runs: list[tuple[int, int]] = [] # list of (start_idx, end_idx)
|
||||
run_start = None
|
||||
gap_count = 0
|
||||
|
||||
def _matching_count(a: int, b: int) -> int:
|
||||
return sum(1 for k in range(a, b + 1) if frame_matches[k][0])
|
||||
|
||||
def _close_run(a: int, b: int) -> None:
|
||||
if b >= a and _matching_count(a, b) >= min_consecutive:
|
||||
runs.append((a, b))
|
||||
|
||||
for i in range(n):
|
||||
cand = candidates[i]
|
||||
if run_last_t is None:
|
||||
chosen = cand[0] if cand else None
|
||||
else:
|
||||
chosen = next(
|
||||
(c for c in cand if abs(c[0] - run_last_t) <= neighbor_window + 1),
|
||||
None,
|
||||
)
|
||||
|
||||
if chosen is not None:
|
||||
tidx, dist = chosen
|
||||
frame_matches.append((True, dist, tidx))
|
||||
for i, (is_match, _dist, _idx) in enumerate(frame_matches):
|
||||
if is_match:
|
||||
if run_start is None:
|
||||
run_start = i
|
||||
gap_count = 0
|
||||
run_last_t = tidx
|
||||
gap_count = 0 # 重置间隙
|
||||
else:
|
||||
frame_matches.append((False, match_threshold + 1, -1))
|
||||
if run_start is not None:
|
||||
gap_count += 1
|
||||
if gap_count > max_gap:
|
||||
# 非匹配帧从 i-gap_count+1 开始,run 结束于其前一帧
|
||||
_close_run(run_start, i - gap_count)
|
||||
run_start, run_last_t, gap_count = None, None, 0
|
||||
# 中断当前 run
|
||||
run_end = i - gap_count # 最后一个匹配帧的索引
|
||||
# 计算 run 内的实际匹配帧数(总跨度 - 间隙数)
|
||||
total_gaps = sum(1 for k in range(run_start, run_end + 1) if not frame_matches[k][0])
|
||||
matching_count = (run_end - run_start + 1) - total_gaps
|
||||
if matching_count >= min_consecutive:
|
||||
runs.append((run_start, run_end))
|
||||
run_start = None
|
||||
gap_count = 0
|
||||
|
||||
# 处理末尾 run
|
||||
if run_start is not None:
|
||||
last_idx = n - 1
|
||||
last_idx = len(frame_matches) - 1
|
||||
# 回退找到最后一个匹配帧的位置(跳过尾部非匹配帧)
|
||||
while last_idx >= run_start and not frame_matches[last_idx][0]:
|
||||
last_idx -= 1
|
||||
_close_run(run_start, last_idx)
|
||||
if last_idx >= run_start:
|
||||
# 计算 run 内的总间隙数
|
||||
total_gaps = sum(1 for k in range(run_start, last_idx + 1) if not frame_matches[k][0])
|
||||
matching_count = (last_idx - run_start + 1) - total_gaps
|
||||
if matching_count >= min_consecutive:
|
||||
runs.append((run_start, last_idx))
|
||||
|
||||
# Step 3: 构建 DuplicateSegment
|
||||
segments: list[DuplicateSegment] = []
|
||||
for start, end in runs:
|
||||
query_start = _get_start(query_chunks[start])
|
||||
query_end = _get_end(query_chunks[end])
|
||||
|
||||
# 取目标范围(按最佳匹配的目标 chunk 时间范围)
|
||||
target_indices = [frame_matches[k][2] for k in range(start, end + 1) if frame_matches[k][0]]
|
||||
t_min, t_max = min(target_indices), max(target_indices)
|
||||
avg_dist = sum(frame_matches[k][1] for k in range(start, end + 1) if frame_matches[k][0]) / len(target_indices)
|
||||
if target_indices:
|
||||
t_min = min(target_indices)
|
||||
t_max = max(target_indices)
|
||||
target_start = _get_start(target_chunks[t_min])
|
||||
target_end = _get_end(target_chunks[t_max])
|
||||
else:
|
||||
target_start = _get_start(target_chunks[0])
|
||||
target_end = _get_end(target_chunks[-1])
|
||||
|
||||
avg_dist = sum(frame_matches[k][1] for k in range(start, end + 1)) / (end - start + 1)
|
||||
segments.append(
|
||||
DuplicateSegment(
|
||||
query_start_ms=_get(query_chunks[start], "start_time_ms"),
|
||||
query_end_ms=_get(query_chunks[end], "end_time_ms"),
|
||||
target_start_ms=_get(target_chunks[t_min], "start_time_ms"),
|
||||
target_end_ms=_get(target_chunks[t_max], "end_time_ms"),
|
||||
query_start_ms=query_start,
|
||||
query_end_ms=query_end,
|
||||
target_start_ms=target_start,
|
||||
target_end_ms=target_end,
|
||||
avg_distance=avg_dist,
|
||||
)
|
||||
)
|
||||
@@ -502,9 +423,7 @@ def find_duplicate_segments(
|
||||
class VideoDeduplicator:
|
||||
"""Video deduplication using multiple fingerprint methods."""
|
||||
|
||||
# Issue #1702: 阈值统一来源为模块常量 PHASH_THRESHOLD(#1658 曾收紧到 8,
|
||||
# 后经 staging 真实同源/异源指纹分布重新校准,见 test_phash_threshold_calibration_1702)。
|
||||
PHASH_THRESHOLD = PHASH_THRESHOLD
|
||||
PHASH_THRESHOLD = 8 # Issue #1658: pHash 汉明距离阈值由 10 收紧到 8,降低不同视频误判率
|
||||
HISTOGRAM_THRESHOLD = 0.85
|
||||
|
||||
@staticmethod
|
||||
@@ -528,36 +447,27 @@ class VideoDeduplicator:
|
||||
# 单帧不视为坏指纹(短视频或抽帧不足)
|
||||
if len(phashes) == 1:
|
||||
return False
|
||||
# Issue #1702: 旧逻辑"所有 phash 完全相同即判黑屏"会误杀短视频——
|
||||
# 11s 视频只有几个不同镜头时,相邻 1s 采样帧可能 phash 完全一致(内容
|
||||
# 连续但非黑屏)。黑屏的特征是「大量帧全部无内容」,要求至少 8 帧
|
||||
# 且相同帧占比 >=80% 才判坏;短视频(<8 帧)只有真正单值时交给
|
||||
# _bhattacharyya/融合分兜底,不因"帧都一样"直接跳过。
|
||||
if len(phashes) < 8:
|
||||
return False
|
||||
# 多帧但所有 phash 完全相同 → 黑屏/纯色视频
|
||||
unique = set(phashes)
|
||||
same_ratio = sum(1 for x in phashes if x == phashes[0]) / len(phashes)
|
||||
if len(unique) == 1 and same_ratio >= 0.8:
|
||||
if len(unique) == 1:
|
||||
return True
|
||||
# 多帧但所有唯一 phash 之间的汉明距离都极小(<3)且占比 >=80% → 近似黑屏
|
||||
# 多帧但所有 phash 之间的汉明距离都极小(<3)→ 近似黑屏
|
||||
phash_list = list(unique)
|
||||
if len(phash_list) >= 2 and same_ratio >= 0.8:
|
||||
all_distances = [
|
||||
hamming_distance(phash_list[i], phash_list[j])
|
||||
for i in range(len(phash_list))
|
||||
for j in range(i + 1, len(phash_list))
|
||||
]
|
||||
if len(phash_list) >= 2:
|
||||
all_distances = []
|
||||
for i in range(len(phash_list)):
|
||||
for j in range(i + 1, len(phash_list)):
|
||||
all_distances.append(hamming_distance(phash_list[i], phash_list[j]))
|
||||
if all_distances and max(all_distances) < 3:
|
||||
return True
|
||||
return False
|
||||
|
||||
def compute_fingerprint(self, video_path: str) -> VideoFingerprint:
|
||||
"""Compute video fingerprint using dense uniform sampling.
|
||||
"""Compute video fingerprint using dynamic keyframe detection.
|
||||
|
||||
Issue #1702: 使用 sample_fingerprint_timestamps() 固定 1s 间隔密集均匀
|
||||
采样(替代动态场景检测抽帧),保证两个同源视频复用片段的帧时刻天然
|
||||
对齐;每帧取中心 90% 区域(center_crop_frame)计算 pHash + color_histogram,
|
||||
绕开 random_edge_crop 降重裁剪污染;MD5 仍基于原始帧。
|
||||
使用 detect_keyframe_timestamps() 检测内容感知关键帧,
|
||||
在每个关键帧处取帧计算 pHash + color_histogram。
|
||||
同时保留 MD5 计算和分片数据结构。
|
||||
"""
|
||||
cap = cv2.VideoCapture(video_path)
|
||||
if not cap.isOpened():
|
||||
@@ -571,8 +481,8 @@ class VideoDeduplicator:
|
||||
|
||||
cap.release()
|
||||
|
||||
# 1. 固定间隔密集采样(Issue #1702:替代动态场景检测,保证跨视频时序对齐)
|
||||
keyframe_times = sample_fingerprint_timestamps(duration)
|
||||
# 1. 检测关键帧时间戳
|
||||
keyframe_times = detect_keyframe_timestamps(video_path)
|
||||
|
||||
if not keyframe_times:
|
||||
return VideoFingerprint(
|
||||
@@ -596,15 +506,12 @@ class VideoDeduplicator:
|
||||
if not ret:
|
||||
continue
|
||||
|
||||
# MD5 计算(基于原始帧,指纹文件级去重不受裁剪影响)
|
||||
# MD5 计算
|
||||
_, buffer = cv2.imencode(".jpg", frame)
|
||||
md5_hash.update(buffer)
|
||||
|
||||
# Issue #1702: pHash / 颜色直方图基于中心 90% 区域,绕开 random_edge_crop
|
||||
# 降重裁剪对指纹的污染(降重只服务外部平台,不污染自查重)。
|
||||
fp_frame = center_crop_frame(frame)
|
||||
phash = compute_phash(fp_frame)
|
||||
hist = compute_color_histogram(fp_frame)
|
||||
phash = compute_phash(frame)
|
||||
hist = compute_color_histogram(frame)
|
||||
|
||||
# 计算分片时间范围(从前一个关键帧到下一个关键帧的中点)
|
||||
prev_boundary = keyframe_times[i - 1] * 1000 if i > 0 else 0
|
||||
@@ -657,22 +564,12 @@ class VideoDeduplicator:
|
||||
|
||||
@staticmethod
|
||||
def _bhattacharyya_coefficient(hist_a: list[float], hist_b: list[float]) -> float:
|
||||
"""Bhattacharyya 系数(概率分布版,范围 [0,1],1=完全相同)。
|
||||
|
||||
Issue #1702: compute_color_histogram 输出 3 通道拼接、每通道独立 NORM_L1
|
||||
(单通道 Σ=1,三通道拼接向量 Σ=3)。旧实现直接 Σ√(a*b) 对三通道拼接向量
|
||||
算出 ~3(旧 L2 归一化更是算出 ~14.9),不是合法的概率系数。
|
||||
这里按两个直方图各自的总量归一:BC = Σ√(a*b) / √(Σa·Σb)。
|
||||
- 单通道概率分布(Σa=Σb=1):分母 1,与旧测试/教科书定义一致;
|
||||
- 三通道拼接(Σa=Σb=3):分母 3,结果在 [0,1]。
|
||||
"""
|
||||
"""Bhattacharyya 系数:Σ √(a[i] * b[i]),范围 [0, 1],1=完全相同。"""
|
||||
min_len = min(len(hist_a), len(hist_b))
|
||||
a = [max(0.0, float(x)) for x in hist_a[:min_len]]
|
||||
b = [max(0.0, float(x)) for x in hist_b[:min_len]]
|
||||
# max(0.0, ...) 防御上游异常负值导致 sqrt domain error
|
||||
coeff = sum(math.sqrt(ai * bi) for ai, bi in zip(a, b, strict=False))
|
||||
norm = math.sqrt(sum(a) * sum(b))
|
||||
return float(coeff / norm) if norm > 0 else 0.0
|
||||
a = hist_a[:min_len]
|
||||
b = hist_b[:min_len]
|
||||
# 纯标准库计算(不依赖 numpy);max(0.0, ...) 防御上游异常负值导致 sqrt domain error
|
||||
return float(sum(math.sqrt(max(0.0, ai * bi)) for ai, bi in zip(a, b, strict=False)))
|
||||
|
||||
@staticmethod
|
||||
def _compute_histogram_similarity(
|
||||
@@ -714,72 +611,6 @@ class VideoDeduplicator:
|
||||
hist_similarity = VideoDeduplicator._compute_histogram_similarity(hist_a, hist_b) if hist_b else 0.5
|
||||
return PHASH_WEIGHT * phash_similarity + HISTOGRAM_WEIGHT * hist_similarity
|
||||
|
||||
@staticmethod
|
||||
def _evaluate_candidate(
|
||||
fingerprint: VideoFingerprint,
|
||||
existing_phashes: list[str],
|
||||
existing_histograms: list,
|
||||
existing_chunk_objects: list,
|
||||
*,
|
||||
query_duration_sec: float,
|
||||
) -> dict:
|
||||
"""评估新视频指纹与单个候选视频的相似度(Issue #1702 共享逻辑)。
|
||||
|
||||
指标:
|
||||
- min_distances / frame_match_rate:每个新分片到候选视频全局最近邻的汉明距离,
|
||||
分母取两视频分片数的较小值(支持局部片段复用:短视频复用长视频片段时不被长视频分母稀释)。
|
||||
- temporal_coverage:时序一致连续匹配片段总时长 / 新视频时长(局部复用主指标)。
|
||||
- fusion:pHash 中位数距离 + 颜色直方图的加权融合分。
|
||||
|
||||
Returns:
|
||||
{frame_match_rate, temporal_coverage, segments, median_distance,
|
||||
fusion, matching_frames, min_distances}
|
||||
"""
|
||||
query_phashes = fingerprint.keyframe_phashes or []
|
||||
if not query_phashes or not existing_phashes:
|
||||
return {
|
||||
"frame_match_rate": 0.0,
|
||||
"temporal_coverage": 0.0,
|
||||
"segments": [],
|
||||
"median_distance": 64,
|
||||
"fusion": 0.0,
|
||||
"matching_frames": 0,
|
||||
"min_distances": [],
|
||||
}
|
||||
|
||||
min_distances = [min(hamming_distance(ph, ep) for ep in existing_phashes) for ph in query_phashes]
|
||||
matching_frames = sum(1 for d in min_distances if d <= PHASH_THRESHOLD)
|
||||
# 分母取 min(两视频分片数):局部复用时(如 B 的 5 片复用 A 9 片中的若干片)
|
||||
# 命中帧占比不因候选视频更长而被稀释。
|
||||
frame_match_rate = matching_frames / min(len(query_phashes), len(existing_phashes))
|
||||
|
||||
segments = find_duplicate_segments(fingerprint.chunks, existing_chunk_objects)
|
||||
duration_ms = query_duration_sec * 1000 if query_duration_sec else 0
|
||||
if duration_ms > 0 and segments:
|
||||
covered_ms = sum(s.query_end_ms - s.query_start_ms for s in segments)
|
||||
temporal_coverage = min(covered_ms / duration_ms, 1.0)
|
||||
elif matching_frames > 0:
|
||||
# 无连续片段(时序连贯性不足)时,按匹配帧占比估计覆盖:
|
||||
# 密集 1s 采样下每个分片≈1s 等权时间片,匹配帧数≈命中秒数。
|
||||
temporal_coverage = min(frame_match_rate, 1.0)
|
||||
else:
|
||||
temporal_coverage = 0.0
|
||||
|
||||
median_distance = statistics.median(min_distances) if min_distances else 64
|
||||
fusion = VideoDeduplicator._compute_fusion_score(
|
||||
median_distance, fingerprint.color_histograms, existing_histograms
|
||||
)
|
||||
|
||||
return {
|
||||
"frame_match_rate": frame_match_rate,
|
||||
"temporal_coverage": temporal_coverage,
|
||||
"segments": segments,
|
||||
"median_distance": median_distance,
|
||||
"fusion": fusion,
|
||||
"matching_frames": matching_frames,
|
||||
"min_distances": min_distances,
|
||||
}
|
||||
|
||||
def check_duplicate(
|
||||
self,
|
||||
fingerprint: VideoFingerprint,
|
||||
@@ -789,7 +620,6 @@ class VideoDeduplicator:
|
||||
scope: str = "project",
|
||||
user_id: str = "",
|
||||
duration_sec: float = 0,
|
||||
exclude_video_id: str | None = None,
|
||||
) -> Optional[dict]:
|
||||
"""检查视频是否与已有视频重复。
|
||||
|
||||
@@ -806,9 +636,6 @@ class VideoDeduplicator:
|
||||
scope: "project" 项目内查重(默认),"user" 跨项目全局查重
|
||||
user_id: 用户 ID(scope="user" 时使用)
|
||||
duration_sec: 视频时长(秒),用于时长预过滤 ±15%
|
||||
exclude_video_id: 排除的视频 ID(查重自身时用)。recompute-dedup
|
||||
重算时视频记录已存在,不排除会自匹配(距离 0 分最高)导致
|
||||
duplicate_of 指向自己(Issue #1702 连带修复)。
|
||||
|
||||
Returns:
|
||||
重复信息字典(含 duplicate, duplicate_of, reason, similarity, duplicate_segments),
|
||||
@@ -816,22 +643,13 @@ class VideoDeduplicator:
|
||||
"""
|
||||
video_repo = SQLAlchemyGeneratedVideoRepository(session)
|
||||
if scope == "user" and user_id:
|
||||
# Issue #1702: 不做 ±15% 时长预过滤。旧逻辑按 duration_sec 缩小候选窗口,
|
||||
# 但局部片段复用的两个视频时长必然不同(证据视频 20s vs 11s,差 42%),
|
||||
# ±15% 窗口让同源视频互相不可见 → is_duplicate 恒 False。
|
||||
# 全量遍历同用户视频(与 compute_duplicate_rate 口径一致),异源视频由
|
||||
# fusion/temporal_coverage 阈值天然过滤(校准:异源最小汉明距离 24)。
|
||||
existing_videos = video_repo.list_by_user(user_id)
|
||||
dur_min = duration_sec * 0.85 if duration_sec > 0 else 0
|
||||
dur_max = duration_sec * 1.15 if duration_sec > 0 else 0
|
||||
existing_videos = video_repo.list_by_user(user_id, duration_min=dur_min, duration_max=dur_max)
|
||||
else:
|
||||
existing_videos = video_repo.list_by_project(project_id)
|
||||
|
||||
best_score = 0.0
|
||||
best_result: Optional[dict] = None
|
||||
|
||||
for existing in existing_videos:
|
||||
# 排除自身(recompute 时当前视频已在候选列表里,否则自匹配距离 0 必最高分)
|
||||
if exclude_video_id and existing.id == exclude_video_id:
|
||||
continue
|
||||
if not existing.video_fingerprint:
|
||||
continue
|
||||
|
||||
@@ -859,70 +677,61 @@ class VideoDeduplicator:
|
||||
if not existing_phashes:
|
||||
continue
|
||||
|
||||
# 直方图 / 分片对象(chunk 表优先,回退 JSON 字段;JSON NULL 显式回退空列表)
|
||||
# 计算每个新关键帧到已有关键帧的最小汉明距离
|
||||
min_distances = []
|
||||
for phash in fingerprint.keyframe_phashes:
|
||||
distances = [hamming_distance(phash, ep) for ep in existing_phashes]
|
||||
min_distances.append(min(distances))
|
||||
|
||||
# 帧匹配比例检查
|
||||
matching_frames = sum(1 for d in min_distances if d < self.PHASH_THRESHOLD)
|
||||
match_ratio = matching_frames / len(min_distances) if min_distances else 0
|
||||
if match_ratio < MATCH_RATIO_THRESHOLD:
|
||||
continue
|
||||
|
||||
# 中位数距离
|
||||
median_distance = statistics.median(min_distances) if min_distances else 64
|
||||
if median_distance >= self.PHASH_THRESHOLD:
|
||||
continue
|
||||
|
||||
# 直方图融合(chunk 表优先,回退 JSON 字段;JSON NULL 显式回退空列表)
|
||||
if chunk_data:
|
||||
existing_histograms = [c["color_histogram"] for c in chunk_data if c.get("color_histogram")]
|
||||
existing_chunk_objects = chunk_data
|
||||
else:
|
||||
existing_histograms = ef.get("color_histograms") or []
|
||||
existing_chunk_objects = [
|
||||
{"phash_binary": pp, "start_time_ms": 0, "end_time_ms": 0} for pp in existing_phashes
|
||||
]
|
||||
|
||||
# Issue #1702: 统一评估每个候选(含局部片段复用),不再用
|
||||
# "frame_match_rate<0.7 整条跳过" 的硬门槛——局部复用(如 B 结尾 2s
|
||||
# ≈ A 中间 2s)帧比例天然低,但 coverage 能检出。
|
||||
ev = self._evaluate_candidate(
|
||||
fingerprint,
|
||||
existing_phashes,
|
||||
existing_histograms,
|
||||
existing_chunk_objects,
|
||||
query_duration_sec=fingerprint.duration,
|
||||
)
|
||||
logger.debug(
|
||||
"check_duplicate candidate=%s min_distances=%s frame_match_rate=%.3f "
|
||||
"temporal_coverage=%.3f median=%.1f fusion=%.3f segments=%d",
|
||||
existing.id,
|
||||
ev["min_distances"],
|
||||
ev["frame_match_rate"],
|
||||
ev["temporal_coverage"],
|
||||
ev["median_distance"],
|
||||
ev["fusion"],
|
||||
len(ev["segments"]),
|
||||
combined_score = self._compute_fusion_score(
|
||||
median_distance, fingerprint.color_histograms, existing_histograms
|
||||
)
|
||||
|
||||
# 全片重复判定:融合分过阈 且(帧匹配比例 >=70% 或 局部覆盖 >=50%)
|
||||
is_full_duplicate = ev["fusion"] >= DUPLICATE_THRESHOLD and (
|
||||
ev["frame_match_rate"] >= MATCH_RATIO_THRESHOLD or ev["temporal_coverage"] >= PARTIAL_COVERAGE_THRESHOLD
|
||||
if combined_score < DUPLICATE_THRESHOLD:
|
||||
continue
|
||||
|
||||
# 滑动窗口时序匹配:获取具体重复片段
|
||||
existing_chunk_objects = (
|
||||
chunk_data
|
||||
if chunk_data
|
||||
else [{"phash_binary": p, "start_time_ms": 0, "end_time_ms": 0} for p in existing_phashes]
|
||||
)
|
||||
if is_full_duplicate and ev["fusion"] > best_score:
|
||||
best_score = ev["fusion"]
|
||||
best_result = {
|
||||
"duplicate": True,
|
||||
"duplicate_of": existing.id,
|
||||
"reason": "phash_histogram_fusion",
|
||||
"similarity": ev["fusion"],
|
||||
"duplicate_segments": [
|
||||
{
|
||||
"query_start_ms": s.query_start_ms,
|
||||
"query_end_ms": s.query_end_ms,
|
||||
"target_start_ms": s.target_start_ms,
|
||||
"target_end_ms": s.target_end_ms,
|
||||
"avg_distance": round(s.avg_distance, 2),
|
||||
}
|
||||
for s in ev["segments"]
|
||||
],
|
||||
}
|
||||
segments = find_duplicate_segments(fingerprint.chunks, existing_chunk_objects)
|
||||
|
||||
return {
|
||||
"duplicate": True,
|
||||
"duplicate_of": existing.id,
|
||||
"reason": "phash_histogram_fusion",
|
||||
"similarity": combined_score,
|
||||
"duplicate_segments": [
|
||||
{
|
||||
"query_start_ms": s.query_start_ms,
|
||||
"query_end_ms": s.query_end_ms,
|
||||
"target_start_ms": s.target_start_ms,
|
||||
"target_end_ms": s.target_end_ms,
|
||||
"avg_distance": round(s.avg_distance, 2),
|
||||
}
|
||||
for s in segments
|
||||
],
|
||||
}
|
||||
|
||||
if best_result:
|
||||
return best_result
|
||||
logger.info(
|
||||
"check_duplicate no match (project=%s scope=%s): %d candidates evaluated, best_fusion=%.3f",
|
||||
project_id,
|
||||
scope,
|
||||
len(existing_videos),
|
||||
best_score,
|
||||
)
|
||||
return None
|
||||
|
||||
def check_batch_duplicate(
|
||||
@@ -954,9 +763,6 @@ class VideoDeduplicator:
|
||||
video_repo = SQLAlchemyGeneratedVideoRepository(session)
|
||||
batch_videos = video_repo.list_by_batch(batch_id)
|
||||
|
||||
best_score = 0.0
|
||||
best_result: Optional[dict] = None
|
||||
|
||||
for existing in batch_videos:
|
||||
if existing.id == current_video_id:
|
||||
continue
|
||||
@@ -990,59 +796,59 @@ class VideoDeduplicator:
|
||||
if not existing_phashes:
|
||||
continue
|
||||
|
||||
min_distances = []
|
||||
for phash in fingerprint.keyframe_phashes:
|
||||
distances = [hamming_distance(phash, ep) for ep in existing_phashes]
|
||||
min_distances.append(min(distances))
|
||||
|
||||
# 帧匹配比例检查
|
||||
matching_frames = sum(1 for d in min_distances if d < self.PHASH_THRESHOLD)
|
||||
match_ratio = matching_frames / len(min_distances) if min_distances else 0
|
||||
if match_ratio < MATCH_RATIO_THRESHOLD:
|
||||
continue
|
||||
|
||||
median_distance = statistics.median(min_distances) if min_distances else 64
|
||||
if median_distance >= self.PHASH_THRESHOLD:
|
||||
continue
|
||||
|
||||
# 直方图融合(chunk 表优先,回退 JSON 字段;JSON NULL 显式回退空列表)
|
||||
if chunk_data:
|
||||
existing_histograms = [c["color_histogram"] for c in chunk_data if c.get("color_histogram")]
|
||||
existing_chunk_objects = chunk_data
|
||||
else:
|
||||
existing_histograms = ef.get("color_histograms") or []
|
||||
existing_chunk_objects = [
|
||||
{"phash_binary": pp, "start_time_ms": 0, "end_time_ms": 0} for pp in existing_phashes
|
||||
]
|
||||
|
||||
ev = self._evaluate_candidate(
|
||||
fingerprint,
|
||||
existing_phashes,
|
||||
existing_histograms,
|
||||
existing_chunk_objects,
|
||||
query_duration_sec=fingerprint.duration,
|
||||
)
|
||||
logger.debug(
|
||||
"check_batch_duplicate candidate=%s min_distances=%s frame_match_rate=%.3f "
|
||||
"temporal_coverage=%.3f median=%.1f fusion=%.3f segments=%d",
|
||||
existing.id,
|
||||
ev["min_distances"],
|
||||
ev["frame_match_rate"],
|
||||
ev["temporal_coverage"],
|
||||
ev["median_distance"],
|
||||
ev["fusion"],
|
||||
len(ev["segments"]),
|
||||
combined_score = self._compute_fusion_score(
|
||||
median_distance, fingerprint.color_histograms, existing_histograms
|
||||
)
|
||||
|
||||
is_full_duplicate = ev["fusion"] >= DUPLICATE_THRESHOLD and (
|
||||
ev["frame_match_rate"] >= MATCH_RATIO_THRESHOLD or ev["temporal_coverage"] >= PARTIAL_COVERAGE_THRESHOLD
|
||||
if combined_score < DUPLICATE_THRESHOLD:
|
||||
continue
|
||||
|
||||
# 滑动窗口时序匹配
|
||||
existing_chunk_objects = (
|
||||
chunk_data
|
||||
if chunk_data
|
||||
else [{"phash_binary": p, "start_time_ms": 0, "end_time_ms": 0} for p in existing_phashes]
|
||||
)
|
||||
if is_full_duplicate and ev["fusion"] > best_score:
|
||||
best_score = ev["fusion"]
|
||||
best_result = {
|
||||
"duplicate": True,
|
||||
"duplicate_of": existing.id,
|
||||
"reason": "batch_phash_histogram_fusion",
|
||||
"similarity": ev["fusion"],
|
||||
"duplicate_segments": [
|
||||
{
|
||||
"query_start_ms": s.query_start_ms,
|
||||
"query_end_ms": s.query_end_ms,
|
||||
"target_start_ms": s.target_start_ms,
|
||||
"target_end_ms": s.target_end_ms,
|
||||
"avg_distance": round(s.avg_distance, 2),
|
||||
}
|
||||
for s in ev["segments"]
|
||||
],
|
||||
}
|
||||
segments = find_duplicate_segments(fingerprint.chunks, existing_chunk_objects)
|
||||
|
||||
return {
|
||||
"duplicate": True,
|
||||
"duplicate_of": existing.id,
|
||||
"reason": "batch_phash_histogram_fusion",
|
||||
"similarity": combined_score,
|
||||
"duplicate_segments": [
|
||||
{
|
||||
"query_start_ms": s.query_start_ms,
|
||||
"query_end_ms": s.query_end_ms,
|
||||
"target_start_ms": s.target_start_ms,
|
||||
"target_end_ms": s.target_end_ms,
|
||||
"avg_distance": round(s.avg_distance, 2),
|
||||
}
|
||||
for s in segments
|
||||
],
|
||||
}
|
||||
|
||||
if best_result:
|
||||
return best_result
|
||||
logger.info("check_batch_duplicate no match (batch=%s): best_fusion=%.3f", batch_id, best_score)
|
||||
return None
|
||||
|
||||
def compute_duplicate_rate(
|
||||
@@ -1091,7 +897,8 @@ class VideoDeduplicator:
|
||||
max_duplicate_rate = 0.0
|
||||
max_visual_similarity = 0.0
|
||||
match_count = 0
|
||||
evaluated = 0
|
||||
|
||||
total_duration_ms = fingerprint.duration if fingerprint.duration else 0
|
||||
|
||||
for existing in existing_videos:
|
||||
if current_video_id and existing.id == current_video_id:
|
||||
@@ -1126,63 +933,57 @@ class VideoDeduplicator:
|
||||
if not existing_phashes or not fingerprint.keyframe_phashes:
|
||||
continue
|
||||
|
||||
# 直方图 / 分片对象(chunk 表优先,回退 JSON 字段;JSON NULL 显式回退空列表)
|
||||
min_distances = []
|
||||
for phash in fingerprint.keyframe_phashes:
|
||||
distances = [hamming_distance(phash, ep) for ep in existing_phashes]
|
||||
min_distances.append(min(distances))
|
||||
|
||||
# frame_match_rate
|
||||
total_frames = len(min_distances)
|
||||
if total_frames == 0:
|
||||
continue
|
||||
matching_frames = sum(1 for d in min_distances if d < self.PHASH_THRESHOLD)
|
||||
frame_match_rate = matching_frames / total_frames
|
||||
|
||||
# 帧匹配比例太低则跳过
|
||||
if frame_match_rate < 0.3:
|
||||
continue
|
||||
|
||||
# temporal_coverage_rate via find_duplicate_segments
|
||||
existing_chunk_objects = (
|
||||
chunk_data
|
||||
if chunk_data
|
||||
else [{"phash_binary": p, "start_time_ms": 0, "end_time_ms": 0} for p in existing_phashes]
|
||||
)
|
||||
segments = find_duplicate_segments(fingerprint.chunks, existing_chunk_objects)
|
||||
|
||||
if total_duration_ms > 0 and segments:
|
||||
covered_ms = sum(s.query_end_ms - s.query_start_ms for s in segments)
|
||||
temporal_coverage_rate = min(covered_ms / total_duration_ms, 1.0)
|
||||
else:
|
||||
temporal_coverage_rate = 0.0
|
||||
|
||||
# duplicate_rate = 0.4 * frame_match_rate + 0.6 * temporal_coverage_rate
|
||||
dup_rate = (frame_match_rate * 0.4 + temporal_coverage_rate * 0.6) * 100
|
||||
|
||||
# visual_similarity (融合相似度,归一化 0~1)
|
||||
median_distance = statistics.median(min_distances) if min_distances else 64
|
||||
if chunk_data:
|
||||
existing_histograms = [c["color_histogram"] for c in chunk_data if c.get("color_histogram")]
|
||||
existing_chunk_objects = chunk_data
|
||||
else:
|
||||
# JSON NULL 显式回退空列表
|
||||
existing_histograms = ef.get("color_histograms") or []
|
||||
existing_chunk_objects = [
|
||||
{"phash_binary": pp, "start_time_ms": 0, "end_time_ms": 0} for pp in existing_phashes
|
||||
]
|
||||
|
||||
# Issue #1702: 统一评估;frame_match_rate 分母为 min(两视频分片数),
|
||||
# temporal_coverage 时长量纲在 _evaluate_candidate 内统一为毫秒。
|
||||
ev = self._evaluate_candidate(
|
||||
fingerprint,
|
||||
existing_phashes,
|
||||
existing_histograms,
|
||||
existing_chunk_objects,
|
||||
query_duration_sec=fingerprint.duration,
|
||||
)
|
||||
evaluated += 1
|
||||
logger.debug(
|
||||
"compute_duplicate_rate candidate=%s min_distances=%s frame_match_rate=%.3f "
|
||||
"temporal_coverage=%.3f median=%.1f fusion=%.3f segments=%d",
|
||||
existing.id,
|
||||
ev["min_distances"],
|
||||
ev["frame_match_rate"],
|
||||
ev["temporal_coverage"],
|
||||
ev["median_distance"],
|
||||
ev["fusion"],
|
||||
len(ev["segments"]),
|
||||
)
|
||||
visual_sim = self._compute_fusion_score(median_distance, fingerprint.color_histograms, existing_histograms)
|
||||
|
||||
# Issue #1702: 去掉 "frame_match_rate<0.3 整条跳过" 硬门槛——
|
||||
# 局部片段复用帧比例天然低;coverage 为主指标,0 匹配自然得 0 分。
|
||||
# duplicate_rate = 0.4 * frame_match_rate + 0.6 * temporal_coverage
|
||||
dup_rate = (min(ev["frame_match_rate"], 1.0) * 0.4 + ev["temporal_coverage"] * 0.6) * 100
|
||||
|
||||
# 全片重复计数与 check_duplicate 判定口径一致
|
||||
if ev["fusion"] >= DUPLICATE_THRESHOLD and (
|
||||
ev["frame_match_rate"] >= MATCH_RATIO_THRESHOLD or ev["temporal_coverage"] >= PARTIAL_COVERAGE_THRESHOLD
|
||||
):
|
||||
# 判定是否为重复(融合分数超过阈值)
|
||||
if visual_sim >= DUPLICATE_THRESHOLD:
|
||||
match_count += 1
|
||||
|
||||
if dup_rate > max_duplicate_rate:
|
||||
max_duplicate_rate = dup_rate
|
||||
max_visual_similarity = ev["fusion"]
|
||||
max_visual_similarity = visual_sim
|
||||
|
||||
logger.info(
|
||||
"compute_duplicate_rate done (project=%s scope=%s): evaluated=%d max_rate=%.2f%% "
|
||||
"max_visual_sim=%.3f matches=%d",
|
||||
project_id,
|
||||
scope,
|
||||
evaluated,
|
||||
max_duplicate_rate,
|
||||
max_visual_similarity,
|
||||
match_count,
|
||||
)
|
||||
return {
|
||||
"duplicate_rate": round(max(max_duplicate_rate, 0.0), 2),
|
||||
"visual_similarity": round(max_visual_similarity, 4),
|
||||
@@ -1198,20 +999,18 @@ def _save_fingerprint_chunks(
|
||||
session: Session,
|
||||
) -> None:
|
||||
"""将指纹分片数据批量写入 video_fingerprint_chunks 表。幂等:已有数据时跳过。"""
|
||||
# 幂等检查:已有分片数据则跳过
|
||||
existing_count = (
|
||||
session.query(VideoFingerprintChunkModel).filter(VideoFingerprintChunkModel.video_id == video_id).count()
|
||||
)
|
||||
if existing_count > 0:
|
||||
logger.debug("Fingerprint chunks already exist for video %s (%d chunks), skipping", video_id, existing_count)
|
||||
return
|
||||
|
||||
if not fingerprint.chunks:
|
||||
logger.warning("No chunks in fingerprint for video %s, skipping chunk save", video_id)
|
||||
return
|
||||
|
||||
# Issue #1702: recompute-dedup 重算时指纹算法已变(中心裁剪 + 新阈值),
|
||||
# 旧分片必须替换而非跳过(旧实现"有数据就跳过"导致重算不刷新分片表)。
|
||||
deleted = (
|
||||
session.query(VideoFingerprintChunkModel)
|
||||
.filter(VideoFingerprintChunkModel.video_id == video_id)
|
||||
.delete(synchronize_session=False)
|
||||
)
|
||||
if deleted:
|
||||
logger.info("Replaced %d stale fingerprint chunks for video %s", deleted, video_id)
|
||||
|
||||
chunk_models = fingerprint.to_chunk_models(video_id, project_id, user_id)
|
||||
session.bulk_save_objects(chunk_models)
|
||||
logger.info("Saved %d fingerprint chunks for video %s", len(chunk_models), video_id)
|
||||
@@ -1233,17 +1032,9 @@ def check_duplicate_task(self: Task, generated_video_id: str) -> dict:
|
||||
raise ValueError(f"Generated video {generated_video_id} not found")
|
||||
|
||||
local_path = os.path.join(temp_dir, f"{generated_video_id}.mp4")
|
||||
# Issue #1702: recompute 走的是 OSS 重新下载路径(正常生成流程用本地渲染文件,
|
||||
# 不经此任务)。成片真实 OSS key 是生成时的
|
||||
# generated/projects/{pid}/tasks/{task_id}/rendered_*.mp4(见 generation.py
|
||||
# _upload_and_record),旧代码硬编码 projects/{pid}/generated/{vid}/{vid}.mp4
|
||||
# 这个从不存在的 key,导致所有 recompute 任务下载 404、查重数据永远无法重算。
|
||||
# 优先从 file_url 解析真实 key,旧 key 模式仅作回退。
|
||||
download_key = getattr(video, "file_url", "") or ""
|
||||
if not download_key:
|
||||
download_key = f"projects/{video.project_id}/generated/{generated_video_id}/{generated_video_id}.mp4"
|
||||
logger.warning("video %s has no file_url, falling back to legacy key %s", generated_video_id, download_key)
|
||||
storage_service.download_file(download_key, local_path)
|
||||
storage_service.download_file(
|
||||
f"projects/{video.project_id}/generated/{generated_video_id}/{generated_video_id}.mp4", local_path
|
||||
)
|
||||
|
||||
fingerprint = deduplicator.compute_fingerprint(local_path)
|
||||
|
||||
@@ -1254,10 +1045,7 @@ def check_duplicate_task(self: Task, generated_video_id: str) -> dict:
|
||||
session,
|
||||
scope="user",
|
||||
user_id=video.user_id,
|
||||
# Issue #1702: fingerprint.duration 单位已经是秒,旧代码 /1000 导致
|
||||
# ±15% 时长预过滤窗口缩到 ~0.013s,scope=user 的跨项目查重永远返回 None。
|
||||
duration_sec=fingerprint.duration if fingerprint.duration else 0,
|
||||
exclude_video_id=generated_video_id,
|
||||
duration_sec=fingerprint.duration / 1000 if fingerprint.duration else 0,
|
||||
)
|
||||
|
||||
video.video_fingerprint = fingerprint.to_dict()
|
||||
|
||||
@@ -92,8 +92,7 @@ def create_video_record_and_dedup(
|
||||
logger.warning("Failed to save fingerprint chunks for %s: %s", video_id, chunk_err)
|
||||
|
||||
# (a) 历史成片查重(跨项目全局 + 时长预过滤)
|
||||
# Issue #1702: fingerprint.duration 单位是秒,旧代码 /1000 让时长预过滤失效
|
||||
duration_sec = fingerprint.duration if fingerprint.duration else 0
|
||||
duration_sec = fingerprint.duration / 1000 if fingerprint.duration else 0
|
||||
duplicate_result = deduplicator.check_duplicate(
|
||||
fingerprint,
|
||||
project_id,
|
||||
@@ -101,7 +100,6 @@ def create_video_record_and_dedup(
|
||||
scope="user",
|
||||
user_id=user_id,
|
||||
duration_sec=duration_sec,
|
||||
exclude_video_id=video_id,
|
||||
)
|
||||
|
||||
# (b) 批次内查重(仅当有 batch_id 时)
|
||||
|
||||
@@ -198,13 +198,8 @@ class UnifiedRenderService:
|
||||
# 2. 分组为 RenderLayers
|
||||
layers = self._group_clips_into_layers(resolved)
|
||||
|
||||
# 2.5 配音时长对齐:如果有配音素材,调整片段时长以匹配配音时长
|
||||
voice_duration = self._get_voice_audio_duration()
|
||||
if voice_duration > 0:
|
||||
self._align_clips_to_voice_duration(layers, voice_duration)
|
||||
|
||||
# 3. 计算视频总时长(用于字幕显示时长)
|
||||
video_duration_final = self._estimate_total_duration(layers)
|
||||
video_duration = self._estimate_total_duration(layers)
|
||||
# Debug: 输出各图层时长明细
|
||||
for layer in layers:
|
||||
layer_total = sum(UnifiedRenderService._clip_adjusted_duration(c) for c in layer.clips)
|
||||
@@ -220,16 +215,16 @@ class UnifiedRenderService:
|
||||
self.transition_duration,
|
||||
", ".join(clip_details),
|
||||
)
|
||||
logger.info("[debug] estimated video_duration=%.3f", video_duration_final)
|
||||
logger.info("[debug] estimated video_duration=%.3f", video_duration)
|
||||
|
||||
# 3.5 TTS 配音生成(如果配置了)
|
||||
self._maybe_add_voiceover_layer(layers, video_duration=video_duration_final)
|
||||
self._maybe_add_voiceover_layer(layers, video_duration=video_duration)
|
||||
|
||||
# 3.6 配音素材库音频(如果传入了本地路径)
|
||||
self._maybe_add_voice_library_layer(layers, video_duration=video_duration_final)
|
||||
self._maybe_add_voice_library_layer(layers, video_duration=video_duration)
|
||||
|
||||
# 4. 生成 ASS 字幕文件(如果有 title/subtitle 配置)
|
||||
ass_path = self._maybe_generate_ass(video_duration_final)
|
||||
ass_path = self._maybe_generate_ass(video_duration)
|
||||
|
||||
# 4.5 解析画中画配置
|
||||
pip_config = PiPConfig.from_dict((self.plan.config or {}).get("pip_config"))
|
||||
@@ -262,7 +257,7 @@ class UnifiedRenderService:
|
||||
# 先尝试 stream copy 优化(无重编码,性能提升 10 倍+)
|
||||
# 条件不满足或失败时回退到带滤镜的直通渲染
|
||||
stream_copy_ok = self._try_render_stream_copy(
|
||||
layers, output_path, ass_path=ass_path, video_duration=video_duration_final
|
||||
layers, output_path, ass_path=ass_path, video_duration=video_duration
|
||||
)
|
||||
if stream_copy_ok:
|
||||
used_stream_copy = True
|
||||
@@ -276,7 +271,7 @@ class UnifiedRenderService:
|
||||
layers,
|
||||
output_path,
|
||||
ass_path=ass_path,
|
||||
video_duration=video_duration_final,
|
||||
video_duration=video_duration,
|
||||
)
|
||||
else:
|
||||
filter_complex, input_args = self._build_filter_complex(layers, ass_path=ass_path)
|
||||
@@ -332,7 +327,7 @@ class UnifiedRenderService:
|
||||
from video_processing.ffmpeg_utils import run_ffmpeg
|
||||
|
||||
run_ffmpeg(extract_cmd)
|
||||
final_audio = mix_bgm_with_main(ctx, main_audio_path, bgm_cfg, video_duration_final)
|
||||
final_audio = mix_bgm_with_main(ctx, main_audio_path, bgm_cfg, video_duration)
|
||||
# 合并回视频
|
||||
|
||||
bgm_output = self.work_dir / f"rendered_{self.plan.id}_bgm.mp4"
|
||||
@@ -358,7 +353,7 @@ class UnifiedRenderService:
|
||||
audio_path = mix_audio(
|
||||
ctx,
|
||||
layers,
|
||||
video_duration_final,
|
||||
video_duration,
|
||||
bgm_path=self.bgm_path,
|
||||
bgm_config=bgm_config,
|
||||
audio_tracks_config=audio_tracks_config,
|
||||
@@ -492,147 +487,6 @@ class UnifiedRenderService:
|
||||
"""
|
||||
return _estimate_total_duration_pure(layers, self.transition_duration)
|
||||
|
||||
def _get_voice_audio_duration(self) -> float:
|
||||
"""获取配音音频文件的时长(秒)。
|
||||
|
||||
Returns:
|
||||
配音音频时长,如果无配音或探测失败则返回 0.0
|
||||
"""
|
||||
if not self.voiceover_audio_path:
|
||||
return 0.0
|
||||
|
||||
audio_path = Path(self.voiceover_audio_path)
|
||||
if not audio_path.exists() or audio_path.stat().st_size == 0:
|
||||
return 0.0
|
||||
|
||||
try:
|
||||
duration = probe_duration(audio_path)
|
||||
logger.info("[voice-align] 配音音频时长: %.3fs path=%s", duration, self.voiceover_audio_path)
|
||||
return duration
|
||||
except Exception as e:
|
||||
logger.warning("[voice-align] 探测配音音频时长失败: %s", e)
|
||||
return 0.0
|
||||
|
||||
def _align_clips_to_voice_duration(
|
||||
self,
|
||||
layers: list[RenderLayer],
|
||||
voice_duration: float,
|
||||
) -> None:
|
||||
"""调整片段时长以对齐配音时长。
|
||||
|
||||
核心逻辑:
|
||||
- 计算片段总时长与配音时长的比例
|
||||
- ±5% 以内不调整
|
||||
- ratio < 1(片段比配音长):按比例裁剪每段末尾
|
||||
- ratio > 1(片段比配音短):按比例慢放每段
|
||||
|
||||
Args:
|
||||
layers: 渲染图层列表
|
||||
voice_duration: 配音时长(秒)
|
||||
"""
|
||||
if voice_duration <= 0:
|
||||
return
|
||||
|
||||
# 只调整视频图层(main/broll/background),不调整音频图层
|
||||
video_layers = [layer for layer in layers if layer.role in ("main", "broll", "background")]
|
||||
if not video_layers:
|
||||
return
|
||||
|
||||
# 计算所有视频图层的总时长
|
||||
total_clips_duration = 0.0
|
||||
for layer in video_layers:
|
||||
for clip in layer.clips:
|
||||
clip_dur = self._clip_adjusted_duration(clip)
|
||||
total_clips_duration += clip_dur
|
||||
|
||||
if total_clips_duration <= 0:
|
||||
return
|
||||
|
||||
ratio = voice_duration / total_clips_duration
|
||||
|
||||
# ±5% 以内不调整
|
||||
if abs(ratio - 1.0) <= 0.05:
|
||||
logger.info(
|
||||
"[voice-align] 比例接近1:1,跳过调整: ratio=%.4f voice=%.3f clips=%.3f",
|
||||
ratio,
|
||||
voice_duration,
|
||||
total_clips_duration,
|
||||
)
|
||||
return
|
||||
|
||||
logger.info(
|
||||
"[voice-align] 开始调整片段时长: ratio=%.4f voice=%.3f clips=%.3f",
|
||||
ratio,
|
||||
voice_duration,
|
||||
total_clips_duration,
|
||||
)
|
||||
|
||||
# 收集所有视频 clip
|
||||
all_clips: list[tuple[RenderLayer, ResolvedClip]] = []
|
||||
for layer in video_layers:
|
||||
for clip in layer.clips:
|
||||
all_clips.append((layer, clip))
|
||||
|
||||
if not all_clips:
|
||||
return
|
||||
|
||||
if ratio < 1.0:
|
||||
# 片段比配音长,按比例裁剪每段末尾
|
||||
# 减少每个 clip 的 duration
|
||||
for _layer, clip in all_clips:
|
||||
old_duration = clip.duration if clip.duration > 0 else clip.actual_duration
|
||||
new_duration = old_duration * ratio
|
||||
|
||||
# 更新 duration
|
||||
clip.duration = max(0.1, new_duration) # 至少 0.1s
|
||||
|
||||
# 如果有 trim_config,也需要调整
|
||||
if clip.trim_config is not None:
|
||||
new_trim_duration = clip.trim_config.duration * ratio
|
||||
clip.trim_config = TrimConfig(
|
||||
start_time=clip.trim_config.start_time,
|
||||
duration=max(0.1, new_trim_duration),
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
"[voice-align] trim clip=%s: %.3f -> %.3f",
|
||||
clip.clip_id,
|
||||
old_duration,
|
||||
clip.duration,
|
||||
)
|
||||
|
||||
else:
|
||||
# ratio > 1.0: 片段比配音短,按比例慢放每段
|
||||
# 降低 playback_speed
|
||||
for _layer, clip in all_clips:
|
||||
old_speed = clip.playback_speed if clip.playback_speed > 0 else 1.0
|
||||
# speed = old_speed / ratio 会使视频变慢(ratio > 1 时)
|
||||
new_speed = old_speed / ratio
|
||||
|
||||
# 下限 0.25x(避免过慢)
|
||||
new_speed = max(0.25, round(new_speed, 4))
|
||||
clip.playback_speed = new_speed
|
||||
|
||||
logger.debug(
|
||||
"[voice-align] slowdown clip=%s: speed %.4f -> %.4f",
|
||||
clip.clip_id,
|
||||
old_speed,
|
||||
new_speed,
|
||||
)
|
||||
|
||||
# 调整后重新计算总时长用于日志
|
||||
new_total = 0.0
|
||||
for layer in video_layers:
|
||||
for clip in layer.clips:
|
||||
new_total += self._clip_adjusted_duration(clip)
|
||||
|
||||
logger.info(
|
||||
"[voice-align] 调整完成: 新总时长=%.3fs (目标=%.3fs, 差异=%.3fs)",
|
||||
new_total,
|
||||
voice_duration,
|
||||
abs(new_total - voice_duration),
|
||||
)
|
||||
|
||||
def _maybe_generate_ass(self, video_duration: float) -> Path | None:
|
||||
"""根据 plan.config 生成 ASS 字幕文件。
|
||||
|
||||
|
||||
@@ -22,18 +22,10 @@ celery_app.conf.imports = (
|
||||
)
|
||||
|
||||
# Celery Beat 定时任务调度
|
||||
# 注:worker 单实例内嵌 beat(entrypoint-worker.sh -B),定时任务不会重复执行
|
||||
celery_app.conf.beat_schedule = {
|
||||
# pending 任务超时清理:worker 停止消费后,卡 pending 的任务 15 分钟内释放限流名额
|
||||
"cleanup-stale-pending-tasks": {
|
||||
"task": "worker.cleanup_stale_pending_tasks",
|
||||
"schedule": 300.0, # 每 5 分钟(秒)
|
||||
"options": {"expires": 240}, # 4 分钟过期,避免堆积
|
||||
},
|
||||
# running 孤儿任务巡检:容器重启/进程被杀后卡 running 的任务,20 分钟无更新则判失败
|
||||
"cleanup-stale-running-tasks": {
|
||||
"task": "worker.cleanup_stale_running_tasks",
|
||||
"schedule": 300.0, # 每 5 分钟(秒)
|
||||
"options": {"expires": 240},
|
||||
"schedule": 600.0, # 每 10 分钟(秒)
|
||||
"options": {"expires": 300}, # 5 分钟过期,避免堆积
|
||||
},
|
||||
}
|
||||
|
||||
@@ -7,34 +7,11 @@ from worker_app.db import SessionLocal
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 孤儿任务超时阈值:渲染任务超过此时间未更新则视为卡死
|
||||
ORPHAN_TASK_TIMEOUT_MINUTES = 10
|
||||
|
||||
def cleanup_stale_running_with_session(repo, timeout_minutes: int) -> int:
|
||||
"""清理超时未更新的 running GenerationTask(可注入 repo 的纯核心,便于单测)。
|
||||
|
||||
Returns:
|
||||
清理的任务数量
|
||||
"""
|
||||
return repo.cleanup_stale_running(timeout_minutes)
|
||||
|
||||
|
||||
def cleanup_stale_pending_with_session(repo, timeout_minutes: int) -> int:
|
||||
"""清理超时 pending GenerationTask(可注入 repo 的纯核心,便于单测)。
|
||||
|
||||
Returns:
|
||||
清理的任务数量
|
||||
"""
|
||||
return repo.cleanup_stale_pending(timeout_minutes)
|
||||
|
||||
|
||||
# 孤儿任务超时阈值:running 任务超过此时间无进度更新则视为卡死。
|
||||
# 依据:worker.generate_video 硬超时 time_limit=11 分钟,正常任务不可能超过;
|
||||
# 20 分钟阈值覆盖硬超时 + 重试 + 余量,绝不误杀正常任务。
|
||||
ORPHAN_TASK_TIMEOUT_MINUTES = 20
|
||||
|
||||
# Pending 任务超时阈值:任务创建后超过此时间仍未被 worker 拉取,
|
||||
# 说明 worker 已停止消费(容器异常/卡死),清掉释放限流名额。
|
||||
# 依据:满队列(20 pending)× 平均 2 分钟 / 并发 4 ≈ 10 分钟,15 分钟留余量。
|
||||
PENDING_TASK_TIMEOUT_MINUTES = 15
|
||||
# Pending 任务超时阈值:pending 任务在队列中等待超过此时间则自动清理
|
||||
PENDING_TASK_TIMEOUT_MINUTES = 30
|
||||
|
||||
|
||||
def cleanup_orphan_tasks(timeout_minutes: int = ORPHAN_TASK_TIMEOUT_MINUTES) -> int: # pragma: no cover
|
||||
@@ -55,11 +32,9 @@ def cleanup_orphan_tasks(timeout_minutes: int = ORPHAN_TASK_TIMEOUT_MINUTES) ->
|
||||
|
||||
try:
|
||||
session = SessionLocal()
|
||||
try:
|
||||
repo = SQLAlchemyGenerationTaskRepository(session)
|
||||
count = cleanup_stale_running_with_session(repo, timeout_minutes)
|
||||
finally:
|
||||
session.close()
|
||||
repo = SQLAlchemyGenerationTaskRepository(session)
|
||||
count = repo.cleanup_stale_running(timeout_minutes)
|
||||
session.close()
|
||||
if count > 0:
|
||||
logger.warning("清理了 %d 个超时的孤儿 GenerationTask(超过 %d 分钟未更新)", count, timeout_minutes)
|
||||
else:
|
||||
@@ -130,7 +105,7 @@ def cleanup_stale_pending_tasks(timeout_minutes: int = PENDING_TASK_TIMEOUT_MINU
|
||||
session = SessionLocal()
|
||||
try:
|
||||
repo = SQLAlchemyGenerationTaskRepository(session)
|
||||
count = cleanup_stale_pending_with_session(repo, timeout_minutes)
|
||||
count = repo.cleanup_stale_pending(timeout_minutes)
|
||||
if count > 0:
|
||||
logger.warning("清理了 %d 个超时的 pending GenerationTask(超过 %d 分钟未处理)", count, timeout_minutes)
|
||||
else:
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
"""定期清理任务 — Celery Beat 调度。
|
||||
|
||||
包含:
|
||||
- cleanup_stale_pending_tasks: 定期清理卡在 pending 超时的 generation_tasks(worker 停止消费时占位)
|
||||
- cleanup_stale_running_tasks: 定期清理卡在 running 超时的 generation_tasks(容器重启/进程被杀后的孤儿)
|
||||
- cleanup_stale_pending_tasks: 定期清理卡在 pending 超时的 generation_tasks
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from celery import shared_task
|
||||
from worker_app.tasks._startup import (
|
||||
ORPHAN_TASK_TIMEOUT_MINUTES,
|
||||
PENDING_TASK_TIMEOUT_MINUTES,
|
||||
cleanup_orphan_tasks,
|
||||
cleanup_stale_jobs,
|
||||
cleanup_stale_pending_tasks,
|
||||
)
|
||||
|
||||
@@ -23,12 +19,12 @@ logger = logging.getLogger(__name__)
|
||||
def scheduled_cleanup_stale_pending(timeout_minutes: int = PENDING_TASK_TIMEOUT_MINUTES) -> dict:
|
||||
"""Celery Beat 调度的定期任务:清理超时的 pending 任务。
|
||||
|
||||
每 5 分钟执行一次(由 celery_app.py 的 beat_schedule 配置),
|
||||
每 10 分钟执行一次(由 celery_app.py 的 beat_schedule 配置),
|
||||
查找所有 status='pending' 且 created_at < NOW() - timeout_minutes
|
||||
的 generation_tasks,批量更新为 failed,释放限流名额。
|
||||
的 generation_tasks,批量更新为 failed。
|
||||
|
||||
Args:
|
||||
timeout_minutes: 超时时间(分钟),默认 15 分钟
|
||||
timeout_minutes: 超时时间(分钟),默认 30 分钟
|
||||
|
||||
Returns:
|
||||
{"cleaned": int}
|
||||
@@ -37,33 +33,3 @@ def scheduled_cleanup_stale_pending(timeout_minutes: int = PENDING_TASK_TIMEOUT_
|
||||
if count > 0:
|
||||
logger.info("[Beat] 清理了 %d 个超时 pending 任务(超时阈值 %d 分钟)", count, timeout_minutes)
|
||||
return {"cleaned": count}
|
||||
|
||||
|
||||
@shared_task(name="worker.cleanup_stale_running_tasks")
|
||||
def scheduled_cleanup_stale_running(timeout_minutes: int = ORPHAN_TASK_TIMEOUT_MINUTES) -> dict:
|
||||
"""Celery Beat 调度的定期任务:清理超时的 running 孤儿任务。
|
||||
|
||||
每 5 分钟执行一次。worker_ready 信号只在 worker 启动时清一次,
|
||||
若 worker 没重启但任务卡死(上传挂起、进程 OOM 被内核杀掉等),
|
||||
任务会永久卡在 running 占位。此任务做持续兜底:
|
||||
查找 status='running' 且 updated_at < NOW() - timeout_minutes 的任务,
|
||||
标记为 failed(原因:容器重启/超时中断),同时清理 Job 表孤儿。
|
||||
|
||||
Args:
|
||||
timeout_minutes: 超时时间(分钟),默认 20 分钟
|
||||
(worker.generate_video 硬超时 11 分钟,正常任务不可能超过 20 分钟)
|
||||
|
||||
Returns:
|
||||
{"generation_tasks": int, "jobs": int}
|
||||
"""
|
||||
gen_count = cleanup_orphan_tasks(timeout_minutes)
|
||||
job_count = cleanup_stale_jobs(timeout_minutes)
|
||||
total = gen_count + job_count
|
||||
if total > 0:
|
||||
logger.warning(
|
||||
"[Beat] 清理孤儿任务: running GenerationTask=%d, Job=%d(超时阈值 %d 分钟)",
|
||||
gen_count,
|
||||
job_count,
|
||||
timeout_minutes,
|
||||
)
|
||||
return {"generation_tasks": gen_count, "jobs": job_count}
|
||||
|
||||
@@ -359,54 +359,6 @@ def validate_transcode_output(
|
||||
return True
|
||||
|
||||
|
||||
def _original_key_from_storage_key(storage_key: str) -> str:
|
||||
"""从可能被 HEVC 转码改写的 storage_key 还原原始 key。
|
||||
|
||||
转码成功后 key 形如 uploads/<id>/IMG_2282_h264.MOV,
|
||||
占位 asset 以原始 key uploads/<id>/IMG_2282.MOV 创建。
|
||||
"""
|
||||
if not storage_key:
|
||||
return storage_key
|
||||
_p = Path(storage_key)
|
||||
if _p.stem.endswith("_h264"):
|
||||
return str(_p.parent / (_p.stem[: -len("_h264")] + _p.suffix))
|
||||
return storage_key
|
||||
|
||||
|
||||
def _resolve_placeholder_asset(asset_repo, job, original_storage_key):
|
||||
"""找到 complete 阶段创建的 PROCESSING 占位 asset(Issue #1714)。
|
||||
|
||||
HEVC 转码成功后 job.storage_key 会被改写为 *_h264 新 key,旧实现用新 key
|
||||
回查占位必然落空,进而兜底新建一条 READY 记录,导致原占位永久卡 processing。
|
||||
|
||||
查找优先级:
|
||||
1. job.asset_id(complete 派单时透传的占位 id,最可靠,不依赖 key);
|
||||
2. 原始 storage_key(占位记录以原始 key 创建);
|
||||
3. 当前 job.storage_key(未转码/降级场景与原始 key 相同)。
|
||||
|
||||
找不到返回 None(旧链路兼容,由调用方兜底新建并告警)。
|
||||
"""
|
||||
asset_id = getattr(job, "asset_id", "") or ""
|
||||
if asset_id:
|
||||
try:
|
||||
found = asset_repo.find_by_id(asset_id)
|
||||
if found is not None:
|
||||
return found
|
||||
except Exception as find_err:
|
||||
logger.warning("占位 asset 按 id 查询失败 asset_id=%s: %s", asset_id, find_err)
|
||||
for key in (original_storage_key, getattr(job, "storage_key", "")):
|
||||
if not key:
|
||||
continue
|
||||
try:
|
||||
found = asset_repo.find_by_storage_key(key)
|
||||
except Exception:
|
||||
logger.warning("find_by_storage_key not available, trying fallback lookup")
|
||||
found = None
|
||||
if found is not None:
|
||||
return found
|
||||
return None
|
||||
|
||||
|
||||
@celery_app.task(name="worker.ingest_asset")
|
||||
def ingest_asset(job_id: str) -> dict:
|
||||
"""
|
||||
@@ -428,10 +380,6 @@ def ingest_asset(job_id: str) -> dict:
|
||||
if job is None:
|
||||
return {"status": "failed", "error": "job not found"}
|
||||
|
||||
# 记录原始 storage_key:HEVC 转码成功后 job.storage_key 会改写为 *_h264,
|
||||
# 而 complete 阶段的占位 asset 始终以原始 key 创建,关联回写必须保留它。
|
||||
original_storage_key = job.storage_key
|
||||
|
||||
# Update job status to PROCESSING
|
||||
job.status = IngestJobStatus.PROCESSING
|
||||
job.updated_at = datetime.now(timezone.utc)
|
||||
@@ -676,41 +624,22 @@ def ingest_asset(job_id: str) -> dict:
|
||||
error_reason,
|
||||
)
|
||||
|
||||
placeholder = _resolve_placeholder_asset(asset_repo, job, original_storage_key)
|
||||
if placeholder is not None:
|
||||
# 回写占位记录:标 ERROR(Issue #1714:禁止新建第二条导致占位孤儿)
|
||||
asset = placeholder
|
||||
asset.mime_type = mime_type
|
||||
asset.metadata = {"source": "upload", "ingest_error": error_reason}
|
||||
asset.file_size = int(metadata.get("size_bytes", 0))
|
||||
asset.duration = float(metadata.get("duration", 0)) or None
|
||||
asset.width = int(metadata.get("width", 0)) or None
|
||||
asset.height = int(metadata.get("height", 0)) or None
|
||||
codec_val = metadata.get("codec")
|
||||
if codec_val:
|
||||
asset.codec = str(codec_val)
|
||||
asset.status = AssetStatus.ERROR
|
||||
asset.updated_at = datetime.now(timezone.utc)
|
||||
asset_repo.update(asset)
|
||||
else:
|
||||
# 旧链路兜底:无占位记录(如历史 job 重跑)才新建
|
||||
logger.warning("无效素材且未找到占位记录,兜底新建 ERROR asset: job_id=%s", job_id)
|
||||
asset = Asset.create(
|
||||
project_id=job.project_id,
|
||||
library_id=job.library_id,
|
||||
name=filename,
|
||||
storage_key=job.storage_key,
|
||||
mime_type=mime_type,
|
||||
metadata={"source": "upload", "ingest_error": error_reason},
|
||||
file_size=int(metadata.get("size_bytes", 0)),
|
||||
duration=float(metadata.get("duration", 0)),
|
||||
width=int(metadata.get("width", 0)),
|
||||
height=int(metadata.get("height", 0)),
|
||||
codec=metadata.get("codec") or None,
|
||||
status=AssetStatus.ERROR,
|
||||
file_hash=job.file_hash,
|
||||
)
|
||||
asset_repo.create(asset)
|
||||
asset = Asset.create(
|
||||
project_id=job.project_id,
|
||||
library_id=job.library_id,
|
||||
name=filename,
|
||||
storage_key=job.storage_key,
|
||||
mime_type=mime_type,
|
||||
metadata={"source": "upload", "ingest_error": error_reason},
|
||||
file_size=int(metadata.get("size_bytes", 0)),
|
||||
duration=float(metadata.get("duration", 0)),
|
||||
width=int(metadata.get("width", 0)),
|
||||
height=int(metadata.get("height", 0)),
|
||||
codec=metadata.get("codec") or None,
|
||||
status=AssetStatus.ERROR,
|
||||
file_hash=job.file_hash,
|
||||
)
|
||||
asset_repo.create(asset)
|
||||
|
||||
# Update job status to FAILED
|
||||
job.status = IngestJobStatus.FAILED
|
||||
@@ -727,19 +656,16 @@ def ingest_asset(job_id: str) -> dict:
|
||||
"error": error_reason,
|
||||
}
|
||||
|
||||
# 查找 complete 阶段创建的占位 Asset 记录(Issue #1714)。
|
||||
# 必须用原始 storage_key / job.asset_id 关联——HEVC 转码后 job.storage_key
|
||||
# 已改写为 *_h264,用新 key 回查占位必然落空,旧实现因此兜底新建 READY 记录,
|
||||
# 导致原 PROCESSING 占位永久卡住(每个 HEVC 视频产生两条记录)。
|
||||
existing_asset = _resolve_placeholder_asset(asset_repo, job, original_storage_key)
|
||||
# 查找已存在的 Asset 记录(由 API 端在上传完成时立即创建为 PROCESSING 状态)
|
||||
existing_asset = None
|
||||
try:
|
||||
existing_asset = asset_repo.find_by_storage_key(job.storage_key)
|
||||
except Exception:
|
||||
logger.warning("find_by_storage_key not available, trying fallback lookup")
|
||||
|
||||
if existing_asset is None:
|
||||
# 兜底:仅当确实没有占位记录(旧版本 API / 历史 job 重跑)才新建。
|
||||
logger.warning(
|
||||
"No placeholder asset found for job_id=%s original_key=%s, creating new",
|
||||
job_id,
|
||||
original_storage_key,
|
||||
)
|
||||
# 兜底:如果 API 端没有预先创建 Asset(旧版本兼容),则创建新记录
|
||||
logger.info("No pre-created asset found for storage_key=%s, creating new", job.storage_key)
|
||||
metadata["source"] = "upload"
|
||||
asset = Asset.create(
|
||||
project_id=job.project_id,
|
||||
@@ -759,13 +685,8 @@ def ingest_asset(job_id: str) -> dict:
|
||||
)
|
||||
asset_repo.create(asset)
|
||||
else:
|
||||
# 更新占位记录:补充元数据、置 READY。转码成功时 storage_key 同步改写为
|
||||
# *_h264(播放/下载走转码产物),原始 key 记入 metadata 可溯源。
|
||||
# 更新已有的 Asset 记录,补充元数据并将状态改为 READY
|
||||
asset = existing_asset
|
||||
if job.storage_key != asset.storage_key:
|
||||
metadata["original_storage_key"] = asset.storage_key
|
||||
metadata["hevc_transcoded"] = True
|
||||
asset.storage_key = job.storage_key
|
||||
asset.mime_type = mime_type
|
||||
metadata["source"] = "upload"
|
||||
asset.metadata = metadata
|
||||
@@ -816,28 +737,9 @@ def ingest_asset(job_id: str) -> dict:
|
||||
job_repo.update(job)
|
||||
|
||||
# 将上传时创建的占位 Asset(PROCESSING/UPLOADING)标记为 ERROR,
|
||||
# 避免素材永远卡在中间状态。转码可能已把 job.storage_key 改写为
|
||||
# *_h264,需用 asset_id / 原始 key 多路径关联占位(Issue #1714)。
|
||||
# 避免素材永远卡在中间状态
|
||||
try:
|
||||
existing = None
|
||||
_asset_id = getattr(job, "asset_id", "") or ""
|
||||
if _asset_id:
|
||||
try:
|
||||
existing = asset_repo.find_by_id(_asset_id)
|
||||
except Exception:
|
||||
existing = None
|
||||
if existing is None:
|
||||
_candidate_keys = [
|
||||
_original_key_from_storage_key(job.storage_key),
|
||||
job.storage_key,
|
||||
]
|
||||
for _key in _candidate_keys:
|
||||
try:
|
||||
existing = asset_repo.find_by_storage_key(_key)
|
||||
except Exception:
|
||||
existing = None
|
||||
if existing is not None:
|
||||
break
|
||||
existing = asset_repo.find_by_storage_key(job.storage_key)
|
||||
if existing and existing.status in (
|
||||
AssetStatus.PROCESSING,
|
||||
AssetStatus.UPLOADING,
|
||||
|
||||
@@ -214,10 +214,3 @@ MEDIAKIT_TIMEOUT=60
|
||||
# ==================== 监控(可选)====================
|
||||
# Sentry DSN(取消注释并填入实际值以启用错误追踪)
|
||||
# SENTRY_DSN=${SENTRY_DSN}
|
||||
|
||||
|
||||
# ==================== 微信开放平台 OAuth(网页扫码登录)====================
|
||||
# 回调域名:xiaoxiajianji.com(微信开放平台已配置)
|
||||
WECHAT_OPEN_APP_ID=${WECHAT_APP_ID}
|
||||
WECHAT_OPEN_APP_SECRET=${WECHAT_APP_SECRET}
|
||||
WECHAT_OPEN_REDIRECT_URI=https://saas.xiaoxiajianji.com/auth/wechat/callback
|
||||
|
||||
@@ -231,10 +231,3 @@ DASHSCOPE_API_KEY=${DASHSCOPE_API_KEY}
|
||||
MEDIAKIT_API_KEY=${MEDIAKIT_API_KEY}
|
||||
MEDIAKIT_BASE_URL=https://mediakit.cn-beijing.volces.com/api/v1
|
||||
MEDIAKIT_TIMEOUT=60
|
||||
|
||||
|
||||
# ==================== 微信开放平台 OAuth(网页扫码登录)====================
|
||||
# 回调域名:xiaoxiajianji.com(微信开放平台已配置)
|
||||
WECHAT_OPEN_APP_ID=${WECHAT_APP_ID}
|
||||
WECHAT_OPEN_APP_SECRET=${WECHAT_APP_SECRET}
|
||||
WECHAT_OPEN_REDIRECT_URI=https://staging.xiaoxiajianji.com/auth/wechat/callback
|
||||
|
||||
@@ -146,44 +146,3 @@ class InMemoryAssetRepository:
|
||||
if asset.library_id == library_id and asset.file_hash == file_hash:
|
||||
return asset
|
||||
return None
|
||||
|
||||
def find_by_library_and_client_upload_id(
|
||||
self,
|
||||
library_id: str,
|
||||
client_upload_id: str,
|
||||
) -> Asset | None:
|
||||
"""按素材库 + 客户端幂等 token 查找已有素材。"""
|
||||
if not client_upload_id:
|
||||
return None
|
||||
for asset in self._assets.values():
|
||||
if asset.library_id == library_id and getattr(asset, "client_upload_id", "") == client_upload_id:
|
||||
return asset
|
||||
return None
|
||||
|
||||
def find_recent_active_by_library_and_name(
|
||||
self,
|
||||
library_id: str,
|
||||
name: str,
|
||||
within_minutes: int = 30,
|
||||
file_size: int = 0,
|
||||
) -> Asset | None:
|
||||
"""兜底去重:同库 + 同文件名(+同大小)且近期活动状态的素材。"""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
if not name:
|
||||
return None
|
||||
from packages.domain import AssetStatus
|
||||
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(minutes=within_minutes)
|
||||
candidates = [
|
||||
a
|
||||
for a in self._assets.values()
|
||||
if a.library_id == library_id
|
||||
and a.name == name
|
||||
and a.status in (AssetStatus.UPLOADING, AssetStatus.PROCESSING)
|
||||
and a.created_at >= cutoff
|
||||
and (not file_size or file_size <= 0 or a.file_size == file_size)
|
||||
]
|
||||
if not candidates:
|
||||
return None
|
||||
return max(candidates, key=lambda a: a.created_at)
|
||||
|
||||
@@ -134,7 +134,6 @@ class SQLAlchemyAssetRepository:
|
||||
quality_score=asset.quality_score,
|
||||
uploaded_by_user_id=asset.uploaded_by_user_id or "system",
|
||||
file_hash=asset.file_hash or None,
|
||||
client_upload_id=asset.client_upload_id or None,
|
||||
created_at=asset.created_at,
|
||||
updated_at=now,
|
||||
)
|
||||
@@ -164,8 +163,6 @@ class SQLAlchemyAssetRepository:
|
||||
model.quality_score = asset.quality_score
|
||||
model.uploaded_by_user_id = asset.uploaded_by_user_id or model.uploaded_by_user_id
|
||||
model.file_hash = asset.file_hash or model.file_hash
|
||||
if getattr(model, "client_upload_id", None) is None and asset.client_upload_id:
|
||||
model.client_upload_id = asset.client_upload_id
|
||||
model.updated_at = datetime.now(timezone.utc)
|
||||
self.session.flush()
|
||||
self._sync_asset_tags(asset.id, asset.tag_ids)
|
||||
@@ -391,7 +388,6 @@ class SQLAlchemyAssetRepository:
|
||||
quality_score=model.quality_score,
|
||||
uploaded_by_user_id=model.uploaded_by_user_id,
|
||||
file_hash=model.file_hash or "",
|
||||
client_upload_id=getattr(model, "client_upload_id", None) or "",
|
||||
metadata=metadata,
|
||||
tag_ids=tag_ids,
|
||||
created_at=model.created_at,
|
||||
@@ -456,53 +452,3 @@ class SQLAlchemyAssetRepository:
|
||||
if model is None:
|
||||
return None
|
||||
return self._to_domain(model)
|
||||
|
||||
def find_by_library_and_client_upload_id(
|
||||
self,
|
||||
library_id: str,
|
||||
client_upload_id: str,
|
||||
) -> Asset | None:
|
||||
"""按素材库 + 客户端幂等 token 查找已有素材(complete 幂等)。"""
|
||||
if not client_upload_id:
|
||||
return None
|
||||
model = (
|
||||
self.session.query(AssetModel)
|
||||
.filter(
|
||||
AssetModel.asset_library_id == library_id,
|
||||
AssetModel.client_upload_id == client_upload_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if model is None:
|
||||
return None
|
||||
return self._to_domain(model)
|
||||
|
||||
def find_recent_active_by_library_and_name(
|
||||
self,
|
||||
library_id: str,
|
||||
name: str,
|
||||
within_minutes: int = 30,
|
||||
file_size: int = 0,
|
||||
) -> Asset | None:
|
||||
"""兜底去重:同库 + 同文件名(+同大小)且近期仍处活动状态(uploading/processing)的素材。
|
||||
|
||||
用于旧客户端未传 file_hash/client_upload_id 时,防止 complete 超时重试
|
||||
反复创建 PROCESSING 占位记录。只命中"活动中"的近期记录,READY 历史素材不拦。
|
||||
"""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
if not name:
|
||||
return None
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(minutes=within_minutes)
|
||||
query = self.session.query(AssetModel).filter(
|
||||
AssetModel.asset_library_id == library_id,
|
||||
AssetModel.name == name,
|
||||
AssetModel.status.in_([AssetStatus.UPLOADING.value, AssetStatus.PROCESSING.value]),
|
||||
AssetModel.created_at >= cutoff,
|
||||
)
|
||||
if file_size and file_size > 0:
|
||||
query = query.filter(AssetModel.file_size == file_size)
|
||||
model = query.order_by(AssetModel.created_at.desc()).first()
|
||||
if model is None:
|
||||
return None
|
||||
return self._to_domain(model)
|
||||
|
||||
@@ -138,52 +138,6 @@ class SQLAlchemyGenerationTaskRepository:
|
||||
.count()
|
||||
)
|
||||
|
||||
def count_running_by_user(self, user_id: str) -> int:
|
||||
"""统计指定用户处于 running 状态的任务数(用于限流提示展示)。"""
|
||||
return (
|
||||
self.session.query(GenerationTaskModel)
|
||||
.filter(
|
||||
GenerationTaskModel.created_by_user_id == user_id,
|
||||
GenerationTaskModel.status == GenerationTaskStatus.RUNNING.value,
|
||||
)
|
||||
.count()
|
||||
)
|
||||
|
||||
def count_running_total(self) -> int:
|
||||
"""统计全局处于 running 状态的任务数(worker 实际在执行的任务数)。"""
|
||||
return (
|
||||
self.session.query(GenerationTaskModel)
|
||||
.filter(GenerationTaskModel.status == GenerationTaskStatus.RUNNING.value)
|
||||
.count()
|
||||
)
|
||||
|
||||
def estimate_avg_duration_seconds(self, limit: int = 20, default_seconds: float = 120.0) -> float:
|
||||
"""估算最近完成任务的平均耗时(秒),用于 429 限流提示的等待预估。
|
||||
|
||||
取最近 N 条 completed 任务的 (completed_at - started_at) 平均值;
|
||||
无足够历史数据时返回 default_seconds。
|
||||
用 Python 侧计算差值,避免 SQLite/PostgreSQL 方言差异。
|
||||
"""
|
||||
rows = (
|
||||
self.session.query(GenerationTaskModel.started_at, GenerationTaskModel.completed_at)
|
||||
.filter(
|
||||
GenerationTaskModel.status == GenerationTaskStatus.COMPLETED.value,
|
||||
GenerationTaskModel.started_at.isnot(None),
|
||||
GenerationTaskModel.completed_at.isnot(None),
|
||||
)
|
||||
.order_by(GenerationTaskModel.completed_at.desc())
|
||||
.limit(limit)
|
||||
.all()
|
||||
)
|
||||
durations = [
|
||||
(completed - started).total_seconds()
|
||||
for started, completed in rows
|
||||
if completed and started and (completed - started).total_seconds() > 0
|
||||
]
|
||||
if not durations:
|
||||
return default_seconds
|
||||
return sum(durations) / len(durations)
|
||||
|
||||
def list_recent_by_user(self, user_id: str, limit: int = 5) -> list[GenerationTask]:
|
||||
models = (
|
||||
self.session.query(GenerationTaskModel)
|
||||
|
||||
@@ -18,7 +18,6 @@ class SQLAlchemyIngestJobRepository:
|
||||
error_message=job.error_message,
|
||||
result_asset_id=job.result_asset_id,
|
||||
file_hash=job.file_hash,
|
||||
asset_id=job.asset_id or "",
|
||||
created_at=job.created_at,
|
||||
updated_at=job.updated_at,
|
||||
)
|
||||
@@ -39,7 +38,6 @@ class SQLAlchemyIngestJobRepository:
|
||||
error_message=model.error_message,
|
||||
result_asset_id=model.result_asset_id,
|
||||
file_hash=model.file_hash or "",
|
||||
asset_id=getattr(model, "asset_id", "") or "",
|
||||
created_at=model.created_at,
|
||||
updated_at=model.updated_at,
|
||||
)
|
||||
@@ -56,9 +54,6 @@ class SQLAlchemyIngestJobRepository:
|
||||
model.error_message = job.error_message
|
||||
model.result_asset_id = job.result_asset_id
|
||||
model.file_hash = job.file_hash
|
||||
model.storage_key = job.storage_key
|
||||
if job.asset_id:
|
||||
model.asset_id = job.asset_id
|
||||
model.updated_at = job.updated_at
|
||||
self.session.commit()
|
||||
return job
|
||||
|
||||
@@ -94,7 +94,6 @@ class AssetModel(Base):
|
||||
quality_score = Column(Float, nullable=True)
|
||||
uploaded_by_user_id = Column(String(36), nullable=False)
|
||||
file_hash = Column(String(64), nullable=True, index=True)
|
||||
client_upload_id = Column(String(64), nullable=True, index=True)
|
||||
extra_meta = Column("metadata", JSON, nullable=False, default=dict)
|
||||
created_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc), index=True)
|
||||
updated_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
|
||||
@@ -245,7 +244,6 @@ class IngestJobModel(Base):
|
||||
error_message = Column(Text, nullable=False, default="")
|
||||
result_asset_id = Column(String(36), nullable=False, default="")
|
||||
file_hash = Column(String(64), nullable=True, index=True)
|
||||
asset_id = Column(String(36), nullable=False, default="", index=True)
|
||||
created_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
|
||||
updated_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
|
||||
|
||||
|
||||
@@ -200,15 +200,7 @@ class WechatOAuthService:
|
||||
return None, "微信登录处理失败"
|
||||
|
||||
|
||||
# 模块级单例:state 存储必须跨请求共享,否则 /wechat/url 生成的 state
|
||||
# 与 /wechat/callback 校验时不在同一个 MemoryStateStore,回调必然 400。
|
||||
# 多实例部署时应替换为 Redis state store(单容器多 worker 也需如此)。
|
||||
_oauth_service_singleton: WechatOAuthService | None = None
|
||||
|
||||
|
||||
def get_wechat_oauth_service() -> WechatOAuthService:
|
||||
"""获取微信 OAuth 服务单例(state store 跨请求共享)"""
|
||||
global _oauth_service_singleton
|
||||
if _oauth_service_singleton is None:
|
||||
_oauth_service_singleton = WechatOAuthService()
|
||||
return _oauth_service_singleton
|
||||
"""获取微信 OAuth 服务单例"""
|
||||
# TODO: 可替换为 Redis state store
|
||||
return WechatOAuthService()
|
||||
|
||||
@@ -12,7 +12,6 @@ class SubmitIngestJobCommand:
|
||||
library_id: str
|
||||
storage_key: str
|
||||
file_hash: str = ""
|
||||
asset_id: str = ""
|
||||
|
||||
|
||||
class SubmitIngestJobUseCase:
|
||||
@@ -25,6 +24,5 @@ class SubmitIngestJobUseCase:
|
||||
library_id=command.library_id,
|
||||
storage_key=command.storage_key,
|
||||
file_hash=command.file_hash,
|
||||
asset_id=command.asset_id,
|
||||
)
|
||||
return self.ingest_job_repository.create(job)
|
||||
|
||||
@@ -174,7 +174,6 @@ class Asset:
|
||||
quality_score: float | None = None
|
||||
uploaded_by_user_id: str = ""
|
||||
file_hash: str = ""
|
||||
client_upload_id: str = ""
|
||||
metadata: dict[str, Any] = field(default_factory=dict)
|
||||
tag_ids: list[str] = field(default_factory=list)
|
||||
created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
|
||||
@@ -209,7 +208,6 @@ class Asset:
|
||||
quality_score: float | None = None,
|
||||
uploaded_by_user_id: str = "",
|
||||
file_hash: str = "",
|
||||
client_upload_id: str = "",
|
||||
) -> "Asset":
|
||||
clean_name = name.strip()
|
||||
if not clean_name:
|
||||
@@ -237,7 +235,6 @@ class Asset:
|
||||
quality_score=quality_score,
|
||||
uploaded_by_user_id=uploaded_by_user_id.strip(),
|
||||
file_hash=file_hash.strip(),
|
||||
client_upload_id=client_upload_id.strip(),
|
||||
metadata=metadata or {},
|
||||
tag_ids=[],
|
||||
)
|
||||
@@ -269,7 +266,6 @@ class IngestJob:
|
||||
error_message: str = ""
|
||||
result_asset_id: str = ""
|
||||
file_hash: str = ""
|
||||
asset_id: str = ""
|
||||
created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
|
||||
updated_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
|
||||
|
||||
@@ -280,7 +276,6 @@ class IngestJob:
|
||||
library_id: str,
|
||||
storage_key: str,
|
||||
file_hash: str = "",
|
||||
asset_id: str = "",
|
||||
) -> "IngestJob":
|
||||
if not project_id.strip():
|
||||
raise ValueError("project_id 不能为空")
|
||||
@@ -294,5 +289,4 @@ class IngestJob:
|
||||
library_id=library_id.strip(),
|
||||
storage_key=storage_key.strip(),
|
||||
file_hash=file_hash.strip(),
|
||||
asset_id=asset_id.strip(),
|
||||
)
|
||||
|
||||
@@ -125,23 +125,3 @@ class AssetRepository(ABC):
|
||||
) -> Asset | None:
|
||||
"""按素材库 + 文件哈希查找已有素材(去重检测)。"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def find_by_library_and_client_upload_id(
|
||||
self,
|
||||
library_id: str,
|
||||
client_upload_id: str,
|
||||
) -> Asset | None:
|
||||
"""按素材库 + 客户端幂等 token 查找已有素材(complete 幂等)。"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def find_recent_active_by_library_and_name(
|
||||
self,
|
||||
library_id: str,
|
||||
name: str,
|
||||
within_minutes: int = 30,
|
||||
file_size: int = 0,
|
||||
) -> Asset | None:
|
||||
"""兜底去重:同库 + 同文件名(+同大小)且近期仍在 uploading/processing 的素材。"""
|
||||
pass
|
||||
|
||||
@@ -20,12 +20,6 @@ class GenerationTaskRepository(Protocol):
|
||||
|
||||
def count_pending_total(self) -> int: ...
|
||||
|
||||
def count_running_by_user(self, user_id: str) -> int: ...
|
||||
|
||||
def count_running_total(self) -> int: ...
|
||||
|
||||
def estimate_avg_duration_seconds(self, limit: int = 20, default_seconds: float = 120.0) -> float: ...
|
||||
|
||||
def list_recent_by_user(self, user_id: str, limit: int = 5) -> list[GenerationTask]: ...
|
||||
|
||||
def list_by_source_edit_plan(self, plan_id: str) -> list[GenerationTask]: ...
|
||||
|
||||
@@ -57,7 +57,7 @@ if [ "$TARGET_ENV" = "staging" ]; then
|
||||
fi
|
||||
|
||||
# 共用 secrets 直接导出(如果存在)
|
||||
SHARED_SECRETS="OSS_ACCESS_KEY_ID OSS_ACCESS_KEY_SECRET COSYVOICE_API_KEY DASHSCOPE_API_KEY MEDIAKIT_API_KEY WECHAT_APP_ID WECHAT_APP_SECRET"
|
||||
SHARED_SECRETS="OSS_ACCESS_KEY_ID OSS_ACCESS_KEY_SECRET COSYVOICE_API_KEY DASHSCOPE_API_KEY MEDIAKIT_API_KEY"
|
||||
for var in $SHARED_SECRETS; do
|
||||
value="${!var:-}"
|
||||
# 已经在环境中了,无需额外操作
|
||||
|
||||
@@ -1,507 +0,0 @@
|
||||
"""Issue #1677 多视频批量生成 — 变体独立配置与批量预览/批量生成测试。
|
||||
|
||||
覆盖:
|
||||
- 批量预览:preview_count=N 一次创建 N 个独立任务,返回变体数组
|
||||
- 变体克隆链路:N 个预览/正式任务各自关联独立克隆 plan
|
||||
- 变体独立配置:titles[]/voice_library_ids[]/cover_urls[] 按变体注入
|
||||
- 长度校验:数组长度必须为 1 或 N(共用或独立),非法长度报错
|
||||
- N=1 向后兼容:旧字段单值行为不变
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from app.core.task_enqueue import GlobalQueueFull, UserPendingLimitExceeded
|
||||
from app.schemas.generation_task import (
|
||||
BatchPreviewGenerationTaskResponse,
|
||||
CreateGenerationTaskRequest,
|
||||
CreatePreviewGenerationTaskRequest,
|
||||
)
|
||||
|
||||
from packages.domain import GenerationTask
|
||||
from packages.domain.generation_task import GenerationTaskStatus
|
||||
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
# 辅助构造
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
def _make_user(user_id="test_user_001"):
|
||||
mock_user = MagicMock()
|
||||
mock_user.id = user_id
|
||||
auth = MagicMock()
|
||||
auth.user = mock_user
|
||||
return auth
|
||||
|
||||
|
||||
def _make_task(task_id=None, status=GenerationTaskStatus.PENDING, source_plan_id=None):
|
||||
task = GenerationTask.create(
|
||||
project_id="",
|
||||
asset_library_id="",
|
||||
template_id="tpl_001",
|
||||
asset_ids=["asset_1"],
|
||||
)
|
||||
if task_id:
|
||||
task.id = task_id
|
||||
task.status = status
|
||||
task.is_preview = True
|
||||
task.source_edit_plan_id = source_plan_id or ""
|
||||
task.voice_library_id = ""
|
||||
task.title_config = {}
|
||||
task.cover_url = ""
|
||||
return task
|
||||
|
||||
|
||||
def _make_preview_request(**kwargs):
|
||||
defaults = {
|
||||
"template_id": "tpl_001",
|
||||
"asset_ids": ["asset_1", "asset_2"],
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
return CreatePreviewGenerationTaskRequest(**defaults)
|
||||
|
||||
|
||||
def _repo_mock():
|
||||
repo = MagicMock()
|
||||
repo.count_pending_by_user.return_value = 0
|
||||
repo.count_pending_total.return_value = 0
|
||||
repo.get.side_effect = lambda tid: None
|
||||
return repo
|
||||
|
||||
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
# Schema 校验:变体数组长度
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestVariantArrayValidation:
|
||||
"""变体数组字段长度校验。"""
|
||||
|
||||
def test_preview_titles_length_matches_count(self):
|
||||
"""titles 长度 = preview_count 合法"""
|
||||
req = _make_preview_request(preview_count=3, titles=["标题A", "标题B", "标题C"])
|
||||
assert len(req.titles) == 3
|
||||
|
||||
def test_preview_titles_single_shared(self):
|
||||
"""titles 长度 1 = 所有变体共用,合法"""
|
||||
req = _make_preview_request(preview_count=3, titles=["共用标题"])
|
||||
assert req.titles == ["共用标题"]
|
||||
|
||||
def test_preview_titles_wrong_length_raises(self):
|
||||
"""titles 长度 2 与 preview_count=3 不匹配 → 报错"""
|
||||
with pytest.raises(ValueError, match="titles"):
|
||||
_make_preview_request(preview_count=3, titles=["A", "B"])
|
||||
|
||||
def test_preview_voice_ids_wrong_length_raises(self):
|
||||
"""voice_library_ids 长度非法 → 报错"""
|
||||
from pydantic import ValidationError
|
||||
|
||||
with pytest.raises(ValidationError, match="voice_library_ids"):
|
||||
_make_preview_request(preview_count=4, voice_library_ids=["v1", "v2"])
|
||||
|
||||
def test_preview_empty_arrays_ok(self):
|
||||
"""空数组(回退单值字段)合法"""
|
||||
req = _make_preview_request(preview_count=3)
|
||||
assert req.titles == []
|
||||
assert req.voice_library_ids == []
|
||||
assert req.cover_urls == []
|
||||
|
||||
def test_generation_titles_length_matches_count(self):
|
||||
"""正式生成 titles 长度 = count 合法"""
|
||||
req = CreateGenerationTaskRequest(
|
||||
template_id="tpl_1",
|
||||
asset_ids=["a1"],
|
||||
count=3,
|
||||
titles=["A", "B", "C"],
|
||||
)
|
||||
assert len(req.titles) == 3
|
||||
|
||||
def test_generation_arrays_wrong_length_raises(self):
|
||||
"""正式生成 cover_urls 长度与 count 不匹配 → 报错"""
|
||||
from pydantic import ValidationError
|
||||
|
||||
with pytest.raises(ValidationError, match="cover_urls"):
|
||||
CreateGenerationTaskRequest(
|
||||
template_id="tpl_1",
|
||||
asset_ids=["a1"],
|
||||
count=3,
|
||||
cover_urls=["c1", "c2"],
|
||||
)
|
||||
|
||||
def test_generation_single_count_no_arrays(self):
|
||||
"""N=1 且不传数组:完全旧行为"""
|
||||
req = CreateGenerationTaskRequest(template_id="tpl_1", asset_ids=["a1"])
|
||||
assert req.count == 1
|
||||
assert req.titles == []
|
||||
assert req.voice_library_ids == []
|
||||
assert req.cover_urls == []
|
||||
|
||||
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
# 批量预览路由
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestBatchPreviewRoute:
|
||||
"""POST /preview 批量变体。"""
|
||||
|
||||
def test_preview_count_1_returns_single_item_array(self):
|
||||
"""N=1 返回 items 长度 1 的批量响应(结构统一)"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
task = _make_task(task_id="task_1")
|
||||
repo = _repo_mock()
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
MockUC.return_value.execute.return_value = task
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
resp = create_preview_generation_task(
|
||||
_make_preview_request(preview_count=1),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert isinstance(resp, BatchPreviewGenerationTaskResponse)
|
||||
assert resp.total == 1
|
||||
assert len(resp.items) == 1
|
||||
assert resp.items[0].task_id == "task_1"
|
||||
assert resp.items[0].variant_index == 0
|
||||
|
||||
def test_preview_count_3_creates_three_independent_tasks(self):
|
||||
"""N=3 创建 3 个独立任务,返回 3 个变体,task_id 各不相同"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
tasks = [_make_task(task_id=f"task_{i}") for i in range(3)]
|
||||
repo = _repo_mock()
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
MockUC.return_value.execute.side_effect = tasks
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
resp = create_preview_generation_task(
|
||||
_make_preview_request(preview_count=3),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert resp.total == 3
|
||||
task_ids = [item.task_id for item in resp.items]
|
||||
assert task_ids == ["task_0", "task_1", "task_2"]
|
||||
assert len(set(task_ids)) == 3
|
||||
for i, item in enumerate(resp.items):
|
||||
assert item.variant_index == i
|
||||
|
||||
def test_preview_count_3_clones_three_variant_plans(self):
|
||||
"""有源 plan 时,N=3 克隆 3 个独立变体 plan(预览全部克隆,不用源 plan)"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
tasks = [_make_task(task_id=f"task_{i}", source_plan_id="source_plan") for i in range(3)]
|
||||
repo = _repo_mock()
|
||||
cloned_plan_ids = ["clone_1", "clone_2", "clone_3"]
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
MockUC.return_value.execute.side_effect = tasks
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
with patch("app.services.edit_plan_service.EditPlanService") as MockPlanSvc:
|
||||
clone_results = [MagicMock(id=pid) for pid in cloned_plan_ids]
|
||||
MockPlanSvc.return_value.clone_plan_for_variant.side_effect = clone_results
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(preview_count=3),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
# 克隆被调用 3 次
|
||||
assert MockPlanSvc.return_value.clone_plan_for_variant.call_count == 3
|
||||
# 每个任务关联到不同的克隆 plan
|
||||
for i, task in enumerate(tasks):
|
||||
assert task.source_edit_plan_id == cloned_plan_ids[i]
|
||||
|
||||
def test_preview_variant_titles_injected_per_variant(self):
|
||||
"""titles[] 按变体注入 title_config.text"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
tasks = [_make_task(task_id=f"task_{i}") for i in range(3)]
|
||||
repo = _repo_mock()
|
||||
captured_commands = []
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured_commands.append(cmd)
|
||||
return tasks[len(captured_commands) - 1]
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(
|
||||
preview_count=3,
|
||||
title_config={"font": "黑体", "position": "bottom"},
|
||||
titles=["标题A", "标题B", "标题C"],
|
||||
),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert len(captured_commands) == 3
|
||||
assert captured_commands[0].title_config["text"] == "标题A"
|
||||
assert captured_commands[1].title_config["text"] == "标题B"
|
||||
assert captured_commands[2].title_config["text"] == "标题C"
|
||||
# 样式全局共用
|
||||
assert all(c.title_config["font"] == "黑体" for c in captured_commands)
|
||||
|
||||
def test_preview_shared_title_when_single_length(self):
|
||||
"""titles 长度 1 = 所有变体共用同一标题"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
tasks = [_make_task(task_id=f"task_{i}") for i in range(3)]
|
||||
repo = _repo_mock()
|
||||
captured = []
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured.append(cmd)
|
||||
return tasks[len(captured) - 1]
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(preview_count=3, titles=["共用标题"]),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert all(c.title_config["text"] == "共用标题" for c in captured)
|
||||
|
||||
def test_preview_independent_voice_per_variant(self):
|
||||
"""voice_library_ids[] 按变体注入独立配音"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
tasks = [_make_task(task_id=f"task_{i}") for i in range(3)]
|
||||
repo = _repo_mock()
|
||||
captured = []
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured.append(cmd)
|
||||
return tasks[len(captured) - 1]
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(
|
||||
preview_count=3,
|
||||
voice_library_ids=["voice_a", "voice_b", "voice_c"],
|
||||
),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert [c.voice_library_id for c in captured] == ["voice_a", "voice_b", "voice_c"]
|
||||
|
||||
def test_preview_voice_fallback_to_single_field(self):
|
||||
"""voice_library_ids 为空时回退 voice_library_id 单值字段(向后兼容)"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
task = _make_task(task_id="task_1")
|
||||
repo = _repo_mock()
|
||||
captured = []
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured.append(cmd)
|
||||
return task
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch("app.api.routes.generation_preview.safe_enqueue_generation_task", return_value=True):
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(voice_library_id="legacy_voice"),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert captured[0].voice_library_id == "legacy_voice"
|
||||
|
||||
def test_preview_queue_limit_checks_total_count(self):
|
||||
"""限流预检查按变体总数计:用户 pending + N 超限 → 429"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
from fastapi import HTTPException
|
||||
|
||||
repo = MagicMock()
|
||||
repo.count_pending_by_user.return_value = 3
|
||||
repo.count_pending_total.return_value = 0
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(preview_count=5),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert exc.value.status_code == 429
|
||||
|
||||
def test_preview_clone_failure_marks_all_failed(self):
|
||||
"""克隆变体 plan 失败 → 已创建任务全部标记 failed 并 500"""
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
from fastapi import HTTPException
|
||||
|
||||
tasks = [_make_task(task_id=f"task_{i}", source_plan_id="source_plan") for i in range(3)]
|
||||
repo = _repo_mock()
|
||||
with patch("app.api.routes.generation_preview.CreateGenerationTaskUseCase") as MockUC:
|
||||
MockUC.return_value.execute.side_effect = tasks
|
||||
with patch("app.services.edit_plan_service.EditPlanService") as MockPlanSvc:
|
||||
MockPlanSvc.return_value.clone_plan_for_variant.side_effect = RuntimeError("db down")
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
create_preview_generation_task(
|
||||
_make_preview_request(preview_count=3),
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
db=MagicMock(),
|
||||
)
|
||||
assert exc.value.status_code == 500
|
||||
# 所有已创建任务都被标记 failed
|
||||
assert all(t.status == GenerationTaskStatus.FAILED for t in tasks)
|
||||
|
||||
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
# 批量正式生成:变体配置注入
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestBatchGenerationVariantConfig:
|
||||
"""POST /tasks count=N 时变体独立配置。"""
|
||||
|
||||
def _call_create_tasks(self, request, repo=None):
|
||||
from app.api.routes.generation_tasks import create_generation_task
|
||||
|
||||
repo = repo or MagicMock()
|
||||
repo.count_pending_by_user.return_value = 0
|
||||
repo.count_pending_total.return_value = 0
|
||||
repo.update.return_value = None
|
||||
|
||||
# 模板模式:asset_repository.find_by_id 返回 None(无 project 关联,
|
||||
# 纯模板模式 project_id/library_id 都为空),避免 MagicMock 属性污染
|
||||
asset_repo = MagicMock()
|
||||
asset_repo.find_by_id.return_value = None
|
||||
|
||||
# db.query().filter()...first() 返回 None:不走兜底关联编辑计划
|
||||
db = MagicMock()
|
||||
db.query.return_value.filter.return_value.order_by.return_value.first.return_value = None
|
||||
|
||||
return create_generation_task(
|
||||
request,
|
||||
authenticated_user=_make_user(),
|
||||
generation_task_repository=repo,
|
||||
project_repository=MagicMock(),
|
||||
asset_library_repository=MagicMock(),
|
||||
asset_repository=asset_repo,
|
||||
db=db,
|
||||
)
|
||||
|
||||
def test_count_3_variant_titles_voices_covers_injected(self):
|
||||
"""count=3:titles/voice_library_ids/cover_urls 按变体注入"""
|
||||
from app.api.routes import generation_tasks as routes
|
||||
|
||||
tasks = [_make_task(task_id=f"gen_{i}") for i in range(3)]
|
||||
captured = []
|
||||
with patch.object(routes, "CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured.append(cmd)
|
||||
t = tasks[len(captured) - 1]
|
||||
t.title_config = cmd.title_config
|
||||
t.voice_library_id = cmd.voice_library_id
|
||||
t.cover_url = cmd.cover_url
|
||||
return t
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch.object(routes, "safe_enqueue_generation_task", return_value=True):
|
||||
req = CreateGenerationTaskRequest(
|
||||
template_id="tpl_1",
|
||||
asset_ids=["a1"],
|
||||
count=3,
|
||||
title_config={"font": "宋体"},
|
||||
titles=["成片标题1", "成片标题2", "成片标题3"],
|
||||
voice_library_ids=["v1", "v2", "v3"],
|
||||
cover_urls=["http://c1", "http://c2", "http://c3"],
|
||||
)
|
||||
resp = self._call_create_tasks(req)
|
||||
assert resp.total == 3
|
||||
assert [c.title_config["text"] for c in captured] == ["成片标题1", "成片标题2", "成片标题3"]
|
||||
assert [c.voice_library_id for c in captured] == ["v1", "v2", "v3"]
|
||||
assert [c.cover_url for c in captured] == ["http://c1", "http://c2", "http://c3"]
|
||||
# 样式共用
|
||||
assert all(c.title_config["font"] == "宋体" for c in captured)
|
||||
|
||||
def test_count_1_legacy_fields_unchanged(self):
|
||||
"""N=1 不传数组:旧字段 voice_library_id/cover_url/title_config 行为不变"""
|
||||
from app.api.routes import generation_tasks as routes
|
||||
|
||||
task = _make_task(task_id="gen_1")
|
||||
task.is_preview = False
|
||||
captured = []
|
||||
with patch.object(routes, "CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured.append(cmd)
|
||||
return task
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch.object(routes, "safe_enqueue_generation_task", return_value=True):
|
||||
req = CreateGenerationTaskRequest(
|
||||
template_id="tpl_1",
|
||||
asset_ids=["a1"],
|
||||
count=1,
|
||||
voice_library_id="legacy_voice",
|
||||
cover_url="http://legacy-cover",
|
||||
title_config={"text": "旧标题", "font": "黑体"},
|
||||
)
|
||||
resp = self._call_create_tasks(req)
|
||||
assert resp.total == 1
|
||||
assert captured[0].voice_library_id == "legacy_voice"
|
||||
assert captured[0].cover_url == "http://legacy-cover"
|
||||
assert captured[0].title_config["text"] == "旧标题"
|
||||
|
||||
def test_count_3_shared_single_value_arrays(self):
|
||||
"""数组长度 1:3 个变体共用同一配音/封面"""
|
||||
from app.api.routes import generation_tasks as routes
|
||||
|
||||
tasks = [_make_task(task_id=f"gen_{i}") for i in range(3)]
|
||||
captured = []
|
||||
with patch.object(routes, "CreateGenerationTaskUseCase") as MockUC:
|
||||
|
||||
def _execute(cmd):
|
||||
captured.append(cmd)
|
||||
return tasks[len(captured) - 1]
|
||||
|
||||
MockUC.return_value.execute.side_effect = _execute
|
||||
with patch.object(routes, "safe_enqueue_generation_task", return_value=True):
|
||||
req = CreateGenerationTaskRequest(
|
||||
template_id="tpl_1",
|
||||
asset_ids=["a1"],
|
||||
count=3,
|
||||
voice_library_ids=["shared_voice"],
|
||||
cover_urls=["http://shared"],
|
||||
)
|
||||
self._call_create_tasks(req)
|
||||
assert all(c.voice_library_id == "shared_voice" for c in captured)
|
||||
assert all(c.cover_url == "http://shared" for c in captured)
|
||||
|
||||
|
||||
class TestVariantValueHelper:
|
||||
"""_variant_value 取值逻辑。"""
|
||||
|
||||
def test_empty_returns_fallback(self):
|
||||
from app.api.routes.generation_preview import _variant_value
|
||||
|
||||
assert _variant_value([], 0, fallback="fb") == "fb"
|
||||
|
||||
def test_single_length_shared(self):
|
||||
from app.api.routes.generation_preview import _variant_value
|
||||
|
||||
assert _variant_value(["only"], 5) == "only"
|
||||
|
||||
def test_indexed_access(self):
|
||||
from app.api.routes.generation_preview import _variant_value
|
||||
|
||||
assert _variant_value(["a", "b", "c"], 1) == "b"
|
||||
|
||||
def test_index_out_of_range_fallback(self):
|
||||
from app.api.routes.generation_preview import _variant_value
|
||||
|
||||
assert _variant_value(["a", "b"], 9, fallback="x") == "x"
|
||||
@@ -85,18 +85,17 @@ class TestIsBadFingerprint:
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["abcdef0123456789"]) is False
|
||||
|
||||
def test_all_identical_phashes_is_bad(self):
|
||||
""">=8 帧且所有 phash 完全相同 → 黑屏/纯色视频(#1702:短帧不误杀)。"""
|
||||
phashes = ["aaaaaaaaaaaaaaaa"] * 10
|
||||
"""多帧但所有 phash 完全相同 → 黑屏/纯色视频。"""
|
||||
phashes = ["aaaaaaaaaaaaaaaa"] * 5
|
||||
assert VideoDeduplicator._is_bad_fingerprint(phashes) is True
|
||||
|
||||
def test_short_identical_phashes_not_bad(self):
|
||||
"""<8 帧完全相同不判坏——短视频内容连续时相邻采样帧 phash 天然相同(#1702)。"""
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["bbbbbbbbbbbbbbbb"] * 5) is False
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["bbbbbbbbbbbbbbbb", "bbbbbbbbbbbbbbbb"]) is False
|
||||
def test_two_identical_phashes_is_bad(self):
|
||||
"""两帧完全相同也视为坏指纹。"""
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["bbbbbbbbbbbbbbbb", "bbbbbbbbbbbbbbbb"]) is True
|
||||
|
||||
def test_all_very_similar_phashes_is_bad(self):
|
||||
""">=8 帧 phash 之间的汉明距离都 < 3 且高占比 → 近似黑屏。"""
|
||||
phashes = ["0000000000000000"] * 8 + ["0000000000000001", "0000000000000002"]
|
||||
"""多帧 phash 之间的汉明距离都 < 3 → 近似黑屏。"""
|
||||
phashes = ["0000000000000000", "0000000000000001", "0000000000000002"]
|
||||
assert VideoDeduplicator._is_bad_fingerprint(phashes) is True
|
||||
|
||||
def test_diverse_phashes_is_good(self):
|
||||
@@ -123,9 +122,7 @@ class TestIsBadFingerprint:
|
||||
"""已知黑屏视频的 phash 特征(全零或均匀分布)。"""
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["0000000000000000"] * 10) is True
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["ffffffffffffffff"] * 8) is True
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["9999999999999966"] * 8) is True
|
||||
# <8 帧不判坏(#1702 短视频保护)
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["9999999999999966"] * 5) is False
|
||||
assert VideoDeduplicator._is_bad_fingerprint(["9999999999999966"] * 6) is True
|
||||
|
||||
|
||||
# ── Helper ──────────────────────────────────────────────────────
|
||||
@@ -154,13 +151,13 @@ class TestCheckDuplicateBadFingerprint:
|
||||
deduplicator = VideoDeduplicator()
|
||||
mock_session = MagicMock()
|
||||
|
||||
black_screen = _make_existing_video("vid-black", "md5_black", ["aaaaaaaaaaaaaaaa"] * 10)
|
||||
black_screen = _make_existing_video("vid-black", "md5_black", ["aaaaaaaaaaaaaaaa"] * 5)
|
||||
mock_repo = MagicMock()
|
||||
mock_repo.list_by_user.return_value = [black_screen]
|
||||
|
||||
fingerprint = VideoFingerprint(
|
||||
md5="md5_normal",
|
||||
keyframe_phashes=["aaaaaaaaaaaaaaaa"] * 10,
|
||||
keyframe_phashes=["aaaaaaaaaaaaaaaa"] * 5,
|
||||
color_histograms=[],
|
||||
duration=10.0,
|
||||
resolution=(1280, 720),
|
||||
@@ -209,7 +206,7 @@ class TestCheckDuplicateBadFingerprint:
|
||||
deduplicator = VideoDeduplicator()
|
||||
mock_session = MagicMock()
|
||||
|
||||
black_screen = _make_existing_video("vid-black", "same_md5", ["aaaaaaaaaaaaaaaa"] * 10)
|
||||
black_screen = _make_existing_video("vid-black", "same_md5", ["aaaaaaaaaaaaaaaa"] * 5)
|
||||
mock_repo = MagicMock()
|
||||
mock_repo.list_by_user.return_value = [black_screen]
|
||||
|
||||
@@ -286,8 +283,8 @@ class TestComputeDuplicateRateBadFingerprint:
|
||||
mock_session = MagicMock()
|
||||
|
||||
videos = [
|
||||
_make_existing_video("vid-b1", "md5_b1", ["aaaaaaaaaaaaaaaa"] * 10),
|
||||
_make_existing_video("vid-b2", "md5_b2", ["cccccccccccccccc"] * 5), # hamming(a,c)=32 > PHASH_THRESHOLD
|
||||
_make_existing_video("vid-b1", "md5_b1", ["aaaaaaaaaaaaaaaa"] * 5),
|
||||
_make_existing_video("vid-b2", "md5_b2", ["bbbbbbbbbbbbbbbb"] * 5),
|
||||
]
|
||||
mock_repo = MagicMock()
|
||||
mock_repo.list_by_user.return_value = videos
|
||||
|
||||
@@ -1,548 +0,0 @@
|
||||
"""Issue #1702 — 查重率恒为 0% 修复:单测.
|
||||
|
||||
覆盖验收要求:
|
||||
1. 同源不同裁剪的两个视频能检出非 0 相似度(指纹中心裁剪绕开降重 + 阈值校准)
|
||||
2. 局部片段复用(B 结尾 2s ≈ A 中间 2s)能检出
|
||||
3. 异源视频不误报(相似度接近 0)
|
||||
4. N=1 现有流程不回归
|
||||
5. P1 确定性 bug:时长预过滤单位 /1000、直方图归一化、temporal_coverage 量纲、阈值比较统一
|
||||
6. P0:±1 邻接对齐、短视频自适应连续门槛
|
||||
7. P2:0 匹配也要落日志
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
sys.modules.setdefault("cv2", MagicMock())
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "apps" / "worker"))
|
||||
sys.path.insert(0, str(ROOT / "packages"))
|
||||
|
||||
|
||||
from video_processing.dedup import ( # noqa: E402
|
||||
PHASH_THRESHOLD,
|
||||
SEGMENT_MATCH_THRESHOLD,
|
||||
FingerprintChunk,
|
||||
VideoDeduplicator,
|
||||
VideoFingerprint,
|
||||
find_duplicate_segments,
|
||||
)
|
||||
|
||||
# ── helpers ────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _h(d: int) -> str:
|
||||
"""64-bit phash with exactly d bits set vs zero hash."""
|
||||
bits = ["0"] * 64
|
||||
for i in range(d):
|
||||
bits[i] = "1"
|
||||
return f"{int(''.join(bits), 2):016x}"
|
||||
|
||||
|
||||
def _chunk(phash: str, t0: float, t1: float):
|
||||
|
||||
return FingerprintChunk(
|
||||
start_time_ms=int(t0 * 1000),
|
||||
end_time_ms=int(t1 * 1000),
|
||||
phash_binary=phash,
|
||||
color_histogram=[],
|
||||
frame_count=1,
|
||||
)
|
||||
|
||||
|
||||
def _fingerprint(phashes, duration, chunks=None, md5="fp-md5-x"):
|
||||
|
||||
return VideoFingerprint(
|
||||
md5=md5,
|
||||
keyframe_phashes=list(phashes),
|
||||
color_histograms=[],
|
||||
duration=duration,
|
||||
resolution=(1280, 720),
|
||||
chunks=chunks or [],
|
||||
)
|
||||
|
||||
|
||||
def _video(vid, phashes, duration=10.0, project_id="proj1"):
|
||||
from packages.domain import GeneratedVideo
|
||||
|
||||
return GeneratedVideo(
|
||||
id=vid,
|
||||
project_id=project_id,
|
||||
generation_task_id=f"task-{vid}",
|
||||
name=f"video-{vid}.mp4",
|
||||
file_url=f"https://example.com/{vid}.mp4",
|
||||
file_size=1000,
|
||||
duration=duration,
|
||||
width=1280,
|
||||
height=720,
|
||||
fps=25.0,
|
||||
video_fingerprint={"md5": f"md5-{vid}", "keyframe_phashes": list(phashes)},
|
||||
)
|
||||
|
||||
|
||||
def _rate(deduplicator, fp, videos, session=None):
|
||||
session_magic = MagicMock()
|
||||
# 分片表无数据 -> 回退 JSON keyframe_phashes
|
||||
session_magic.query.return_value.filter.return_value.order_by.return_value.all.return_value = []
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
repo = MockRepo.return_value
|
||||
repo.list_by_project.return_value = videos
|
||||
repo.list_by_user.return_value = videos
|
||||
return deduplicator.compute_duplicate_rate(fp, "proj1", "new-vid", session_magic, scope="project")
|
||||
|
||||
|
||||
def _check(deduplicator, fp, videos, scope="project", **kw):
|
||||
session_magic = MagicMock()
|
||||
session_magic.query.return_value.filter.return_value.order_by.return_value.all.return_value = []
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
repo = MockRepo.return_value
|
||||
repo.list_by_project.return_value = videos
|
||||
repo.list_by_user.return_value = videos
|
||||
return deduplicator.check_duplicate(fp, "proj1", session_magic, scope=scope, **kw)
|
||||
|
||||
|
||||
# ── P0-1/P0-2: 同源不同裁剪(距离 6~10)检出非 0 ──────────────
|
||||
|
||||
|
||||
class TestSameSourceDifferentCrop:
|
||||
"""同源成片:random_edge_crop 后 pHash 距离 6~10,应检出非 0 相似度。"""
|
||||
|
||||
def test_same_source_high_similarity_detected(self):
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
# 新视频 5 个分片,每个 phash 与已有视频对应分片距离 6(< 阈值)
|
||||
base = [_h(0) for _ in range(5)]
|
||||
new = [_h(6) for _ in range(5)]
|
||||
existing = _video("v-old", base, duration=11.0)
|
||||
chunks = [_chunk(h, i * 2.2, (i + 1) * 2.2) for i, h in enumerate(new)]
|
||||
fp = _fingerprint(new, 11.0, chunks=chunks)
|
||||
|
||||
result = _rate(ddp, fp, [existing], MagicMock())
|
||||
assert result["duplicate_rate"] > 0
|
||||
assert result["visual_similarity"] > 0
|
||||
|
||||
def test_same_source_distance_at_threshold_still_detected(self):
|
||||
"""距离正好等于阈值(<=)也要算匹配——阈值比较统一为 <=。"""
|
||||
|
||||
assert PHASH_THRESHOLD <= 16, "阈值应经真实数据校准保持在能检出同源裁剪/降重对的范围(#1702 二次校准为 16)"
|
||||
ddp = VideoDeduplicator()
|
||||
base = [_h(0) for _ in range(6)]
|
||||
new = [_h(PHASH_THRESHOLD) for _ in range(6)]
|
||||
existing = _video("v-old", base, duration=12.0)
|
||||
chunks = [_chunk(h, i * 2, (i + 1) * 2) for i, h in enumerate(new)]
|
||||
fp = _fingerprint(new, 12.0, chunks=chunks)
|
||||
|
||||
result = _rate(ddp, fp, [existing], MagicMock())
|
||||
assert result["duplicate_rate"] > 0
|
||||
|
||||
|
||||
# ── P0-2: 局部片段复用(B 结尾 2s ≈ A 中间 2s) ────────────────
|
||||
|
||||
|
||||
class TestPartialReuse:
|
||||
def test_partial_reuse_tail_overlap_detected(self):
|
||||
"""新视频 6 片,最后 2 片命中已有视频中间 2 片(距离 4),其余不匹配。
|
||||
|
||||
旧逻辑 frame_match_rate=2/6≈0.33(<0.3 硬跳过边界)+ MIN_CONSECUTIVE=5
|
||||
导致完全检不出;新逻辑 coverage 为主指标 + 自适应门槛应检出。
|
||||
"""
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
# 已有 8 片:索引 3、4 是被复用的镜头
|
||||
old = [_h(20 + i) for i in range(8)]
|
||||
# 新视频 6 片:最后 2 片对应 old[3], old[4],距离 4;其余距离 30
|
||||
new = [_h(50 + i) for i in range(4)] + [_h(4)] * 2
|
||||
# 让 new[4] 与 old[3] 距离 4、new[5] 与 old[4] 距离 4(构造近似)
|
||||
new[4] = f"{int('1' * 4 + '0' * 60, 2):016x}"
|
||||
new[5] = f"{int('1' * 4 + '0' * 60, 2):016x}"
|
||||
old[3] = _h(0)
|
||||
old[4] = _h(0)
|
||||
|
||||
existing = _video("v-old", old, duration=16.0)
|
||||
chunks = [_chunk(h, i * 2, (i + 1) * 2) for i, h in enumerate(new)]
|
||||
fp = _fingerprint(new, 12.0, chunks=chunks)
|
||||
|
||||
result = _rate(ddp, fp, [existing], MagicMock())
|
||||
# 局部复用:duplicate_rate 必须非 0
|
||||
assert result["duplicate_rate"] > 0
|
||||
|
||||
def test_short_video_adaptive_consecutive_threshold(self):
|
||||
"""11s/5 片短视频:MIN_CONSECUTIVE 自适应 min(5, max(2, 5//2))=2,
|
||||
2 片连续命中即报片段(旧值 5 让短视频永远无法报片段)。"""
|
||||
|
||||
q = [
|
||||
FingerprintChunk(0, 2000, "f" * 16, []),
|
||||
FingerprintChunk(2000, 4000, "0" * 16, []),
|
||||
FingerprintChunk(4000, 6000, f"{int('11110000', 2):016x}", []),
|
||||
]
|
||||
t = [
|
||||
FingerprintChunk(0, 2000, "f" * 16, []),
|
||||
FingerprintChunk(2000, 4000, "0" * 16, []),
|
||||
FingerprintChunk(4000, 6000, "e" * 16, []),
|
||||
]
|
||||
# 3 片视频自适应门槛 = min(5, max(2, 3//2)) = 2
|
||||
segs = find_duplicate_segments(q, t)
|
||||
assert len(segs) >= 1
|
||||
|
||||
|
||||
# ── P0-3: ±1 邻接窗口对齐 ─────────────────────────────────────
|
||||
|
||||
|
||||
class TestNeighborAlignment:
|
||||
def test_neighbor_window_absorbs_boundary_jitter(self):
|
||||
"""切点错位导致目标索引偏移 ±1 时,连续匹配不应被中断。"""
|
||||
|
||||
q = [FingerprintChunk(i * 1000, (i + 1) * 1000, f"{i:016x}", []) for i in range(4)]
|
||||
# 目标:前 3 片与 q 相同,但第 3 片最佳匹配偏移 +1(t[4]),t[3] 是无关内容
|
||||
t_hashes = [f"{i:016x}" for i in range(3)] + ["f" * 16, f"{3:016x}"]
|
||||
t = [FingerprintChunk(i * 1000, (i + 1) * 1000, h, []) for i, h in enumerate(t_hashes)]
|
||||
segs = find_duplicate_segments(q, t)
|
||||
# q[0],q[1] 精确匹配 t[0],t[1];q[2]->t[2];q[3]->t[4](步进 2,窗口 ±1 内)
|
||||
assert len(segs) >= 1
|
||||
assert segs[0].query_end_ms >= 3000
|
||||
|
||||
|
||||
# ── P0-5 / 验收:异源不误报 ───────────────────────────────────
|
||||
|
||||
|
||||
class TestDifferentSourceNoFalsePositive:
|
||||
def test_unrelated_videos_near_zero(self):
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
# 异源:所有分片距离 >= 20
|
||||
old = [_h(40 + i * 3 % 20) for i in range(6)]
|
||||
new = [_h(0 + i) for i in range(6)]
|
||||
existing = _video("v-old", old, duration=12.0)
|
||||
chunks = [_chunk(h, i * 2, (i + 1) * 2) for i, h in enumerate(new)]
|
||||
fp = _fingerprint(new, 12.0, chunks=chunks)
|
||||
|
||||
result = _rate(ddp, fp, [existing], MagicMock())
|
||||
assert result["duplicate_rate"] == 0
|
||||
assert result["visual_similarity"] < 0.7
|
||||
assert result["match_count"] == 0
|
||||
|
||||
def test_check_duplicate_returns_none_for_unrelated(self):
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
old = [_h(40 + i) for i in range(6)]
|
||||
new = [_h(i) for i in range(6)]
|
||||
existing = _video("v-old", old, duration=12.0)
|
||||
fp = _fingerprint(new, 12.0)
|
||||
|
||||
result = _check(ddp, fp, [existing])
|
||||
assert result is None
|
||||
|
||||
|
||||
# ── N=1 不回归 ────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestSingleChunkNoRegression:
|
||||
def test_single_chunk_identical_detected(self):
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
h = _h(2)
|
||||
existing = _video("v-old", [h], duration=3.0)
|
||||
chunks = [_chunk(h, 0, 3000)]
|
||||
fp = _fingerprint([h], 3.0, chunks=chunks)
|
||||
result = _rate(ddp, fp, [existing], MagicMock())
|
||||
assert result["duplicate_rate"] > 0
|
||||
|
||||
def test_single_chunk_md5_exact_match(self):
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
existing = _video("v-old", [_h(0)], duration=3.0)
|
||||
existing.video_fingerprint["md5"] = "same"
|
||||
fp = _fingerprint([_h(0)], 3.0, md5="same")
|
||||
result = _check(ddp, fp, [existing])
|
||||
assert result is not None
|
||||
assert result["reason"] == "exact_md5_match"
|
||||
|
||||
|
||||
# ── P1-6: 时长预过滤单位 bug ──────────────────────────────────
|
||||
|
||||
|
||||
class TestDurationPrefilterUnit:
|
||||
def test_user_scope_skips_duration_prefilter(self):
|
||||
"""Issue #1702: scope=user 跨项目查重不做 ±15% 时长预过滤。
|
||||
|
||||
旧逻辑 duration/1000 单位 bug 先修成秒,但 ±15% 窗口与局部片段复用
|
||||
根本矛盾——复用片段的两个视频时长必然不同(证据视频 20s vs 11s 差 42%),
|
||||
窗口内找不到对方导致 is_duplicate 恒 False。最终口径:scope=user 全量
|
||||
遍历同用户视频(与 compute_duplicate_rate 一致),不传 duration_min/max。
|
||||
"""
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
fp = _fingerprint([_h(0)], 13.5)
|
||||
session_magic = MagicMock()
|
||||
session_magic.query.return_value.filter.return_value.order_by.return_value.all.return_value = []
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
repo = MockRepo.return_value
|
||||
repo.list_by_user.return_value = []
|
||||
ddp.check_duplicate(fp, "proj1", session_magic, scope="user", user_id="u1", duration_sec=fp.duration)
|
||||
args, kwargs = repo.list_by_user.call_args
|
||||
# 全量查询:不带任何时长过滤参数(局部复用必须跨时长比较)
|
||||
assert "duration_min" not in kwargs
|
||||
assert "duration_max" not in kwargs
|
||||
assert args == ("u1",) or args == ()
|
||||
|
||||
|
||||
# ── P1-7: 颜色直方图归一化 ────────────────────────────────────
|
||||
|
||||
|
||||
class TestHistogramNormalization:
|
||||
def test_bhattacharyya_coefficient_in_unit_range(self):
|
||||
"""Bhattacharyya 系数必须在 [0,1](旧 L2 + 3 通道拼接算出 ~14.9)。"""
|
||||
|
||||
# 3 通道拼接、每通道概率分布(Σ=1)
|
||||
hist_a = [0.5, 0.5] + [0.0] * 94 + [0.5, 0.5] + [0.0] * 94 + [0.5, 0.5] + [0.0] * 94
|
||||
# 长度裁剪到 96(3 通道 × 32 bins)
|
||||
hist_a = ([0.5, 0.5] + [0.0] * 30) * 3
|
||||
hist_b = ([0.5, 0.5] + [0.0] * 30) * 3
|
||||
|
||||
coeff = VideoDeduplicator._bhattacharyya_coefficient(hist_a, hist_b)
|
||||
assert 0.0 <= coeff <= 1.0
|
||||
assert coeff > 0.99 # 完全相同 -> 1.0
|
||||
|
||||
def test_bhattacharyya_disjoint_hist_low(self):
|
||||
|
||||
hist_a = ([1.0] + [0.0] * 31) * 3
|
||||
hist_b = ([0.0] * 31 + [1.0]) * 3
|
||||
coeff = VideoDeduplicator._bhattacharyya_coefficient(hist_a, hist_b)
|
||||
assert coeff < 0.05
|
||||
|
||||
|
||||
# ── P1-8: temporal_coverage 量纲 ──────────────────────────────
|
||||
|
||||
|
||||
class TestTemporalCoverageUnits:
|
||||
def test_coverage_uses_milliseconds(self):
|
||||
"""命中片段 6s / 视频 12s -> coverage=0.5;旧 bug 把 duration(秒)当毫秒,
|
||||
covered_ms(6000)/duration(12) = 500 -> min(1.0)=1.0 误判 100% 覆盖。"""
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
old = [_h(0) for _ in range(6)]
|
||||
new = [_h(0) for _ in range(3)] + [_h(30) for _ in range(3)]
|
||||
existing = _video("v-old", old, duration=12.0)
|
||||
# 新视频 12s,前 6s(3 片)与 old 相同
|
||||
chunks = [_chunk(h, i * 2, (i + 1) * 2) for i, h in enumerate(new)]
|
||||
fp = _fingerprint(new, 12.0, chunks=chunks)
|
||||
result = _rate(ddp, fp, [existing], MagicMock())
|
||||
# coverage 应约 0.5(3 片 × 2s = 6s / 12s),duplicate_rate ≈ (0.5*0.4 + 0.5*0.6)*100 = 50
|
||||
assert 30 < result["duplicate_rate"] < 70
|
||||
|
||||
|
||||
# ── P1-9: 阈值比较统一 ────────────────────────────────────────
|
||||
|
||||
|
||||
class TestThresholdConsistency:
|
||||
def test_frame_and_segment_thresholds_same_source(self):
|
||||
|
||||
assert SEGMENT_MATCH_THRESHOLD == PHASH_THRESHOLD
|
||||
assert VideoDeduplicator.PHASH_THRESHOLD == PHASH_THRESHOLD
|
||||
|
||||
|
||||
# ── P2: 0 匹配也要有日志痕迹 ──────────────────────────────────
|
||||
|
||||
|
||||
class TestZeroMatchLogging:
|
||||
def test_no_match_emits_info_log(self, caplog):
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
old = [_h(40 + i) for i in range(5)]
|
||||
existing = _video("v-old", old, duration=10.0)
|
||||
fp = _fingerprint([_h(i) for i in range(5)], 10.0)
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="video_processing.dedup"):
|
||||
result = _check(ddp, fp, [existing])
|
||||
assert result is None
|
||||
assert any("no match" in r.message for r in caplog.records)
|
||||
|
||||
|
||||
# ── recompute 任务下载路径(#1702 连带修复:旧硬编码 key 404) ─────
|
||||
|
||||
|
||||
class TestRecomputeDownloadPath:
|
||||
"""recompute-dedup 走 check_duplicate_task,需要从 OSS 重新下载成片。
|
||||
|
||||
旧代码硬编码 projects/{pid}/generated/{vid}/{vid}.mp4(从不存在),
|
||||
真实 key 在 file_url:generated/projects/{pid}/tasks/{tid}/rendered_*.mp4。
|
||||
"""
|
||||
|
||||
def test_task_downloads_from_file_url(self):
|
||||
import inspect
|
||||
|
||||
import video_processing.dedup as dedup_mod
|
||||
|
||||
source = inspect.getsource(dedup_mod.check_duplicate_task)
|
||||
# 下载 key 必须来自 video.file_url
|
||||
assert 'getattr(video, "file_url"' in source or "video.file_url" in source
|
||||
# 旧的硬编码 key 只能作为回退存在,不能是主路径
|
||||
assert "falling back to legacy key" in source
|
||||
# download_file 接收的是派生 key 而非硬编码 f-string
|
||||
assert "storage_service.download_file(download_key" in source
|
||||
assert '/generated/{generated_video_id}/{generated_video_id}.mp4"' not in source.replace(
|
||||
'download_key = f"projects/{video.project_id}/generated/{generated_video_id}/{generated_video_id}.mp4"',
|
||||
"",
|
||||
)
|
||||
|
||||
|
||||
# ── check_duplicate 排除自身(#1702 连带修复:recompute 自匹配) ─────
|
||||
|
||||
|
||||
class TestCheckDuplicateExcludesSelf:
|
||||
def test_exclude_video_id_skips_self_match(self):
|
||||
"""recompute 时当前视频已在候选列表:自匹配距离 0 分会让 duplicate_of
|
||||
指向自己。exclude_video_id 必须跳过自身,返回真实的其他匹配或 None。
|
||||
"""
|
||||
|
||||
ddp = VideoDeduplicator()
|
||||
h = _h(0)
|
||||
# 候选列表里同时放「自己」(完全相同)和一个异源视频
|
||||
self_video = _video("v-self", [h], duration=10.0)
|
||||
other_video = _video("v-other", [_h(40 + i) for i in range(3)], duration=10.0)
|
||||
fp = _fingerprint([h], 10.0)
|
||||
|
||||
session = MagicMock()
|
||||
session.query.return_value.filter.return_value.order_by.return_value.all.return_value = []
|
||||
|
||||
# 不传 exclude → 自匹配命中(错误行为复现)
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
MockRepo.return_value.list_by_project.return_value = [self_video, other_video]
|
||||
result = ddp.check_duplicate(fp, "proj1", session)
|
||||
assert result is not None and result["duplicate_of"] == "v-self"
|
||||
|
||||
# 传 exclude_video_id → 跳过自己,异源不匹配 → None
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
MockRepo.return_value.list_by_project.return_value = [self_video, other_video]
|
||||
result = ddp.check_duplicate(fp, "proj1", session, exclude_video_id="v-self")
|
||||
assert result is None
|
||||
|
||||
# 排除自己后,真实同源其他视频仍能检出
|
||||
real_dup = _video("v-real", [h], duration=10.0)
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
MockRepo.return_value.list_by_project.return_value = [self_video, real_dup]
|
||||
result = ddp.check_duplicate(fp, "proj1", session, exclude_video_id="v-self")
|
||||
assert result is not None and result["duplicate_of"] == "v-real"
|
||||
|
||||
|
||||
# ── 阈值 16 二次校准 + 时序抖动对齐(#1702 第二轮真实数据校准) ──────
|
||||
|
||||
|
||||
class TestThreshold16Calibration:
|
||||
"""二次校准:staging 15 个真实成片实测——同源降重对中位数距离 14、
|
||||
<=16 命中 8/11=0.73;异源 13 个候选每帧全局最近邻最小距离 18、<=16
|
||||
命中全 0。阈值 16 检出同源且异源零误报(>=2bit 安全裕度)。"""
|
||||
|
||||
def test_threshold_calibrated_to_16(self):
|
||||
assert PHASH_THRESHOLD == 16
|
||||
|
||||
@staticmethod
|
||||
def _variant(phash: str, d: int) -> str:
|
||||
"""在 phash 基础上翻转恰好 d 个低位 bit → 与原哈希汉明距离恰为 d。"""
|
||||
v = int(phash, 16)
|
||||
for b in range(d):
|
||||
v ^= 1 << b
|
||||
return f"{v:016x}"
|
||||
|
||||
def test_distance_18_unrelated_not_matched(self):
|
||||
"""距离 18(异源实测最小最近邻距离)不判匹配,距离 16 判匹配。"""
|
||||
ddp = VideoDeduplicator()
|
||||
# 多样化 base(相邻帧各不相同,避免黑屏过滤器)
|
||||
base = [_h(i + 4) for i in range(8)]
|
||||
near = [self._variant(h, 16) for h in base] # 同源降重:每帧距离恰 16
|
||||
far = [self._variant(h, 18) for h in base] # 异源边界:每帧距离恰 18
|
||||
|
||||
fp_near = _fingerprint(near, 8.0, chunks=[_chunk(h, i, i + 1) for i, h in enumerate(near)])
|
||||
fp_far = _fingerprint(far, 8.0, chunks=[_chunk(h, i, i + 1) for i, h in enumerate(far)])
|
||||
|
||||
r_near = _rate(ddp, fp_near, [_video("v-base", base, duration=8.0)])
|
||||
r_far = _rate(ddp, fp_far, [_video("v-base", base, duration=8.0)])
|
||||
|
||||
assert r_near["duplicate_rate"] > 0, "距离16的同源降重对必须检出"
|
||||
assert r_far["duplicate_rate"] == 0.0, "距离18的异源对不得误报"
|
||||
assert r_far["match_count"] == 0
|
||||
|
||||
def test_deduped_pair_frame_match_rate_over_threshold(self):
|
||||
"""真实场景比例:11 帧中 8 帧距离 <=16(0.73 >= 0.7),
|
||||
其余 3 帧异源距离(>=18)——frame_match_rate 必须过 0.7 门槛。"""
|
||||
ddp = VideoDeduplicator()
|
||||
base = [_h(i + 4) for i in range(11)]
|
||||
near = [self._variant(h, 14) for h in base[:8]] # 中位数 14 的同源降重帧
|
||||
# 异源帧用完全不同前缀(与 base 距离 >=30)
|
||||
far = [_h(52 + i) for i in range(3)]
|
||||
query = near + far
|
||||
|
||||
fp = _fingerprint(query, 11.0, chunks=[_chunk(h, i, i + 1) for i, h in enumerate(query)])
|
||||
r = _rate(ddp, fp, [_video("v-base", base, duration=11.0)])
|
||||
# frame_match_rate=8/11=0.73、时序片段覆盖 ~0.73
|
||||
# → duplicate_rate = 0.4*0.73+0.6*0.73 ≈ 73%(空直方图回退下 fusion=0.6965
|
||||
# 略低于 is_duplicate 的 0.70 判定阈值,故此处断言查重率而非 match_count;
|
||||
# 真实视频带颜色直方图时 fusion≈0.80,staging A-C 实测 is_duplicate=True)
|
||||
assert r["duplicate_rate"] >= 70.0
|
||||
|
||||
|
||||
class TestTemporalJitterAlignment:
|
||||
"""时序对齐允许目标索引正/反向 ±(neighbor_window+1) 抖动。
|
||||
|
||||
密集 1s 采样下相邻帧 pHash 接近,全局最近邻会在目标相邻帧间
|
||||
正负 1 跳变(场景切割/取帧错位/局部倒退);旧逻辑只允许正向
|
||||
delta,把同源连续匹配拆碎,min_consecutive 门槛够不上而漏检。
|
||||
"""
|
||||
|
||||
def test_backward_jitter_keeps_run_continuous(self):
|
||||
"""匹配目标索引序列 0,1,2,1,2,3(含一次 -1 倒退)应保持同一 run。"""
|
||||
from video_processing.dedup import find_duplicate_segments
|
||||
|
||||
# 构造 target 相邻帧 pHash 相同(距离0),query 帧的最近邻在
|
||||
# target[1]/target[2] 之间抖动;全部 <= 阈值
|
||||
t_hash = _h(0)
|
||||
other = _h(40)
|
||||
# target: 帧0-3 相同场景,帧4+ 异源
|
||||
t_chunks = [_chunk(t_hash, i, i + 1) for i in range(4)] + [_chunk(other, i, i + 1) for i in range(4, 8)]
|
||||
# query 6 帧同场景(最近邻会落到 target 0~3,索引可正可负)
|
||||
q_chunks = [_chunk(t_hash, i, i + 1) for i in range(6)]
|
||||
|
||||
segments = find_duplicate_segments(q_chunks, t_chunks)
|
||||
assert segments, "含 ±1 时序抖动的连续匹配必须形成片段"
|
||||
# 6 帧匹配 >= min_consecutive(min(5,max(2,6//2))=5),报为一个片段
|
||||
assert len(segments) == 1
|
||||
seg = segments[0]
|
||||
assert seg.query_end_ms - seg.query_start_ms >= 5000
|
||||
|
||||
def test_large_backward_jump_breaks_run(self):
|
||||
"""目标索引倒退 > neighbor_window+1(如从 5 跳回 0)不属于抖动,
|
||||
不桥接为同一片段;孤立短匹配 < min_consecutive 不报片段。"""
|
||||
from video_processing.dedup import find_duplicate_segments
|
||||
|
||||
# 异源段:9-bit 不重叠段(相邻段隔 3 bit),跨段距离 18~24 > 阈值 16
|
||||
def _bit_seg(start):
|
||||
bits = ["0"] * 64
|
||||
for b in range(9):
|
||||
bits[start + b] = "1"
|
||||
return f"{int(''.join(bits), 2):016x}"
|
||||
|
||||
t_hash = _bit_seg(0) # 复用场景:bit 0-8
|
||||
t_other = [_bit_seg(22 + 4 * i) for i in range(4)] # target 异源段
|
||||
q_other = [_bit_seg(40 + 4 * i) for i in range(3)] # query 异源段
|
||||
# target: 帧0 同场景;帧1-4 异源;帧5-6 同场景
|
||||
t_chunks = (
|
||||
[_chunk(t_hash, 0, 1)]
|
||||
+ [_chunk(t_other[i - 1], i, i + 1) for i in range(1, 5)]
|
||||
+ [_chunk(t_hash, i, i + 1) for i in range(5, 7)]
|
||||
)
|
||||
# query: 帧0 匹配 target[0];帧1-3 异源(与 target 任何帧距离 >16);帧4-5 匹配 target[5,6]
|
||||
q_chunks = (
|
||||
[_chunk(t_hash, 0, 1)]
|
||||
+ [_chunk(q_other[i - 1], i, i + 1) for i in range(1, 4)]
|
||||
+ [_chunk(t_hash, i, i + 1) for i in range(4, 6)]
|
||||
)
|
||||
segments = find_duplicate_segments(q_chunks, t_chunks)
|
||||
# 两段各 1、2 帧 < min_consecutive=5 → 不报片段(大跳跃不桥接)
|
||||
assert segments == []
|
||||
@@ -358,11 +358,11 @@ class TestVideoDeduplicatorCheckDuplicate:
|
||||
finally:
|
||||
self._restore_repo(mod, orig)
|
||||
|
||||
def test_highest_score_match_returned(self, deduplicator, mock_session):
|
||||
"""Issue #1702: 遍历所有候选取融合分最高者(旧逻辑首个过阈即返回)。"""
|
||||
# vid-1: 距离=1 bit(0x03 XOR 0x01 = 0x02 → 1 bit),通过阈值
|
||||
def test_first_match_returned(self, deduplicator, mock_session):
|
||||
"""返回第一个通过阈值的匹配(非最优匹配)。"""
|
||||
# vid-1: 距离=2 bits(0x03 XOR 0x01 = 0x02 → 1 bit),通过阈值
|
||||
vid1 = self._make_existing_video("vid-1", "md5_1", phashes=["0000000000000003"])
|
||||
# vid-2: 距离=0 bits(完全匹配),融合分更高
|
||||
# vid-2: 距离=0 bits(完全匹配)
|
||||
vid2 = self._make_existing_video("vid-2", "md5_2", phashes=["0000000000000001"])
|
||||
|
||||
mock_repo = MagicMock()
|
||||
@@ -380,8 +380,8 @@ class TestVideoDeduplicatorCheckDuplicate:
|
||||
try:
|
||||
result = deduplicator.check_duplicate(fingerprint, "proj-1", mock_session)
|
||||
assert result is not None
|
||||
# 两个候选都过阈,返回融合分最高的 vid-2(距离 0 < 1)
|
||||
assert result["duplicate_of"] == "vid-2"
|
||||
# 返回第一个通过阈值的匹配(vid-1 距离=1 < 10)
|
||||
assert result["duplicate_of"] == "vid-1"
|
||||
finally:
|
||||
self._restore_repo(mod, orig)
|
||||
|
||||
|
||||
@@ -185,14 +185,11 @@ class TestBhattacharyyaCoefficient:
|
||||
"""_bhattacharyya_coefficient Bhattacharyya 系数测试."""
|
||||
|
||||
def test_identical_histograms(self):
|
||||
"""完全相同的直方图系数为1.0(#1702:按 Σ 归一,概率分布语义)。"""
|
||||
hist = [0.5, 0.5, 0.0, 0.0] # Σ=1 的概率分布
|
||||
"""完全相同的直方图系数为1.0."""
|
||||
hist = [0.5, 0.5, 0.0, 0.3]
|
||||
bc = VideoDeduplicator._bhattacharyya_coefficient(hist, hist)
|
||||
assert bc == pytest.approx(1.0)
|
||||
# 非归一化输入也归一到 1.0(三通道拼接 Σ=3 的等价情形)
|
||||
hist3 = [0.5, 0.5, 0.0, 0.3]
|
||||
bc3 = VideoDeduplicator._bhattacharyya_coefficient(hist3, hist3)
|
||||
assert bc3 == pytest.approx(1.0)
|
||||
# Σ √(a[i]*a[i]) = Σ a[i] = 1.0 (normalized)
|
||||
assert bc == pytest.approx(sum(h for h in hist))
|
||||
|
||||
def test_zero_histograms(self):
|
||||
"""全零直方图系数为0."""
|
||||
@@ -205,10 +202,10 @@ class TestBhattacharyyaCoefficient:
|
||||
assert bc == pytest.approx(0.0)
|
||||
|
||||
def test_different_lengths(self):
|
||||
"""不同长度直方图取最小长度对齐,并按各自总量归一(#1702 概率分布语义)。"""
|
||||
# 对齐到前 2 维:coeff = 2,norm = √(Σa·Σb) = √(2·2) = 2 → 1.0
|
||||
"""不同长度直方图取最小长度对齐."""
|
||||
bc = VideoDeduplicator._bhattacharyya_coefficient([1.0, 1.0, 0.0, 0.0], [1.0, 1.0])
|
||||
assert bc == pytest.approx(1.0)
|
||||
# 对齐到前2维: √(1*1) + √(1*1) = 2.0
|
||||
assert bc == pytest.approx(2.0)
|
||||
|
||||
def test_known_value(self):
|
||||
"""已知值验证."""
|
||||
|
||||
+23
-26
@@ -103,7 +103,6 @@ from video_processing.dedup import ( # noqa: E402
|
||||
MIN_CONSECUTIVE_MATCHES,
|
||||
MIN_KEYFRAME_INTERVAL_SEC,
|
||||
MIN_KEYFRAMES,
|
||||
PHASH_THRESHOLD,
|
||||
PHASH_WEIGHT,
|
||||
SCENE_CHANGE_THRESHOLD,
|
||||
SEGMENT_MATCH_THRESHOLD,
|
||||
@@ -270,17 +269,22 @@ class TestFindDuplicateSegments:
|
||||
注意:使用不同的 hash 对,确保后半部分帧距离 > 阈值。
|
||||
"""
|
||||
same_hash = "aaaaaaaaaaaaaaaa"
|
||||
# 4 帧匹配,后面 6 帧用与匹配哈希距离 32 的不匹配哈希(> PHASH_THRESHOLD=16)
|
||||
nomatch_hash = "cccccccccccccccc" # hamming(aaaa, cccc)=32
|
||||
# 4 帧匹配,后面 6 帧各自不同(在 query 和 target 中使用不同 hash)
|
||||
chunks_a = [_make_chunk(i * 1000, (i + 1) * 1000, same_hash) for i in range(4)] + [
|
||||
_make_chunk(i * 1000, (i + 1) * 1000, nomatch_hash) for i in range(4, 10)
|
||||
_make_chunk(i * 1000, (i + 1) * 1000, "bbbbbbbbbbbbbbbb") for i in range(4, 10)
|
||||
]
|
||||
chunks_b = [_make_chunk(i * 1000, (i + 1) * 1000, same_hash) for i in range(4)] + [
|
||||
_make_chunk(i * 1000, (i + 1) * 1000, nomatch_hash) for i in range(4, 10)
|
||||
_make_chunk(i * 1000, (i + 1) * 1000, "cccccccccccccccc") for i in range(4, 10)
|
||||
]
|
||||
# hamming(aaaa..., cccc...) = 32 > PHASH_THRESHOLD(16),后半段不匹配;
|
||||
# 前 4 帧匹配 < min_consecutive=5,不形成片段
|
||||
|
||||
# hamming("bbbb...", "cccc...") should be > 8 (SEGMENT_MATCH_THRESHOLD)
|
||||
# b=1011, c=1100 → 4 bits differ per hex digit × 16 digits = 64 bits total? No...
|
||||
# Actually: hamming_distance("bbbbbbbbbbbbbbbb", "cccccccccccccccc")
|
||||
# b=0xb=1011, c=0xc=1100 → XOR=0111=0x7 → 3 bits per digit × 16 = 48
|
||||
# That's > 8 so won't match
|
||||
|
||||
segments = find_duplicate_segments(chunks_a, chunks_b)
|
||||
# 只有 4 帧匹配(< min_consecutive=5),所以不报告
|
||||
assert segments == []
|
||||
|
||||
def test_max_gap_behavior(self):
|
||||
@@ -289,12 +293,10 @@ class TestFindDuplicateSegments:
|
||||
关键:间隙帧必须在 query 和 target 中使用不同 hash,使其真正不匹配。
|
||||
"""
|
||||
match_hash = "aaaaaaaaaaaaaaaa"
|
||||
# 间隙/尾部哈希与 match_hash 及彼此之间汉明距离均 >64 (> PHASH_THRESHOLD=16),
|
||||
# 确保在 ±(neighbor_window+1) 时序抖动对齐窗口内也不会误匹配
|
||||
gap_hash_a = "ffffffffffffffff" # hamming(a,f)=128
|
||||
gap_hash_b = "9999999999999999" # hamming(a,9)=128, hamming(f,9)=128
|
||||
tail_hash_a = "7777777777777777" # hamming(a,7)=192
|
||||
tail_hash_b = "1111111111111111" # hamming(a,1)=192, hamming(7,1)=128
|
||||
gap_hash_a = "bbbbbbbbbbbbbbbb" # query 端
|
||||
gap_hash_b = "cccccccccccccccc" # target 端(与 query 端距离 > 8)
|
||||
tail_hash_a = "dddddddddddddddd"
|
||||
tail_hash_b = "eeeeeeeeeeeeeeee"
|
||||
|
||||
# 5 帧匹配, 1 帧间隙, 3 帧匹配, 5 帧不匹配
|
||||
hashes_a = [match_hash] * 5 + [gap_hash_a] + [match_hash] * 3 + [tail_hash_a] * 5
|
||||
@@ -316,10 +318,10 @@ class TestFindDuplicateSegments:
|
||||
def test_max_gap_exceeded(self):
|
||||
"""间隙超过 max_gap → 分成两段."""
|
||||
match_hash = "aaaaaaaaaaaaaaaa"
|
||||
gap_hash_a = "ffffffffffffffff" # hamming(a,f)=128
|
||||
gap_hash_b = "9999999999999999" # hamming(a,9)=128
|
||||
tail_hash_a = "7777777777777777" # hamming(a,7)=192
|
||||
tail_hash_b = "1111111111111111" # hamming(a,1)=192
|
||||
gap_hash_a = "bbbbbbbbbbbbbbbb"
|
||||
gap_hash_b = "cccccccccccccccc"
|
||||
tail_hash_a = "dddddddddddddddd"
|
||||
tail_hash_b = "eeeeeeeeeeeeeeee"
|
||||
|
||||
# 5 帧匹配, 3 帧间隙 (> max_gap=2), 5 帧匹配, 5 帧不匹配
|
||||
hashes_a = [match_hash] * 5 + [gap_hash_a] * 3 + [match_hash] * 5 + [tail_hash_a] * 5
|
||||
@@ -482,10 +484,8 @@ class TestBackwardCompatibility:
|
||||
chunks_b = [{"phash_binary": "aaaaaaaaaaaaaaaa", "start_time_ms": 0, "end_time_ms": 5000}]
|
||||
|
||||
segments = find_duplicate_segments(chunks_a, chunks_b)
|
||||
# Issue #1702: 自适应门槛 min(5, max(2, 1//2))=2,1 帧不成段;
|
||||
# N=1 的检出由 _evaluate_candidate 匹配帧回退兜底(见 test_dedup_1702)。
|
||||
# 这里只要求不崩溃。
|
||||
assert isinstance(segments, list)
|
||||
# 1 帧 < min_consecutive=5,不会报重复
|
||||
assert segments == []
|
||||
|
||||
|
||||
# ── TestConstants ───────────────────────────────────────────────
|
||||
@@ -495,11 +495,8 @@ class TestConstants:
|
||||
"""常量值验证 — 使用已在模块顶部导入的常量,避免重新 import."""
|
||||
|
||||
def test_segment_match_threshold(self):
|
||||
# Issue #1702 二次校准:阈值经 staging 真实数据两轮回归——
|
||||
# 第一轮同源 4/11、异源 min=24 定 12;第二轮扩样本(15 个真实成片)
|
||||
# 同源降重对中位数距离 14、<=16 命中 8/11=0.73,异源 13 个候选
|
||||
# <=16 命中全 0、最近邻最小距离 18 → 校准为 16。
|
||||
assert SEGMENT_MATCH_THRESHOLD == PHASH_THRESHOLD == 16
|
||||
# 从已导入的 find_duplicate_segments 默认参数间接验证
|
||||
assert SEGMENT_MATCH_THRESHOLD == 8
|
||||
|
||||
def test_min_consecutive_matches(self):
|
||||
assert MIN_CONSECUTIVE_MATCHES == 5
|
||||
|
||||
@@ -132,14 +132,9 @@ class TestCheckDuplicateScopeUser:
|
||||
|
||||
|
||||
class TestDurationPrefilter:
|
||||
"""Issue #1702: scope=user 跨项目查重不做时长预过滤。
|
||||
"""test_duration_prefilter:时长 ±15% 过滤."""
|
||||
|
||||
局部片段复用的两个视频时长必然不同(证据视频 20s vs 11s,差 42%),
|
||||
旧的 ±15% 窗口会让同源视频互相不可见 → is_duplicate 恒 False。
|
||||
全量遍历同用户视频,异源视频由 fusion/temporal_coverage 阈值天然过滤。
|
||||
"""
|
||||
|
||||
def test_user_scope_no_duration_filter(self):
|
||||
def test_duration_prefilter_passes_correct_range(self):
|
||||
from video_processing.dedup import VideoDeduplicator
|
||||
|
||||
deduplicator = VideoDeduplicator()
|
||||
@@ -158,13 +153,12 @@ class TestDurationPrefilter:
|
||||
duration_sec=30.0,
|
||||
)
|
||||
|
||||
# scope=user 全量遍历:位置参数只传 user_id,kwargs 不含时长过滤
|
||||
# Should pass duration_min=25.5, duration_max=34.5 (30 ± 15%)
|
||||
call_args = mock_repo.list_by_user.call_args
|
||||
assert call_args[0] == ("user1",)
|
||||
assert "duration_min" not in call_args[1]
|
||||
assert "duration_max" not in call_args[1]
|
||||
assert call_args[1]["duration_min"] == pytest.approx(25.5, abs=0.1)
|
||||
assert call_args[1]["duration_max"] == pytest.approx(34.5, abs=0.1)
|
||||
|
||||
def test_user_scope_no_duration_filter_when_zero(self):
|
||||
def test_no_duration_prefilter_when_zero(self):
|
||||
from video_processing.dedup import VideoDeduplicator
|
||||
|
||||
deduplicator = VideoDeduplicator()
|
||||
@@ -184,34 +178,8 @@ class TestDurationPrefilter:
|
||||
)
|
||||
|
||||
call_args = mock_repo.list_by_user.call_args
|
||||
assert "duration_min" not in call_args[1]
|
||||
assert "duration_max" not in call_args[1]
|
||||
|
||||
def test_project_scope_also_no_duration_filter(self):
|
||||
"""scope=project 走 list_by_project,本来就不做时长过滤。"""
|
||||
from video_processing.dedup import VideoDeduplicator
|
||||
|
||||
deduplicator = VideoDeduplicator()
|
||||
fingerprint = _make_fingerprint(duration_ms=30000)
|
||||
session = MagicMock()
|
||||
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
mock_repo = MockRepo.return_value
|
||||
mock_repo.list_by_project.return_value = []
|
||||
deduplicator.check_duplicate(
|
||||
fingerprint,
|
||||
"proj1",
|
||||
session,
|
||||
scope="project",
|
||||
user_id="user1",
|
||||
duration_sec=30.0,
|
||||
)
|
||||
|
||||
mock_repo.list_by_project.assert_called_once()
|
||||
call_args = mock_repo.list_by_project.call_args
|
||||
assert call_args[0] == ("proj1",)
|
||||
assert "duration_min" not in call_args[1]
|
||||
assert "duration_max" not in call_args[1]
|
||||
assert call_args[1]["duration_min"] == 0
|
||||
assert call_args[1]["duration_max"] == 0
|
||||
|
||||
|
||||
class TestComputeDuplicateRateFormula:
|
||||
@@ -221,25 +189,14 @@ class TestComputeDuplicateRateFormula:
|
||||
from video_processing.dedup import VideoDeduplicator
|
||||
|
||||
deduplicator = VideoDeduplicator()
|
||||
# 10 frames with varied phashes (2 unique) → not bad fingerprint
|
||||
# All close in hamming distance to existing → frame_match_rate = 1.0
|
||||
phashes = ["aa00aa00aa00aa00", "ab00ab00ab00ab00"] * 5
|
||||
# 10 frames, all identical to existing → frame_match_rate = 1.0
|
||||
phashes = ["aa00aa00aa00aa00"] * 10
|
||||
fingerprint = _make_fingerprint(md5="new", phashes=phashes, duration_ms=20000)
|
||||
session = MagicMock()
|
||||
|
||||
# 5 unique phashes to pass _is_bad_fingerprint check (PR #1688)
|
||||
existing = _make_video(
|
||||
"vid2",
|
||||
{
|
||||
"md5": "other",
|
||||
"keyframe_phashes": [
|
||||
"aa00aa00aa00aa00",
|
||||
"ab00ab00ab00ab00",
|
||||
"ac00ac00ac00ac00",
|
||||
"aa10aa10aa10aa10",
|
||||
"ba00ba00ba00ba00",
|
||||
],
|
||||
},
|
||||
{"md5": "other", "keyframe_phashes": ["aa00aa00aa00aa00"] * 5},
|
||||
)
|
||||
|
||||
with patch("video_processing.dedup.SQLAlchemyGeneratedVideoRepository") as MockRepo:
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user