"""Prépare les prises de voix de la vidéo écosystème pour Remotion (écrit par Claude, 20/10/2026). 1. Coupe les silences de début et de fin de chaque pNN.wav (seuil d'énergie), normalise le volume (loudnorm). 2. Cale les mots du SCRIPT (texte affiché) sur l'horodatage Whisper de la prise (mots.json) : 1 pour 1 si le nombre de mots est le même, sinon au prorata des lettres (Whisper découpe « n 'est »…). 3. Écrit public/voix-ecosysteme/pNN.wav, mots-cales.json et src/voixEcosysteme.ts. Usage : python -I outils-voix/preparer.py out/voix-ecosysteme-brut """ import json, re, subprocess, sys, unicodedata, wave from pathlib import Path import numpy as np RACINE = Path(__file__).resolve().parent.parent FFMPEG = RACINE / "node_modules/@remotion/compositor-win32-x64-msvc/ffmpeg.exe" BRUT = Path(sys.argv[1]) SORTIE = RACINE / "public/voix-ecosysteme" TEXTES = json.loads((SORTIE / "phrases-affichees.json").read_text(encoding="utf-8")) MOTS = json.loads((BRUT / "mots.json").read_text(encoding="utf-8")) AVANT, APRES, SEUIL = 0.06, 0.18, 0.01 def bornes(f): with wave.open(str(f)) as w: sr, n = w.getframerate(), w.getnframes() a = np.frombuffer(w.readframes(n), dtype=np.int16).astype(np.float32) / 32768.0 hop = sr // 50 # 20 ms rms = np.array([np.sqrt(np.mean(a[i:i + hop] ** 2)) for i in range(0, len(a) - hop, hop)]) actif = np.where(rms > SEUIL)[0] t0 = max(0.0, actif[0] * hop / sr - AVANT) t1 = min(len(a) / sr, (actif[-1] + 1) * hop / sr + APRES) return t0, t1 def norm(m): m = unicodedata.normalize("NFD", m.lower()) return re.sub(r"[^a-z0-9]", "", "".join(c for c in m if unicodedata.category(c) != "Mn")) def caler(affiche, whisper, t0, dur): w = [(x, max(0.0, s - t0), min(dur, e - t0)) for x, s, e in whisper] if len(w) == len(affiche): return [[m, round(s, 2), round(e, 2)] for m, (_, s, e) in zip(affiche, w)] # prorata des lettres : position (0..1) -> temps, par morceaux sur les mots Whisper lw = [max(1, len(norm(x))) for x, _, _ in w] tot = sum(lw) xs, ts, c = [], [], 0 for l, (_, s, e) in zip(lw, w): xs += [c / tot, (c + l) / tot] ts += [s, e] c += l la = [max(1, len(norm(m))) for m in affiche] tota, c, res = sum(la), 0, [] for m, l in zip(affiche, la): s = float(np.interp(c / tota, xs, ts)) e = float(np.interp((c + l) / tota, xs, ts)) res.append([m, round(s, 2), round(e, 2)]) c += l return res phrases, cales = [], {} for i, texte in enumerate(TEXTES, 1): pid = f"p{i:02d}" src = BRUT / f"{pid}.wav" t0, t1 = bornes(src) dst = SORTIE / f"{pid}.wav" subprocess.run([str(FFMPEG), "-loglevel", "error", "-y", "-i", str(src), "-af", f"atrim={t0:.3f}:{t1:.3f},asetpts=PTS-STARTPTS,loudnorm=I=-16:TP=-1.5:LRA=11", "-ar", "48000", "-ac", "1", str(dst)], check=True) with wave.open(str(dst)) as w: dur = w.getnframes() / w.getframerate() # ponctuation française : « : » et « ? » restent collés au mot (espace insécable) mots = caler(texte.replace(" :", " :").replace(" ?", " ?").split(" "), MOTS[pid], t0, dur) phrases.append({"id": pid, "dur": round(dur, 2), "mots": mots}) cales[pid] = {"coupe": [round(t0, 2), round(t1, 2)], "whisper": " ".join(x[0] for x in MOTS[pid])} print(f"{pid} {t0:.2f}-{t1:.2f} -> {dur:.2f}s | {texte}") (SORTIE / "mots-cales.json").write_text(json.dumps({"phrases": phrases, "controle": cales}, ensure_ascii=False, indent=1), encoding="utf-8") ts = ("// Généré par outils-voix/preparer.py depuis public/voix-ecosysteme (horodatage faster-whisper, texte = script).\n" "// Voix PROVISOIRE (dots.tts-soar, voix par défaut), en attendant la voix clonée de Théo.\n" "export type Phrase = {id: string; dur: number; mots: [string, number, number][]};\n" f"export const PHRASES_ECO: Phrase[] = {json.dumps(phrases, ensure_ascii=False)};\n") (RACINE / "src/voixEcosysteme.ts").write_text(ts, encoding="utf-8") print("total", round(sum(p["dur"] for p in phrases), 2), "s")