- app/dub.py: frases traduzidas → uma fala TTS por frase no serviço de voz (POST /v1/synthesize, WAV), cache em disco por (voz, velocidade, texto), encaixe sem sincronia labial (velocidade natural, escorre até 1,5 s sem acumular atraso, acelera no máximo 1,2× e corta com fade só se preciso, nunca duas falas juntas), frase partida no meio vira uma fala só, voz reserva offline e faixa única do trecho nivelada pelo volume do corte. - editor_render.render_project: o áudio original abaixa (dubDuck, rampa de 0,15 s) só onde há dublagem; nada muda com dev=False. Devolve resumo. - layout: dev.dubVoice, dev.dubVoiceOther (speaker "other") e dev.dubDuck. - editor_api: prévia e render da fila passam o dublador; translation_for também traduz quando só a dublagem está ligada. - Painel: switch "Dublagem em português (teste)", vozes TTS prontas (sem clone) e volume do original. - doc/clipador/DUBLAGEM.md e tests/test_dub.py (TTS falso). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
9.4 KiB
Python
168 lines
9.4 KiB
Python
"""Fonte em inglês (18/11/2026, doc/clipador/IDIOMAS.md): juízes em inglês com o português do painel na mesma
|
|
resposta, risada/gíria do chat em inglês, fala do corte traduzida por frase e legenda em português de desenvolvimento
|
|
(desligada por padrão)."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from app import editor, editor_render, layout
|
|
from app.judge import (ANALYSIS_SCHEMA_EN, JUDGE_SYSTEM_EN, SCAN_SCHEMA_EN, VERDICT_SCHEMA_EN, Judge, Verdict,
|
|
analysis_block, build_prompt, lang_key)
|
|
from app.scoring import REACTION_PATTERNS, ChatMessage, Word
|
|
from app.subtitles import phrases
|
|
|
|
from .test_editor import validate
|
|
from .test_render_and_duplicates import add_clip
|
|
from .test_sources_and_api import env, seed_session # noqa: F401 - fixture reaproveitada
|
|
|
|
|
|
class EnglishJudge(Judge):
|
|
"""Juiz falso: registra o idioma pedido e devolve o bloco `pt`, como os schemas em inglês exigem."""
|
|
|
|
def __init__(self):
|
|
super().__init__("codex", "fake", Path("."))
|
|
self.seen: dict = {}
|
|
self.translated: list[list[str]] = []
|
|
|
|
def analyze(self, prompt, images, lang="pt"):
|
|
self.seen["analyze"] = (lang, prompt)
|
|
return {"situation": "jogada", "what_happened": "He robbed the bank.", "screen": "", "speech": "",
|
|
"chat_meaning": "", "confidence": 0.9, "action_start_s": 0, "reaction_end_s": 0, "hook": "NO WAY",
|
|
"pt": {"what_happened": "Ele roubou o banco.", "screen": "", "speech": "", "chat_meaning": "", "hook": "NÃO ACREDITO"}}
|
|
|
|
def judge(self, prompt, start_s, end_s, images=None, lang="pt", **_limits):
|
|
self.seen["judge"] = (lang, prompt)
|
|
return Verdict(True, 8.0, start_s, end_s, "He robbed the bank solo", "Insane solo heist.", "great play", "fake",
|
|
("heist",), pt={"title": "Ele roubou o banco sozinho", "caption": "Assalto solo insano.",
|
|
"reason": "jogada ótima"})
|
|
|
|
def translate_lines(self, lines):
|
|
self.translated.append(list(lines))
|
|
return [f"PT: {text}" for text in lines]
|
|
|
|
|
|
def _english_session(engine) -> int:
|
|
sid = seed_session(engine)
|
|
engine.db.run("UPDATE sessions SET language = 'en-US' WHERE id = ?", sid)
|
|
words = [[281.0, 281.3, "No"], [281.3, 281.6, "way"], [281.6, 282.0, "bro."], [284.0, 284.4, "Let's"], [284.4, 284.9, "go!"]]
|
|
engine.db.run("UPDATE segments SET words_json = '[]' WHERE session_id = ?", sid)
|
|
engine.db.run("UPDATE segments SET words_json = ? WHERE session_id = ? AND idx = 9", json.dumps(words), sid)
|
|
return sid
|
|
|
|
|
|
def test_lang_key():
|
|
assert lang_key("en-US") == "en" and lang_key("en") == "en"
|
|
assert lang_key("pt-BR") == "pt" and lang_key("es-419") == "pt" and lang_key("") == "pt" and lang_key(None) == "pt"
|
|
|
|
|
|
def test_english_schemas_require_the_portuguese_block():
|
|
assert ANALYSIS_SCHEMA_EN["required"][-1] == "pt" and VERDICT_SCHEMA_EN["required"][-1] == "pt"
|
|
assert "summary_pt" in SCAN_SCHEMA_EN["required"]
|
|
assert "pt" in SCAN_SCHEMA_EN["properties"]["moments"]["items"]["required"]
|
|
# Os CLIs exigem todas as propriedades em `required` (strict).
|
|
for schema in (ANALYSIS_SCHEMA_EN, VERDICT_SCHEMA_EN, SCAN_SCHEMA_EN):
|
|
assert set(schema["properties"]) == set(schema["required"])
|
|
assert "LMAO" in JUDGE_SYSTEM_EN and "💀" in JUDGE_SYSTEM_EN
|
|
|
|
|
|
def test_english_prompt_labels_and_analysis_block_without_pt():
|
|
prompt = build_prompt("Tfue", "twitch", 100, 160, 150, [Word(140, 141, "bro")], [ChatMessage(151, "LMAO")], {},
|
|
"GTA 6 Waiting Room", lang="en")
|
|
assert "Channel: Tfue" in prompt and "Transcript:" in prompt and "Chat sample:" in prompt
|
|
assert "Transcrição" not in prompt and "Canal:" not in prompt
|
|
block = analysis_block({"situation": "jogada", "pt": {"what_happened": "x"}}, "en")
|
|
assert block.startswith("ANALYST's analysis") and '"pt"' not in block
|
|
|
|
|
|
def test_english_chat_laughter_and_slang_count_as_reaction():
|
|
def tags(text):
|
|
return {tag for tag, (pattern, _) in REACTION_PATTERNS.items() if pattern.search(text)}
|
|
assert "risada" in tags("LMAO") and "risada" in tags("lol") and "risada" in tags("💀💀") and "risada" in tags("im dead")
|
|
assert "risada" in tags("😭😭") and "risada" not in tags("lollipop")
|
|
assert "susto" in tags("no way") and "susto" in tags("bruhhh") and "susto" in tags("nahhh")
|
|
assert "hype" in tags("LETS GOOO") and "hype" in tags("sheesh") and "hype" in tags("clutch")
|
|
assert "clip" in tags("clip that")
|
|
|
|
|
|
def test_phrases_split_on_sentence_end_and_pause():
|
|
words = [Word(0, 0.3, "No"), Word(0.3, 0.6, "way"), Word(0.6, 0.9, "bro."), Word(0.9, 1.2, "This"), Word(1.2, 1.5, "is"),
|
|
Word(1.5, 1.8, "crazy"), Word(1.8, 2.1, "man."), Word(3.5, 3.8, "Okay")]
|
|
assert [t for _, _, t in phrases(words)] == ["No way bro. This is crazy man.", "Okay"]
|
|
assert phrases([]) == []
|
|
|
|
|
|
def test_english_session_judges_in_english_and_stores_portuguese(env): # noqa: F811
|
|
client, engine = env
|
|
engine.judge = EnglishJudge()
|
|
sid = _english_session(engine)
|
|
add_clip(engine, sid, 1, "candidate", 285, 270, 340)
|
|
engine.judge_clip(engine.db.get("SELECT * FROM clips WHERE id = 1"))
|
|
assert engine.judge.seen["analyze"][0] == "en" and engine.judge.seen["judge"][0] == "en"
|
|
assert "Transcript:" in engine.judge.seen["judge"][1] and "ANALYST's analysis" in engine.judge.seen["judge"][1]
|
|
clip = client.get("/v1/clips/1", headers=_auth()).json()
|
|
assert clip["title"] == "He robbed the bank solo" # o vídeo/publicação: inglês
|
|
assert clip["judge"]["pt"]["title"] == "Ele roubou o banco sozinho" # o painel: português
|
|
assert clip["judge"]["analysis"]["pt"]["what_happened"] == "Ele roubou o banco."
|
|
lines = clip["translation"]["lines"]
|
|
assert [x["text"] for x in lines] == ["No way bro.", "Let's go!"]
|
|
assert lines[0]["pt"] == "PT: No way bro." and clip["translation"]["language"] == "pt-BR"
|
|
# Tradução que já cobre o trecho é reaproveitada (sem chamar a IA de novo).
|
|
engine.translate_clip(1)
|
|
assert len(engine.judge.translated) == 1
|
|
|
|
|
|
def test_portuguese_session_has_no_translation(env): # noqa: F811
|
|
client, engine = env
|
|
engine.judge = EnglishJudge()
|
|
sid = seed_session(engine)
|
|
add_clip(engine, sid, 1, "candidate", 300, 280, 350)
|
|
engine.judge_clip(engine.db.get("SELECT * FROM clips WHERE id = 1"))
|
|
assert engine.judge.seen["judge"][0] == "pt" and "Transcrição:" in engine.judge.seen["judge"][1]
|
|
assert client.get("/v1/clips/1", headers=_auth()).json()["translation"] is None
|
|
assert client.post("/v1/clips/1/translate", headers=_auth()).status_code == 409
|
|
|
|
|
|
def test_dev_translation_line_only_when_enabled(tmp_path):
|
|
clip = {"session_id": 1, "start_s": 10.0, "end_s": 75.0, "peak_s": 50.0, "title": "He robbed the bank", "source": "chat"}
|
|
words = [Word(20, 20.4, "No"), Word(20.4, 20.8, "way"), Word(20.8, 21.2, "bro.")]
|
|
lines = [{"s": 20.0, "e": 21.2, "text": "No way bro.", "pt": "Não acredito, mano."}]
|
|
for enabled in (False, True):
|
|
cfg = layout.merge({"caption": {"translation": enabled}})
|
|
p = validate(editor.auto_project(clip, None, words, [], 100.0, cfg), tmp_path)
|
|
ass = editor_render.build_project_ass(p, editor.effective_settings(cfg, p), "twitch.tv/tfue", translation=lines)
|
|
has = "Não acredito, mano." in ass and "Style: Trans," in ass
|
|
assert has is enabled
|
|
# Sem tradução disponível nada muda, mesmo ligado.
|
|
cfg = layout.merge({"caption": {"translation": True}})
|
|
p = validate(editor.auto_project(clip, None, words, [], 100.0, cfg), tmp_path)
|
|
assert "Style: Trans," not in editor_render.build_project_ass(p, editor.effective_settings(cfg, p), "x")
|
|
assert layout.DEFAULTS["caption"]["translation"] is False
|
|
assert layout.validate({"caption": {"translation": True}})["caption"]["translation"] is True
|
|
|
|
|
|
def _auth():
|
|
from .test_sources_and_api import auth
|
|
return auth()
|
|
|
|
|
|
def test_voice_language_maps_audience_codes_to_whisper_codes():
|
|
from app.worker import voice_language
|
|
assert voice_language("en-US") == "en" and voice_language("es-419") == "es" and voice_language("pt-BR") == "pt-BR"
|
|
assert voice_language("en-GB") == "en" and voice_language("") == "pt-BR"
|
|
|
|
|
|
def test_dev_layers_never_reach_the_final_render(tmp_path):
|
|
"""Contrato `dev` (18/11/2026): camadas de desenvolvimento só no vídeo de revisão; `dev=False` = vídeo final limpo."""
|
|
assert {k: layout.DEFAULTS["dev"][k] for k in ("translation", "dub")} == {"translation": False, "dub": False}
|
|
assert layout.validate({"dev": {"translation": True, "dub": True, "x": 1}})["dev"] == {"translation": True, "dub": True}
|
|
cfg = layout.merge({"dev": {"translation": True, "dub": True}})
|
|
assert layout.dev_flags(cfg) == {"translation": True, "dub": True}
|
|
assert layout.dev_flags(cfg, dev=False) == {"translation": False, "dub": False}
|
|
assert layout.dev_flags(layout.merge({"caption": {"translation": True}}))["translation"] is True # legado
|
|
clip = {"session_id": 1, "start_s": 10.0, "end_s": 75.0, "peak_s": 50.0, "title": "x", "source": "chat"}
|
|
words = [Word(20, 20.4, "No"), Word(20.4, 20.8, "way")]
|
|
lines = [{"s": 20.0, "e": 20.8, "text": "No way", "pt": "Não acredito"}]
|
|
p = validate(editor.auto_project(clip, None, words, [], 100.0, cfg), tmp_path)
|
|
eff = editor.effective_settings(cfg, p)
|
|
assert "Não acredito" in editor_render.build_project_ass(p, eff, "x", translation=lines)
|
|
assert "Não acredito" not in editor_render.build_project_ass(p, eff, "x", translation=lines, dev=False)
|