nex_animation/backend/app/services/compose.py

609 lines
33 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""长视频合成引擎(渲染面):片段归一化 → 转场拼接 → 配音/字幕/配乐混音 → 导出。
两阶段渲染:
1) 归一化:把每个镜头统一到项目画布/帧率/像素格式,产物不足语音长度时定格补帧;
2) 合成:xfade/concat 串接视频,adelay+amix 混入配音与配乐,BGM 侧链闪避,
ASS 烧录字幕,loudnorm 归一响度,最后按导出预设编码。
"""
from __future__ import annotations
import json
import math
import re
import shutil
import time
from pathlib import Path
from sqlalchemy.orm import Session
from app.config import settings
from app.constants import AudioTrackKind, ProjectStatus, Transition
from app.models import AudioClip, ExportRecord, Project, Shot, Timeline, now_utc
from app.services import dispatch, events, storage, timeline as timeline_service
from app.services import ffmpeg as ff
XFADE_MAP = {Transition.DISSOLVE: "dissolve", Transition.FADE_IN: "fade", Transition.FADE_OUT: "fade",
Transition.FADE_THROUGH_BLACK: "fadeblack", Transition.WIPE: "wipeleft"}
class ComposeError(RuntimeError):
pass
# ---------------------------------------------------------------- 字幕方式
# off = 完全不输出字幕
# sidecar = 只在成片旁边产出 .srt 外挂文件(成片内无字幕轨)
# embed = 成片内嵌软字幕轨(mov_text,播放器可开关、画质无损)+ 外挂 .srt
# burn = 字幕烧进画面(硬字幕,任何播放器都能看到)+ 外挂 .srt
# both = 烧进画面 + 内嵌软字幕轨(注意:会自动开字幕的播放器可能看着像双份)
SUBTITLE_MODES = ("off", "sidecar", "embed", "burn", "both")
SUBTITLE_MODE_LABELS = {"off": "不输出字幕", "sidecar": "外挂字幕文件", "embed": "内嵌软字幕轨",
"burn": "烧录进画面", "both": "烧录 + 内嵌软字幕轨"}
def resolve_subtitle_mode(conf: dict | None, *, subtitle_mode: str | None = None,
burn_subtitle: bool | None = None, has_subtitles: bool = True,
container: str = ".mp4") -> dict:
"""把「时间线设置 + 本次请求 + 本机 ffmpeg 能力」折算成最终字幕方式。
优先级:请求显式 subtitle_mode > 请求显式 burn_subtitle >
时间线 subtitle_mode > 时间线 burn_subtitle(兼容老版本)> embed。
本机能力不足时按 burn → embed → sidecar 逐级降级(绝不静默丢字幕),
只有「用户点名要字幕而本机一种都做不出来」才报错。
"""
conf = dict(conf or {})
requested, origin, explicit = "", "时间线默认", False
if subtitle_mode:
requested, origin, explicit = str(subtitle_mode).strip().lower(), "本次导出请求", True
elif burn_subtitle is not None: # 兼容旧接口:true/false 二值
requested, origin, explicit = ("burn" if burn_subtitle else "sidecar"), "本次导出请求", True
elif str(conf.get("subtitle_mode") or "").strip().lower() in SUBTITLE_MODES:
requested, origin = str(conf["subtitle_mode"]).strip().lower(), "时间线设置"
if requested not in SUBTITLE_MODES:
requested = "burn" if conf.get("burn_subtitle", True) else "sidecar"
origin = "时间线设置(burn_subtitle)"
if not has_subtitles:
return {"requested": requested, "mode": "off" if requested == "off" else "sidecar",
"label": SUBTITLE_MODE_LABELS.get(requested, requested), "origin": origin,
"explicit": explicit, "burn": False, "embed": False, "sidecar": requested != "off",
"downgraded": False, "burn_available": ff.can_burn_subtitles(),
"embed_available": ff.can_embed_subtitles(container)}
burn_ok, embed_ok = ff.can_burn_subtitles(), ff.can_embed_subtitles(container)
mode = requested
if requested == "off":
pass
elif requested == "burn" and not burn_ok:
mode = "embed" if embed_ok else "sidecar"
elif requested == "embed" and not embed_ok:
mode = "burn" if burn_ok else "sidecar"
elif requested == "both":
mode = "both" if (burn_ok and embed_ok) else ("burn" if burn_ok else
("embed" if embed_ok else "sidecar"))
return {"requested": requested, "mode": mode,
"label": SUBTITLE_MODE_LABELS.get(mode, mode), "origin": origin, "explicit": explicit,
"burn": mode in ("burn", "both"), "embed": mode in ("embed", "both"),
"sidecar": mode != "off", "downgraded": mode != requested,
"burn_available": burn_ok, "embed_available": embed_ok}
def _slug(text: str) -> str:
text = re.sub(r"[^0-9A-Za-z\u4e00-\u9fff_-]+", "-", (text or "film").strip())
return text.strip("-")[:60] or "film"
def _sec(ms: int) -> str:
return f"{max(0, int(ms)) / 1000.0:.3f}"
def workdir_for(project_id: int, timeline_id: int) -> Path:
folder = storage.media_root() / str(project_id) / "_work" / f"timeline-{timeline_id}"
folder.mkdir(parents=True, exist_ok=True)
return folder
# ---------------------------------------------------------------- 阶段 1:片段归一化
def normalize_segment(project: Project, timeline: Timeline, segment, index: int, work: Path,
*, width: int, height: int) -> tuple[Path, bool]:
"""把单个镜头片段归一化到统一画布/帧率,返回 (文件, 源是否含音轨)。"""
src = storage.to_absolute(segment.source_path)
if not src.exists():
raise ComposeError(f"片段 {segment.order_index} 缺少视频文件:{segment.source_path}")
out = work / f"seg-{index:03d}.mp4"
fps = max(1, int(timeline.fps or project.fps))
seg_ms = int(segment.duration_ms or 0)
if seg_ms <= 0:
raise ComposeError(f"片段 {segment.order_index} 时长为 0")
in_ms = int(segment.source_in_ms or 0)
out_ms = int(segment.source_out_ms or 0)
clip_ms = (out_ms - in_ms) if (out_ms > in_ms) else seg_ms
clip_ms = min(clip_ms, seg_ms)
extra_ms = max(0, seg_ms - clip_ms)
try:
probe = ff.probe(src)
has_audio = bool(probe.has_audio)
except Exception:
probe = None
has_audio = False
# 缩放方向要交给 normalize_filter 挑算法:模型原生出 736x1312、交付 1080x1920
# 时是放大,默认 bilinear 会把画面糊成一层雾,档位白升了。
src_size = (int(probe.width), int(probe.height)) if probe is not None else None
vf = ff.normalize_filter(width, height, fps, src_size=src_size)
if extra_ms > 0 and (timeline.settings_json or {}).get("hold_last_frame", True):
vf += f",tpad=stop_mode=clone:stop_duration={extra_ms / 1000.0:.3f}"
cmd = [ff.FFMPEG, "-v", "error", "-y"]
if in_ms > 0:
cmd += ["-ss", _sec(in_ms)]
cmd += ["-i", str(src)]
if not has_audio:
cmd += ["-f", "lavfi", "-t", _sec(seg_ms), "-i", "anullsrc=r=48000:cl=stereo"]
cmd += ["-map", "0:v:0", "-map", "1:a:0"]
else:
cmd += ["-map", "0:v:0", "-map", "0:a:0"]
cmd += ["-t", _sec(clip_ms + extra_ms),
"-vf", vf,
"-af", f"aresample=48000:async=1,apad",
"-r", str(fps), "-c:v", "libx264", "-preset", "veryfast", "-crf", "22", "-pix_fmt", "yuv420p",
"-c:a", "aac", "-ar", "48000", "-ac", "2", "-b:a", "128k",
"-video_track_timescale", "90000", str(out)]
ff.run(cmd, timeout=1800)
if not out.exists() or out.stat().st_size < 512:
raise ComposeError(f"片段 {segment.order_index} 归一化失败(输出为空)")
return out, has_audio
# ---------------------------------------------------------------- 阶段 2:合成
def native_voice_windows(timeline: Timeline, segments: list) -> list[tuple[float, float]]:
"""走原声台词的镜头在时间线上的区间(秒),供原生总线做逐段静音。"""
native = {int(sid) for sid in ((timeline.settings_json or {}).get("native_voice_shots") or [])}
if not native:
native = {int(c.shot_id) for c in (timeline.audio_clips or [])
if getattr(c, "native_audio", False) and c.shot_id}
if not native:
return []
return [(max(0, int(seg.start_ms or 0)) / 1000.0,
max(0, int(seg.start_ms or 0) + int(seg.duration_ms or 0)) / 1000.0)
for seg in segments if int(seg.shot_id or 0) in native]
def muted_native_windows(timeline: Timeline, segments: list) -> list[tuple[float, float]]:
"""按 ``native_audio_policy`` 需要静音模型原声的镜头区间(秒)。
与 :func:`native_voice_windows` 的区别只在「为什么要静音」:那边是避免
同一段人声混两遍,这边是用户不想要模型自己加的环境声/背景音乐。
效果一样——都是在原生总线上把这几段关掉。
"""
muted = {int(sid) for sid in ((timeline.settings_json or {}).get("muted_native_shots") or [])}
if not muted:
return []
return [(max(0, int(seg.start_ms or 0)) / 1000.0,
max(0, int(seg.start_ms or 0) + int(seg.duration_ms or 0)) / 1000.0)
for seg in segments if int(seg.shot_id or 0) in muted]
def build_graph(timeline: Timeline, segments: list, seg_files: list[Path], clips: list[AudioClip],
clip_files: dict[int, Path], *, total_ms: int, width: int, height: int, fps: int,
ass_path: Path | None, target_lufs: float,
audio_inputs: list[tuple[int, AudioClip | None, str]],
loudnorm_tail: str = "") -> tuple[str, str]:
"""生成 ffmpeg filter_complex。audio_inputs: [(输入序号, clip 或 None, 描述)]。
keep_shot_audio=True 时,镜头片段自带的音轨(MiniMax H3 这类「音画同步一次成片」的模型)
会和视频一起串接:视频 xfade ↔ 音频 acrossfade(重叠时长相同,天然对齐),
直切则用 concat=v=1:a=1。原生音轨随后与配音/BGM 一起进混音总线。
"""
conf = timeline.settings_json or {}
keep_audio = bool(conf.get("keep_shot_audio"))
parts: list[str] = []
# --- 视频:xfade / concat 串接(必要时并行处理音频,保证音画同步)
#
# 每一路先过一遍 settb=AVTB 再进链,这一步不是装饰,是「转场 + 直切混排」的命门:
# 阶段 1 用 -video_track_timescale 90000 写 mp4,解出来链路时间基是 1/90000;
# 而 concat 滤镜的输出链路时间基是 ffmpeg 默认的 AV_TIME_BASE(1/1000000)。
# 链是「累积链 + 下一路裸输入」,所以只要时间线里「前面某处直切、后面还有转场」,
# 那条 xfade 就会拿到 main=1/1000000、second=1/90000,它要求两侧时间基完全一致,
# 直接拒绝配置滤镜图(任务 #36 的原文):
# [Parsed_xfade_2] First input link main timebase (1/1000000) do not match
# the corresponding second input link xfade timebase (1/90000)
# Failed to configure output pad on Parsed_xfade_2 / Error reinitializing filters!
# 症状随转场分布而变,不随素材变(五种分布实测:直切在前/交替必炸,转场在前/全直切/
# 全转场都出片 —— 逐条钉在 tests/test_compose_transition_timebase.py),所以绝不该靠运气。
# 统一成 AVTB 之后 xfade ↔ concat 任意交替都成立。音频那一路的 asettb 是保险不是命门:
# 实测 acrossfade 与 amix 都不校验时间基(concat 输出直接接 acrossfade、48k 归一混 24k
# 裸样本都 EXIT=0),但原生音轨链与视频链同构,顺手钉成同一个口径,免得将来换滤镜再踩。
vsrc = [f"[vsrc{i}]" for i in range(len(segments))]
asrc = [f"[asrc{i}]" for i in range(len(segments))]
for idx in range(len(segments)):
parts.append(f"[{idx}:v]settb=AVTB{vsrc[idx]}")
if keep_audio:
parts.append(f"[{idx}:a]asettb=AVTB{asrc[idx]}")
cur = vsrc[0]
acur = asrc[0] if keep_audio else ""
running = int(segments[0].duration_ms)
for idx in range(1, len(segments)):
prev = segments[idx - 1]
nxt = segments[idx]
td = int(prev.transition_duration_ms or 0)
label = f"[v{idx}]"
if td > 60:
kind = XFADE_MAP.get(prev.transition_out or Transition.DISSOLVE, "dissolve")
offset = max(0.0, (running - td) / 1000.0)
parts.append(f"{cur}{vsrc[idx]}xfade=transition={kind}:duration={td / 1000.0:.3f}"
f":offset={offset:.3f}{label}")
if keep_audio:
alabel = f"[av{idx}]"
parts.append(f"{acur}{asrc[idx]}acrossfade=d={td / 1000.0:.3f}:c1=tri:c2=tri{alabel}")
acur = alabel
running = running + int(nxt.duration_ms) - td
else:
if keep_audio:
alabel = f"[av{idx}]"
parts.append(f"{cur}{acur}{vsrc[idx]}{asrc[idx]}concat=n=2:v=1:a=1{label}{alabel}")
acur = alabel
else:
parts.append(f"{cur}{vsrc[idx]}concat=n=2:v=1:a=0{label}")
running += int(nxt.duration_ms)
cur = label
video_out = cur
if ass_path is not None:
parts.append(f"{cur}{ff.subtitle_filter() or 'ass'}=filename='"
+ ff.escape_filter_path(ass_path) + "'[vsub]")
video_out = "[vsub]"
# --- 音频:配音/BGM/音效 + 侧链闪避 + 响度归一
voice_labels: list[str] = []
bgm_labels: list[str] = []
other_labels: list[str] = []
for stream_no, clip, desc in audio_inputs:
if clip is None:
parts.append(f"[{stream_no}:a]anull[silence{stream_no}]")
other_labels.append(f"[silence{stream_no}]")
continue
chain = [f"[{stream_no}:a]"]
if clip.loop:
chain.append(f"atrim=0:{total_ms / 1000.0:.3f},asetpts=PTS-STARTPTS,")
elif clip.kind == AudioTrackKind.SFX and int(clip.duration_ms or 0) > 0:
# 镜头级音效只属于它自己那一镜:素材比画面长就必须按配置裁,
# 否则键盘声会拖进下一个镜头的台词里。
chain.append(f"atrim=0:{int(clip.duration_ms) / 1000.0:.3f},asetpts=PTS-STARTPTS,")
if abs(float(clip.gain_db or 0)) > 0.05:
chain.append(f"volume={float(clip.gain_db):.1f}dB,")
if clip.start_ms > 0:
chain.append(f"adelay={int(clip.start_ms)}:all=1,")
else:
chain.append("adelay=0:all=1,")
chain.append("aresample=48000,apad")
tag = f"a{stream_no}"
parts.append("".join(chain) + f"[{tag}]")
if clip.kind == AudioTrackKind.VOICEOVER:
voice_labels.append(f"[{tag}]")
elif clip.kind == AudioTrackKind.BGM:
bgm_labels.append(f"[{tag}]")
else:
other_labels.append(f"[{tag}]")
def mix(labels: list[str], out: str, *, normalize: str = "0") -> str | None:
if not labels:
return None
if len(labels) == 1:
return f"{labels[0]}anull[{out}]"
return "".join(labels) + f"amix=inputs={len(labels)}:duration=longest:normalize={normalize}[{out}]"
mix_parts: list[str] = []
voice_bus = mix(voice_labels, "voicebus")
if voice_bus:
mix_parts.append(voice_bus)
bgm_bus = mix(bgm_labels, "bgmbus")
if bgm_bus:
mix_parts.append(bgm_bus)
final_sources: list[str] = []
if bgm_bus and voice_bus and any(c.ducking for c in clips if c.kind == AudioTrackKind.BGM):
mix_parts.append("[bgmbus][voicebus]sidechaincompress=threshold=0.03:ratio=8:attack=25:"
f"release=420:makeup=1[bgmduck]")
final_sources.append("[bgmduck]")
final_sources.append("[voicebus]")
else:
if bgm_bus:
final_sources.append("[bgmbus]")
if voice_bus:
final_sources.append("[voicebus]")
final_sources.extend(other_labels)
if keep_audio:
# 镜头自带音轨:轻微垫底后与配音/BGM 同混,避免盖住人声
chain = (f"{acur}aresample=48000,"
f"volume={float(conf.get('shot_audio_gain_db', -2.0)):.1f}dB")
# 台词已经用原声的镜头,那份人声已经作为配音轨进来了;原生总线在这几段
# 必须静音,否则同一段语音混两遍(+6dB 且有梳状回声)。
# 再并上「按音轨策略要静音」的镜头(模型自己加的背景音乐、无台词镜头的乱语)。
windows = sorted(set(native_voice_windows(timeline, segments))
| set(muted_native_windows(timeline, segments)), key=lambda w: w[0])
if windows:
enable = "+".join(f"between(t,{a:.3f},{b:.3f})" for a, b in windows)
chain += f",volume=0:enable='{enable}'"
parts.append(chain + "[nativebus]")
final_sources.append("[nativebus]")
if not final_sources:
raise ComposeError("时间线没有任何音频输入")
if len(final_sources) > 1:
mix_parts.append("".join(final_sources) +
f"amix=inputs={len(final_sources)}:duration=longest:normalize=0[mixbus]")
mixed = "[mixbus]"
else:
mixed = final_sources[0]
mix_parts.append(f"{mixed}loudnorm=I={target_lufs:.1f}:TP=-1.5:LRA=11{loudnorm_tail},"
f"atrim=0:{total_ms / 1000.0:.3f},asetpts=PTS-STARTPTS[aout]")
return ";".join(parts + mix_parts), video_out
def _without_loglevel(args: list[str]) -> list[str]:
"""去掉调用方传入的 -v/-loglevel(合成主命令是 -v error,会吞掉 loudnorm 的测量报告)。"""
out: list[str] = []
i = 0
while i < len(args):
if args[i] in ("-v", "-loglevel") and i + 1 < len(args):
i += 2
continue
out.append(args[i])
i += 1
return out
def loudnorm_measure(cmd: list[str], graph: str, vlabel: str, total_ms: int) -> dict:
"""第一遍:跑同一张混音图只测量响度(loudnorm print_format=json)。
ffmpeg 的单遍 loudnorm 是动态归一,十几秒的短片经常偏离目标 2~3 LUFS
(实测 20s 成片目标 -16、落点 -18.7)。先测量再把实测值喂给
linear=true 的第二遍,整片响度才能稳定命中 EBU R128 目标。
"""
proc = ff.run([cmd[0], "-loglevel", "info"] + _without_loglevel(cmd[1:])
+ ["-filter_complex", graph, "-map", vlabel, "-map", "[aout]",
"-t", _sec(total_ms), "-f", "null", "-"], timeout=2400, check=False)
text = (proc.stderr or "") + (proc.stdout or "")
match = re.search(r"\{[^{}]*\"input_i\"[^{}]*\}", text, re.S)
if proc.returncode != 0 or not match:
raise ComposeError(f"响度测量失败:{text[-200:]}")
measured = json.loads(match.group(0))
values = {k: float(measured[k]) for k in
("input_i", "input_tp", "input_lra", "input_thresh", "target_offset")}
if not all(math.isfinite(v) for v in values.values()):
raise ComposeError(f"响度测量结果不可用:{measured}")
return values
def loudnorm_tail(values: dict) -> str:
"""把实测值拼成第二遍 loudnorm 的参数串(精确线性归一)。"""
return (f":measured_I={values['input_i']:.2f}:measured_TP={values['input_tp']:.2f}"
f":measured_LRA={values['input_lra']:.2f}:measured_thresh={values['input_thresh']:.2f}"
f":offset={values['target_offset']:.2f}:linear=true")
def _precheck_failed(db: Session, project: Project, timeline: Timeline, message: str,
*, actor: str = "studio") -> None:
"""预检阶段(尚未创建导出记录)失败:先留痕再抛错,避免前端只看到任务失败却不知原因。"""
events.log_event(db, "compose.failed", project_id=project.id, level="error",
message=f"合成失败:{message[:300]}", entity_type="timeline",
entity_id=timeline.id if timeline else None, actor=actor)
db.commit()
raise ComposeError(message)
def render(db: Session, project: Project, timeline: Timeline, *, preset: str = "web_1080p",
burn_subtitle: bool | None = None, subtitle_mode: str | None = None, name: str = "",
task=None, actor: str = "studio") -> dict:
"""执行整片合成并导出成片。"""
if not ff.available():
_precheck_failed(db, project, timeline, "本机缺少 ffmpeg/ffprobe,无法合成成片", actor=actor)
# 音轨策略可能在面板里刚被改过、但时间线没重建:这里现算一次静音镜头
timeline_service.refresh_muted_native(db, timeline)
report = timeline_service.timeline_report(db, timeline, project=project)
if not report["ready"]:
_precheck_failed(db, project, timeline,
"时间线尚未就绪:" + ";".join(i["message"] for i in report["issues"]
if i["level"] == "error")[:600], actor=actor)
segments = sorted(timeline.segments or [], key=lambda s: (s.order_index, s.id))
if not segments:
_precheck_failed(db, project, timeline, "时间线没有任何片段", actor=actor)
conf = dict(timeline.settings_json or {})
preset_conf = ff.EXPORT_PRESETS.get(preset, ff.EXPORT_PRESETS["web_1080p"])
width = int(preset_conf.get("width") or timeline.width)
height = int(preset_conf.get("height") or timeline.height)
base_w, base_h = int(timeline.width or width), int(timeline.height or height)
if base_w > 0 and base_h > 0 and abs(base_w / base_h - width / height) > 0.05:
# 画幅与导出预设不一致时,保持项目画幅并缩放进预设的分辨率框内
scale = min(width / base_w, height / base_h)
width = max(2, int(base_w * scale) // 2 * 2)
height = max(2, int(base_h * scale) // 2 * 2)
fps = max(1, int(timeline.fps or project.fps))
total_ms = int(timeline.total_duration_ms or sum(s.duration_ms for s in segments))
folder = storage.project_dir(project.id, "exports")
stamp = time.strftime("%Y%m%d-%H%M%S")
title = name or f"{project.name}-v{timeline.version}-{stamp}"
out_path = folder / f"{_slug(project.name)}-v{timeline.version}-{stamp}-{preset}.mp4"
sub = resolve_subtitle_mode(conf, subtitle_mode=subtitle_mode, burn_subtitle=burn_subtitle,
has_subtitles=bool(timeline.subtitles), container=out_path.suffix)
burn, embed = sub["burn"], sub["embed"]
if sub["downgraded"] and sub["explicit"] and not sub["burn_available"] \
and not sub["embed_available"] and sub["requested"] != "off":
# 用户点名要成片里有字幕,而本机既烧不了也内嵌不了 → 明确报错,绝不静默产出无字幕成片
_precheck_failed(db, project, timeline,
"本机 ffmpeg 既不支持字幕烧录滤镜(subtitles/ass,需要 libass 构建),"
"也不支持内嵌字幕(mov_text)。请设置 FFMPEG_BIN 指向带 libass 的 ffmpeg,"
"或关闭字幕后用外挂 .srt 交付", actor=actor)
if timeline.subtitles and sub["downgraded"]:
events.log_event(db, "compose.subtitle_skipped", project_id=project.id, level="warn",
message=f"{sub['origin']}要求「{SUBTITLE_MODE_LABELS.get(sub['requested'], sub['requested'])}」,"
f"本机 ffmpeg 不支持(烧录={'可用' if sub['burn_available'] else '缺失'} / "
f"内嵌={'可用' if sub['embed_available'] else '缺失'}),"
f"已自动改为「{sub['label']}」",
entity_type="timeline", entity_id=timeline.id, actor=actor,
data={"requested": sub["requested"], "mode": sub["mode"],
"ffmpeg_path": ff.FFMPEG})
record = ExportRecord(project_id=project.id, timeline_id=timeline.id, task_id=task.id if task else None,
name=title, preset=preset, status="running", started_at=now_utc(),
subtitle_burned=bool(burn and timeline.subtitles))
db.add(record)
db.flush()
events.log_event(db, "compose.started", project_id=project.id, task_id=record.task_id,
message=f"开始合成整片({len(segments)} 段 / {total_ms / 1000:.1f}s / {preset})",
entity_type="timeline", entity_id=timeline.id, actor=actor)
work = workdir_for(project.id, timeline.id)
seg_files: list[Path] = []
try:
# ---- 阶段 1
for idx, seg in enumerate(segments):
seg_file, _ = normalize_segment(project, timeline, seg, idx, work, width=width, height=height)
seg_files.append(seg_file)
if task is not None:
dispatch.heartbeat(db, task, progress=0.1 + 0.55 * (idx + 1) / max(1, len(segments)),
note=f"归一化片段 {idx + 1}/{len(segments)}")
db.commit()
# ---- 字幕:始终产出 SRT;本机支持字幕滤镜时才额外烧录
ass_path = None
srt_path = None
if timeline.subtitles:
cues = [{"start_ms": c.start_ms, "end_ms": c.end_ms, "text": c.text, "speaker": c.speaker}
for c in timeline.subtitles or []]
# 外挂 SRT 始终留一份(内嵌模式的源文件也用它);off 模式才完全不产出
if sub["sidecar"]:
srt_path = ff.write_srt(cues, folder / (out_path.stem + ".srt"))
if burn:
ass_path = ff.write_ass(cues, work / "subtitles.ass", width=width, height=height,
style=conf.get("subtitle_style") or {})
# ---- 阶段 2 输入
cmd = [ff.FFMPEG, "-v", "error", "-y"]
audio_inputs: list[tuple[int, AudioClip | None, str]] = []
for seg_file in seg_files:
cmd += ["-i", str(seg_file)]
stream_no = len(seg_files)
clip_files: dict[int, Path] = {}
for clip in timeline.audio_clips or []:
path = storage.to_absolute(clip.source_path)
if not path.exists():
continue
if clip.kind == AudioTrackKind.BGM and clip.loop:
cmd += ["-stream_loop", "-1"]
cmd += ["-i", str(path)]
clip_files[stream_no] = path
audio_inputs.append((stream_no, clip, clip.label))
stream_no += 1
if not audio_inputs:
cmd += ["-f", "lavfi", "-t", _sec(total_ms + 200), "-i", "anullsrc=r=48000:cl=stereo"]
audio_inputs.append((stream_no, None, "silence"))
# 静音兜底也占一路输入号:漏了这一步,下面字幕输入会拿到同一个编号,
# ffmpeg 于是拿 -map 2:s:0 去静音流上找字幕轨,导出直接失败
# (没有 BGM + 内嵌字幕的成片必炸,自检实测返回码 234)。
stream_no += 1
# 内嵌软字幕:把 SRT 作为额外一路输入,最后 -map 成字幕轨(省一次 remux)
sub_stream_no = None
if embed and srt_path is not None:
cmd += ["-i", str(srt_path)]
sub_stream_no = stream_no
stream_no += 1
graph_kwargs = dict(total_ms=total_ms + 200, width=width, height=height, fps=fps,
ass_path=ass_path, target_lufs=float(conf.get("target_lufs", settings.target_lufs)),
audio_inputs=audio_inputs)
clips_arg = list(timeline.audio_clips or [])
graph, vlabel = build_graph(timeline, segments, seg_files, clips_arg, clip_files, **graph_kwargs)
# 两遍响度归一:先测量,再把实测值喂给第二遍(linear=true)。测量失败退回单遍,不拖垮导出。
loud_note = ""
try:
measured = loudnorm_measure(
cmd, build_graph(timeline, segments, seg_files, clips_arg, clip_files,
loudnorm_tail=":print_format=json", **graph_kwargs)[0],
vlabel, total_ms)
graph = build_graph(timeline, segments, seg_files, clips_arg, clip_files,
loudnorm_tail=loudnorm_tail(measured), **graph_kwargs)[0]
loud_note = (f"响度两遍归一:实测混音 {measured['input_i']:.1f} LUFS → 目标 "
f"{graph_kwargs['target_lufs']:.1f} LUFS(linear=true)")
except Exception as exc: # noqa: BLE001 兜底路径,保留单遍动态归一
loud_note = f"响度两遍归一降级为单遍动态归一:{exc}"
cmd += ["-filter_complex", graph, "-map", vlabel, "-map", "[aout]", "-t", _sec(total_ms)]
if sub_stream_no is not None:
codec = ff.subtitle_codec(out_path.suffix) or "mov_text"
lang = str(conf.get("subtitle_language") or "chi").strip()[:3] or "chi"
cmd += ["-map", f"{sub_stream_no}:s:0", "-c:s", codec,
"-metadata:s:s:0", f"language={lang}"]
cmd += ff.export_args(out_path, crf=int(conf.get("crf", preset_conf["crf"])),
preset=str(conf.get("encoder_preset", preset_conf["preset"])),
audio_bitrate=str(preset_conf["audio_bitrate"]),
threads=int(settings.ffmpeg_threads),
shortest=sub_stream_no is None)
if task is not None:
dispatch.heartbeat(db, task, progress=0.75, note="拼接与混音")
db.commit()
ff.run(cmd, timeout=5400)
if not out_path.exists() or out_path.stat().st_size < 1024:
raise ComposeError("合成输出为空,已保留中间产物供排查")
# ---- 校验与登记
probe = ff.probe(out_path)
drift = abs(probe.duration * 1000 - total_ms) / max(1, total_ms)
record.status = "succeeded"
record.path = storage.to_relative(out_path)
record.size_bytes = out_path.stat().st_size
record.duration_ms = int(round(probe.duration * 1000))
record.width, record.height, record.fps = probe.width, probe.height, int(round(probe.fps))
record.checksum = ff.checksum(out_path)
record.finished_at = now_utc()
notes: list[str] = [] if drift < 0.06 else [
f"成片时长偏差 {drift * 100:.1f}%,请检查转场与补帧设置"]
if loud_note:
notes.append(loud_note)
if sub_stream_no is not None and not probe.subtitle_tracks:
notes.append("成片未检测到内嵌字幕轨,请检查 ffmpeg 是否支持 mov_text")
record.error_message = ";".join(notes)
storage.register_asset(db, project_id=project.id, owner_type="project", owner_id=project.id,
kind="output", path=record.path, name=title, source="derive",
info={"path": record.path, "size_bytes": record.size_bytes,
"duration_ms": record.duration_ms, "width": record.width,
"height": record.height, "mime": "video/mp4",
"original_name": out_path.name},
metadata={"preset": preset, "timeline_id": timeline.id,
"timeline_version": timeline.version, "drift": round(drift, 4)})
project.status = ProjectStatus.DELIVERED
project.current_stage = "export"
if task is not None:
dispatch.mark_succeeded(db, task, output_path=record.path,
result={"duration_ms": record.duration_ms, "preset": preset,
"size_bytes": record.size_bytes, "drift": round(drift, 4)})
events.log_event(db, "compose.succeeded", project_id=project.id, task_id=record.task_id,
message=f"成片导出完成:{record.duration_ms / 1000:.1f}s / "
f"{record.width}x{record.height} / {record.size_bytes / 1024 / 1024:.1f}MB",
entity_type="export", entity_id=record.id, actor=actor,
data={"path": record.path, "preset": preset, "subtitle_mode": sub["mode"],
"subtitle_tracks": len(probe.subtitle_tracks)})
db.commit()
if not conf.get("keep_intermediate", False):
shutil.rmtree(work, ignore_errors=True)
return {"export_id": record.id, "path": record.path, "url": storage.url_for(record.path),
"duration_ms": record.duration_ms, "size_bytes": record.size_bytes,
"width": record.width, "height": record.height, "fps": record.fps,
"preset": preset, "drift": round(drift, 4), "subtitle_burned": record.subtitle_burned,
"subtitle_mode": sub["mode"], "subtitle_embedded": bool(probe.subtitle_tracks),
"subtitle_tracks": len(probe.subtitle_tracks),
"subtitle_file": storage.url_for(srt_path) if srt_path else ""}
except Exception as exc:
record.status = "failed"
record.error_message = str(exc)[:4000]
record.finished_at = now_utc()
events.log_event(db, "compose.failed", project_id=project.id, task_id=record.task_id, level="error",
message=f"合成失败:{str(exc)[:300]}", entity_type="timeline", entity_id=timeline.id,
actor=actor)
db.commit()
raise
finally:
if task is not None:
db.commit()