Files
CenaForge/clipper/tests/test_reframe_insets.py
T
MateuusandClaude Opus 5.5 84f30bbc2d CenaForge: primeira versão pública
Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz,
Clipador de lives e Publicador para YouTube, TikTok e Instagram.

Inclui a leitura das visualizações dos posts para o Dashboard e o botão
"Publicar no <rede>" nos cartões das publicações.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:35:09 -03:00

253 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Reenquadramento do jogo no tempo (keyframes) e camadas inseridas (pedido do dono, 17/11/2026, corte 61)."""
import shutil
import subprocess
from pathlib import Path
import pytest
from app import editor, editor_render, layout, reframe
from .test_editor import make_project, validate
FFMPEG, FFPROBE = shutil.which("ffmpeg"), shutil.which("ffprobe")
needs_ffmpeg = pytest.mark.skipif(not (FFMPEG and FFPROBE), reason="FFmpeg não instalado")
SRC_W, SRC_H = 1280, 720
DOT = (808.0, 308.0) # centro do quadrado branco de 16 px no vídeo de teste
def _project(**over):
base = make_project(source={"sessionId": 1, "lo": 0, "hi": 8}, cuts=[{"start": 0, "end": 4, "mode": None}],
overlays=[], captions={"enabled": False, "preset": "pop", "style": {}, "emphColor": "#22E36B",
"words": []},
progress={"enabled": False}, cover={"enabled": False, "t": 1})
base.update(over)
return base
# ---------------------------------------------------------------- interpolação e validação
def test_interpolation_semantics():
keys = [{"t": 1.0, "cx": 0.2, "ease": "linear"}, {"t": 3.0, "cx": 0.6, "ease": "hold"},
{"t": 5.0, "cx": 0.9, "ease": "smooth"}, {"t": 7.0, "cx": 0.1, "ease": "smooth"}]
assert reframe.interp(keys, 0.0, "cx") == 0.2 # antes do 1º: segura
assert reframe.interp(keys, 2.0, "cx") == pytest.approx(0.4) # linear
assert reframe.interp(keys, 4.99, "cx") == 0.6 and reframe.interp(keys, 5.0, "cx") == 0.9 # hold salta no key seguinte
assert reframe.interp(keys, 6.0, "cx") == pytest.approx(0.5) # smooth: meio exato no meio
assert reframe.interp(keys, 6.5, "cx") < 0.9 - 0.8 * 0.75 + 0.05 # smooth acelera no meio (mais que linear)
assert reframe.interp(keys, 9.0, "cx") == 0.1 # depois do último: segura
def test_window_keeps_aspect_and_stays_inside():
keys = [{"t": 0, "cx": 0.98, "cy": 0.02, "w": 0.4, "ease": "smooth"}]
x, y, w, h = reframe.window(keys, 0, SRC_W, SRC_H, 1080 / 1268)
assert w / h == pytest.approx(1080 / 1268) and x + w <= SRC_W + 1e-6 and y >= 0 and x >= 0
big = [{"t": 0, "cx": 0.5, "cy": 0.5, "w": 1.0, "ease": "smooth"}]
x, y, w, h = reframe.window(big, 0, SRC_W, SRC_H, 1080 / 1920) # zoom que não cabe: encolhe, não deforma
assert h == pytest.approx(SRC_H) and w == pytest.approx(SRC_H * 1080 / 1920)
def test_validation(tmp_path):
keys = [{"t": 3, "cx": 0.5, "cy": 0.5, "w": 0.5}, {"t": 1, "cx": 0.2, "cy": 0.3, "w": 1, "ease": "hold"}]
p = validate(_project(reframe={"keys": keys}), tmp_path)
assert [k["t"] for k in p["reframe"]["keys"]] == [1, 3] and p["reframe"]["keys"][1]["ease"] == "smooth"
assert "reframe" not in validate(_project(), tmp_path) and "reframe" not in validate(_project(reframe={"keys": []}), tmp_path)
inset = {"id": "i", "kind": "inset", "start": 1, "end": 3, "src": {"x": 0.7, "y": 0.0, "w": 0.3, "h": 0.25},
"x": 0.5, "y": 0.3, "size": 0.5, "border": "#ffffff"}
q = validate(_project(overlays=[inset]), tmp_path)["overlays"][0]
assert q["border"] == "#FFFFFF" and q["borderW"] == 6 and q["radius"] == 18 and q["shadow"] is True and q["keys"] == []
bad = [
{"reframe": {"keys": [{"t": 1, "cx": 0.5, "cy": 0.5, "w": 0.01}]}},
{"reframe": {"keys": [{"t": 1, "cx": 0.5, "cy": 0.5, "w": 0.5, "ease": "bounce"}]}},
{"reframe": {"keys": [{"t": 1, "cx": 0.5, "cy": 0.5, "w": 0.5}, {"t": 1.01, "cx": 0.5, "cy": 0.5, "w": 0.5}]}},
{"reframe": {"keys": [{"t": 1 + i * 0.05, "cx": 0.5, "cy": 0.5, "w": 0.5} for i in range(61)]}},
{"overlays": [{**inset, "src": {"x": 0.9, "y": 0, "w": 0.3, "h": 0.2}}]}, # fora do quadro
{"overlays": [{**inset, "size": 0.05}]},
{"overlays": [{**inset, "anim": "spin"}]},
{"overlays": [{**inset, "src": None}]},
{"overlays": [{**inset, "id": f"i{n}"} for n in range(13)]}, # mais de 12 camadas
]
for change in bad:
with pytest.raises(editor.EditError):
validate(_project(**change), tmp_path)
def test_old_project_graph_unchanged_and_inset_not_in_ass(tmp_path):
p = validate(make_project(), tmp_path)
cfg = editor.effective_settings(layout.merge(), p)
graph = editor_render.build_filter(p, cfg, editor.pieces(p["cuts"]), 10.0, "e.ass", [])
assert "perspective" not in graph and "[vraw]" not in graph and "split=2[tl" not in graph
inset = {"id": "i", "kind": "inset", "start": 11, "end": 13, "src": {"x": 0, "y": 0, "w": 0.3, "h": 0.3}}
q = validate(make_project(overlays=[inset]), tmp_path)
assert "Dialogue: 2" not in editor_render.build_project_ass(q, cfg, "")
# ---------------------------------------------------------------- render real
@pytest.fixture(scope="module")
def dot_video(tmp_path_factory):
if not FFMPEG:
pytest.skip("FFmpeg não instalado")
folder = tmp_path_factory.mktemp("reframe")
seg = folder / "dot.ts"
subprocess.run([FFMPEG, "-v", "error", "-f", "lavfi", "-i", f"color=c=0x101010:s={SRC_W}x{SRC_H}:r=30:d=8",
"-f", "lavfi", "-i", "sine=frequency=440:duration=8",
"-vf", "drawbox=x=800:y=300:w=16:h=16:color=white:t=fill", "-c:v", "libx264", "-preset", "ultrafast",
"-crf", "8", "-g", "15", "-c:a", "aac", "-shortest", str(seg)], check=True)
return seg
def _frames(video: Path, size: tuple[int, int]):
import numpy as np
raw = subprocess.run([FFMPEG, "-v", "error", "-i", str(video), "-f", "rawvideo", "-pix_fmt", "gray", "-"],
capture_output=True, check=True).stdout
return np.frombuffer(raw, np.uint8).reshape(-1, size[1], size[0]).astype(float)
def _centroid(frame):
import numpy as np
m = np.where(frame > 128, frame, 0)
s = m.sum()
if not s:
return None
ys, xs = np.mgrid[0:frame.shape[0], 0:frame.shape[1]]
return (m * xs).sum() / s, (m * ys).sum() / s
def _expected(keys, t, dest):
x, y, w, h = reframe.window(keys, t, SRC_W, SRC_H, dest[0] / dest[1])
return (DOT[0] - x) / w * dest[0], (DOT[1] - y) / h * dest[1]
def _render(tmp_path, video, project, settings=None, name="out.mp4"):
p = validate(project, tmp_path, duration=8.0)
out = tmp_path / name
editor_render.render_project(FFMPEG, FFPROBE, ["-c:v", "libx264", "-preset", "ultrafast", "-crf", "12"],
[(video, 0.0, 8.0)], p, layout.merge(settings or {}), "", out, tmp_path)
return p, out
@needs_ffmpeg
@pytest.mark.parametrize("aspect", ["vertical", "horizontal"])
def test_reframe_window_follows_keys_smoothly(tmp_path, dot_video, aspect):
keys = [{"t": 0.0, "cx": 0.5, "cy": 0.5, "w": 1.0, "ease": "smooth"},
{"t": 2.0, "cx": 0.62, "cy": 0.42, "w": 0.3, "ease": "hold"},
{"t": 3.0, "cx": 0.66, "cy": 0.45, "w": 0.45, "ease": "linear"}]
settings = {"format": {"aspect": aspect}, "layout": {"mode": "game_only"}}
_, out = _render(tmp_path, dot_video, _project(reframe={"keys": keys}, settings=settings))
size = layout.FRAME_SIZES[aspect]
frames = _frames(out, size)
for n in (0, 15, 30, 45, 60, 75, 89, 91, 105): # quadro n = sessão n/30 (um trecho só)
got = _centroid(frames[n])
want = _expected(keys, n / 30, size)
assert got and abs(got[0] - want[0]) < 4 and abs(got[1] - want[1]) < 4, (n, got, want)
xs = [_centroid(f)[0] for f in frames[1:60]]
steps = [b - a for a, b in zip(xs, xs[1:])]
assert all(s <= 0.05 for s in steps) or all(s >= -0.05 for s in steps) # sempre no mesmo sentido: sem tremida
jump_before, jump_after = _centroid(frames[89]), _centroid(frames[91]) # hold: salta no key de 3,0 s
assert abs(jump_after[0] - jump_before[0]) > 20
@needs_ffmpeg
def test_reframe_uses_session_time_across_cuts(tmp_path, dot_video):
keys = [{"t": 0.0, "cx": 0.5, "cy": 0.5, "w": 1.0, "ease": "linear"}, {"t": 6.0, "cx": 0.66, "cy": 0.45, "w": 0.35}]
cuts = [{"start": 0.0, "end": 1.5, "mode": None}, {"start": 3.0, "end": 5.0, "mode": None}]
settings = {"layout": {"mode": "game_only"}}
_, out = _render(tmp_path, dot_video, _project(reframe={"keys": keys}, cuts=cuts, settings=settings))
frames = _frames(out, (1080, 1920))
for out_t, session_t in ((0.5, 0.5), (2.0, 3.5), (3.0, 4.5)):
got, want = _centroid(frames[int(out_t * 30)]), _expected(keys, session_t, (1080, 1920))
assert abs(got[0] - want[0]) < 4 and abs(got[1] - want[1]) < 4, (out_t, got, want)
@needs_ffmpeg
def test_reframe_in_cam_top_uses_game_area_and_cover(tmp_path, dot_video):
keys = [{"t": 0.0, "cx": 0.62, "cy": 0.42, "w": 0.3}]
settings = {"layout": {"mode": "cam_top", "cam": {"x": 0.0, "y": 0.0, "w": 0.2, "h": 0.25}}}
p, out = _render(tmp_path, dot_video, _project(reframe={"keys": keys}, settings=settings))
top = layout.cam_panel_height(layout.merge(settings))
got = _centroid(_frames(out, (1080, 1920))[10][top:])
want = _expected(keys, 0, (1080, 1920 - top))
assert abs(got[0] - want[0]) < 4 and abs(got[1] - want[1]) < 4
cover = editor_render.render_cover(FFMPEG, [(dot_video, 0.0, 8.0)], {**p, "cover": {**p["cover"], "t": 1.0}},
layout.merge(settings), "", tmp_path / "cover.jpg")
assert cover.is_file()
@pytest.fixture(scope="module")
def color_video(tmp_path_factory):
if not FFMPEG:
pytest.skip("FFmpeg não instalado")
folder = tmp_path_factory.mktemp("insets")
seg = folder / "colors.ts"
boxes = ("drawbox=x=0:y=0:w=300:h=200:color=0x00FF00:t=fill,"
"drawbox=x=900:y=500:w=380:h=220:color=0x0000FF:t=fill")
subprocess.run([FFMPEG, "-v", "error", "-f", "lavfi", "-i", f"color=c=0xFF0000:s={SRC_W}x{SRC_H}:r=30:d=8",
"-f", "lavfi", "-i", "sine=frequency=440:duration=8", "-vf", boxes, "-c:v", "libx264",
"-preset", "ultrafast", "-crf", "8", "-g", "15", "-c:a", "aac", "-shortest", str(seg)], check=True)
return seg
def _rgb(video: Path, t: float, size: tuple[int, int]):
import numpy as np
raw = subprocess.run([FFMPEG, "-v", "error", "-ss", f"{t:.3f}", "-i", str(video), "-frames:v", "1", "-f", "rawvideo",
"-pix_fmt", "rgb24", "-"], capture_output=True, check=True).stdout
return np.frombuffer(raw, np.uint8).reshape(size[1], size[0], 3).astype(int)
def _is(px, color, tol=60):
return all(abs(int(a) - b) < tol for a, b in zip(px, color))
@needs_ffmpeg
@pytest.mark.parametrize("aspect", ["vertical", "horizontal"])
def test_two_overlapping_insets_with_border_and_anim(tmp_path, color_video, aspect):
green = {"id": "g", "kind": "inset", "start": 0.5, "end": 3.5, "src": {"x": 0.0, "y": 0.0, "w": 0.2344, "h": 0.2778},
"x": 0.25, "y": 0.25, "size": 0.4, "border": "#FFFFFF", "borderW": 8, "radius": 20, "shadow": True, "anim": "fade"}
blue = {"id": "b", "kind": "inset", "start": 1.0, "end": 3.5, "src": {"x": 0.7031, "y": 0.6944, "w": 0.2969, "h": 0.3056},
"x": 0.45, "y": 0.35, "size": 0.4, "border": "#FFFF00", "borderW": 8, "radius": 0, "shadow": False, "anim": "pop"}
settings = {"format": {"aspect": aspect}, "layout": {"mode": "game_only", "game": {"x": 0.35, "y": 0.3, "w": 0.3, "h": 0.4}}}
_, out = _render(tmp_path, color_video, _project(overlays=[green, blue], settings=settings))
w, h = size = layout.FRAME_SIZES[aspect]
frame = _rgb(out, 2.0, size)
gx, gy = int(0.25 * w), int(0.25 * h)
gw = 0.4 * w
gh = gw * (0.2778 * SRC_H) / (0.2344 * SRC_W)
bw = 0.4 * w
bh = bw * (0.3056 * SRC_H) / (0.2969 * SRC_W)
bx0, by0 = 0.45 * w - bw / 2, 0.35 * h - bh / 2
assert _is(frame[gy, int(gx - gw / 2 + 12)], (0, 255, 0)) # camada verde (dentro, longe da azul)
assert _is(frame[gy, int(gx - gw / 2 - 3)], (255, 255, 255)) # borda branca do lado de fora
assert _is(frame[int(by0 + bh / 2), int(0.45 * w)], (0, 0, 255)) # camada azul
ox, oy = int(bx0 + 15), int(by0 + 15) # canto da azul que cai em cima da verde: a azul (última) fica por cima
assert gx - gw / 2 < ox < gx + gw / 2 and gy - gh / 2 < oy < gy + gh / 2
assert _is(frame[oy, ox], (0, 0, 255))
assert _is(frame[int(0.9 * h), int(0.9 * w)], (255, 0, 0)) # fora das camadas: o jogo (vermelho)
early = _rgb(out, 0.56, size)[gy, int(gx - gw / 2 + 12)] # fade: no começo a verde ainda está entrando
assert not _is(early, (0, 255, 0), tol=30)
assert _is(_rgb(out, 3.8, size)[gy, gx], (255, 0, 0)) # depois do fim: some
@needs_ffmpeg
def test_inset_keys_move_it_in_session_time(tmp_path, color_video):
blue = {"id": "b", "kind": "inset", "start": 0.0, "end": 6.0, "src": {"x": 0.7031, "y": 0.6944, "w": 0.2969, "h": 0.3056},
"x": 0.3, "y": 0.5, "size": 0.3, "border": None, "shadow": False, "anim": "none",
"keys": [{"t": 1.0, "x": 0.3, "y": 0.5, "size": 0.3, "ease": "linear"}, {"t": 5.0, "x": 0.7, "y": 0.5, "size": 0.3}]}
cuts = [{"start": 0.0, "end": 2.0, "mode": None}, {"start": 3.0, "end": 6.0, "mode": None}]
settings = {"layout": {"mode": "game_only", "game": {"x": 0.35, "y": 0.3, "w": 0.3, "h": 0.4}}}
_, out = _render(tmp_path, color_video, _project(overlays=[blue], cuts=cuts, settings=settings))
frame = _rgb(out, 2.0, (1080, 1920)) # saída 2,0 s = sessão 3,0 s → x = 0,3 + 0,4 × (2/4) = 0,5
assert _is(frame[960, 540], (0, 0, 255)) and _is(frame[960, int(0.3 * 1080) - 120], (255, 0, 0))
@needs_ffmpeg
def test_sixty_keys_render_through_graph_file(tmp_path, dot_video):
"""60 keyframes num trecho: o grafo passa do limite da linha de comando e vai por arquivo (-/filter_complex)."""
keys = [{"t": i * 0.06, "cx": 0.5 + 0.1 * (i % 2), "cy": 0.5, "w": 0.4 + 0.3 * (i % 3) / 2, "ease": "linear"}
for i in range(60)]
p = validate(_project(reframe={"keys": keys}, settings={"layout": {"mode": "game_only"}}), tmp_path, duration=8.0)
graph = editor_render.build_filter(p, editor.effective_settings(layout.merge(), p), editor.pieces(p["cuts"]), 0.0,
None, [])
assert len(graph) > 20000
_, out = _render(tmp_path, dot_video, _project(reframe={"keys": keys}, settings={"layout": {"mode": "game_only"}}))
got, want = _centroid(_frames(out, (1080, 1920))[100]), _expected(keys, 100 / 30, (1080, 1920))
assert abs(got[0] - want[0]) < 4 and abs(got[1] - want[1]) < 4
assert not list(tmp_path.glob("*.graph.txt")) # arquivo temporário apagado