Gemini STT: compressione audio in Opus/OGG (audio/ogg) -60% payload vs MP3
This commit is contained in:
+7
-6
@@ -448,21 +448,22 @@ async function transcribeWithGeminiAPI(file: string): Promise<TranscriptResult>
|
|||||||
|
|
||||||
const model = process.env.AGY_GEMINI_MODEL ?? "gemini-3.5-flash";
|
const model = process.env.AGY_GEMINI_MODEL ?? "gemini-3.5-flash";
|
||||||
try {
|
try {
|
||||||
// Comprimi in MP3 per evitare HTTP 413 su registrazioni lunghe
|
// Comprimi in Opus/OGG (audio/ogg) — molto più efficiente dell'MP3 per il parlato,
|
||||||
|
// riduce il payload di ~60% evitando HTTP 413 su registrazioni lunghe
|
||||||
let audioFile = file;
|
let audioFile = file;
|
||||||
let mimeType = "audio/wav";
|
let mimeType = "audio/wav";
|
||||||
if (file.toLowerCase().endsWith(".wav")) {
|
if (file.toLowerCase().endsWith(".wav")) {
|
||||||
const mp3 = file.replace(/\.wav$/i, ".mp3");
|
const ogg = file.replace(/\.wav$/i, ".ogg");
|
||||||
try {
|
try {
|
||||||
await execFileAsync(
|
await execFileAsync(
|
||||||
"ffmpeg",
|
"ffmpeg",
|
||||||
["-y", "-i", file, "-ac", "1", "-ar", "16000", "-c:a", "libmp3lame", "-q:a", "5", mp3],
|
["-y", "-i", file, "-ac", "1", "-ar", "16000", "-c:a", "libopus", "-b:a", "16k", ogg],
|
||||||
{ timeout: 60_000 },
|
{ timeout: 60_000 },
|
||||||
);
|
);
|
||||||
audioFile = mp3;
|
audioFile = ogg;
|
||||||
mimeType = "audio/mpeg";
|
mimeType = "audio/ogg";
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return { text: "", error: `Conversione MP3 fallita: ${e.message}` };
|
return { text: "", error: `Conversione Opus fallita: ${e.message}` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const b64 = fs.readFileSync(audioFile).toString("base64");
|
const b64 = fs.readFileSync(audioFile).toString("base64");
|
||||||
|
|||||||
Reference in New Issue
Block a user