feat(reels): voix Gemini complète, sous-titres mot à mot et rendu de qualité

Service ffmpeg-api réécrit (ffmpeg-service/app/) :
- Appels FFmpeg asynchrones : le service ne se fige plus pendant un rendu
- Vidéo récupérée par téléchargement (GET /files/…) au lieu de base64 en JSON
- Vraies erreurs HTTP ; échec explicite si la voix demandée est impossible
- 30 voix Gemini + ton de lecture (dynamique, chaleureux, promo, calme),
  clé en en-tête, modèle configurable avec repli
- Edge TTS 7 (minutage des mots restauré), secours en voix françaises
- Voix traitée : filtre, compression, niveau constant ; musique bouclée et
  baissée automatiquement sous la voix ; mix final à -14 LUFS
- Sous-titres calés mot à mot (Whisper pour Gemini et la voix d'origine,
  à la place de ffsubsync), style Montserrat, placés hors des boutons Reels
- Vidéo : plus de retouche luminosité forcée, scaling lanczos, HDR iPhone
  converti, BT.709, AAC 48 kHz 192k ; la vidéo s'allonge si la voix dépasse
- Grand logo de fin affiché après la voix ; FFmpeg 7.0.2 épinglé, polices et
  modèle Whisper intégrés à l'image ; tests pytest et ruff

Application :
- Sélecteur de voix partagé (4 pages) : moteur, 30 voix, ton, écoute
- Reel images : minutage réel des mots, interrupteur voix respecté
- sync-info ne génère plus de voix à chaque frappe (estimation locale)
- Stabilisation désactivée par défaut, route /reels/preview inutilisée retirée
- Log « [ReelQueue] Worker démarré » pour vérifier la version déployée

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Ze4bs7tpF1KGWUk6ZZSZ4
This commit is contained in:
Claude committed 2026-09-24 12:31:27 +00:00
1 parent d24ab0d4b7
commit 3c6a66bf64
47 files changed
+2206 -2285

No files matched your search

