"""Camadas novas do editor (17/11/2026): câmera em tela cheia por trecho, trecho mudo, texto com posição livre, chat da live na tela e capa nos primeiros quadros.""" from pathlib import Path from app import editor, editor_render, layout from app.scoring import ChatMessage from .test_editor import make_project, validate CAM = {"layout": {"cam": {"x": 0.02, "y": 0.45, "w": 0.2, "h": 0.25}}} def test_old_projects_get_defaults_for_new_fields(tmp_path): p = validate(make_project(), tmp_path) assert p["chat"]["enabled"] is False and p["chat"]["lines"] == 5 assert p["cover"]["atStart"] is False assert all(c["muted"] is False for c in p["cuts"]) assert p["overlays"][0]["x"] == 0.5 def test_cam_only_layout_fills_the_screen_with_the_camera(): cfg = layout.merge(CAM) chain = layout.layout_chain(cfg, "in", "out", mode="cam_only") assert "crop=iw*0.2:ih*0.25:iw*0.02:ih*0.45" in chain and "vstack" not in chain and "boxblur" not in chain assert f"crop={layout.W}:{layout.H}" in chain # sem câmera marcada não há o que mostrar: cai no desfoque assert "boxblur" in layout.layout_chain(layout.merge(), "in", "out", mode="cam_only") def test_cut_mode_and_mute_survive_and_do_not_merge(tmp_path): cuts = [{"start": 10, "end": 15, "mode": "cam_only"}, {"start": 15, "end": 20, "mode": "cam_only", "muted": True}, {"start": 20, "end": 24, "mode": "cam_only", "muted": True}] p = validate(make_project(cuts=cuts), tmp_path) assert [(c["start"], c["end"], c["muted"]) for c in p["cuts"]] == [(10, 15, False), (15, 24, True)] cfg = editor.effective_settings(layout.merge(CAM), p) graph = editor_render.build_filter(p, cfg, editor.pieces(p["cuts"]), 10.0, None, []) first, second = graph.split("[pa0]")[0], graph.split("[pa0]")[1].split("[pa1]")[0] assert "volume=0" not in first.split("[sa0]atrim")[1] # 1º trecho com som assert second.rstrip().endswith("volume=0") # 2º trecho mudo assert graph.count("crop=iw*0.2:ih*0.25") == 2 # câmera em tela cheia nos dois def test_text_position_and_chat_block_in_ass(tmp_path): data = make_project(chat={"enabled": True, "x": 0.05, "y": 0.6, "width": 0.5, "lines": 2, "size": 30}) data["overlays"][0]["x"] = 0.25 p = validate(data, tmp_path) cfg = editor.effective_settings(layout.merge(), p) chat = [ChatMessage(11.0, "KKKKKK", "ana"), ChatMessage(12.0, "server chinês? {\\b1}", "beto"), ChatMessage(13.0, "terceira mensagem bem comprida que não cabe na largura do bloco de chat", "caio"), ChatMessage(22.0, "fora do corte", "dani")] ass = editor_render.build_project_ass(p, cfg, "twitch.tv/x", chat=chat) assert f"\\pos({int(1080 * 0.25)},{int(1920 * 0.2)})" in ass # gancho na posição arrastada chat_lines = [line for line in ass.splitlines() if ",Chat," in line] assert len(chat_lines) == 3 and "fora do corte" not in ass assert f"\\an1\\pos({int(1080 * 0.05)},{int(1920 * 0.6)})" in chat_lines[0] assert "ana:" in chat_lines[0] and "\\N" not in chat_lines[0] assert chat_lines[2].count("\\N") == 1 and "ana:" not in chat_lines[2] # só as 2 últimas assert "{\\b1}" not in ass and "…" in chat_lines[2] # texto do chat nunca vira tag; linha longa é cortada # desligado (padrão): nenhum evento de chat off = editor_render.build_project_ass(validate(make_project(), tmp_path), cfg, "", chat=chat) assert ",Chat,," not in off.split("[Events]")[1] def test_cover_at_start_overlays_first_frames(tmp_path): p = validate(make_project(cover={"enabled": True, "t": 12, "text": "LAG", "atStart": True}), tmp_path) assert p["cover"]["atStart"] is True cfg = editor.effective_settings(layout.merge(), p) graph = editor_render.build_filter(p, cfg, editor.pieces(p["cuts"]), 10.0, "e.ass", [], cover_input=1) assert "[vbase]" in graph and "[1:v]scale=1080:1920" in graph and "overlay=enable='lt(t,0.0667)'" in graph assert graph.rstrip().split(";")[-1] != "" and "[v]" in graph plain = editor_render.build_filter(p, cfg, editor.pieces(p["cuts"]), 10.0, "e.ass", []) assert "[vbase]" not in plain def test_real_render_with_new_layers(tmp_path): """Render de verdade (FFmpeg) com câmera inteira, trecho mudo, chat na tela e capa no início.""" import shutil import subprocess ffmpeg = shutil.which("ffmpeg") if not ffmpeg: import pytest pytest.skip("sem ffmpeg") seg = tmp_path / "seg0.ts" subprocess.run([ffmpeg, "-v", "error", "-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30:duration=8", "-f", "lavfi", "-i", "sine=frequency=440:duration=8", "-c:v", "libx264", "-preset", "ultrafast", "-g", "15", "-c:a", "aac", "-shortest", str(seg)], check=True) # -g 15: quadro-chave a cada 0,5 s (senão não dá para saltar no .ts) cuts = [{"start": 1, "end": 4, "mode": "cam_only"}, {"start": 4, "end": 7, "mode": None, "muted": True}] data = make_project(source={"sessionId": 1, "lo": 0, "hi": 8}, cuts=cuts, overlays=[], captions={"enabled": False, "preset": "pop", "style": {}, "emphColor": "#22E36B", "words": []}, cover={"enabled": True, "t": 2, "text": "CAPA", "atStart": True}, chat={"enabled": True, "lines": 3}) p = validate(data, tmp_path, duration=8.0) base = layout.merge(CAM) segments = [(seg, 0.0, 8.0)] cover = editor_render.render_cover(ffmpeg, segments, p, base, "twitch.tv/x", tmp_path / "cover.jpg") out = tmp_path / "out.mp4" editor_render.render_project(ffmpeg, shutil.which("ffprobe"), ["-c:v", "libx264", "-preset", "ultrafast"], segments, p, base, "twitch.tv/x", out, tmp_path, chat=[ChatMessage(2.0, "KKKK", "ana"), ChatMessage(5.0, "F", "beto")], cover_file=cover) probe = subprocess.run([shutil.which("ffprobe") or "ffprobe", "-v", "error", "-show_entries", "format=duration:stream=width,height", "-of", "csv=p=0", str(out)], capture_output=True, text=True, check=True).stdout assert "1080,1920" in probe and abs(float(probe.strip().splitlines()[-1]) - 6.0) < 0.2 # Trecho mudo: o volume médio da 2ª metade é silêncio; a 1ª tem o tom de teste. def mean_db(start: float) -> float: err = subprocess.run([ffmpeg, "-v", "info", "-ss", str(start), "-t", "2", "-i", str(out), "-af", "volumedetect", "-vn", "-f", "null", "-"], capture_output=True, text=True).stderr return float(err.split("mean_volume:")[1].split("dB")[0]) assert mean_db(0.5) > -40 and mean_db(3.6) < -70