"""Edições no projeto: silêncio, corte por texto, preenchimento, marcadores. Extraído de models_api.py — a tabela de comandos segue lá. """ from __future__ import annotations import asyncio 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_silence_config, save_silence_config, ) from . import shared from .shared import ( _derived_output, _emit_no_change_or_error, ) def cmd_remove_silences(args: dict) -> int: """Run the canonical server silence remover into a suffixed copy.""" 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 try: from server import handle_remove_media_silence output = _derived_output(path, "_silence_removed", args) contents = asyncio.run(handle_remove_media_silence({**args, "filepath": path, "output_path": output})) message = "\n".join(getattr(content, "text", str(content)) for content in contents) if not Path(output).exists(): return _emit_no_change_or_error(path, message) shared.emit({"ok": True, "path": output, "message": message}) return 0 except Exception as exc: shared.emit({"ok": False, "error": str(exc)}) return 1 def cmd_edit_by_transcript(args: dict) -> int: """Cut (or keep only) spoken phrases, using each media's cached transcript.""" path = str(args.get("path", "")) phrases = args.get("phrases") or [] if not path or not Path(path).exists(): shared.emit({"ok": False, "error": "Arquivo de projeto não encontrado."}) return 1 if not isinstance(phrases, list) or not [p for p in phrases if str(p).strip()]: shared.emit({"ok": False, "error": "Informe ao menos uma frase para cortar."}) return 1 try: from server import handle_edit_by_transcript output = _derived_output(path, "_transcript_edit", args) contents = asyncio.run(handle_edit_by_transcript({**args, "filepath": path, "output_path": output})) message = "\n".join(getattr(content, "text", str(content)) for content in contents) if not Path(output).exists(): shared.emit({"ok": False, "error": message}) return 1 shared.emit({"ok": True, "path": output, "message": message}) return 0 except Exception as exc: shared.emit({"ok": False, "error": str(exc)}) return 1 def cmd_remove_filler_words(args: dict) -> int: """Cut filler words (um, uh, ...) out, using each media's cached transcript.""" 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 try: from server import handle_remove_filler_words output = _derived_output(path, "_defillered", args) contents = asyncio.run(handle_remove_filler_words({**args, "filepath": path, "output_path": output})) message = "\n".join(getattr(content, "text", str(content)) for content in contents) if not Path(output).exists(): return _emit_no_change_or_error(path, message) shared.emit({"ok": True, "path": output, "message": message}) return 0 except Exception as exc: shared.emit({"ok": False, "error": str(exc)}) return 1 def cmd_transcript_markers(args: dict) -> int: """Add a marker per transcribed segment, using each media's cached transcript.""" 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 try: from server import handle_transcript_markers output = _derived_output(path, "_transcript_markers", args) contents = asyncio.run(handle_transcript_markers({**args, "filepath": path, "output_path": output})) message = "\n".join(getattr(content, "text", str(content)) for content in contents) if not Path(output).exists(): shared.emit({"ok": False, "error": message}) return 1 shared.emit({"ok": True, "path": output, "message": message}) return 0 except Exception as exc: shared.emit({"ok": False, "error": str(exc)}) return 1 def cmd_silence_config(args: dict) -> int: """Read the persisted silence thresholds (noise floor, duration, padding).""" shared.emit({"ok": True, **load_silence_config()}) return 0 def cmd_set_silence_config(args: dict) -> int: """Persist silence thresholds. Only the given fields change.""" config = save_silence_config( noise_db=args.get("noise_db"), min_silence=args.get("min_silence"), padding=args.get("padding"), ) shared.emit({"ok": True, **config}) return 0