diff --git a/extensions/index.ts b/extensions/index.ts index 08fa64b..161e0ea 100644 --- a/extensions/index.ts +++ b/extensions/index.ts @@ -409,13 +409,31 @@ async function transcribeWithGeminiAPI(file: string): Promise { const model = process.env.AGY_GEMINI_MODEL ?? "gemini-3.5-flash"; try { - const b64 = fs.readFileSync(file).toString("base64"); + // Comprimi in MP3 per evitare HTTP 413 (payload troppo grande) su registrazioni lunghe + // (WAV non compresso supera facilmente i 20MB; MP3 è ~19x più piccolo) + let audioFile = file; + let mimeType = "audio/wav"; + if (file.toLowerCase().endsWith(".wav")) { + const mp3 = file.replace(/\.wav$/i, ".mp3"); + try { + await execFileAsync( + "ffmpeg", + ["-y", "-i", file, "-c:a", "libmp3lame", "-q:a", "4", mp3], + { timeout: 30_000 }, + ); + audioFile = mp3; + mimeType = "audio/mpeg"; + } catch { + /* fallback al WAV originale */ + } + } + const b64 = fs.readFileSync(audioFile).toString("base64"); const body = { contents: [ { parts: [ { text: "Trascrivi fedelmente il contenuto di questo audio in italiano. Restituisci SOLO la trascrizione testuale." }, - { inline_data: { mime_type: "audio/wav", data: b64 } }, + { inline_data: { mime_type: mimeType, data: b64 } }, ], }, ], @@ -489,7 +507,7 @@ async function transcribeWithEnne2(file: string): Promise { ], }, ], - max_tokens: 500, + max_tokens: 2000, }; const headers: Record = { "Content-Type": "application/json" }; if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;