214 lines
11 KiB
Python
214 lines
11 KiB
Python
"""镜头级「审片预览」:让评审听到的语音和成片一致。
|
||
|
||
语音来源跟随时间线设置 ``voice_source``:
|
||
- 走 TTS 的镜头 → 把模型自带音轨换成平台配音(画面直接 copy,不重编码);
|
||
- 走模型原声的镜头(原生音画同步)→ 预览就是镜头产物本身,不再多此一举换声。
|
||
否则「预览里是对的、成片里是错的」(或反过来),评审结论全废。
|
||
|
||
MiniMax H3 这类音画同出的模型,成片里自带一条语音轨,音色由模型即兴决定,
|
||
与项目登记的克隆声线无关(实测会把六十岁老船夫念成年轻女声,基频 110Hz → 224Hz)。
|
||
评审在分镜/审核页只能听到这条模型原声,就会把「配音」误判成错的;
|
||
真正的成片语音(IndexTTS2 声线)过去只有在导出成片后才听得到。
|
||
|
||
平台能力补齐:用 ffmpeg 把镜头视频轨(直接 copy,不重编码、不损画质)
|
||
与镜头配音重新封装成预览,配音入点与时间线一致(lead_padding_ms),
|
||
尾部空档补静音,保证音轨与画面等长。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
import secrets
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.constants import AssetKind
|
||
from app.models import Asset, Project, Shot, Timeline
|
||
from app.services import events, storage
|
||
from app.services import ffmpeg as ff
|
||
from app.services.timeline import DEFAULT_LEAD_MS, uses_native_voice
|
||
|
||
KIND = AssetKind.PREVIEW
|
||
|
||
# 镜头产物是否自带音轨只需要探测一次(按文件指纹缓存),
|
||
# 否则分镜列表每个镜头都要多跑一次 ffprobe,列表页会明显变慢。
|
||
_audio_cache: dict[str, bool] = {}
|
||
|
||
|
||
def has_native_audio(path: str | None) -> bool:
|
||
if not path or not storage.exists(path):
|
||
return False
|
||
key = _stamp(path)
|
||
cached = _audio_cache.get(key)
|
||
if cached is not None:
|
||
return cached
|
||
try:
|
||
flag = bool(ff.probe(storage.to_absolute(path)).has_audio)
|
||
except ff.FFmpegError:
|
||
flag = False
|
||
if len(_audio_cache) > 512:
|
||
_audio_cache.clear()
|
||
_audio_cache[key] = flag
|
||
return flag
|
||
|
||
|
||
def lead_ms_of(db: Session, project_id: int) -> int:
|
||
"""配音在镜头内的入点留白:跟随项目时间线设置,缺省用时间线默认值。"""
|
||
timeline = db.query(Timeline).filter(Timeline.project_id == project_id) \
|
||
.order_by(Timeline.id.desc()).first()
|
||
conf = (timeline.settings_json or {}) if timeline else {}
|
||
try:
|
||
return max(0, int(conf.get("lead_padding_ms", DEFAULT_LEAD_MS)))
|
||
except (TypeError, ValueError):
|
||
return DEFAULT_LEAD_MS
|
||
|
||
|
||
def _stamp(path: str | None) -> str:
|
||
if not path or not storage.exists(path):
|
||
return "0:0"
|
||
info = storage.to_absolute(path).stat()
|
||
return f"{int(info.st_mtime)}:{info.st_size}"
|
||
|
||
|
||
def timeline_conf(db: Session, project_id: int) -> dict:
|
||
"""当前时间线的语音/字幕设置;还没排过时间线时用出厂默认。"""
|
||
from app.services.timeline import DEFAULT_TIMELINE_SETTINGS
|
||
|
||
row = db.query(Timeline).filter(Timeline.project_id == project_id) \
|
||
.order_by(Timeline.id.desc()).first()
|
||
if row is None:
|
||
return dict(DEFAULT_TIMELINE_SETTINGS)
|
||
return {**DEFAULT_TIMELINE_SETTINGS, **(row.settings_json or {})}
|
||
|
||
|
||
def voice_source_of(db: Session, shot: Shot) -> str:
|
||
"""这条镜头的台词在成片里到底用谁的声音:native(模型原声)/ dub(平台配音)。"""
|
||
return "native" if uses_native_voice(shot, timeline_conf(db, shot.project_id)) else "dub"
|
||
|
||
|
||
def signature(db: Session, project_id: int, shot: Shot) -> str:
|
||
"""预览内容指纹:画面、配音、入点、语音来源任一变化都必须重新封装。"""
|
||
raw = (f"{_stamp(shot.output_path)}|{_stamp(shot.voiceover_path)}|{lead_ms_of(db, project_id)}"
|
||
f"|{voice_source_of(db, shot)}")
|
||
return hashlib.sha256(raw.encode()).hexdigest()[:16]
|
||
|
||
|
||
def _asset_for(db: Session, shot: Shot) -> Asset | None:
|
||
return db.query(Asset).filter(Asset.project_id == shot.project_id, Asset.owner_type == "shot",
|
||
Asset.owner_id == shot.id, Asset.kind == KIND) \
|
||
.order_by(Asset.id.desc()).first()
|
||
|
||
|
||
def preview_state(db: Session, shot: Shot) -> dict:
|
||
"""不改文件地回答「现在有没有可用的审片预览」,供镜头负载与前端渲染。"""
|
||
native = ff.available() and has_native_audio(shot.output_path)
|
||
asset = _asset_for(db, shot)
|
||
ready = bool(asset and storage.exists(asset.path)
|
||
and (asset.metadata_json or {}).get("signature") == signature(db, shot.project_id, shot))
|
||
return {
|
||
"preview_url": storage.url_for(asset.path) if ready else "",
|
||
"preview_path": asset.path if ready else "",
|
||
"preview_ready": ready,
|
||
"native_audio": native,
|
||
"voiceover_ready": bool(storage.exists(shot.voiceover_path)),
|
||
# 成片与预览的语音口径:native = 保留模型原声(音画同步),dub = 用平台配音
|
||
"voice_source": voice_source_of(db, shot),
|
||
}
|
||
|
||
|
||
def build_shot_preview(db: Session, project: Project, shot: Shot, *, force: bool = False,
|
||
actor: str = "studio", commit: bool = True) -> dict:
|
||
"""生成(或复用)镜头审片预览:画面 = 模型产物,声音 = 平台配音。"""
|
||
if not storage.exists(shot.output_path):
|
||
return {"ok": False, "shot_id": shot.id, "reason": "missing_output",
|
||
"message": "镜头还没有生成视频,无法封装审片预览"}
|
||
if voice_source_of(db, shot) == "native":
|
||
# 台词本来就用模型原声:预览无需换声,直接给镜头产物,避免「预览换了嗓子」
|
||
return {"ok": True, "shot_id": shot.id, "reused": True, "mode": "native",
|
||
"path": storage.to_relative(shot.output_path),
|
||
"url": storage.url_for(shot.output_path),
|
||
"message": "该镜头台词使用模型原声(原生音画同步),预览即镜头产物"}
|
||
sig = signature(db, project.id, shot)
|
||
existing = _asset_for(db, shot)
|
||
if (not force and existing and storage.exists(existing.path)
|
||
and (existing.metadata_json or {}).get("signature") == sig):
|
||
return {"ok": True, "shot_id": shot.id, "reused": True, "mode": "voiceover",
|
||
"path": existing.path, "url": storage.url_for(existing.path),
|
||
"duration_ms": existing.duration_ms or 0}
|
||
|
||
if not storage.exists(shot.voiceover_path):
|
||
# 没有配音就没有可替换的语音,直接用模型产物(并说明原因)
|
||
return {"ok": True, "shot_id": shot.id, "reused": True, "mode": "native",
|
||
"path": storage.to_relative(shot.output_path),
|
||
"url": storage.url_for(shot.output_path),
|
||
"message": "镜头尚无配音,预览使用模型自带音轨"}
|
||
if not ff.available():
|
||
return {"ok": False, "shot_id": shot.id, "reason": "ffmpeg_missing",
|
||
"message": "本机 ffmpeg 不可用,无法封装审片预览"}
|
||
|
||
lead = lead_ms_of(db, project.id)
|
||
folder = storage.project_dir(project.id, "preview")
|
||
out = folder / f"shot-{shot.order_index:02d}-preview_{secrets.token_hex(5)}.mp4"
|
||
video = storage.to_absolute(shot.output_path)
|
||
voice = storage.to_absolute(shot.voiceover_path)
|
||
# 预览长度以画面为准。apad 补的是「无限」静音,必须同时给出 whole_dur 并
|
||
# 用 -t 封口:apad + -c:v copy 的组合下 -shortest 不会触发,
|
||
# 封装进程会停在 100% CPU 一直编码,产物永远只有文件头 48 字节(实测挂死)。
|
||
try:
|
||
video_s = round(float(ff.probe(video).duration or 0), 3)
|
||
except ff.FFmpegError:
|
||
video_s = 0.0
|
||
if video_s <= 0:
|
||
return {"ok": False, "shot_id": shot.id, "reason": "output_unreadable",
|
||
"message": "镜头视频读不到时长,无法封装审片预览"}
|
||
delay = f"adelay=delays={lead}:all=1," if lead > 0 else ""
|
||
ff.run([ff.FFMPEG, "-hide_banner", "-loglevel", "error", "-y",
|
||
"-i", str(video), "-i", str(voice),
|
||
"-filter_complex", f"[1:a]{delay}apad=whole_dur={video_s}[previewa]",
|
||
"-map", "0:v:0", "-map", "[previewa]",
|
||
"-c:v", "copy", "-c:a", "aac", "-b:a", "160k",
|
||
"-t", f"{video_s:.3f}", "-movflags", "+faststart", str(out)], timeout=600)
|
||
|
||
info = ff.probe(out).as_dict()
|
||
duration_ms = int(round(float(info.get("duration") or 0) * 1000))
|
||
if duration_ms <= 0:
|
||
out.unlink(missing_ok=True)
|
||
return {"ok": False, "shot_id": shot.id, "reason": "preview_unreadable",
|
||
"message": "审片预览封装完成但读不到时长"}
|
||
rel = storage.to_relative(out)
|
||
asset = storage.register_asset(
|
||
db, project_id=project.id, owner_type="shot", owner_id=shot.id, kind=KIND,
|
||
path=rel, name=f"镜头 {shot.order_index:02d} 审片预览", source="compose",
|
||
info={"duration_ms": duration_ms, "size_bytes": out.stat().st_size, "mime": "video/mp4",
|
||
"width": info.get("width") or 0, "height": info.get("height") or 0},
|
||
metadata={"signature": sig, "lead_ms": lead, "audio_source": "voiceover",
|
||
"video_source": storage.to_relative(shot.output_path),
|
||
"voiceover_path": storage.to_relative(shot.voiceover_path)})
|
||
events.log_event(db, "preview.built", project_id=project.id, shot_id=shot.id,
|
||
message=f"镜头 {shot.order_index:02d} 审片预览封装完成(配音替换模型原声)",
|
||
entity_type="shot", entity_id=shot.id,
|
||
data={"path": rel, "duration_ms": duration_ms, "lead_ms": lead,
|
||
"voiceover": storage.to_relative(shot.voiceover_path)}, actor=actor)
|
||
if commit:
|
||
db.commit()
|
||
else:
|
||
db.flush()
|
||
return {"ok": True, "shot_id": shot.id, "reused": False, "mode": "voiceover", "path": rel,
|
||
"url": storage.url_for(rel), "duration_ms": duration_ms,
|
||
"version": asset.current_version, "lead_ms": lead}
|
||
|
||
|
||
def build_project_previews(db: Session, project: Project, *, shot_ids: list[int] | None = None,
|
||
force: bool = False, actor: str = "studio") -> dict:
|
||
"""批量封装:调度台与验收脚本一次把全片镜头的审片预览准备好。"""
|
||
wanted = set(shot_ids) if shot_ids else None
|
||
items = []
|
||
for shot in sorted(project.shots or [], key=lambda s: (s.order_index, s.id)):
|
||
if wanted is not None and shot.id not in wanted:
|
||
continue
|
||
items.append(build_shot_preview(db, project, shot, force=force, actor=actor))
|
||
db.commit()
|
||
return {"project_id": project.id, "count": len(items),
|
||
"built": sum(1 for i in items if i.get("ok") and not i.get("reused")),
|
||
"reused": sum(1 for i in items if i.get("ok") and i.get("reused")),
|
||
"failed": sum(1 for i in items if not i.get("ok")), "items": items}
|