Skip to content

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

fix(oss,billing): stop OSS upload false positives and raise Seedance estimates

yi77e582a945c4
diff --git a/backend/app/api/drama/episodes.py b/backend/app/api/drama/episodes.py
index 195c36f..9aefd5f 100644
--- a/backend/app/api/drama/episodes.py
+++ b/backend/app/api/drama/episodes.py
@@ -44,6 +44,7 @@ from app.services.drama.generation import (
project_link_last_frame_enabled,
reconcile_orphaned_fragment_generations,
)
+from app.services.drama.fragment_content_duration import resolve_seedance_duration_from_content
from app.services.drama.jobs import (
cancel_all_episode_video_jobs,
cancel_episode_video_jobs,
@@ -498,6 +499,10 @@ async def generate_episode(
if defer_activation:
deferred_count += 1
has_video = bool((f.video or "").strip())
+ duration_sec = resolve_seedance_duration_from_content(
+ f.content or "",
+ fallback=int(f.duration_sec or 8),
+ )
try:
task = await create_task(
db,
@@ -516,6 +521,7 @@ async def generate_episode(
"batch_key": batch_key,
"batch_index": index,
"replace_existing_video": has_video,
+ "duration_sec": duration_sec,
},
drama_project_id=ep.project_id,
episode_id=episode_id,
diff --git a/backend/app/config.py b/backend/app/config.py
index 1caab3d..48c5651 100644
--- a/backend/app/config.py
+++ b/backend/app/config.py
@@ -113,7 +113,7 @@ class Settings(BaseSettings):
# Seedream 单张实测约 3–3.5 万 tokens;预估略留余量,避免预扣远高于实扣
billing_est_seedream_tokens: int = 45_000
billing_est_tts_tokens: int = 5_000
- billing_est_seedance_tokens_per_sec: int = 20_000
+ billing_est_seedance_tokens_per_sec: int = 32_000
# Signup grant (fen)
billing_signup_grant_fen: int = 500
diff --git a/backend/app/schemas_settings.py b/backend/app/schemas_settings.py
index 7e9969a..c27aa7f 100644
--- a/backend/app/schemas_settings.py
+++ b/backend/app/schemas_settings.py
@@ -107,7 +107,7 @@ class AdminModelSettingsOut(BaseModel):
billing_est_llm_tokens: int = 80_000
billing_est_seedream_tokens: int = 45_000
billing_est_tts_tokens: int = 5_000
- billing_est_seedance_tokens_per_sec: int = 20_000
+ billing_est_seedance_tokens_per_sec: int = 32_000
billing_signup_grant_fen: int = 500
quota_enabled: bool = False
new_user_quota: int = 5
diff --git a/backend/app/services/billing/estimates.py b/backend/app/services/billing/estimates.py
index 3a51524..acf1486 100644
--- a/backend/app/services/billing/estimates.py
+++ b/backend/app/services/billing/estimates.py
@@ -90,9 +90,37 @@ async def estimate_task_fen(db: AsyncSession, task: TaskRun, settings: Settings
_, c = charge_fen_for_tokens(s.billing_est_seedream_tokens, "seedream", settings=s)
return max(1, math.ceil(c * buf))
if task_type in {"asset_video", "fragment_video"}:
- dur = float(payload.get("duration_sec") or payload.get("duration") or 8)
+ dur = float(payload.get("duration_sec") or payload.get("duration") or 0)
+ if dur <= 0 and isinstance(payload.get("prepared"), dict):
+ dur = float(payload["prepared"].get("duration") or 0)
+ if dur <= 0:
+ frag_id = task.fragment_id or (
+ (payload.get("fragment_ids") or [None])[0]
+ if isinstance(payload.get("fragment_ids"), list)
+ else None
+ )
+ if frag_id:
+ from app.models_drama import DramaEpisodeFragment
+ from app.services.drama.fragment_content_duration import (
+ resolve_seedance_duration_from_content,
+ )
+
+ frag = await db.get(DramaEpisodeFragment, int(frag_id))
+ if frag is not None:
+ dur = float(
+ resolve_seedance_duration_from_content(
+ frag.content or "",
+ fallback=int(frag.duration_sec or 8),
+ )
+ )
+ if dur <= 0:
+ 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)
return max(1, math.ceil(c * buf))
if task_type == "voice_synthesis":
_, c = charge_fen_for_tokens(s.billing_est_tts_tokens, "tts", settings=s)
diff --git a/backend/app/services/oss_queue.py b/backend/app/services/oss_queue.py
index a35dbae..991d00d 100644
--- a/backend/app/services/oss_queue.py
+++ b/backend/app/services/oss_queue.py
@@ -230,7 +230,8 @@ def upload_local_url_sync(local_url: str) -> str | None:
logger.warning("oss backfill skip missing file %s", url)
return None
oss_url = storage.upload_local_sync(path)
- if storage.is_local_static_url(oss_url):
+ # 成功须为公网 http(s);勿用 is_local_static_url(本地副本仍在会误判 OSS URL)
+ if not oss_url or not (oss_url.startswith("http://") or oss_url.startswith("https://")):
raise RuntimeError(f"upload returned local URL for {url}")
return oss_url
diff --git a/backend/app/services/storage.py b/backend/app/services/storage.py
index 88606df..fa3b484 100644
--- a/backend/app/services/storage.py
+++ b/backend/app/services/storage.py
@@ -125,7 +125,10 @@ def rel_static_url(path: Path) -> str:
def is_local_static_url(url: str | None) -> bool:
- """True when url points at media under backend/static (relative or site /static/)."""
+ """True when url points at media under backend/static (relative or site /static/).
+
+ HTTPS OSS/CDN 地址即使本地仍保留 FFmpeg 副本,也不算本地 URL。
+ """
if not url:
return False
if url.startswith("/static/"):
@@ -146,13 +149,9 @@ def is_local_static_url(url: str | None) -> bool:
for scheme in ("https://", "http://"):
if url.startswith(f"{scheme}{host}/static/"):
return True
- local = local_path_from_url(url)
- if local and local.is_file():
- try:
- local.resolve().relative_to(STATIC_ROOT.resolve())
- return True
- except ValueError:
- return False
+ # 远程 http(s)(含 OSS public_base)一律非本地;勿因磁盘副本误判
+ if url.startswith("http://") or url.startswith("https://"):
+ return False
return False
diff --git a/backend/tests/test_fragment_video_estimate.py b/backend/tests/test_fragment_video_estimate.py
new file mode 100644
index 0000000..bf6341a
--- /dev/null
+++ b/backend/tests/test_fragment_video_estimate.py
@@ -0,0 +1,75 @@
+"""fragment_video 预扣估算:时长来自正文 / payload。"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from app.models_tasks import TaskRun
+from app.services.billing.estimates import estimate_task_fen
+
+
+async def test_fragment_video_estimate_uses_payload_duration_sec(monkeypatch):
+ """有 duration_sec 时按秒数估算,不再默认 8s。"""
+ from app.config import get_settings
+
+ settings = get_settings()
+ monkeypatch.setattr(settings, "billing_est_seedance_tokens_per_sec", 32_000)
+ monkeypatch.setattr(settings, "billing_estimate_buffer", 1.2)
+ monkeypatch.setattr(settings, "billing_markup", 1.5)
+ monkeypatch.setattr(settings, "billing_seedance_video0", 46.0)
+
+ task = TaskRun(
+ id=1,
+ domain="drama",
+ task_type="fragment_video",
+ requested_by=1,
+ payload={"duration_sec": 16, "fragment_ids": [9]},
+ fragment_id=9,
+ )
+ db = MagicMock()
+ db.get = AsyncMock(return_value=None)
+
+ fen8 = await estimate_task_fen(
+ db,
+ TaskRun(
+ id=2,
+ domain="drama",
+ task_type="fragment_video",
+ requested_by=1,
+ payload={"duration_sec": 8},
+ fragment_id=9,
+ ),
+ settings=settings,
+ )
+ fen16 = await estimate_task_fen(db, task, settings=settings)
+ assert fen16 > fen8
+
+
+async def test_fragment_video_estimate_reads_fragment_content(monkeypatch):
+ """无 payload duration 时从分镜正文 @duration 解析。"""
+ from app.config import get_settings
+
+ settings = get_settings()
+ monkeypatch.setattr(settings, "billing_est_seedance_tokens_per_sec", 32_000)
+ monkeypatch.setattr(settings, "billing_estimate_buffer", 1.2)
+
+ frag = SimpleNamespace(content="@duration:12 台词", duration_sec=8)
+ db = MagicMock()
+ db.get = AsyncMock(return_value=frag)
+
+ task = TaskRun(
+ id=3,
+ domain="drama",
+ task_type="fragment_video",
+ requested_by=1,
+ payload={"fragment_ids": [11]},
+ fragment_id=11,
+ )
+ fen = await estimate_task_fen(db, task, settings=settings)
+ assert fen > 0
+ db.get.assert_awaited()
diff --git a/backend/tests/test_oss_queue_backfill.py b/backend/tests/test_oss_queue_backfill.py
index f285985..e3b55d3 100644
--- a/backend/tests/test_oss_queue_backfill.py
+++ b/backend/tests/test_oss_queue_backfill.py
@@ -33,3 +33,39 @@ def test_collect_local_urls_from_params():
found,
)
assert found == {"/static/a.mp3"}
+
+
+def test_upload_local_url_sync_accepts_oss_https_even_if_local_file_exists(tmp_path, monkeypatch):
+ """上传成功返回 OSS https 时,即使本地仍有文件也不应 raise。"""
+ from pathlib import Path
+ from unittest.mock import patch
+
+ from app.services import oss_queue
+ from app.services import storage as storage_mod
+
+ local = tmp_path / "generated" / "p1" / "shot.png"
+ local.parent.mkdir(parents=True)
+ local.write_bytes(b"png")
+ local_url = "/static/generated/p1/shot.png"
+ oss_https = "https://lsj-cc.oss-cn-beijing.aliyuncs.com/kepu/generated/p1/shot.png"
+
+ monkeypatch.setattr(storage_mod, "STATIC_ROOT", tmp_path)
+ monkeypatch.setattr(storage_mod, "is_local_static_url", lambda url: str(url or "").startswith("/static/"))
+ monkeypatch.setattr(storage_mod, "local_path_from_url", lambda url: local if url == local_url else None)
+ monkeypatch.setattr(storage_mod, "upload_local_sync", lambda path: oss_https)
+
+ with patch.object(oss_queue.oss_svc, "oss_enabled", return_value=True):
+ out = oss_queue.upload_local_url_sync(local_url)
+ assert out == oss_https
+
+
+def test_is_local_static_url_rejects_oss_https():
+ """OSS https 不应因本地副本存在而被判为本地。"""
+ from app.services.storage import is_local_static_url
+
+ assert is_local_static_url("/static/generated/p1/a.png") is True
+ assert (
+ is_local_static_url("https://lsj-cc.oss-cn-beijing.aliyuncs.com/kepu/generated/p1/a.png")
+ is False
+ )
+ assert is_local_static_url("https://www.printfilm.com/static/generated/p1/a.png") is True
diff --git a/docs/releases/2026-09-06.md b/docs/releases/2026-09-06.md
index 4bc94b1..57d1b6e 100644
--- a/docs/releases/2026-09-06.md
+++ b/docs/releases/2026-09-06.md
@@ -1,3 +1,20 @@
+## 2026-09-06 22:16 · OSS 上传误判修复 + Seedance 预扣抬高(热修)
+
+- **操作者**:Agent
+- **方式**:SSH 热修
+- **范围**:api / env
+- **变更摘要**:
+ - P0:`is_local_static_url` 对 http(s) OSS URL 一律返回 False;`upload_local_url_sync` 成功判定改为须以 `http(s)://` 开头,避免本地 FFmpeg 副本导致「upload returned local URL」误抛、库字段不回写
+ - P0:线上 `backfill_oss_media` 补传存量 `/static`(两轮合计 uploaded≈760 / failed=0;仍有少量 pending 多为缺本地文件 skipped)
+ - P1:`fragment_video` 估时优先 `payload.duration_sec`,否则按分镜正文 `resolve_seedance_duration_from_content`;入队写入 `duration_sec`
+ - P1:默认 `billing_est_seedance_tokens_per_sec` 20000→32000(并写入生产 `.env`);`fragment_video` 预估加半张 seedream 余量
+- **验证**:
+ - [x] `https://www.printfilm.com/api/health`(`ok:true`,`task_runtime.healthy`,scheduler/poller/watchdog running)
+ - [x] 线上断言:`tokens_per_sec=32000`,OSS https `is_local_static_url=False`
+ - [x] journal 近 5–10min:`upload returned local URL` = 0
+ - [x] `tests/test_oss_queue_backfill.py` + `tests/test_fragment_video_estimate.py` 6 passed
+- **备注 / 回滚**:回滚 `storage.py` / `oss_queue.py` / `estimates.py` / `episodes.py` / `config.py` / `schemas_settings.py`,去掉 `.env` 中 `BILLING_EST_SEEDANCE_TOKENS_PER_SEC` 后 `systemctl restart ai-movie-api`
+
## 2026-09-06 21:25 · 任务调度看门狗 + 运行中孤儿恢复(热修)
- **操作者**:Agent
diff --git a/docs/releases/README.md b/docs/releases/README.md
index b151ace..82de038 100644
--- a/docs/releases/README.md
+++ b/docs/releases/README.md
@@ -33,6 +33,7 @@
| 日期 | 摘要 | 文件 |
|------|------|------|
+| 2026-09-06 | OSS 上传误判修复 + Seedance 预扣抬高;杭州热修 | [2026-09-06.md](./2026-09-06.md) |
| 2026-09-06 | 作废任务补 settle 解冻;杭州热修 | [2026-09-06.md](./2026-09-06.md) |
| 2026-09-06 | 分集剧本进度回写 + DeepSeek JSON + force 续跑;杭州热修 | [2026-09-06.md](./2026-09-06.md) |
| 2026-09-06 | 任务调度看门狗 + 运行中孤儿恢复;杭州热修 | [2026-09-06.md](./2026-09-06.md) |