A ponte JSON do app tinha 1.395 linhas e 37 comandos de oito assuntos diferentes num arquivo só. Agora models_api.py guarda apenas a referência dos comandos, a tabela de despacho e o main(); cada assunto virou um módulo em admin/api/ (models, project, editing, zoom, subtitles, transcription, voice, review), com a base comum em shared.py. Nada muda para o app: ele continua chamando admin/models_api.py por caminho, e os 37 comandos respondem igual — verificado rodando a ponte de verdade. Duas coisas que a divisão obrigou a arrumar: - A saída passa por `shared.emit` chamada pelo módulo, não pelo nome importado. Isso preserva a propriedade de que trocar `emit` num lugar só captura a saída de todos os comandos — que era acidental quando tudo morava no mesmo arquivo, e vira intencional agora. - `_CANCEL` e o lock eram globais compartilhados. O registro de downloads foi para models.py, junto de quem o usa, com lock próprio: o antigo protegia ao mesmo tempo o dicionário e a escrita em stdout, duas coisas sem relação. Também: admin/test_models_api.py estava fora de `testpaths` e nunca rodava. Movido para code/tests/ e ligado ao gate — 1441 → 1454 testes (ver Engine/docs/05_EXPERIENCIAS.md #24). Lint zerado, 1454 testes passando. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
213 lines
8.3 KiB
Python
213 lines
8.3 KiB
Python
"""Análise de voz e aplicação das decisões de edição.
|
|
|
|
Extraído de models_api.py — a tabela de comandos segue lá.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# code/ is the package root for fcpxml and server modules.
|
|
_CODE_DIR = str(Path(__file__).resolve().parent.parent / "code")
|
|
if _CODE_DIR not in sys.path:
|
|
sys.path.insert(0, _CODE_DIR)
|
|
|
|
from fcpxml.model_manager import (
|
|
load_hf_token,
|
|
load_num_speakers,
|
|
load_selected_model,
|
|
load_transcript_language,
|
|
load_voice_analysis_config,
|
|
save_voice_analysis_config,
|
|
)
|
|
|
|
from . import shared
|
|
from .shared import (
|
|
_load_cached_voice_timeline,
|
|
_project_media_paths,
|
|
_transcript_json_path,
|
|
_voice_timeline_json_path,
|
|
)
|
|
from .shared import _load_cached_transcript # noqa: E402
|
|
|
|
|
|
def cmd_analyze_voice(args: dict) -> int:
|
|
"""Build the voice timeline (transcript+diarization+acoustics -> emphasis)
|
|
for every unique source media in the project, so `refine_voice_timeline`
|
|
and friends have something to read without ever reopening the audio.
|
|
|
|
Analysis only — writes _voice_timeline.json next to each media, doesn't
|
|
touch the project XML. `path` passes through unchanged so it composes
|
|
with the other batch steps (silence removal, captions) regardless of
|
|
where in the list it runs.
|
|
"""
|
|
path = str(args.get("path", ""))
|
|
if not path or not Path(path).exists():
|
|
shared.emit({"ok": False, "error": "Arquivo de projeto não encontrado."})
|
|
return 1
|
|
|
|
model = str(args.get("model", "") or load_selected_model() or "")
|
|
language = args.get("language")
|
|
if language is None:
|
|
language = load_transcript_language()
|
|
if language == "auto":
|
|
language = None
|
|
token = str(args.get("hf_token") or load_hf_token() or "")
|
|
num_speakers = str(args.get("num_speakers") or load_num_speakers() or "")
|
|
|
|
try:
|
|
media_paths = _project_media_paths(path)
|
|
except Exception as exc:
|
|
shared.emit({"ok": False, "error": f"Erro ao ler o projeto: {exc}"})
|
|
return 1
|
|
if not media_paths:
|
|
shared.emit({"ok": False, "error": "Nenhum arquivo de mídia acessível encontrado."})
|
|
return 1
|
|
|
|
from server import handle_build_voice_timeline
|
|
|
|
messages: list[str] = []
|
|
output_dir = str(args.get("output_dir") or "").strip()
|
|
existing: list[Path] = []
|
|
for mp in media_paths:
|
|
timeline_path = _voice_timeline_json_path(mp, output_dir)
|
|
if _load_cached_voice_timeline(timeline_path, mp) is not None:
|
|
existing.append(timeline_path)
|
|
if existing and len(existing) == len(media_paths) and not bool(args.get("force_reprocess", False)):
|
|
message = "# Voice Timeline Cache\n\n"
|
|
message += "Reaproveitando análise de voz existente. Nada foi reprocessado.\n\n"
|
|
for timeline_path in existing:
|
|
message += f"- **Timeline JSON**: {timeline_path}\n"
|
|
shared.emit({
|
|
"ok": True,
|
|
"path": path,
|
|
"reused": True,
|
|
"timelines": [str(p) for p in existing],
|
|
"message": message,
|
|
})
|
|
return 0
|
|
|
|
for mp in media_paths:
|
|
transcript_path = _transcript_json_path(mp, output_dir)
|
|
reused_prefix = ""
|
|
if _load_cached_transcript(transcript_path) is not None:
|
|
reused_prefix = f"# Cache\n\nReaproveitando transcrição existente: `{transcript_path}`\n\n"
|
|
try:
|
|
contents = asyncio.run(handle_build_voice_timeline({
|
|
"media_path": mp, "model": model, "language": language,
|
|
"hf_token": token, "num_speakers": num_speakers,
|
|
"output_dir": output_dir,
|
|
}))
|
|
except Exception as exc:
|
|
shared.emit({"ok": False, "error": f"Falha analisando {Path(mp).name}: {exc}"})
|
|
return 1
|
|
messages.append(reused_prefix + "\n".join(getattr(c, "text", str(c)) for c in contents))
|
|
|
|
shared.emit({"ok": True, "path": path, "message": "\n\n---\n\n".join(messages)})
|
|
return 0
|
|
|
|
def cmd_acoustics_capability(args: dict) -> int:
|
|
"""Whether librosa (pitch/energy extraction) is installed in this venv.
|
|
|
|
Surfaces `features_capability()` — previously computed but never
|
|
exposed to the app, so `layers.acoustics: false` in a voice timeline
|
|
had no explanation the user could act on.
|
|
"""
|
|
from fcpxml.voice_features import features_capability
|
|
ok, msg = features_capability()
|
|
shared.emit({"ok": True, "available": ok, "message": msg})
|
|
return 0
|
|
|
|
def cmd_voice_analysis(args: dict) -> int:
|
|
"""Read the persisted voice-analysis settings (energy/emphasis/emotion)."""
|
|
config = load_voice_analysis_config()
|
|
shared.emit({"ok": True, **config, "emphasis_threshold": config["emphasis_floor"]})
|
|
return 0
|
|
|
|
def cmd_set_voice_analysis(args: dict) -> int:
|
|
"""Persist voice-analysis settings. Only the given fields change."""
|
|
weights = args.get("emphasis_weights")
|
|
config = save_voice_analysis_config(
|
|
energy_threshold=args.get("energy_threshold"),
|
|
emphasis_weights=weights if isinstance(weights, dict) else None,
|
|
emphasis_floor=args.get("emphasis_threshold"),
|
|
emotion_enabled=args.get("emotion_enabled"),
|
|
emotion_sensitivity=args.get("emotion_sensitivity"),
|
|
zoom_scale=args.get("zoom_scale"),
|
|
zoom_mode=args.get("zoom_mode"),
|
|
zoom_ease_in=args.get("zoom_ease_in"),
|
|
zoom_ease_out=args.get("zoom_ease_out"),
|
|
)
|
|
shared.emit({"ok": True, **config})
|
|
return 0
|
|
|
|
def cmd_apply_voice_actions(args: dict) -> int:
|
|
"""Apply a decision list (cuts/zooms/texts/markers) to the project XML.
|
|
|
|
The list is produced by a model reading the _voice_timeline.json — this
|
|
is the step that turns those decisions into an edit, and the one the
|
|
batch chain was missing: without it the app could measure the voice and
|
|
caption the result, but never cut by it.
|
|
|
|
`actions_path` points at the JSON; either a bare list or the
|
|
``{"actions": [...]}`` wrapper the skill emits is accepted. Times stay in
|
|
ORIGINAL source seconds — the handler resolves cuts first and shifts
|
|
everything else itself.
|
|
"""
|
|
path = str(args.get("path", ""))
|
|
if not path or not Path(path).exists():
|
|
shared.emit({"ok": False, "error": "Arquivo de projeto não encontrado."})
|
|
return 1
|
|
|
|
actions = args.get("actions")
|
|
if actions is None:
|
|
actions_path = str(args.get("actions_path", ""))
|
|
if not actions_path or not Path(actions_path).exists():
|
|
shared.emit({"ok": False, "error": "Arquivo de decisões (JSON) não encontrado."})
|
|
return 1
|
|
try:
|
|
with open(actions_path, encoding="utf-8") as fh:
|
|
loaded = json.load(fh)
|
|
except (OSError, ValueError) as exc:
|
|
shared.emit({"ok": False, "error": f"Erro ao ler as decisões: {exc}"})
|
|
return 1
|
|
actions = loaded.get("actions") if isinstance(loaded, dict) else loaded
|
|
|
|
# The documented output format is {"source": ..., "actions": [...]} —
|
|
# callers passing that whole object inline (e.g. the wizard pasting the
|
|
# skill's JSON verbatim) need the same unwrap the actions_path branch
|
|
# above already does, or a well-formed payload gets rejected as
|
|
# "malformed" for having one extra layer of nesting.
|
|
if isinstance(actions, dict):
|
|
actions = actions.get("actions")
|
|
|
|
if not isinstance(actions, list) or not actions:
|
|
shared.emit({"ok": False, "error": "A lista de decisões está vazia ou malformada."})
|
|
return 1
|
|
|
|
from server import handle_apply_voice_actions
|
|
|
|
try:
|
|
contents = asyncio.run(handle_apply_voice_actions({
|
|
"filepath": path,
|
|
"actions": actions,
|
|
"output_dir": args.get("output_dir"),
|
|
}))
|
|
except Exception as exc:
|
|
shared.emit({"ok": False, "error": f"Falha ao aplicar as decisões: {exc}"})
|
|
return 1
|
|
|
|
message = "\n".join(getattr(c, "text", str(c)) for c in contents)
|
|
# The handler reports dropped/rejected actions individually; hand the
|
|
# whole report back so the app can surface them instead of only the count.
|
|
out_path = path
|
|
for line in message.splitlines():
|
|
if line.startswith("- **Saved to**:"):
|
|
out_path = line.split("`")[1] if "`" in line else path
|
|
break
|
|
shared.emit({"ok": True, "path": out_path, "message": message})
|
|
return 0
|