v 0.1.2
parent
2d58807478
commit
a8348d9edd
|
|
@ -200,8 +200,29 @@ def build_graph(timeline: Timeline, segments: list, seg_files: list[Path], clips
|
|||
keep_audio = bool(conf.get("keep_shot_audio"))
|
||||
parts: list[str] = []
|
||||
# --- 视频:xfade / concat 串接(必要时并行处理音频,保证音画同步)
|
||||
cur = "[0:v]"
|
||||
acur = "[0:a]" if keep_audio else ""
|
||||
#
|
||||
# 每一路先过一遍 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]
|
||||
|
|
@ -211,20 +232,20 @@ def build_graph(timeline: Timeline, segments: list, seg_files: list[Path], clips
|
|||
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}[{idx}:v]xfade=transition={kind}:duration={td / 1000.0:.3f}"
|
||||
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}[{idx}:a]acrossfade=d={td / 1000.0:.3f}:c1=tri:c2=tri{alabel}")
|
||||
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}[{idx}:v][{idx}:a]concat=n=2:v=1:a=1{label}{alabel}")
|
||||
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}[{idx}:v]concat=n=2:v=1:a=0{label}")
|
||||
parts.append(f"{cur}{vsrc[idx]}concat=n=2:v=1:a=0{label}")
|
||||
running += int(nxt.duration_ms)
|
||||
cur = label
|
||||
video_out = cur
|
||||
|
|
|
|||
|
|
@ -229,6 +229,9 @@ def capabilities() -> dict:
|
|||
"libass": bool(burn),
|
||||
"ffmpeg_path": FFMPEG, "ffprobe_path": FFPROBE, "ffmpeg_version": ffmpeg_version(),
|
||||
"xfade": has_filter("xfade"), "concat": has_filter("concat"),
|
||||
# settb/asettb(4.4+):转场与直切混排时把每路链路钉成同一个时间基,
|
||||
# 缺了它 xfade 会直接拒绝配置滤镜图(compose.py 里有实测说明)
|
||||
"timebase": has_filter("settb") and has_filter("asettb"),
|
||||
"loudnorm": has_filter("loudnorm"), "blackdetect": has_filter("blackdetect"),
|
||||
"freezedetect": has_filter("freezedetect"),
|
||||
# 字体:family 给 libass/ASS,path 给 drawtext;两者都必须含中文字形
|
||||
|
|
|
|||
|
|
@ -0,0 +1,187 @@
|
|||
"""转场与直切混排时的链路时间基:xfade 两侧必须一致(任务 #36 的返工)。
|
||||
|
||||
症状:同一条时间线,全直切能出片、全转场能出片,**只要「前面有一处直切、后面还有转场」
|
||||
就必炸**,报错原文(服务端任务 #36 compose):
|
||||
|
||||
[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!
|
||||
|
||||
根因:阶段 1 归一化用 `-video_track_timescale 90000` 写 mp4,解出来的链路时间基是 1/90000;
|
||||
而 `concat` 滤镜的**输出**链路时间基是 ffmpeg 默认的 AV_TIME_BASE(1/1000000)。串接链是
|
||||
「累积链 + 下一路裸输入」,所以一旦 concat 之后又出现 xfade,两侧就变成了
|
||||
1/1000000 vs 1/90000。xfade 要求两侧时间基完全相同,于是滤镜图重配置失败,
|
||||
整条导出退化成「返回码 234 + 空文件」,用户看到的是一句"命令执行失败(返回码 1)"。
|
||||
(这也是为什么它看起来像偶发:炸点取决于转场怎么分布,不取决于素材。)
|
||||
|
||||
口径:每一路进链之前先 `settb=AVTB`(音频 `asettb=AVTB`),把串接链上所有链路钉在同一个
|
||||
时间基上,xfade ↔ concat 怎么交替都成立。
|
||||
|
||||
只有 xfade 严格这件事是量出来的,别误读成"所有混接滤镜都卡时间基"。同一批 90000 素材实测:
|
||||
`[0:a][1:a]concat=v=0:a=1[a1];[a1][2:a]acrossfade` 不给 asettb 也 EXIT=0;
|
||||
48k 归一后的样本与 24k 裸样本直接 amix 也 EXIT=0。音频侧的 asettb 是让整张图只剩一个
|
||||
时间基口径的保险(原生音轨链与视频链同构,视频那一半才是必须的),命门在视频侧。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models import AudioClip, Timeline, TimelineSegment
|
||||
from app.services import compose
|
||||
from app.services import ffmpeg as ff
|
||||
|
||||
_CAPS = ("xfade", "concat", "timebase") # timebase = settb/asettb,ffmpeg 4.4+
|
||||
needs_ffmpeg = pytest.mark.skipif(
|
||||
not ff.available() or not all(ff.capabilities()[c] for c in _CAPS),
|
||||
reason="需要支持 xfade/concat/settb 的 ffmpeg(缺哪个看 /api/system/health)")
|
||||
|
||||
SEG_MS, TD_MS, FPS, W, H, N = 2000, 500, 25, 320, 240, 4
|
||||
|
||||
# 转场分布:三元组依次是「段0→段1 / 段1→段2 / 段2→段3」是否给 500ms 转场。
|
||||
# 最后那列 broken_before 是**去掉 settb 之后实测**的结果(见 test 里的反证),
|
||||
# 只有「直切在前」和「交替」两种分布会炸 —— 与用户那句「有时能生成成功」严丝合缝。
|
||||
SHAPES = {
|
||||
"直切在前": ((False, True, True), True),
|
||||
"转场在前": ((True, False, False), False),
|
||||
"交替": ((True, False, True), True),
|
||||
"全直切": ((False, False, False), False),
|
||||
"全转场": ((True, True, True), False),
|
||||
}
|
||||
|
||||
|
||||
def _timeline(keep_audio: bool, shape: str) -> Timeline:
|
||||
tds, _broken = SHAPES[shape]
|
||||
tl = Timeline(project_id=1, fps=FPS, width=W, height=H,
|
||||
settings_json={"keep_shot_audio": keep_audio})
|
||||
for idx in range(N):
|
||||
seg = TimelineSegment(timeline_id=1, order_index=idx + 1, duration_ms=SEG_MS,
|
||||
source_in_ms=0, source_out_ms=SEG_MS, start_ms=idx * SEG_MS,
|
||||
shot_id=idx + 1)
|
||||
if idx:
|
||||
# transition_duration_ms 挂在「前一段」上:段 idx-1 → 段 idx 的转场
|
||||
tl.segments[idx - 1].transition_duration_ms = TD_MS if tds[idx - 1] else 0
|
||||
tl.segments.append(seg)
|
||||
tl.audio_clips.append(AudioClip(timeline_id=1, kind="voiceover", label="配音",
|
||||
source_path="x.wav", start_ms=200, duration_ms=3000))
|
||||
return tl
|
||||
|
||||
|
||||
def _graph(tl: Timeline, *, keep_audio: bool):
|
||||
"""输入号 0..N-1 是镜头片段,N 是配音缺失时的静音轨(混音总线那边的事)。"""
|
||||
return compose.build_graph(tl, list(tl.segments), [], list(tl.audio_clips), {},
|
||||
total_ms=N * SEG_MS, width=W, height=H, fps=FPS, ass_path=None,
|
||||
target_lufs=-16, audio_inputs=[(N, None, "silence")])
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 图形状:谁都不许是裸输入
|
||||
STEP = re.compile(r"(xfade=|concat=n=|acrossfade=)")
|
||||
|
||||
|
||||
def _inputs_of(part: str) -> list[str]:
|
||||
"""取一步滤镜(xfade / concat / acrossfade)前面的那些输入标签。"""
|
||||
head = part[:STEP.search(part).start()]
|
||||
return re.findall(r"\[([^\]]+)\]", head)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shape", list(SHAPES))
|
||||
@pytest.mark.parametrize("keep", [False, True])
|
||||
def test_chain_inputs_are_timebase_normalized(shape: str, keep: bool):
|
||||
"""`[i:v]` / `[i:a]` 只许出现在 settb 那一行;链上每一步的输入必须是归一化过的标签。"""
|
||||
tds, _broken = SHAPES[shape]
|
||||
graph, _video = _graph(_timeline(keep, shape), keep_audio=keep)
|
||||
parts = graph.split(";")
|
||||
# 每一路(keep_shot_audio 时连音轨一起)都先过一次 settb=AVTB
|
||||
assert len([p for p in parts if "settb=AVTB[" in p]) == N * (2 if keep else 1), shape
|
||||
for part in parts:
|
||||
# 归一化片段那几路(输入号 0..N-1)只许在 settb 行里被引用;
|
||||
# N 之后是配音/BGM/静音轨,它们进的是混音总线,不归这条规矩管
|
||||
head = re.match(r"^\[(\d+):(v|a)\]", part)
|
||||
if head and int(head.group(1)) < N:
|
||||
assert "settb=AVTB[" in part, part
|
||||
# 串接步数:每个接缝一步;带转场且保留原生音轨时,acrossfade 与 xfade 各占一步
|
||||
steps = [p for p in parts if STEP.search(p)]
|
||||
assert len(steps) == (N - 1) + (keep * sum(1 for t in tds if t)), (shape, steps)
|
||||
for part in steps:
|
||||
for label in _inputs_of(part):
|
||||
assert re.fullmatch(r"(vsrc|asrc|v|av)\d+", label), (label, part)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 真实 ffmpeg:五种分布都要出片
|
||||
@pytest.fixture(scope="module")
|
||||
def seg_files(tmp_path_factory) -> list[str]:
|
||||
"""4 段 2s 素材,和阶段 1 一样用 -video_track_timescale 90000 写盘(时间基 1/90000)。"""
|
||||
folder = tmp_path_factory.mktemp("tb-segs")
|
||||
out = []
|
||||
for i in range(N):
|
||||
path = folder / f"seg-{i}.mp4"
|
||||
ff.run([ff.FFMPEG, "-v", "error", "-y",
|
||||
"-f", "lavfi", "-i", f"testsrc=size={W}x{H}:rate={FPS}:duration={SEG_MS / 1000}",
|
||||
"-f", "lavfi", "-i", f"sine=frequency={300 + i * 90}:duration={SEG_MS / 1000}",
|
||||
"-map", "0:v", "-map", "1:a", "-t", f"{SEG_MS / 1000:.3f}",
|
||||
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p",
|
||||
"-c:a", "aac", "-ar", "48000", "-ac", "2",
|
||||
"-video_track_timescale", "90000", str(path)], timeout=300)
|
||||
out.append(str(path))
|
||||
return out
|
||||
|
||||
|
||||
def _render(graph: str, vlabel: str, seg_files: list[str], out) -> subprocess.CompletedProcess:
|
||||
cmd = [ff.FFMPEG, "-v", "error", "-y"]
|
||||
for seg in seg_files:
|
||||
cmd += ["-i", seg]
|
||||
cmd += ["-f", "lavfi", "-t", "8", "-i", "anullsrc=r=48000:cl=stereo",
|
||||
"-filter_complex", graph, "-map", vlabel, "-map", "[aout]",
|
||||
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p",
|
||||
"-c:a", "aac", "-ar", "48000", "-ac", "2", str(out)]
|
||||
return ff.run(cmd, timeout=600, check=False)
|
||||
|
||||
|
||||
def _unfix(graph: str) -> str:
|
||||
"""还原成改前的图:删掉 settb 那一层,链上重新引用裸输入 [i:v]/[i:a]。"""
|
||||
parts = [p for p in graph.split(";") if "settb=AVTB[" not in p]
|
||||
out = []
|
||||
for part in parts:
|
||||
for idx in range(N):
|
||||
part = part.replace(f"[vsrc{idx}]", f"[{idx}:v]").replace(f"[asrc{idx}]", f"[{idx}:a]")
|
||||
out.append(part)
|
||||
return ";".join(out)
|
||||
|
||||
|
||||
def _stream_ms(path, selector: str) -> float:
|
||||
proc = subprocess.run(["ffprobe", "-v", "error", "-select_streams", selector,
|
||||
"-show_entries", "stream=duration", "-of", "csv=p=0", str(path)],
|
||||
capture_output=True, text=True)
|
||||
return float(proc.stdout.strip() or 0) * 1000.0
|
||||
|
||||
|
||||
@needs_ffmpeg
|
||||
@pytest.mark.parametrize("shape", list(SHAPES))
|
||||
@pytest.mark.parametrize("keep", [False, True])
|
||||
def test_every_transition_shape_renders(shape: str, keep: bool, seg_files, tmp_path):
|
||||
"""改后五种转场分布都要出片;改前只有「直切在前 / 交替」两种会炸 —— 两件事一起钉住。"""
|
||||
tds, broken_before = SHAPES[shape]
|
||||
tl = _timeline(keep, shape)
|
||||
graph, vlabel = _graph(tl, keep_audio=keep)
|
||||
|
||||
out = tmp_path / "film.mp4"
|
||||
proc = _render(graph, vlabel, seg_files, out)
|
||||
assert proc.returncode == 0, proc.stderr
|
||||
assert "do not match" not in proc.stderr
|
||||
probe = ff.probe(out)
|
||||
assert (probe.width, probe.height) == (W, H)
|
||||
# 量视频流本身:音频总线按 total_ms 补齐(apad + atrim),容器时长从来不代表画面长度
|
||||
video_ms = _stream_ms(out, "v:0")
|
||||
# 每个转场把时间线缩短 500ms(xfade 的重叠语义),直切不缩短
|
||||
expect_ms = N * SEG_MS - TD_MS * sum(1 for t in tds if t)
|
||||
assert abs(video_ms - expect_ms) < 350, (video_ms, expect_ms, probe.duration, proc.stderr)
|
||||
|
||||
old = tmp_path / "film-before-fix.mp4"
|
||||
before = _render(_unfix(graph), vlabel, seg_files, old)
|
||||
if broken_before:
|
||||
assert before.returncode != 0 and "do not match" in before.stderr, \
|
||||
"改前应当复现任务 #36 的报错;现在却不炸,说明 ffmpeg 或归一化参数变了,口径要重估"
|
||||
else:
|
||||
assert before.returncode == 0, before.stderr
|
||||
Loading…
Reference in New Issue