+144 -289
View File
@@ -1,214 +1,120 @@
/**
* FFmpeg Docker API Service
*
* Intégration avec l'API FFmpeg Docker locale pour le traitement vidéo des Reels.
* L'API attend une vidéo en base64 et retourne la vidéo traitée en base64.
* Client du service FFmpeg (conteneur Python `ffmpeg-service`).
*
* Le service rend le Reel puis expose le MP4 en téléchargement
* (GET /files/{job}/output.mp4) : la vidéo ne transite plus en base64 dans du
* JSON, qui gonflait sa taille d'un tiers et la gardait entière en mémoire.
*/
interface FFmpegReelRequest {
video_base64?: string; // Vidéo source en base64
video_url?: string; // OU URL de la vidéo source
text?: string; // Texte overlay style TikTok
music_id?: string; // ID de la musique (catalogue FFmpeg)
music_url?: string; // OU URL directe de la musique
tts_enabled?: boolean; // Activation du TTS
tts_voice?: string; // Voix TTS (ex: fr-FR-VivienneNeural)
tts_engine?: string; // Moteur TTS: "edge" ou "gemini"
gemini_api_key?: string; // Clé API Google Gemini pour TTS
word_duration?: number; // Durée par mot (default: 0.6s)
font_size?: number; // Taille police (default: 24)
music_volume?: number; // Volume musique (default: 0.25)
draw_text?: boolean; // Dessiner le texte sur la vidéo (default: true)
stabilize?: boolean; // Stabilisation vidéo via vidstab (default: false)
watermark_url?: string; // URL du logo
store_name?: string; // Nom du magasin pour l'outro
enable_ending_effect?: boolean; // Activer l'effet de fin (logo+fondu)
}
interface FFmpegReelResponse {
success: boolean;
output_base64?: string;
duration?: number;
detail?: string;
tts_error?: string;
}
import type { TtsEngine, TtsStyle } from '@shared/voices';
/** Un rendu long (stabilisation + encodage) peut dépasser plusieurs minutes. */
const PROCESS_TIMEOUT_MS = 15 * 60_000;
const TTS_TIMEOUT_MS = 2 * 60_000;
const PROCESS_TIMEOUT_MS = 20 * 60_000;
const DOWNLOAD_TIMEOUT_MS = 5 * 60_000;
const TTS_TIMEOUT_MS = 3 * 60_000;
const HEALTH_TIMEOUT_MS = 5_000;
export interface ReelRenderOptions {
text?: string;
musicUrl?: string;
ttsEnabled?: boolean;
ttsVoice?: string;
ttsEngine?: TtsEngine;
ttsStyle?: TtsStyle;
geminiApiKey?: string;
fontSize?: number;
musicVolume?: number;
drawText?: boolean;
stabilize?: boolean;
watermarkUrl?: string;
storeName?: string;
enableEndingEffect?: boolean;
}
export interface ReelRenderResult {
video: Buffer;
duration: number;
ttsEngine?: string | null;
ttsVoice?: string | null;
warnings: string[];
}
export interface TimedWord {
text: string;
start: number;
end: number;
}
export interface VoicePreview {
audio: Buffer;
duration: number;
words: TimedWord[];
engine: string;
voice: string;
warnings: string[];
}
interface FFmpegConfig {
apiUrl: string;
apiKey: string;
}
/** Erreur renvoyée par le service, avec son message lisible. */
export class FFmpegServiceError extends Error {
constructor(message: string, readonly status?: number) {
super(message);
this.name = 'FFmpegServiceError';
}
}
export class FFmpegService {
private config: FFmpegConfig | null = null;
/**
* Configure le service avec l'URL et la clé API
*/
configure(apiUrl: string, apiKey: string): void {
this.config = { apiUrl, apiKey };
console.log('🎬 FFmpeg Service configured:', apiUrl);
this.config = { apiUrl: apiUrl.replace(/\/$/, ''), apiKey };
console.log('🎬 FFmpeg Service configured:', this.config.apiUrl);
}
/**
* Vérifie que le service est configuré
*/
private ensureConfigured(): FFmpegConfig {
if (!this.config) {
throw new Error('FFmpeg Service not configured. Call configure() first.');
throw new FFmpegServiceError("Le service FFmpeg n'est pas configuré (FFMPEG_API_URL / FFMPEG_API_KEY).");
}
return this.config;
}
/**
* Traite une vidéo pour créer un Reel avec musique et texte overlay
*
* @param videoBase64 - Vidéo source encodée en base64
* @param options - Options de traitement (texte, musique, etc.)
* @returns Vidéo traitée en base64
*/
async processReelVideo(
videoBase64: string,
options: {
text?: string;
musicId?: string;
musicUrl?: string;
ttsEnabled?: boolean;
ttsVoice?: string;
ttsEngine?: string;
geminiApiKey?: string;
wordDuration?: number;
fontSize?: number;
musicVolume?: number;
drawText?: boolean;
stabilize?: boolean;
watermarkUrl?: string;
storeName?: string;
enableEndingEffect?: boolean;
} = {}
): Promise<{ success: boolean; videoBase64?: string; duration?: number; error?: string }> {
private async call(path: string, init: RequestInit & { timeoutMs: number }): Promise<Response> {
const config = this.ensureConfigured();
const requestBody: FFmpegReelRequest = {
video_base64: videoBase64,
text: options.text,
music_id: options.musicId,
music_url: options.musicUrl,
tts_enabled: options.ttsEnabled,
tts_voice: options.ttsVoice,
tts_engine: options.ttsEngine,
gemini_api_key: options.geminiApiKey,
word_duration: options.wordDuration ?? 0.6,
font_size: options.fontSize ?? 64,
music_volume: options.musicVolume ?? 0.25,
draw_text: options.drawText ?? true,
stabilize: options.stabilize ?? false,
watermark_url: options.watermarkUrl,
store_name: options.storeName,
enable_ending_effect: options.enableEndingEffect ?? true,
};
// Remove undefined values
Object.keys(requestBody).forEach(key => {
if (requestBody[key as keyof FFmpegReelRequest] === undefined) {
delete requestBody[key as keyof FFmpegReelRequest];
}
const { timeoutMs, headers, ...rest } = init;
const response = await fetch(`${config.apiUrl}${path}`, {
...rest,
headers: { 'X-API-Key': config.apiKey, ...headers },
signal: AbortSignal.timeout(timeoutMs),
});
console.log('🎬 Processing Reel video:', {
hasVideo: !!videoBase64,
hasText: !!options.text,
hasMusicId: !!options.musicId,
hasMusicUrl: !!options.musicUrl,
hasTTS: options.ttsEnabled,
drawText: options.drawText,
});
try {
const response = await fetch(`${config.apiUrl}/process-reel`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': config.apiKey,
},
body: JSON.stringify(requestBody),
signal: AbortSignal.timeout(PROCESS_TIMEOUT_MS),
});
if (!response.ok) {
const errorText = await response.text();
console.error('❌ FFmpeg API error:', response.status, errorText);
return {
success: false,
error: `FFmpeg API error: ${response.status} - ${errorText}`,
};
}
const data = await response.json() as FFmpegReelResponse;
if (!data.success) {
console.error('❌ FFmpeg processing failed:', data.detail);
return {
success: false,
error: data.detail || 'Unknown FFmpeg processing error',
};
}
console.log('✅ Reel video processed successfully, duration:', data.duration);
return {
success: true,
videoBase64: data.output_base64,
duration: data.duration,
};
} catch (error) {
console.error('❌ FFmpeg Service error:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
if (!response.ok) {
const body = await response.text();
let detail = body;
try {
detail = JSON.parse(body).detail ?? body;
} catch { /* corps non JSON */ }
throw new FFmpegServiceError(String(detail).slice(0, 2000), response.status);
}
return response;
}
/**
* Traite une vidéo depuis une URL (télécharge, traite, retourne base64)
* Rend un Reel à partir de l'URL d'une vidéo et renvoie le MP4 produit.
* Lève FFmpegServiceError en cas d'échec (voix comprise).
*/
async processReelFromUrl(
videoUrl: string,
options: {
text?: string;
musicId?: string;
musicUrl?: string;
ttsEnabled?: boolean;
ttsVoice?: string;
ttsEngine?: string;
geminiApiKey?: string;
wordDuration?: number;
fontSize?: number;
musicVolume?: number;
drawText?: boolean;
stabilize?: boolean;
watermarkUrl?: string;
storeName?: string;
enableEndingEffect?: boolean;
} = {}
): Promise<{ success: boolean; videoBase64?: string; duration?: number; error?: string; ttsError?: string }> {
const config = this.ensureConfigured();
const requestBody: FFmpegReelRequest = {
async renderReel(videoUrl: string, options: ReelRenderOptions = {}): Promise<ReelRenderResult> {
const body = {
video_url: videoUrl,
text: options.text,
music_id: options.musicId,
music_url: options.musicUrl,
tts_enabled: options.ttsEnabled,
tts_enabled: options.ttsEnabled ?? false,
tts_voice: options.ttsVoice,
tts_engine: options.ttsEngine,
tts_style: options.ttsStyle,
gemini_api_key: options.geminiApiKey,
word_duration: options.wordDuration ?? 0.6,
font_size: options.fontSize ?? 64,
music_volume: options.musicVolume ?? 0.25,
draw_text: options.drawText ?? true,
@@ -218,139 +124,88 @@ export class FFmpegService {
enable_ending_effect: options.enableEndingEffect ?? true,
};
// Remove undefined values
Object.keys(requestBody).forEach(key => {
if (requestBody[key as keyof FFmpegReelRequest] === undefined) {
delete requestBody[key as keyof FFmpegReelRequest];
}
});
console.log('🎬 Processing Reel from URL:', {
console.log('🎬 Rendu du Reel :', {
videoUrl,
hasText: !!options.text,
textLength: options.text?.length || 0,
hasMusicId: !!options.musicId,
hasMusicUrl: !!options.musicUrl,
ttsEnabled: options.ttsEnabled,
drawText: options.drawText,
textLength: options.text?.length ?? 0,
music: !!options.musicUrl,
tts: options.ttsEnabled ? `${options.ttsEngine}/${options.ttsVoice}/${options.ttsStyle ?? 'neutral'}` : false,
});
const debugBody = { ...requestBody };
console.log('📤 Sending to FFmpeg API:', JSON.stringify({ ...debugBody, text: debugBody.text ? `[${debugBody.text.length} chars]` : undefined }));
const response = await this.call('/process-reel', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
timeoutMs: PROCESS_TIMEOUT_MS,
});
const data = await response.json() as {
job_id: string;
output_path: string;
duration: number;
tts_engine?: string | null;
tts_voice?: string | null;
warnings?: string[];
};
try {
const response = await fetch(`${config.apiUrl}/process-reel`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': config.apiKey,
},
body: JSON.stringify(requestBody),
signal: AbortSignal.timeout(PROCESS_TIMEOUT_MS),
});
if (!response.ok) {
const errorText = await response.text();
console.error('❌ FFmpeg API error:', response.status, errorText);
return {
success: false,
error: `FFmpeg API error: ${response.status} - ${errorText}`,
};
}
const data = await response.json() as FFmpegReelResponse;
if (!data.success) {
console.error('❌ FFmpeg processing failed:', data.detail);
return {
success: false,
error: data.detail || 'Unknown FFmpeg processing error',
};
}
if (data.tts_error) {
console.error('❌ TTS failed in Python service:', data.tts_error);
}
console.log('✅ Reel video processed successfully from URL');
const file = await this.call(data.output_path, { method: 'GET', timeoutMs: DOWNLOAD_TIMEOUT_MS });
const video = Buffer.from(await file.arrayBuffer());
for (const warning of data.warnings ?? []) console.warn(`⚠️ [FFmpeg] ${warning}`);
return {
success: true,
videoBase64: data.output_base64,
video,
duration: data.duration,
ttsError: data.tts_error,
};
} catch (error) {
console.error('❌ FFmpeg Service error:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
ttsEngine: data.tts_engine,
ttsVoice: data.tts_voice,
warnings: data.warnings ?? [],
};
} finally {
// Le fichier n'est plus utile au service une fois récupéré
this.call(`/jobs/${data.job_id}`, { method: 'DELETE', timeoutMs: HEALTH_TIMEOUT_MS })
.catch(() => { /* purgé plus tard par le service */ });
}
}
/**
* Vérifie la santé de l'API FFmpeg
*/
async healthCheck(): Promise<boolean> {
try {
const config = this.ensureConfigured();
const response = await fetch(`${config.apiUrl}/health`, {
method: 'GET',
headers: {
'X-API-Key': config.apiKey,
},
signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
});
return response.ok;
await this.call('/health', { method: 'GET', timeoutMs: HEALTH_TIMEOUT_MS });
return true;
} catch {
return false;
}
}
async previewTTS(
/** Génère la voix seule (aperçu), avec le minutage de chaque mot. */
async previewVoice(
text: string,
ttsVoice?: string,
ttsEngine?: string,
geminiApiKey?: string
): Promise<{ success: boolean; audioBase64?: string; error?: string }> {
const config = this.ensureConfigured();
try {
const response = await fetch(`${config.apiUrl}/preview-tts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': config.apiKey,
},
body: JSON.stringify({
text,
tts_enabled: true,
tts_voice: ttsVoice,
tts_engine: ttsEngine,
gemini_api_key: geminiApiKey,
}),
signal: AbortSignal.timeout(TTS_TIMEOUT_MS),
});
if (!response.ok) {
const errorText = await response.text();
return { success: false, error: `FFmpeg API error: ${response.status} - ${errorText}` };
}
const data = await response.json();
if (!data.success) {
return { success: false, error: data.detail };
}
return { success: true, audioBase64: data.audio_base64 };
} catch (error) {
console.error('❌ TTS Preview error:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
options: { voice?: string; engine?: TtsEngine; style?: TtsStyle; geminiApiKey?: string } = {},
): Promise<VoicePreview> {
const response = await this.call('/preview-tts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text,
tts_voice: options.voice,
tts_engine: options.engine,
tts_style: options.style,
gemini_api_key: options.geminiApiKey,
}),
timeoutMs: TTS_TIMEOUT_MS,
});
const data = await response.json() as {
audio_base64: string;
duration: number;
words: TimedWord[];
engine: string;
voice: string;
warnings?: string[];
};
return {
audio: Buffer.from(data.audio_base64, 'base64'),
duration: data.duration,
words: data.words ?? [],
engine: data.engine,
voice: data.voice,
warnings: data.warnings ?? [],
};
}
}
+34 -52
View File
@@ -6,11 +6,10 @@
import fs from "fs";
import path from "path";
import * as musicMetadata from "music-metadata";
import { bundle } from "@remotion/bundler";
import { renderMedia, selectComposition } from "@remotion/renderer";
import { imagesReelParamsSchema, type ImagesReelResult } from "@shared/reel";
import { ffmpegService } from "../ffmpeg";
import { ffmpegService, type TimedWord } from "../ffmpeg";
import { generateVideoThumbnail } from "../thumbnail";
import type { JobContext } from "./queue";
import { resolveGeminiApiKey, resolveLogoPath } from "./assets";
@@ -88,43 +87,31 @@ export function stripForTTS(text: string): string {
.trim();
}
/** Nombre de syllabes d'un mot français (groupes de voyelles). */
function countSyllablesFr(word: string): number {
const clean = word.replace(/[^a-zàâéèêëîïôùûüç]/gi, "").toLowerCase();
if (!clean) return 1;
return Math.max(1, clean.match(/[aeiouyàâéèêëîïôùûü]+/gi)?.length ?? 1);
const EDGE_PUNCT = /^[.,!?;:…«»"'()\[\]]+|[.,!?;:…«»"'()\[\]]+$/g;
export interface WordTiming {
word: string;
startFrame: number;
endFrame: number;
}
const PUNCT_ONLY = /^[.,!?;:…\-—«»"''()\[\]]+$/;
const EDGE_PUNCT = /^[.,!?;:…«»"''()\[\]]+|[.,!?;:…«»"''()\[\]]+$/g;
/**
* Timings des mots prononcés (en images), répartis au prorata des syllabes.
* Estimation provisoire : remplacée par les vrais timings de la voix au lot
* « voix et sous-titres ».
* Convertit le minutage réel des mots (en secondes, fourni par la voix) en
* images. Chaque mot reste affiché jusqu'au début du suivant : pas de trou
* pendant les respirations.
*/
export function computeWordTimings(
displayText: string,
audioDurationSeconds: number,
fps: number,
startFrame: number,
): Array<{ word: string; startFrame: number; endFrame: number }> {
const spokenWords = stripForTTS(displayText)
.split(/\s+/)
.filter((w) => w && !PUNCT_ONLY.test(w));
if (spokenWords.length === 0) return [];
const cleanWords = spokenWords.map((w) => w.replace(EDGE_PUNCT, "") || w);
const syllables = cleanWords.map(countSyllablesFr);
const totalSyllables = syllables.reduce((a, b) => a + b, 0);
const totalFrames = audioDurationSeconds * fps;
let currentFrame = startFrame;
return cleanWords.map((word, i) => {
const wordStart = currentFrame;
currentFrame += Math.round((syllables[i] / totalSyllables) * totalFrames);
return { word, startFrame: wordStart, endFrame: currentFrame };
});
export function toWordTimings(words: TimedWord[], fps: number): WordTiming[] {
const timings = words
.map((w) => ({ word: w.text.replace(EDGE_PUNCT, "") || w.text, start: w.start, end: w.end }))
.filter((w) => w.word.trim());
return timings.map((w, i) => ({
word: w.word,
startFrame: Math.round(w.start * fps),
endFrame: Math.max(
Math.round(w.start * fps) + 1,
Math.round((i + 1 < timings.length ? timings[i + 1].start : w.end) * fps),
),
}));
}
export async function runImagesReelJob({ job, progress }: JobContext): Promise<ImagesReelResult> {
@@ -141,32 +128,27 @@ export async function runImagesReelJob({ job, progress }: JobContext): Promise<I
// --- Voix ---
let audioUrl: string | undefined;
let wordTimings: ReturnType<typeof computeWordTimings> | undefined;
let wordTimings: WordTiming[] | undefined;
let audioDuration = 0;
const ttsText = overlayText ? stripForTTS(overlayText) : "";
if (overlayText && ttsText) {
if (params.ttsEnabled && overlayText && ttsText) {
await progress(15, "voice");
const geminiApiKey = await resolveGeminiApiKey(params.ttsEngine);
const tts = await ffmpegService.previewTTS(ttsText, params.ttsVoice, params.ttsEngine, geminiApiKey);
if (!tts.success || !tts.audioBase64) {
throw new Error(`La voix n'a pas pu être générée : ${tts.error ?? "réponse vide"}`);
}
const voice = await ffmpegService.previewVoice(ttsText, {
voice: params.ttsVoice,
engine: params.ttsEngine,
style: params.ttsStyle,
geminiApiKey: await resolveGeminiApiKey(params.ttsEngine),
});
for (const warning of voice.warnings) console.warn(`⚠️ [Reels] ${warning}`);
const audioFilename = `tts-${job.id}.mp3`;
const audioPath = path.join(REMOTION_TEMP_DIR, audioFilename);
const audioBuffer = Buffer.from(tts.audioBase64, "base64");
await fs.promises.writeFile(audioPath, audioBuffer);
await fs.promises.writeFile(audioPath, voice.audio);
jobTempFiles.push(audioPath);
audioUrl = localHttpUrl(`/uploads/temp/${audioFilename}`);
try {
audioDuration = (await musicMetadata.parseFile(audioPath)).format.duration ?? 0;
} catch {
audioDuration = audioBuffer.length / 16000; // estimation à 128 kb/s
}
audioDuration = Math.max(audioDuration, ttsText.split(/\s+/).length * 0.35);
wordTimings = computeWordTimings(overlayText, audioDuration, FPS, 0);
audioDuration = voice.duration;
wordTimings = toWordTimings(voice.words, FPS);
}
// --- Durée : 25 à 30 s ---
+1
View File
@@ -94,6 +94,7 @@ export async function startReelWorker(): Promise<void> {
console.error("❌ [ReelQueue] Récupération des jobs orphelins impossible :", error);
}
console.log(`🎬 [ReelQueue] Worker démarré (traitements : ${Array.from(handlers.keys()).join(", ")})`);
pollTimer = setInterval(kick, POLL_INTERVAL_MS);
pollTimer.unref();
kick();
+4 -12
View File
@@ -30,14 +30,14 @@ export async function runVideoReelJob({ job, progress }: JobContext) {
await progress(15, "render");
const startedAt = Date.now();
const rendered = await ffmpegService.processReelFromUrl(resolveInternalUrl(media.originalUrl), {
const rendered = await ffmpegService.renderReel(resolveInternalUrl(media.originalUrl), {
text: params.overlayText,
musicUrl,
ttsEnabled: params.ttsEnabled,
ttsVoice: params.ttsVoice,
ttsEngine: params.ttsEngine,
ttsStyle: params.ttsStyle,
geminiApiKey,
wordDuration: params.wordDuration,
fontSize: params.fontSize,
musicVolume: params.musicVolume,
drawText: params.drawText,
@@ -46,18 +46,10 @@ export async function runVideoReelJob({ job, progress }: JobContext) {
storeName: params.storeName,
enableEndingEffect: params.enableEndingEffect,
});
console.log(`⏱️ [Reels] FFmpeg : ${((Date.now() - startedAt) / 1000).toFixed(1)} s`);
if (!rendered.success || !rendered.videoBase64) {
throw new Error(rendered.error || "Erreur de traitement vidéo FFmpeg");
}
if (rendered.ttsError) {
// Voix demandée mais absente : ne jamais publier un Reel muet sans le dire
throw new Error(`La voix n'a pas pu être générée : ${rendered.ttsError}`);
}
console.log(`⏱️ [Reels] Rendu : ${((Date.now() - startedAt) / 1000).toFixed(1)} s, vidéo de ${rendered.duration.toFixed(1)} s`);
await progress(65, "store");
const videoBuffer = Buffer.from(rendered.videoBase64, "base64");
const videoBuffer = rendered.video;
const processedMedia = await storeRenderedVideo(job.userId, videoBuffer, `reel-${Date.now()}.mp4`);
await storage.updatePostMedia(postId, [processedMedia.id]);
+35 -62
View File
@@ -1,5 +1,15 @@
import { ffmpegService } from './ffmpeg';
import * as musicMetadata from 'music-metadata';
/**
* Estimation de la durée de lecture d'un texte, affichée pendant la saisie.
*
* Auparavant, chaque pause de frappe générait une voix complète pour la
* mesurer (un appel Gemini facturé à chaque fois). Le minutage réel des mots
* vient désormais de la voix au moment du rendu ; ici, une estimation suffit.
*/
/** Débit moyen d'une voix de synthèse française, en mots par seconde. */
const WORDS_PER_SECOND = 2.6;
/** Au-delà, la voix dépasse la durée confortable d'un Reel. */
const MAX_COMFORTABLE_SECONDS = 45;
export interface SyncTiming {
wordDuration: number;
@@ -10,67 +20,30 @@ export interface SyncTiming {
warnings: string[];
}
export class TtsSyncService {
/**
* Calcule le word_duration optimal pour synchroniser l'affichage du texte
* avec la durée réelle de la voix TTS générée.
*/
async calculateSyncTiming(
text: string,
voice: string,
ttsEngine?: string,
geminiApiKey?: string
): Promise<SyncTiming> {
const cleanText = this.cleanText(text);
export function estimateVoiceTiming(text: string): SyncTiming {
const words = text
.replace(/#[\wÀ-ÿ]+/g, '')
.replace(/https?:\/\/\S+/g, '')
.split(/\s+/)
.filter((w) => /[A-Za-z0-9À-ÿ]/.test(w));
const pauses = (text.match(/[.!?;:]/g) ?? []).length;
const punctuationPause = 0.35;
const audioDuration = words.length / WORDS_PER_SECOND + pauses * punctuationPause;
// 1. Générer le TTS preview (avec la même voix que le rendu) et mesurer sa durée exacte
const ttsResult = await ffmpegService.previewTTS(cleanText, voice, ttsEngine, geminiApiKey);
if (!ttsResult.success || !ttsResult.audioBase64) {
throw new Error('TTS preview failed: ' + (ttsResult.error || 'unknown'));
}
const audioBuffer = Buffer.from(ttsResult.audioBase64, 'base64');
const metadata = await musicMetadata.parseBuffer(audioBuffer, 'audio/mpeg');
const audioDuration = metadata.format.duration || 0;
// 2. Analyser le texte (compte les mots réellement lus par la voix)
const wordCount = this.calculateWordCount(cleanText);
// 3. Calculer le word_duration
const wordDuration = wordCount > 0 ? audioDuration / wordCount : 0.6;
// 4. Validation
const warnings: string[] = [];
const isHealthy = wordDuration >= 0.25 && wordDuration <= 1.5;
if (wordDuration < 0.25) {
warnings.push('Texte trop long : les mots défileront très vite. Envisagez de raccourcir.');
}
if (wordDuration > 1.2) {
warnings.push('Texte très court : affichage lent.');
}
return {
wordDuration,
audioDuration,
wordCount,
punctuationPause: 0,
isHealthy,
warnings,
};
const warnings: string[] = [];
if (audioDuration > MAX_COMFORTABLE_SECONDS) {
warnings.push(`Texte long : environ ${Math.round(audioDuration)} s de voix. Visez moins de ${MAX_COMFORTABLE_SECONDS} s.`);
}
if (words.length > 0 && words.length < 4) {
warnings.push('Texte très court : la voix ne durera que quelques secondes.');
}
private cleanText(text: string): string {
return text
.replace(/#\w+/g, '')
.replace(/https?:\/\/\S+/g, '')
.replace(/[\uD83C-\uD83E][\uDC00-\uDFFF]|[☀-⛿✀-➿]/g, '')
.trim();
}
private calculateWordCount(text: string): number {
const tokens = text.split(/\s+/).filter(w => w.length > 0);
return tokens.filter(w => /[a-zA-Z0-9À-ſ]/.test(w)).length;
}
return {
wordDuration: words.length ? audioDuration / words.length : 0,
audioDuration,
wordCount: words.length,
punctuationPause,
isHealthy: warnings.length === 0,
warnings,
};
}
export const ttsSyncService = new TtsSyncService();