Fix HTTP 413: compressione MP3 prima dell'invio alla Gemini API + max_tokens 2000 per enne2
This commit is contained in:
+21
-3
@@ -409,13 +409,31 @@ async function transcribeWithGeminiAPI(file: string): Promise<string> {
|
||||
|
||||
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<string> {
|
||||
],
|
||||
},
|
||||
],
|
||||
max_tokens: 500,
|
||||
max_tokens: 2000,
|
||||
};
|
||||
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
||||
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
||||
|
||||
Reference in New Issue
Block a user