103 lines
4.5 KiB
Python
103 lines
4.5 KiB
Python
|
|
"""成片响度归一(EBU R128):两遍 loudnorm 必须命中目标响度。
|
|||
|
|
|
|||
|
|
背景(验收 8.4 实测踩到):单遍 loudnorm 是动态归一,十几秒的短片实测目标 -16
|
|||
|
|
LUFS 只落到 -18.5 LUFS,成片偏轻、多个项目之间响度不一致。合成前先跑一遍只测量,
|
|||
|
|
再把实测值喂给第二遍(linear=true),整片响度才稳定命中目标。
|
|||
|
|
"""
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import re
|
|||
|
|
import subprocess
|
|||
|
|
|
|||
|
|
import pytest
|
|||
|
|
|
|||
|
|
from app.services import compose
|
|||
|
|
from app.services import ffmpeg as ff
|
|||
|
|
|
|||
|
|
needs_ffmpeg = pytest.mark.skipif(not ff.available() or not ff.capabilities()["loudnorm"],
|
|||
|
|
reason="需要支持 loudnorm 的 ffmpeg")
|
|||
|
|
|
|||
|
|
PULSED = ("aevalsrc=0.9*sin(2*PI*440*t)*between(mod(t\\,1.2)\\,0\\,0.15)"
|
|||
|
|
":d=6:s=48000") # 短促爆音 + 大段留白:单遍动态归一最容易在这类素材上跑偏
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _base_cmd() -> list[str]:
|
|||
|
|
return [ff.FFMPEG, "-v", "error", "-y",
|
|||
|
|
"-f", "lavfi", "-i", "color=c=black:s=64x64:r=25:d=6",
|
|||
|
|
"-f", "lavfi", "-i", PULSED]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _graph(tail: str = "") -> str:
|
|||
|
|
return f"[1:a]loudnorm=I=-16.0:TP=-1.5:LRA=11{tail},atrim=0:6.000,asetpts=PTS-STARTPTS[aout]"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_without_loglevel_strips_only_log_flags():
|
|||
|
|
args = ["-v", "error", "-y", "-i", "a.mp4", "-loglevel", "warning", "-t", "3"]
|
|||
|
|
assert compose._without_loglevel(args) == ["-y", "-i", "a.mp4", "-t", "3"]
|
|||
|
|
def test_loudnorm_measure_parses_report_and_builds_tail(monkeypatch):
|
|||
|
|
report = ('{ "input_i" : "-9.42", "input_tp" : "-3.10", "input_lra" : "4.20",\n'
|
|||
|
|
' "input_thresh" : "-20.55", "target_offset" : "-0.31" }')
|
|||
|
|
seen: dict = {}
|
|||
|
|
|
|||
|
|
class Proc:
|
|||
|
|
returncode = 0
|
|||
|
|
stderr = report
|
|||
|
|
stdout = ""
|
|||
|
|
|
|||
|
|
def fake_run(cmd, **kw):
|
|||
|
|
seen["cmd"] = cmd
|
|||
|
|
return Proc()
|
|||
|
|
|
|||
|
|
monkeypatch.setattr(ff, "run", fake_run)
|
|||
|
|
measured = compose.loudnorm_measure(_base_cmd(), _graph(":print_format=json"), "[0:v]", 3000)
|
|||
|
|
assert measured["input_i"] == pytest.approx(-9.42)
|
|||
|
|
# 测量必须自己抬升日志级别:主命令是 -v error,会把 loudnorm 的测量报告整段吞掉
|
|||
|
|
assert seen["cmd"][:3] == [ff.FFMPEG, "-loglevel", "info"]
|
|||
|
|
assert "-v" not in seen["cmd"] and "error" not in seen["cmd"]
|
|||
|
|
tail = compose.loudnorm_tail(measured)
|
|||
|
|
assert "measured_I=-9.42" in tail and "linear=true" in tail
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_loudnorm_measure_raises_on_silence(monkeypatch):
|
|||
|
|
class Proc:
|
|||
|
|
returncode = 0
|
|||
|
|
stderr = ('{ "input_i" : "-inf", "input_tp" : "-inf", "input_lra" : "0.00",'
|
|||
|
|
' "input_thresh" : "-inf", "target_offset" : "0.00" }')
|
|||
|
|
stdout = ""
|
|||
|
|
|
|||
|
|
monkeypatch.setattr(ff, "run", lambda *a, **k: Proc())
|
|||
|
|
with pytest.raises(compose.ComposeError):
|
|||
|
|
compose.loudnorm_measure(_base_cmd(), _graph(":print_format=json"), "[0:v]", 3000)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_build_graph_embeds_measured_loudnorm_params():
|
|||
|
|
"""两遍归一的落点:第二遍的 filtergraph 必须带上实测值 + linear=true。"""
|
|||
|
|
tail = ":measured_I=-9.42:measured_TP=-3.10:measured_LRA=4.20:" \
|
|||
|
|
":measured_thresh=-20.55:offset=-0.31:linear=true"
|
|||
|
|
assert tail not in _graph()
|
|||
|
|
assert tail in _graph(tail)
|
|||
|
|
|
|||
|
|
|
|||
|
|
@needs_ffmpeg
|
|||
|
|
def test_two_pass_loudnorm_hits_target(tmp_path):
|
|||
|
|
"""同一段「爆音 + 留白」素材:两遍归一命中目标,单遍动态归一明显跑偏。"""
|
|||
|
|
cmd = _base_cmd()
|
|||
|
|
measured = compose.loudnorm_measure(cmd, _graph(":print_format=json"), "0:v", 6000)
|
|||
|
|
assert measured["input_i"] < -6.0 and measured["input_tp"] > -3.0
|
|||
|
|
|
|||
|
|
def render(tail: str, name: str) -> float:
|
|||
|
|
out = tmp_path / name
|
|||
|
|
subprocess.run(cmd + ["-filter_complex", _graph(tail), "-map", "0:v", "-map", "[aout]",
|
|||
|
|
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p",
|
|||
|
|
str(out)], check=True, capture_output=True)
|
|||
|
|
text = subprocess.run([ff.FFMPEG, "-hide_banner", "-nostats", "-i", str(out),
|
|||
|
|
"-filter_complex", "ebur128=framelog=quiet", "-f", "null", "-"],
|
|||
|
|
capture_output=True, text=True).stderr
|
|||
|
|
return float(re.findall(r"I:\s*(-?\d+\.?\d*)\s*LUFS", text)[-1])
|
|||
|
|
|
|||
|
|
one_pass = render("", "one.mp4")
|
|||
|
|
two_pass = render(compose.loudnorm_tail(measured), "two.mp4")
|
|||
|
|
assert abs(two_pass - (-16.0)) <= 0.5, f"两遍归一偏离目标 -16 LUFS:实测 {two_pass} LUFS"
|
|||
|
|
assert abs(one_pass - (-16.0)) > abs(two_pass - (-16.0)) + 0.3, \
|
|||
|
|
f"单遍动态归一应明显更偏:单遍 {one_pass} vs 两遍 {two_pass}"
|