Skip to content

PRINTFILM: AI short-video marketing and AI short-drama creation platform

fix(kepu): charge storyboard production by stage instead of full-film freeze

yi690518478a81
diff --git a/backend/app/api/projects.py b/backend/app/api/projects.py
index e7e5472..80b221e 100644
--- a/backend/app/api/projects.py
+++ b/backend/app/api/projects.py
@@ -612,9 +612,11 @@ async def generate_project(
"""Start or resume the pipeline.
- First run / restart: script stage only, then pauses at SCRIPT_READY for review.
- - Continue from SCRIPT_READY+: skip script, run assets → videos → compose.
+ - Continue: one billing stage per click — assets → videos → compose(各自预扣)。
- restart=true: wipe shots/media and regenerate storyboard from scratch.
"""
+ from app.services.kepu_stages import resolve_kepu_billing_phase
+
project = await _get_owned_project(db, project_id, user)
if project.status in {
ProjectStatus.SCRIPTING,
@@ -637,26 +639,20 @@ async def generate_project(
project.error_msg = None
project.final_video_url = None
shots = list(project.shots or [])
- phase = "script" if (restart or not shots) else "produce"
+ phase = "script" if (restart or not shots) else resolve_kepu_billing_phase(project)
+ if phase == "compose":
+ # 成片走专用 compose 任务,避免 project_pipeline 重复预扣整片视频
+ raise HTTPException(status_code=409, detail="素材已齐,请点击合成成片")
if restart or not shots:
# First run / restart — actually splitting storyboard
project.status = ProjectStatus.SCRIPTING
project.progress = 1
- else:
- # Continue: label the real next stage so UI never shows「拆分镜中」
- image_text = (project.pipeline_mode or "full") == "image_text"
- has_images = all(bool(s.image_url or s.image_ark_url) for s in shots)
- has_audio = all(bool(s.audio_url) for s in shots)
- has_videos = all(bool(s.video_url) for s in shots)
- if not has_images or not has_audio:
- project.status = ProjectStatus.IMAGING
- project.progress = max(project.progress or 0, 18)
- elif not image_text and not has_videos:
- project.status = ProjectStatus.VIDEOING
- project.progress = max(project.progress or 0, 55)
- else:
- project.status = ProjectStatus.COMPOSING
- project.progress = max(project.progress or 0, 88)
+ elif phase == "assets":
+ project.status = ProjectStatus.IMAGING
+ project.progress = max(project.progress or 0, 18)
+ elif phase == "videos":
+ project.status = ProjectStatus.VIDEOING
+ project.progress = max(project.progress or 0, 55)
try:
await create_task(
db,
diff --git a/backend/app/services/billing/__init__.py b/backend/app/services/billing/__init__.py
index c9f14d5..619282e 100644
--- a/backend/app/services/billing/__init__.py
+++ b/backend/app/services/billing/__init__.py
@@ -11,6 +11,7 @@ from app.services.billing.ephemeral import (
settle_deferred_video_poll,
)
from app.services.billing.estimates import estimate_phase_fen, estimate_task_fen
+from app.services.kepu_stages import resolve_kepu_billing_phase
from app.services.billing.pricing import (
ORDER_EXPIRE_SECONDS,
SKUS,
@@ -50,6 +51,7 @@ __all__ = [
"credit_topup",
"estimate_phase_fen",
"estimate_task_fen",
+ "resolve_kepu_billing_phase",
"ensure_balance_for_task",
"ensure_balance_for_task_batch",
"freeze_for_task",
diff --git a/backend/app/services/billing/estimates.py b/backend/app/services/billing/estimates.py
index acf1486..caf58ad 100644
--- a/backend/app/services/billing/estimates.py
+++ b/backend/app/services/billing/estimates.py
@@ -12,29 +12,80 @@ from app.config import Settings, get_settings
from app.models import Project
from app.models_tasks import TaskRun
from app.services.billing.pricing import charge_fen_for_tokens
+from app.services.kepu_stages import (
+ normalize_kepu_pipeline_phase,
+ project_audio_ready,
+ resolve_kepu_billing_phase,
+ shot_image_ready,
+ shot_video_ready,
+)
+
+__all__ = [
+ "estimate_phase_fen",
+ "estimate_task_fen",
+ "resolve_kepu_billing_phase",
+]
+
+
+def _estimate_assets_fen(project: Project, settings: Settings) -> int:
+ """只估尚未完成的出图 + 整片配音(不含镜头视频)。"""
+ buf = float(settings.billing_estimate_buffer or 1.2)
+ shots = list(project.shots or [])
+ need_img = sum(1 for s in shots if not shot_image_ready(s))
+ # 整片 TTS 一次估算;旁白已就绪(整片文件或全部镜头文件)则不再预扣
+ need_tts = 0 if project_audio_ready(project) else 1
+ if need_img <= 0 and need_tts <= 0:
+ return 1
+ total = 0
+ for _ in range(max(need_img, 0)):
+ _, c_img = charge_fen_for_tokens(settings.billing_est_seedream_tokens, "seedream", settings=settings)
+ total += c_img
+ if need_tts > 0:
+ n = max(len(shots), 1)
+ _, c_tts = charge_fen_for_tokens(
+ settings.billing_est_tts_tokens * n,
+ "tts",
+ settings=settings,
+ )
+ total += c_tts
+ return max(math.ceil(total * buf), 1)
+
+
+def _estimate_videos_fen(project: Project, settings: Settings) -> int:
+ """只估尚未出片的镜头视频。"""
+ buf = float(settings.billing_estimate_buffer or 1.2)
+ shots = [s for s in list(project.shots or []) if not shot_video_ready(s)]
+ if not shots:
+ return 1
+ total = 0
+ for sh in shots:
+ secs = max(float(sh.duration or 4), 2.0)
+ tok = int(secs * settings.billing_est_seedance_tokens_per_sec)
+ _, c_vid = charge_fen_for_tokens(tok, "seedance2:video0", settings=settings)
+ total += c_vid
+ return max(math.ceil(total * buf), 1)
def estimate_phase_fen(project: Project, phase: str, settings: Settings | None = None) -> int:
- """科普 pipeline 阶段估算:script | produce。"""
+ """科普 pipeline 阶段估算:script | assets | videos | compose | produce(兼容→下一段)。"""
s = settings or get_settings()
buf = float(s.billing_estimate_buffer or 1.2)
- if phase == "script":
+ raw = normalize_kepu_pipeline_phase(phase, project)
+
+ if raw == "script":
_, charge = charge_fen_for_tokens(s.billing_est_llm_tokens, "llm_chat", settings=s)
return max(1, math.ceil(charge * buf))
- shots = list(project.shots or [])
- n = max(len(shots), 1)
- total = 0
- for _ in range(n):
- _, c_img = charge_fen_for_tokens(s.billing_est_seedream_tokens, "seedream", settings=s)
- _, c_tts = charge_fen_for_tokens(s.billing_est_tts_tokens, "tts", settings=s)
- total += c_img + c_tts
- if (project.pipeline_mode or "full") != "image_text":
- secs = sum(max(float(sh.duration or 4), 2.0) for sh in shots) or (n * 5.0)
- tok = int(secs * s.billing_est_seedance_tokens_per_sec)
- _, c_vid = charge_fen_for_tokens(tok, "seedance2:video0", settings=s)
- total += c_vid
- return max(math.ceil(total * buf), 1)
+ if raw == "assets":
+ return _estimate_assets_fen(project, s)
+
+ if raw == "videos":
+ return _estimate_videos_fen(project, s)
+
+ if raw == "compose":
+ return 1
+
+ return _estimate_assets_fen(project, s)
async def estimate_task_fen(db: AsyncSession, task: TaskRun, settings: Settings | None = None) -> int:
@@ -117,7 +168,6 @@ async def estimate_task_fen(db: AsyncSession, task: TaskRun, settings: Settings
dur = 8.0
tok = int(max(dur, 2.0) * s.billing_est_seedance_tokens_per_sec)
_, c = charge_fen_for_tokens(tok, "seedance2:video0", settings=s)
- # fragment_video 准备阶段可能有 seedream/LLM,预留半张图额度压小额 overage
if task_type == "fragment_video":
_, c_img = charge_fen_for_tokens(s.billing_est_seedream_tokens, "seedream", settings=s)
c += max(1, c_img // 2)
diff --git a/backend/app/services/kepu_stages.py b/backend/app/services/kepu_stages.py
new file mode 100644
index 0000000..f993452
--- /dev/null
+++ b/backend/app/services/kepu_stages.py
@@ -0,0 +1,77 @@
+# -*- coding: utf-8 -*-
+"""科普流水线阶段判定:计费预扣与 pipeline 共用同一套就绪规则。"""
+from __future__ import annotations
+
+from typing import Any
+
+from app.services import storage
+from app.services.ffmpeg_compose import is_near_silent_audio
+
+
+def _is_image_text(project: Any) -> bool:
+ return (getattr(project, "pipeline_mode", None) or "full") == "image_text"
+
+
+def shot_image_ready(shot: Any) -> bool:
+ """分镜是否已有可用分镜图。"""
+ return bool(getattr(shot, "image_url", None) or getattr(shot, "image_ark_url", None))
+
+
+def shot_video_ready(shot: Any) -> bool:
+ """分镜是否已有镜头视频。"""
+ return bool(getattr(shot, "video_url", None))
+
+
+def shot_audio_file_ok(shot: Any) -> bool:
+ """单镜旁白文件存在且非近静音(与 pipeline _resume_plan 一致)。"""
+ audio_url = getattr(shot, "audio_url", None)
+ if not audio_url:
+ return False
+ path = storage.local_path_from_url(str(audio_url))
+ if not path or not path.exists():
+ return False
+ return not is_near_silent_audio(path)
+
+
+def continuous_narration_ok(project_id: int) -> bool:
+ """整片连贯旁白文件是否可用(与 pipeline _continuous_audio_ok 一致)。"""
+ path = storage.project_dir(int(project_id)) / "full_narration.mp3"
+ return path.exists() and path.stat().st_size > 2000 and not is_near_silent_audio(path)
+
+
+def project_audio_ready(project: Any) -> bool:
+ """项目旁白是否就绪:整片文件 OK,或全部镜头旁白文件 OK。"""
+ project_id = getattr(project, "id", None)
+ if project_id is not None and continuous_narration_ok(int(project_id)):
+ return True
+ shots = list(getattr(project, "shots", None) or [])
+ return bool(shots) and all(shot_audio_file_ok(s) for s in shots)
+
+
+def resolve_kepu_billing_phase(project: Any) -> str:
+ """按分镜进度解析下一段:script | assets | videos | compose。"""
+ shots = list(getattr(project, "shots", None) or [])
+ if not shots:
+ return "script"
+ image_text = _is_image_text(project)
+ need_images = any(not shot_image_ready(s) for s in shots)
+ need_audio = not project_audio_ready(project)
+ if need_images or need_audio:
+ return "assets"
+ if not image_text and any(not shot_video_ready(s) for s in shots):
+ return "videos"
+ return "compose"
+
+
+def normalize_kepu_pipeline_phase(phase: str | None, project: Any | None = None) -> str:
+ """规范化任务 phase;produce 兼容映射到当前应执行的下一段。"""
+ raw = (phase or "").strip().lower()
+ if raw == "produce":
+ if project is None:
+ return "assets"
+ return resolve_kepu_billing_phase(project)
+ if raw in {"script", "assets", "videos", "compose"}:
+ return raw
+ if project is not None:
+ return resolve_kepu_billing_phase(project)
+ return "assets"
diff --git a/backend/app/services/pipeline.py b/backend/app/services/pipeline.py
index 9fa4041..537f763 100644
--- a/backend/app/services/pipeline.py
+++ b/backend/app/services/pipeline.py
@@ -347,8 +347,9 @@ def join_shot_narrations(narrations: list[str]) -> str:
def _continuous_audio_ok(project_id: int) -> bool:
- path = _full_narration_path(project_id)
- return path.exists() and path.stat().st_size > 2000 and not is_near_silent_audio(path)
+ from app.services.kepu_stages import continuous_narration_ok
+
+ return continuous_narration_ok(project_id)
async def _synthesize_continuous_audio(
@@ -414,6 +415,8 @@ async def _resume_plan(project_id: int) -> tuple[bool, bool, bool, bool]:
When shots already have images (+ audio / videos as needed), resume from
the next unfinished stage instead of wiping the storyboard.
"""
+ from app.services.kepu_stages import project_audio_ready, shot_image_ready, shot_video_ready
+
async with AsyncSessionLocal() as db:
result = await db.execute(
select(Project)
@@ -426,24 +429,10 @@ async def _resume_plan(project_id: int) -> tuple[bool, bool, bool, bool]:
image_text = _is_image_text(project)
native_audio = _use_native_video_audio(project)
shots = list(project.shots)
- has_images = all(bool(s.image_url or s.image_ark_url) for s in shots)
-
- def _audio_ok(shot: Shot) -> bool:
- if not shot.audio_url:
- return False
- path = storage.local_path_from_url(shot.audio_url)
- if not path or not path.exists():
- return False
- return not is_near_silent_audio(path)
-
- # 科普改回外部 TTS,成片前必须有旁白音轨
- has_audio = (
- True
- if native_audio
- else (_continuous_audio_ok(project_id) or all(_audio_ok(s) for s in shots))
- )
- has_videos = all(bool(s.video_url) for s in shots)
- # Keep existing storyboard whenever shots already exist (user may edit before continue)
+ has_images = all(shot_image_ready(s) for s in shots)
+ # 与计费 resolve_kepu_billing_phase 共用旁白就绪规则
+ has_audio = True if native_audio else project_audio_ready(project)
+ has_videos = all(shot_video_ready(s) for s in shots)
skip_script = len(shots) > 0
skip_assets = has_images and has_audio
skip_videos = image_text or has_videos
@@ -451,15 +440,33 @@ async def _resume_plan(project_id: int) -> tuple[bool, bool, bool, bool]:
@storage.without_intermediate_oss
-async def run_pipeline(project_id: int) -> None:
+async def run_pipeline(project_id: int, *, phase: str | None = None) -> None:
+ """跑科普流水线;phase 指定时只执行该计费阶段,避免冻 A 跑 B。"""
+ from app.services.kepu_stages import normalize_kepu_pipeline_phase
+
try:
await _ensure_not_cancelled(project_id)
image_text, skip_script, skip_assets, skip_videos = await _resume_plan(project_id)
+ requested: str | None = None
+ if phase is not None:
+ async with AsyncSessionLocal() as db:
+ project = (
+ await db.execute(
+ select(Project)
+ .where(Project.id == project_id)
+ .options(selectinload(Project.shots))
+ )
+ ).scalar_one_or_none()
+ requested = normalize_kepu_pipeline_phase(phase, project)
+
if not skip_script:
+ if requested and requested not in {"script", "produce"}:
+ raise RuntimeError(
+ f"分镜尚未就绪,无法执行阶段 {requested};请先生成分镜脚本"
+ )
await _script_stage(project_id)
await _ensure_not_cancelled(project_id)
- # Checkpoint: stop after storyboard so user can review/edit before assets
await publish_progress(
project_id,
{
@@ -472,8 +479,9 @@ async def run_pipeline(project_id: int) -> None:
return
logger.info(
- "pipeline resume project=%s skip_script=%s skip_assets=%s skip_videos=%s",
+ "pipeline resume project=%s phase=%s skip_script=%s skip_assets=%s skip_videos=%s",
project_id,
+ requested or "(auto)",
skip_script,
skip_assets,
skip_videos,
@@ -488,42 +496,116 @@ async def run_pipeline(project_id: int) -> None:
},
)
- # 分镜图 + 配音并行;完整模式再并行图生视频
+ # 显式 phase:只跑对应阶段并暂停,保证与预扣一致
+ if requested == "assets":
+ if not skip_assets:
+ await _parallel_image_and_audio(project_id)
+ await _ensure_not_cancelled(project_id)
+ await publish_progress(
+ project_id,
+ {
+ "event": "paused",
+ "stage": "ASSETS_READY",
+ "percent": 70 if image_text else 50,
+ "message": (
+ "分镜图与配音已完成,请确认后继续合成"
+ if image_text
+ else "分镜图与配音已完成,请确认后继续生成镜头视频"
+ ),
+ },
+ )
+ return
+
+ if requested == "videos":
+ if not skip_assets:
+ # 预扣按视频,但旁白/出图未齐:先补 assets 并停下,避免冻视频款去跑视频
+ await _parallel_image_and_audio(project_id)
+ await _ensure_not_cancelled(project_id)
+ await publish_progress(
+ project_id,
+ {
+ "event": "paused",
+ "stage": "ASSETS_READY",
+ "percent": 50,
+ "message": "分镜图与配音已补齐,请再次点击继续生成镜头视频",
+ },
+ )
+ return
+ if image_text or skip_videos:
+ await publish_progress(
+ project_id,
+ {
+ "event": "paused",
+ "stage": "VIDEO_READY",
+ "percent": 88,
+ "message": "镜头视频已就绪,请确认后合成成片",
+ },
+ )
+ return
+ await _parallel_videos(project_id)
+ await _ensure_not_cancelled(project_id)
+ await publish_progress(
+ project_id,
+ {
+ "event": "paused",
+ "stage": "VIDEO_READY",
+ "percent": 88,
+ "message": "镜头视频已完成,请确认后合成成片",
+ },
+ )
+ return
+
+ if requested == "compose":
+ await _compose_stage(project_id)
+ if is_cancelled(project_id):
+ raise PipelineCancelled(f"project {project_id} cancelled")
+ async with AsyncSessionLocal() as db:
+ project = await db.get(Project, project_id)
+ if project:
+ if project.status == ProjectStatus.CANCELLED:
+ raise PipelineCancelled(f"project {project_id} cancelled")
+ project.status = ProjectStatus.DONE
+ project.progress = 100
+ project.error_msg = None
+ await db.commit()
+ await publish_progress(
+ project_id,
+ {"event": "done", "percent": 100, "video_url": await _final_url(project_id)},
+ )
+ return
+
+ # 无 phase(旧任务/兼容):每段只跑一步后暂停
if not skip_assets:
await _parallel_image_and_audio(project_id)
await _ensure_not_cancelled(project_id)
- else:
- async with AsyncSessionLocal() as db:
- result = await db.execute(
- select(Project)
- .where(Project.id == project_id)
- .options(selectinload(Project.shots))
- )
- project = result.scalar_one()
- if project.shots:
- project.cover_url = sorted(project.shots, key=lambda s: s.shot_no)[0].image_url
- project.status = ProjectStatus.IMAGE_READY
- project.progress = 70 if image_text else 50
- await db.commit()
await publish_progress(
project_id,
{
- "event": "progress",
+ "event": "paused",
"stage": "ASSETS_READY",
"percent": 70 if image_text else 50,
"message": (
- "沿用已有分镜图与配音,开始合成"
+ "分镜图与配音已完成,请确认后继续合成"
if image_text
- else (
- "沿用已有分镜图与配音,继续生成 AI 视频"
- )
+ else "分镜图与配音已完成,请确认后继续生成镜头视频"
),
},
)
+ return
if not image_text and not skip_videos:
await _parallel_videos(project_id)
await _ensure_not_cancelled(project_id)
+ await publish_progress(
+ project_id,
+ {
+ "event": "paused",
+ "stage": "VIDEO_READY",
+ "percent": 88,
+ "message": "镜头视频已完成,请确认后合成成片",
+ },
+ )
+ return
await _compose_stage(project_id)
diff --git a/backend/app/services/tasks/handlers.py b/backend/app/services/tasks/handlers.py
index f23cd3b..b8ddaa6 100644
--- a/backend/app/services/tasks/handlers.py
+++ b/backend/app/services/tasks/handlers.py
@@ -122,12 +122,17 @@ async def _run_drama_seed_assets(task: TaskRun) -> dict[str, Any] | None:
)
-# 执行科普全流程任务。
+# 执行科普分阶段流水线(严格按 payload.phase 预扣对应阶段)。
async def _run_kepu_project_pipeline(task: TaskRun) -> dict[str, Any] | None:
from app.services.pipeline import run_pipeline
- await run_pipeline(_require_int(task.project_id, "project_id"))
- return {"ok": True, "project_id": task.project_id}
+ payload = task.payload if isinstance(task.payload, dict) else {}
+ phase = payload.get("phase")
+ await run_pipeline(
+ _require_int(task.project_id, "project_id"),
+ phase=str(phase) if phase is not None else None,
+ )
+ return {"ok": True, "project_id": task.project_id, "phase": phase}
# 执行科普单镜生图任务。
diff --git a/backend/tests/test_kepu_phase_billing.py b/backend/tests/test_kepu_phase_billing.py
new file mode 100644
index 0000000..25af424
--- /dev/null
+++ b/backend/tests/test_kepu_phase_billing.py
@@ -0,0 +1,122 @@
+# -*- coding: utf-8 -*-
+"""科普分阶段预扣:确认分镜时不应一次锁全片视频费用。"""
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+from app.config import get_settings
+from app.services.billing.estimates import estimate_phase_fen
+from app.services.kepu_stages import (
+ normalize_kepu_pipeline_phase,
+ resolve_kepu_billing_phase,
+)
+
+
+def _shot(**kwargs):
+ defaults = {
+ "image_url": None,
+ "image_ark_url": None,
+ "audio_url": None,
+ "video_url": None,
+ "duration": 10.0,
+ }
+ defaults.update(kwargs)
+ return SimpleNamespace(**defaults)
+
+
+def _project(shots, pipeline_mode="full", project_id=1):
+ return SimpleNamespace(id=project_id, shots=shots, pipeline_mode=pipeline_mode)
+
+
+def test_resolve_phase_script_when_no_shots() -> None:
+ assert resolve_kepu_billing_phase(_project([])) == "script"
+
+
+def test_resolve_phase_assets_after_storyboard() -> None:
+ shots = [_shot() for _ in range(7)]
+ assert resolve_kepu_billing_phase(_project(shots)) == "assets"
+
+
+def test_resolve_phase_videos_after_assets(monkeypatch) -> None:
+ monkeypatch.setattr(
+ "app.services.kepu_stages.project_audio_ready",
+ lambda _project: True,
+ )
+ shots = [_shot(image_url="/i.png", audio_url="/a.wav") for _ in range(7)]
+ assert resolve_kepu_billing_phase(_project(shots)) == "videos"
+
+
+def test_resolve_phase_videos_when_continuous_audio_ok(monkeypatch) -> None:
+ """整片旁白文件就绪但 shot.audio_url 为空时,应与 pipeline 一样进入 videos。"""
+ monkeypatch.setattr(
+ "app.services.kepu_stages.continuous_narration_ok",
+ lambda _pid: True,
+ )
+ shots = [_shot(image_url="/i.png") for _ in range(3)]
+ assert resolve_kepu_billing_phase(_project(shots)) == "videos"
+
+
+def test_resolve_phase_assets_when_audio_url_but_file_bad(monkeypatch) -> None:
+ """仅有 audio_url 字符串但文件不可用时,仍应留在 assets,避免误冻视频款。"""
+ monkeypatch.setattr(
+ "app.services.kepu_stages.continuous_narration_ok",
+ lambda _pid: False,
+ )
+ monkeypatch.setattr(
+ "app.services.kepu_stages.shot_audio_file_ok",
+ lambda _shot: False,
+ )
+ shots = [_shot(image_url="/i.png", audio_url="/missing.wav") for _ in range(2)]
+ assert resolve_kepu_billing_phase(_project(shots)) == "assets"
+
+
+def test_resolve_phase_compose_when_full_ready(monkeypatch) -> None:
+ monkeypatch.setattr(
+ "app.services.kepu_stages.project_audio_ready",
+ lambda _project: True,
+ )
+ shots = [
+ _shot(image_url="/i.png", audio_url="/a.wav", video_url="/v.mp4") for _ in range(3)
+ ]
+ assert resolve_kepu_billing_phase(_project(shots)) == "compose"
+
+
+def test_produce_estimate_is_assets_not_full_video() -> None:
+ """确认分镜时 phase=produce 应只估出图段,远小于含视频的全片预扣。"""
+ settings = get_settings()
+ shots = [_shot(duration=10.0) for _ in range(7)]
+ project = _project(shots)
+ assets = estimate_phase_fen(project, "assets", settings=settings)
+ videos = estimate_phase_fen(project, "videos", settings=settings)
+ produce = estimate_phase_fen(project, "produce", settings=settings)
+ legacy_full = assets + videos
+ assert produce == assets
+ assert produce < legacy_full
+ assert videos > assets
+
+
+def test_videos_estimate_only_remaining_shots() -> None:
+ settings = get_settings()
+ shots = [
+ _shot(image_url="/i.png", audio_url="/a.wav", video_url="/done.mp4", duration=10),
+ _shot(image_url="/i.png", audio_url="/a.wav", duration=10),
+ _shot(image_url="/i.png", audio_url="/a.wav", duration=10),
+ ]
+ one = estimate_phase_fen(
+ _project([_shot(image_url="/i.png", audio_url="/a.wav", duration=10)]),
+ "videos",
+ settings=settings,
+ )
+ two = estimate_phase_fen(_project(shots), "videos", settings=settings)
+ assert two == one * 2
+
+
+def test_normalize_produce_maps_to_current_phase(monkeypatch) -> None:
+ monkeypatch.setattr(
+ "app.services.kepu_stages.project_audio_ready",
+ lambda _project: True,
+ )
+ shots = [_shot(image_url="/i.png", audio_url="/a.wav") for _ in range(2)]
+ project = _project(shots)
+ assert normalize_kepu_pipeline_phase("produce", project) == "videos"
+ assert normalize_kepu_pipeline_phase("assets", project) == "assets"
diff --git a/docs/BILLING.md b/docs/BILLING.md
index e8b2b2e..841c36d 100644
--- a/docs/BILLING.md
+++ b/docs/BILLING.md
@@ -100,7 +100,7 @@ sequenceDiagram
**不计 LLM 的场景**:分镜规则回退(`rules_fallback`)、分集大纲已就绪跳过大纲 LLM、Skill 优化未勾选任何 Skill。
-科普两阶段:`project_pipeline` 的 `script` 与 `produce` 各对应一次 TaskRun,各自独立预扣与结算。
+科普分阶段:`project_pipeline` 的 `script` / `assets` / `videos` 各对应一次 TaskRun,各自独立预扣与结算(旧 payload `produce` 兼容映射为当前下一段)。成片走 `project_compose_only`(几乎不预扣)。阶段判定与 pipeline 共用 `kepu_stages`(含整片旁白文件就绪)。
`awaiting_poll` 分镜视频:executor 提前返回时不结算;轮询完成标 `succeeded` 时 `settle_task`。
@@ -150,7 +150,7 @@ EPAY_RETURN_URL=https://your-site.example.com/pricing?paid=1
## 验收清单
-1. 科普一次 generate:任务开始前余额减少(冻结),任务结束后按实际用量结算,多余冻结退回。
+1. 科普:确认分镜只冻出图+配音;继续生成视频另冻视频段;合成走 compose。各阶段任务结束按实际用量结算。
2. 漫剧分镜视频/资产生成:有 `task_run_id` 的用量行,任务结束扣费。
3. 漫剧聊天 / 选题扩写 / Skill 优化 / 音色描述 / 开放 API / 工作室工具:响应含 `task_id`(轻量 TaskRun),余额变化正确。
4. 管理端可按 `task_run_id` 或 `billing_key=llm_chat` 查到每条 LLM/图/视频/TTS 费用。
diff --git a/docs/releases/2026-09-07.md b/docs/releases/2026-09-07.md
index 962948c..230ed5e 100644
--- a/docs/releases/2026-09-07.md
+++ b/docs/releases/2026-09-07.md
@@ -15,3 +15,20 @@
- **备注 / 回滚**:回滚下列文件后重启 API,并重发前端 dist:
`episodes.py` / `ark.py` / `generation.py` / `jobs.py` / `executor.py` / `service.py` / `dramaGenError.ts`
提交:`2c09e4f`
+
+---
+
+## 2026-09-07 · 科普分阶段计费(确认分镜不再一次预扣全片)
+
+- **操作者**:Agent
+- **方式**:全量发布(`deploy_kepu_8136.py`)
+- **范围**:api / web
+- **变更摘要**:
+ - 科普 `project_pipeline` 按 `script` / `assets` / `videos` 分段预扣与执行;确认分镜只冻出图+配音
+ - pipeline 严格按 `payload.phase` 执行,避免「冻 A 跑 B」;成片走 `project_compose_only`
+ - 计费与 pipeline 共用 `kepu_stages` 旁白就绪判定(整片旁白文件或镜头文件)
+- **验证**:
+ - [ ] `https://www.printfilm.com/api/health`
+ - [ ] 前台 / admin 首页 200
+ - [ ] 本地 `pytest tests/test_kepu_phase_billing.py`
+- **备注 / 回滚**:回滚 `kepu_stages.py` / `estimates.py` / `pipeline.py` / `handlers.py` / `projects.py` / `StoryboardPage.tsx` 后重启 API 并重发前端 dist
diff --git a/frontend/src/pages/studio/StoryboardPage.tsx b/frontend/src/pages/studio/StoryboardPage.tsx
index 1c7cc63..c42e069 100644
--- a/frontend/src/pages/studio/StoryboardPage.tsx
+++ b/frontend/src/pages/studio/StoryboardPage.tsx
@@ -972,7 +972,7 @@ title="用当前镜头重新拼接"
</p>
) : needsScriptConfirm ? (
<p className="pf-muted" style={{ margin: '0 0 1rem' }}>
- 分镜已就绪,请先检查旁白与画面描述,确认无误后点击「确认分镜,开始生成」。
+ 分镜已就绪,请先检查旁白与画面描述。确认后先生成画面与配音,再按需生成镜头视频并合成成片(分阶段扣费)。
</p>
) : null}
{project.shots.length === 0 ? null : (