567 lines
31 KiB
Python
567 lines
31 KiB
Python
"""长视频合成引擎(数据面):按时间线排布镜头、语音、字幕与配乐。
|
||
|
||
关键规则:
|
||
- 语音来源(``voice_source``)决定台词用谁的声音:
|
||
native(默认)= 台词直接用视频模型自带的原声。MiniMax H3 是原生音画同步模型,
|
||
人声对着口型生成,零错位;平台把角色声线的克隆样本作为「参考音频」一起喂进去
|
||
(见 prompt.reference_voice_audios),所以原声既对得上口型、又不是模型随机挑的陌生人。
|
||
auto = 判定与 native 相同(有原声用原声,缺原声回退 TTS),保留给老项目设置。
|
||
dub = 强制平台配音:音色可控,但 TTS 的长度和停顿与画面无关,必须靠
|
||
「按语音时长补帧」对齐,口型不保证对齐。这是配音的固有取舍,让用户自己选、看得见。
|
||
- 只有走 TTS 时片段时长才 = max(镜头产物时长, 配音时长 + 尾部留白);
|
||
用原声时片段 = 产物时长(此前正是「片段比配音长 → 定格画面里人物还在张嘴」的成因)。
|
||
- 转场(叠化/淡入淡出/黑场/擦除)以重叠方式压缩时间线,时长自动收敛到安全范围。
|
||
- 字幕与配音按所属镜头片段重新对齐,任何顺序/时长调整都会重算全时间线。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from sqlalchemy import select
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.config import settings
|
||
from app.constants import AudioTrackKind, ProjectStatus, ShotStatus, Transition, canvas_for
|
||
from app.models import AudioClip, Project, Shot, SubtitleCue, Timeline, TimelineSegment, now_utc
|
||
from app.services import audio, events, storage
|
||
from app.services import ffmpeg as ff
|
||
|
||
DEFAULT_TAIL_MS = 350
|
||
DEFAULT_LEAD_MS = 160
|
||
DEFAULT_TIMELINE_SETTINGS = {
|
||
# 字幕方式:off / sidecar / embed / burn / both(见 compose.SUBTITLE_MODES)
|
||
# embed = 成片内嵌可开关的软字幕轨(推荐,画质无损);需要硬字幕改 burn / both。
|
||
# burn_subtitle 保留给老版本:没有 subtitle_mode 时才生效。
|
||
"subtitle_mode": "embed",
|
||
"burn_subtitle": True,
|
||
"subtitle_style": {"size": 0, "margin": 0, "outline": 0},
|
||
"voice_gain_db": 0.0,
|
||
"bgm_gain_db": -17.0,
|
||
"duck_bgm": True,
|
||
"target_lufs": settings.target_lufs,
|
||
"tail_padding_ms": DEFAULT_TAIL_MS,
|
||
"lead_padding_ms": DEFAULT_LEAD_MS,
|
||
"hold_last_frame": True,
|
||
# 模型自带音轨(如 MiniMax H3 一次成片的音效/音乐/环境声)是否保留。
|
||
# 关掉就回到「只有配音 + BGM」的老行为;开启后原生音轨与配音一起混音。
|
||
# 台词走模型原声时必须开启:关掉等于把要用的那条音轨剪掉了。
|
||
# 只有在 voice_source=dub(强制 TTS)时才该关掉,避免原声人声和配音叠成双声。
|
||
"keep_shot_audio": True,
|
||
# 台词的语音来源,见 VOICE_SOURCES
|
||
"voice_source": "native",
|
||
# 模型自带音轨里「台词以外」的那部分(环境声、BGM、模型自己哼的旁白)留不留。
|
||
# 联合音视频模型(MiniMax H3)会照着画面自己发声:键盘镜头里这是好事,
|
||
# 对白镜头里它可能顺手加一段背景音乐。三档见 NATIVE_AUDIO_POLICY_LABELS。
|
||
"native_audio_policy": "auto",
|
||
}
|
||
|
||
# 原生音轨(非台词部分)的处理策略:
|
||
# auto = 全保留;dialogue_only = 只留有声镜头的原声,无声镜头静音;
|
||
# off = 一律静音,声音全部由平台配音/BGM/音效供给(模型爱加什么背景音乐都不影响成片)。
|
||
NATIVE_AUDIO_POLICIES = ("auto", "dialogue_only", "off")
|
||
NATIVE_AUDIO_POLICY_LABELS = {
|
||
"auto": "保留模型自带的环境声(键盘声、街道声等,与画面天然同步)",
|
||
"dialogue_only": "只保留有台词镜头的原声,没有台词的镜头静音",
|
||
"off": "全部静音,声音只用平台配音/配乐/音效",
|
||
}
|
||
|
||
# 台词语音来源:auto = 有原声就用原声;native = 一律用镜头原声;dub = 一律用平台配音
|
||
VOICE_SOURCES = ("auto", "native", "dub")
|
||
VOICE_SOURCE_LABELS = {
|
||
"auto": "自动(台词优先用模型原声,缺原声的镜头回退平台配音)",
|
||
"native": "模型原声直出(口型天然同步,音色用角色声线做参考,不再生成 TTS)",
|
||
"dub": "始终用平台配音(音色可控,口型不保证对齐)",
|
||
}
|
||
VOICE_SOURCE_ORIGIN = {"native": "模型原声", "dub": "平台配音", "": ""}
|
||
|
||
|
||
def subtitle_mode_of(conf: dict | None) -> str:
|
||
"""老时间线只存了 burn_subtitle 布尔 → 折算成新的字幕方式,保证展示与渲染口径一致。"""
|
||
conf = conf or {}
|
||
mode = str(conf.get("subtitle_mode") or "").strip().lower()
|
||
if mode in ("off", "sidecar", "embed", "burn", "both"):
|
||
return mode
|
||
return "burn" if conf.get("burn_subtitle", True) else "sidecar"
|
||
|
||
|
||
def _probe_ms(path: str) -> int:
|
||
if not storage.exists(path):
|
||
return 0
|
||
if not ff.available():
|
||
return 0
|
||
try:
|
||
return int(round(ff.probe(storage.to_absolute(path)).duration * 1000))
|
||
except Exception:
|
||
return ff.duration_ms(storage.to_absolute(path))
|
||
|
||
|
||
def voice_source_mode(conf: dict | None) -> str:
|
||
mode = str((conf or {}).get("voice_source") or "auto").strip().lower()
|
||
return mode if mode in VOICE_SOURCES else "auto"
|
||
|
||
|
||
def native_audio_policy(conf: dict | None) -> str:
|
||
"""模型原生音轨里非台词部分的处理策略(读不到就按 auto:模型直出优先)。"""
|
||
mode = str((conf or {}).get("native_audio_policy") or "auto").strip().lower()
|
||
return mode if mode in NATIVE_AUDIO_POLICIES else "auto"
|
||
|
||
|
||
def settings_of(db: Session, project) -> dict:
|
||
"""项目当前时间线的渲染设置(没有时间线就返回默认值)。
|
||
|
||
``build_timeline`` 会把旧时间线的 settings 并回来,所以改了默认值也不会自动
|
||
作用到已存在的时间线上;派发/提示词必须现读现算,口径才和渲染一致。
|
||
"""
|
||
pid = int(getattr(project, "id", project) or 0)
|
||
timeline = db.query(Timeline).filter(Timeline.project_id == pid) \
|
||
.order_by(Timeline.id.desc()).first()
|
||
return {**DEFAULT_TIMELINE_SETTINGS, **((timeline.settings_json or {}) if timeline else {})}
|
||
|
||
|
||
def native_voice_wanted(db: Session, project: Project, shot: Shot) -> bool:
|
||
"""派发前判断「这一镜的台词是否交给模型原声」。
|
||
|
||
此时还没有产物可以探测,所以只看三件事:时间线没被切成 dub、这镜确实有台词、
|
||
这一镜要用的视频模型有「参考音色」槽位(``max_reference_audios``,也就是
|
||
MiniMax H3 这类原生音画同步模型)。第三条是硬条件:不会出声的模型
|
||
(mock、纯 i2v 供应商)必须继续排 TTS,否则台词直接变成哑巴。
|
||
"""
|
||
if not str(shot.dialogue or "").strip():
|
||
return False
|
||
if voice_source_mode(settings_of(db, project)) == "dub":
|
||
return False
|
||
from app.providers import registry as model_registry
|
||
|
||
try:
|
||
spec, _adapter = model_registry.resolve_video(db, shot.provider or project.default_provider,
|
||
shot.model or None)
|
||
return int(model_registry.capabilities_of(spec).max_reference_audios or 0) > 0
|
||
except Exception:
|
||
return False
|
||
|
||
|
||
def uses_native_voice(shot: Shot, conf: dict | None) -> bool:
|
||
"""这条镜头的台词是否直接用模型产物自带的音轨。
|
||
|
||
原生音画同步的模型(MiniMax H3 等)一次成片里人声就是对着口型生成的:
|
||
拿它当配音天然零错位;而 TTS 的长度和停顿跟画面无关,硬盖上去就会出现
|
||
「画面里人物闭着嘴、声音还在说」和「张嘴却静音」。
|
||
|
||
音色不再靠模型即兴:生成时平台会把角色声线的克隆样本作为参考音频喂给 H3
|
||
(Capabilities.max_reference_audios),原声就是「这个角色自己的嗓子」。
|
||
产物里确实没有音轨时仍然回退配音。
|
||
"""
|
||
if voice_source_mode(conf) == "dub":
|
||
return False
|
||
if not str(shot.dialogue or "").strip():
|
||
return False
|
||
from app.services import preview
|
||
|
||
return preview.has_native_audio(shot.output_path)
|
||
|
||
|
||
def eligible_shots(db: Session, project: Project, *, only_approved: bool = True,
|
||
shot_ids: list[int] | None = None) -> list[Shot]:
|
||
wanted = set(shot_ids) if shot_ids else None
|
||
rows: list[Shot] = []
|
||
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
|
||
if not storage.exists(shot.output_path):
|
||
continue
|
||
if only_approved and shot.status != ShotStatus.APPROVED:
|
||
continue
|
||
rows.append(shot)
|
||
return rows
|
||
|
||
|
||
def build_timeline(db: Session, project: Project, *, name: str = "", only_approved: bool = True,
|
||
shot_ids: list[int] | None = None, settings_patch: dict | None = None,
|
||
actor: str = "studio") -> dict:
|
||
"""从审核合格的镜头重建当前时间线(片段 + 配音 + 字幕 + 配乐)。"""
|
||
shots = eligible_shots(db, project, only_approved=only_approved, shot_ids=shot_ids)
|
||
if not shots:
|
||
raise ValueError("没有可排布的镜头:请先让镜头通过质检与人工审核(或勾选包含未审核镜头)")
|
||
|
||
width, height = canvas_for(project.resolution, project.aspect_ratio)
|
||
prev = db.scalars(select(Timeline).where(Timeline.project_id == project.id,
|
||
Timeline.is_current.is_(True))).all()
|
||
next_version = max([t.version or 1 for t in prev], default=0) + 1
|
||
for old in prev:
|
||
old.is_current = False
|
||
conf = dict(DEFAULT_TIMELINE_SETTINGS)
|
||
for row in prev:
|
||
conf.update(row.settings_json or {})
|
||
conf.update(settings_patch or {})
|
||
|
||
timeline = Timeline(project_id=project.id, name=name or (prev[0].name if prev else "主时间线"),
|
||
version=next_version, is_current=True, fps=project.fps, width=width, height=height,
|
||
sample_rate=int(conf.get("sample_rate", 48000)), settings_json=conf)
|
||
db.add(timeline)
|
||
db.flush()
|
||
|
||
tail = int(conf.get("tail_padding_ms", DEFAULT_TAIL_MS))
|
||
policy = native_audio_policy(conf)
|
||
native_shots: list[int] = []
|
||
muted_native: list[int] = []
|
||
sfx_missing: list[dict] = []
|
||
for index, shot in enumerate(shots, start=1):
|
||
clip_ms = _probe_ms(shot.output_path) or int(float(shot.duration or 5) * 1000)
|
||
native = uses_native_voice(shot, conf)
|
||
# 原声与画面等长:为 TTS 拉长片段(再定格补帧)正是音画错位的来源
|
||
voice_ms = 0 if native else audio.shot_voice_duration_ms(db, project, shot)
|
||
seg_ms = max(clip_ms, (voice_ms + tail) if voice_ms else clip_ms)
|
||
segment = TimelineSegment(timeline_id=timeline.id, shot_id=shot.id, order_index=index,
|
||
label=f"{shot.order_index:02d} {shot.title}".strip(),
|
||
source_path=storage.to_relative(shot.output_path),
|
||
duration_ms=int(seg_ms), source_in_ms=0, source_out_ms=int(clip_ms),
|
||
transition_out=shot.transition_out or project.default_transition or Transition.CUT,
|
||
transition_duration_ms=int(shot.transition_duration_ms or 500),
|
||
volume=1.0, muted=False)
|
||
timeline.segments.append(segment)
|
||
# 模型原生音轨里「台词以外」的部分要不要留:off 全静音,
|
||
# dialogue_only 只静音那些「模型没人可说、于是自己加戏」的镜头。
|
||
# 静音窗口交给 compose 在原生总线上做,不改片段本身(配音轨不受影响)。
|
||
if policy == "off" or (policy == "dialogue_only" and not shot.has_dialogue):
|
||
muted_native.append(shot.id)
|
||
# 镜头级音效:模型给不给这份声音都不确定,平台按分镜里的配置再叠一层保底。
|
||
# 以前只有手工在时间线上加的 clip 才会进混音,重建一次时间线就全丢了。
|
||
for row in shot.all_sound_effects():
|
||
if not storage.exists(row["path"]):
|
||
sfx_missing.append({"shot_id": shot.id, "name": row["name"], "path": row["path"]})
|
||
continue
|
||
timeline.audio_clips.append(AudioClip(
|
||
timeline_id=timeline.id, shot_id=shot.id, kind=AudioTrackKind.SFX,
|
||
label=f"音效 {shot.order_index:02d}·{row['name']}",
|
||
source_path=storage.to_relative(row["path"]),
|
||
start_ms=int(row["offset_ms"]), # recompute_timings 再平移到绝对位置
|
||
duration_ms=int(row["duration_ms"]) or _probe_ms(row["path"]),
|
||
gain_db=float(row["gain_db"]), ducking=False))
|
||
if native:
|
||
native_shots.append(shot.id)
|
||
timeline.audio_clips.append(AudioClip(
|
||
timeline_id=timeline.id, shot_id=shot.id, kind=AudioTrackKind.VOICEOVER,
|
||
label=f"原声 {shot.order_index:02d}", source_path=storage.to_relative(shot.output_path),
|
||
duration_ms=int(clip_ms), gain_db=float(conf.get("voice_gain_db", 0.0)),
|
||
text=shot.effective_subtitle, voice_asset_id=None, ducking=True, native_audio=True))
|
||
elif storage.exists(shot.voiceover_path):
|
||
timeline.audio_clips.append(AudioClip(
|
||
timeline_id=timeline.id, shot_id=shot.id, kind=AudioTrackKind.VOICEOVER,
|
||
label=f"配音 {shot.order_index:02d}", source_path=storage.to_relative(shot.voiceover_path),
|
||
duration_ms=int(shot.voiceover_duration_ms or _probe_ms(shot.voiceover_path) or voice_ms),
|
||
gain_db=float(conf.get("voice_gain_db", 0.0)), text=shot.effective_subtitle,
|
||
voice_asset_id=shot.voice_asset_id, ducking=True))
|
||
conf["native_voice_shots"] = native_shots
|
||
conf["native_audio_policy"] = policy
|
||
conf["muted_native_shots"] = muted_native
|
||
timeline.settings_json = conf
|
||
_add_bgm(db, timeline, project, conf)
|
||
recompute_timings(db, timeline, project=project)
|
||
db.flush()
|
||
|
||
missing_voice = [{"shot_id": s.id, "order_index": s.order_index, "chars": len((s.dialogue or "").strip())}
|
||
for s in shots if (s.dialogue or "").strip() and not storage.exists(s.voiceover_path)
|
||
and s.id not in set(native_shots)]
|
||
events.log_event(db, "timeline.built", project_id=project.id, message=f"时间线「{timeline.name}」已排布 "
|
||
f"{len(timeline.segments)} 个片段,总时长 {timeline.total_duration_ms / 1000:.1f}s",
|
||
entity_type="timeline", entity_id=timeline.id, actor=actor,
|
||
data={"segments": len(timeline.segments), "total_ms": timeline.total_duration_ms})
|
||
project.status = ProjectStatus.COMPOSING
|
||
project.current_stage = "compose"
|
||
db.commit()
|
||
return {"timeline_id": timeline.id, "segments": len(timeline.segments),
|
||
"total_duration_ms": timeline.total_duration_ms, "version": timeline.version,
|
||
"subtitles": len(timeline.subtitles or []), "audio_clips": len(timeline.audio_clips or []),
|
||
"voice_source": voice_source_mode(conf), "native_voice_shots": native_shots,
|
||
"native_audio_policy": policy, "muted_native_shots": muted_native,
|
||
"missing_voiceover": missing_voice, "missing_sfx": sfx_missing}
|
||
|
||
|
||
def _add_bgm(db: Session, timeline: Timeline, project: Project, conf: dict) -> None:
|
||
from app.models import Asset
|
||
|
||
bgm = db.query(Asset).filter(Asset.project_id == project.id, Asset.owner_type == "project",
|
||
Asset.kind == "bgm").order_by(Asset.id).first()
|
||
if not bgm or not storage.exists(bgm.path):
|
||
return
|
||
timeline.audio_clips.append(AudioClip(timeline_id=timeline.id, shot_id=None, kind=AudioTrackKind.BGM,
|
||
label=bgm.name or "背景音乐", source_path=bgm.path,
|
||
duration_ms=int(bgm.duration_ms or 0), loop=True,
|
||
gain_db=float(conf.get("bgm_gain_db", -17.0)), ducking=bool(conf.get("duck_bgm", True))))
|
||
|
||
|
||
def overlap_ms(timeline: Timeline, seg: TimelineSegment, nxt: TimelineSegment | None) -> int:
|
||
if nxt is None:
|
||
return 0
|
||
kind = seg.transition_out or Transition.CUT
|
||
if kind == Transition.CUT:
|
||
return 0
|
||
want = int(seg.transition_duration_ms or 0)
|
||
if want <= 0:
|
||
return 0
|
||
limit = max(0, min(seg.duration_ms, nxt.duration_ms) - 400)
|
||
return max(0, min(want, int(settings.xfade_max_duration_ms), limit))
|
||
|
||
|
||
def recompute_timings(db: Session, timeline: Timeline, *, project: Project | None = None,
|
||
retime_subtitles: bool = True) -> dict:
|
||
"""重算所有片段起点、总时长,并把配音与字幕重新对齐。"""
|
||
project = project or db.get(Project, timeline.project_id)
|
||
segments = sorted(timeline.segments or [], key=lambda s: (s.order_index, s.id))
|
||
old_starts = {s.id: int(s.start_ms or 0) for s in segments}
|
||
cursor = 0
|
||
overlaps: dict[int, int] = {}
|
||
for idx, seg in enumerate(segments):
|
||
seg.start_ms = int(cursor)
|
||
nxt = segments[idx + 1] if idx + 1 < len(segments) else None
|
||
td = overlap_ms(timeline, seg, nxt)
|
||
seg.transition_duration_ms = td
|
||
overlaps[seg.id] = td
|
||
cursor += max(1, seg.duration_ms) - td
|
||
timeline.total_duration_ms = int(max(0, cursor))
|
||
|
||
# 音轨跟随所属镜头片段平移
|
||
by_shot = {s.shot_id: s for s in segments if s.shot_id}
|
||
lead = int((timeline.settings_json or {}).get("lead_padding_ms", DEFAULT_LEAD_MS))
|
||
for clip in timeline.audio_clips or []:
|
||
seg = by_shot.get(clip.shot_id) if clip.shot_id else None
|
||
if seg is not None:
|
||
old = old_starts.get(seg.id, seg.start_ms)
|
||
if clip.kind == AudioTrackKind.VOICEOVER:
|
||
# 原声入点 = 画面起点(口型就在那里),lead 留白只属于 TTS 配音
|
||
clip.start_ms = int(seg.start_ms) + (0 if clip.native_audio else lead)
|
||
else:
|
||
clip.start_ms = max(0, int(clip.start_ms) + int(seg.start_ms) - int(old))
|
||
elif clip.kind == AudioTrackKind.BGM:
|
||
clip.start_ms = 0
|
||
if clip.loop:
|
||
clip.duration_ms = int(timeline.total_duration_ms)
|
||
|
||
if retime_subtitles:
|
||
conf = timeline.settings_json or {}
|
||
lead = int(conf.get("lead_padding_ms", DEFAULT_LEAD_MS))
|
||
native_ids = {c.shot_id for c in (timeline.audio_clips or [])
|
||
if c.native_audio and c.shot_id}
|
||
planned: list[dict] = []
|
||
for seg in segments:
|
||
if not seg.shot_id:
|
||
continue
|
||
shot = db.get(Shot, seg.shot_id)
|
||
if shot is None or not shot.subtitle_enabled or project is None:
|
||
continue
|
||
# 走原声的镜头:TTS 时长与画面无关,字幕按片段窗口铺满才对得上人声
|
||
native = seg.shot_id in native_ids
|
||
planned.extend(audio.cue_plan(db, project, shot, window_ms=int(seg.duration_ms),
|
||
start_ms=int(seg.start_ms),
|
||
lead_ms=0 if native else lead,
|
||
voice_ms=0 if native else None))
|
||
# ⚠️ 复用旧行 + 先删后插:同一次 flush 里 SQLAlchemy 是先 INSERT 再 DELETE,
|
||
# SQLite 会立刻复用刚释放的自增主键,于是「删除旧字幕」会顺手删掉刚插入的新字幕,
|
||
# 表现为改一次时长/顺序/全局设置之后,时间线字幕整批消失。这里先把多余行删干净
|
||
# (单独 flush 落库),再复用剩余行、只追加缺口部分。
|
||
existing = sorted(timeline.subtitles or [], key=lambda c: int(c.id or 0))
|
||
for stale in existing[len(planned):]:
|
||
db.delete(stale)
|
||
if len(existing) > len(planned):
|
||
db.flush()
|
||
kept = existing[:len(planned)]
|
||
for cue, item in zip(kept, planned):
|
||
cue.shot_id = item["shot_id"]
|
||
cue.start_ms = int(item["start_ms"])
|
||
cue.end_ms = int(item["end_ms"])
|
||
cue.text = item["text"]
|
||
cue.speaker = item.get("speaker") or ""
|
||
for item in planned[len(kept):]:
|
||
timeline.subtitles.append(SubtitleCue(timeline_id=timeline.id, shot_id=item["shot_id"],
|
||
start_ms=int(item["start_ms"]),
|
||
end_ms=int(item["end_ms"]),
|
||
text=item["text"], speaker=item.get("speaker") or ""))
|
||
db.flush()
|
||
db.flush()
|
||
return {"total_duration_ms": timeline.total_duration_ms, "segments": len(segments),
|
||
"subtitles": len(timeline.subtitles or []), "audio_clips": len(timeline.audio_clips or [])}
|
||
|
||
|
||
# ---------------------------------------------------------------- 编辑操作
|
||
def update_segment(db: Session, segment: TimelineSegment, *, project: Project | None = None, **fields) -> dict:
|
||
timeline = db.get(Timeline, segment.timeline_id)
|
||
for key in ("order_index", "label", "source_path", "duration_ms", "source_in_ms", "source_out_ms",
|
||
"transition_out", "transition_duration_ms", "volume", "muted", "shot_id"):
|
||
if key in fields and fields[key] is not None:
|
||
setattr(segment, key, fields[key])
|
||
db.flush()
|
||
result = recompute_timings(db, timeline, project=project)
|
||
db.commit()
|
||
return result
|
||
|
||
|
||
def reorder_segments(db: Session, timeline: Timeline, shot_ids: list[int], *,
|
||
project: Project | None = None) -> dict:
|
||
index = {s.shot_id: s for s in timeline.segments or [] if s.shot_id}
|
||
for pos, sid in enumerate(shot_ids, start=1):
|
||
seg = index.get(sid)
|
||
if seg is not None:
|
||
seg.order_index = pos
|
||
db.flush()
|
||
result = recompute_timings(db, timeline, project=project)
|
||
db.commit()
|
||
return result
|
||
|
||
|
||
def shot_window(timeline: Timeline, shot_id: int | None) -> tuple[int, int]:
|
||
"""镜头片段在时间线上的可见区间(用于人工新增字幕时给出默认时间轴)。"""
|
||
total = int(timeline.total_duration_ms or 0)
|
||
if shot_id is None:
|
||
return 0, max(1000, total)
|
||
segments = sorted([x for x in timeline.segments or [] if x.shot_id == shot_id],
|
||
key=lambda x: (int(x.order_index or 0), int(x.id or 0)))
|
||
if not segments:
|
||
return 0, max(1000, total)
|
||
seg = segments[0]
|
||
start = int(seg.start_ms or 0)
|
||
end = start + int(seg.duration_ms or 0)
|
||
if total > 0:
|
||
end = min(end, total)
|
||
return start, max(start + 400, end)
|
||
|
||
|
||
def set_subtitle(db: Session, timeline: Timeline, *, shot_id: int | None, text: str,
|
||
start_ms: int | None = None, end_ms: int | None = None, speaker: str = "",
|
||
cue_id: int | None = None) -> SubtitleCue:
|
||
"""人工改写字幕。
|
||
|
||
- 指定 cue_id:精确修改那一条字幕(时间线页「编辑」走这条路径,改起始时间不会新增重复行);
|
||
- 指定 start_ms:兼容旧调用,按该时间点命中已有字幕;
|
||
- 都不指定:视为「整条镜头字幕由人工接管」,用新文案替换该镜头/全局的全部自动字幕;
|
||
- 新建字幕但没给时间:落到所属镜头片段区间,避免生成 0 长度、画面里根本看不见的字幕。
|
||
|
||
镜头字幕的文案会同步写回 shot.subtitle_text,否则下一次重排时间轴时
|
||
(recompute_timings 按镜头重新生成字幕)人工改的文案会被自动文案覆盖回去。
|
||
"""
|
||
existing = [c for c in timeline.subtitles or [] if c.shot_id == shot_id]
|
||
if cue_id is not None:
|
||
cue = next((c for c in timeline.subtitles or [] if int(c.id or 0) == int(cue_id)), None)
|
||
if cue is None:
|
||
raise ValueError(f"字幕 {cue_id} 不在该时间线上")
|
||
if not text.strip(): # 清空文案 = 删除这条字幕
|
||
db.delete(cue)
|
||
db.commit()
|
||
return cue
|
||
elif start_ms is not None:
|
||
cue = next((c for c in existing if int(c.start_ms or 0) == int(start_ms)), None)
|
||
else:
|
||
cue = existing[0] if existing else None
|
||
for other in existing[1:] if existing else []:
|
||
db.delete(other)
|
||
db.flush()
|
||
if cue is None:
|
||
default_start, default_end = shot_window(timeline, shot_id)
|
||
cue = SubtitleCue(timeline_id=timeline.id, shot_id=shot_id,
|
||
start_ms=default_start, end_ms=default_end)
|
||
timeline.subtitles.append(cue)
|
||
cue.text = text
|
||
cue.speaker = speaker or cue.speaker
|
||
if start_ms is not None:
|
||
cue.start_ms = int(start_ms)
|
||
if end_ms is not None:
|
||
cue.end_ms = int(end_ms)
|
||
if int(cue.end_ms or 0) <= int(cue.start_ms or 0):
|
||
cue.end_ms = int(cue.start_ms or 0) + 400
|
||
# 该镜头只有一条字幕时才能安全回写:多段字幕回写其中一段会丢掉其余分段
|
||
if cue.shot_id and text.strip() and len([c for c in timeline.subtitles or [] if c.shot_id == cue.shot_id]) <= 1:
|
||
shot = db.get(Shot, cue.shot_id)
|
||
if shot is not None and (shot.subtitle_enabled or not shot.subtitle_text):
|
||
shot.subtitle_text = text
|
||
db.commit()
|
||
return cue
|
||
|
||
|
||
def add_audio_clip(db: Session, timeline: Timeline, *, kind: str, source_path: str, label: str = "",
|
||
start_ms: int = 0, duration_ms: int = 0, gain_db: float = 0.0, loop: bool = False,
|
||
ducking: bool = True, shot_id: int | None = None) -> AudioClip:
|
||
clip = AudioClip(timeline_id=timeline.id, kind=kind, source_path=storage.to_relative(source_path),
|
||
label=label or kind, start_ms=int(start_ms),
|
||
duration_ms=int(duration_ms or _probe_ms(source_path)), gain_db=float(gain_db),
|
||
loop=bool(loop), ducking=bool(ducking), shot_id=shot_id)
|
||
timeline.audio_clips.append(clip)
|
||
db.commit()
|
||
return clip
|
||
|
||
|
||
def refresh_muted_native(db: Session, timeline: Timeline, *, persist: bool = True) -> list[int]:
|
||
"""按 ``native_audio_policy`` 现算「模型原生音轨要整段静音」的镜头,并写回时间线设置。
|
||
|
||
为什么要现算:这一档和别的渲染设置一样可以在时间线面板里随时改,
|
||
但改完不一定会重建时间线;老时间线里也压根没有 ``muted_native_shots`` 这个键。
|
||
合成前重算一次,界面上的三态下拉和真正下发的 filtergraph 才是同一件事。
|
||
"""
|
||
conf = dict(timeline.settings_json or {})
|
||
policy = native_audio_policy(conf)
|
||
ids: list[int] = []
|
||
if policy != "auto":
|
||
for seg in sorted(timeline.segments or [], key=lambda x: (x.order_index, x.id)):
|
||
if not seg.shot_id:
|
||
continue
|
||
if policy == "off":
|
||
ids.append(int(seg.shot_id))
|
||
continue
|
||
shot = db.get(Shot, seg.shot_id) # dialogue_only
|
||
if shot is not None and not shot.has_dialogue:
|
||
ids.append(int(seg.shot_id))
|
||
if conf.get("muted_native_shots") != ids or conf.get("native_audio_policy") != policy:
|
||
conf["muted_native_shots"] = ids
|
||
conf["native_audio_policy"] = policy
|
||
timeline.settings_json = conf # JSON 列要整块赋值才会标脏
|
||
if persist:
|
||
db.commit()
|
||
return ids
|
||
|
||
|
||
def timeline_report(db: Session, timeline: Timeline, *, project: Project | None = None) -> dict:
|
||
"""合成前置检查:素材缺失、语音超长、转场非法、字幕越界等。"""
|
||
project = project or db.get(Project, timeline.project_id)
|
||
issues: list[dict] = []
|
||
segments = sorted(timeline.segments or [], key=lambda s: (s.order_index, s.id))
|
||
for idx, seg in enumerate(segments):
|
||
if not storage.exists(seg.source_path):
|
||
issues.append({"level": "error", "segment_id": seg.id, "message": f"片段 {seg.order_index} 缺少视频文件"})
|
||
shot = db.get(Shot, seg.shot_id) if seg.shot_id else None
|
||
if shot is not None and shot.status != ShotStatus.APPROVED:
|
||
issues.append({"level": "warning", "segment_id": seg.id,
|
||
"message": f"片段 {seg.order_index} 对应的镜头尚未审核合格({shot.status})"})
|
||
if seg.source_out_ms and seg.duration_ms > seg.source_out_ms + 120:
|
||
issues.append({"level": "info", "segment_id": seg.id,
|
||
"message": f"片段 {seg.order_index} 语音长于画面,将定格补帧 {(seg.duration_ms - seg.source_out_ms) / 1000:.1f}s"})
|
||
nxt = segments[idx + 1] if idx + 1 < len(segments) else None
|
||
if nxt is not None and seg.transition_out != Transition.CUT and seg.transition_duration_ms <= 0:
|
||
issues.append({"level": "warning", "segment_id": seg.id,
|
||
"message": f"片段 {seg.order_index} 设置了转场但时长为 0,将按直切处理"})
|
||
for clip in timeline.audio_clips or []:
|
||
if not storage.exists(clip.source_path):
|
||
issues.append({"level": "error", "clip_id": clip.id, "message": f"音轨「{clip.label}」缺少音频文件"})
|
||
elif clip.start_ms + clip.duration_ms > timeline.total_duration_ms + 250 and not clip.loop:
|
||
issues.append({"level": "warning", "clip_id": clip.id,
|
||
"message": f"音轨「{clip.label}」超出时间线末尾,会被裁切"})
|
||
subtitle_total = len(timeline.subtitles or [])
|
||
overflow = sum(1 for c in (timeline.subtitles or []) if c.end_ms > timeline.total_duration_ms + 10)
|
||
if overflow:
|
||
issues.append({"level": "warning", "message": f"{overflow} 条字幕超出成片时长"})
|
||
conf = timeline.settings_json or {}
|
||
if voice_source_mode(conf) != "dub":
|
||
for seg in segments:
|
||
shot = db.get(Shot, seg.shot_id) if seg.shot_id else None
|
||
if shot is None or not str(shot.dialogue or "").strip():
|
||
continue
|
||
if uses_native_voice(shot, conf):
|
||
issues.append({"level": "info", "segment_id": seg.id,
|
||
"message": f"片段 {seg.order_index} 的台词使用模型原声(原生音画同步,不做 TTS 替换)"})
|
||
return {"timeline_id": timeline.id, "total_duration_ms": timeline.total_duration_ms,
|
||
"segments": len(segments), "subtitles": subtitle_total,
|
||
"audio_clips": len(timeline.audio_clips or []),
|
||
"voice_clips": sum(1 for c in timeline.audio_clips or [] if c.kind == AudioTrackKind.VOICEOVER),
|
||
"bgm_clips": sum(1 for c in timeline.audio_clips or [] if c.kind == AudioTrackKind.BGM),
|
||
"burn_subtitle": bool(conf.get("burn_subtitle", True)),
|
||
"subtitle_mode": subtitle_mode_of(conf), "voice_source": voice_source_mode(conf),
|
||
"native_voice_shots": [c.shot_id for c in (timeline.audio_clips or [])
|
||
if c.native_audio and c.shot_id],
|
||
"ready": not any(
|
||
i["level"] == "error" for i in issues), "issues": issues}
|