From 7b30a50b518436d14fa19cb7ab3e323f26d9be70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 9 Nov 2026 16:21:48 +0000 Subject: [PATCH] fix(cli): transcribe reads whisper output with nan or inf values (#7110) * fix(cli): transcribe reads whisper output with nan or inf values * fix(cli): transcribe also reads a Windows whisper's -nan(ind) as no value --- packages/cli/src/whisper/normalize.test.ts | 35 ++++++++++++++++++++++ packages/cli/src/whisper/normalize.ts | 13 ++++++-- packages/cli/src/whisper/transcribe.ts | 4 +-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/whisper/normalize.test.ts b/packages/cli/src/whisper/normalize.test.ts index b37542bb2a..f4450ced4c 100644 --- a/packages/cli/src/whisper/normalize.test.ts +++ b/packages/cli/src/whisper/normalize.test.ts @@ -124,6 +124,41 @@ describe("loadTranscript", () => { ); }); + it("reads whisper-cli output whose floats are nan, -nan, inf or MSVC's -nan(ind)", () => { + // Token objects as whisper.cpp's cli.cpp writes them: tab-indented, floats via iostream. + const token = (text: string, from: number, to: number, p: string) => + [ + "\t\t\t{", + `\t\t\t\t"text": "${text}",`, + `\t\t\t\t"timestamps": {`, + `\t\t\t\t\t"from": "00:00:00.000",`, + `\t\t\t\t\t"to": "00:00:00.500"`, + `\t\t\t\t},`, + `\t\t\t\t"offsets": {`, + `\t\t\t\t\t"from": ${from},`, + `\t\t\t\t\t"to": ${to}`, + `\t\t\t\t},`, + `\t\t\t\t"id": 50364,`, + `\t\t\t\t"p": ${p},`, + `\t\t\t\t"t_dtw": -1`, + "\t\t\t}", + ].join("\n"); + const raw = `{\n\t"transcription": [\n\t\t{\n\t\t\t"tokens": [\n${[ + token(" nan", 0, 500, "nan"), + token(" world", 600, 1200, "-nan"), + token(" again", 1300, 1800, "inf"), + token(" later", 1900, 2400, "-nan(ind)"), + ].join(",\n")}\n\t\t\t]\n\t\t}\n\t]\n}\n`; + const { words, format } = loadTranscript(tmpFile("transcript.json", raw)); + expect(format).toBe("whisper-cpp"); + expect(words).toEqual([ + { text: "nan", start: 0, end: 0.5, id: "w0" }, + { text: "world", start: 0.6, end: 1.2, id: "w1" }, + { text: "again", start: 1.3, end: 1.8, id: "w2" }, + { text: "later", start: 1.9, end: 2.4, id: "w3" }, + ]); + }); + it("parses whisper-cpp JSON with punctuation merging", () => { const path = tmpFile( "transcript.json", diff --git a/packages/cli/src/whisper/normalize.ts b/packages/cli/src/whisper/normalize.ts index c32a0c6990..7c990087d3 100644 --- a/packages/cli/src/whisper/normalize.ts +++ b/packages/cli/src/whisper/normalize.ts @@ -41,7 +41,7 @@ export function detectFormat(filePath: string): TranscriptFormat { const ext = extname(filePath).toLowerCase(); if (ext === ".srt") return "srt"; if (ext === ".vtt") return "vtt"; - if (ext === ".json") return detectJsonFormat(JSON.parse(readJsonFileText(filePath))); + if (ext === ".json") return detectJsonFormat(parseWhisperJson(readJsonFileText(filePath))); throw new Error(`Unsupported transcript file extension: ${ext}. Use .json, .srt, or .vtt`); } @@ -499,6 +499,15 @@ export function formatVtt(words: Word[], opts?: WordsToCuesOptions): string { // Public API // --------------------------------------------------------------------------- +const BARE_NAN_OR_INF_OUTSIDE_STRINGS = + /"(?:[^"\\]|\\.)*"|-?\b(?:nan(?:\([^)]*\))?|inf(?:inity)?)(?!\w)/gi; + +export function parseWhisperJson(text: string): any { + return JSON.parse( + text.replace(BARE_NAN_OR_INF_OUTSIDE_STRINGS, (m) => (m.startsWith('"') ? m : "null")), + ); +} + /** * Load and normalize a transcript file to a standard word array. * @@ -523,7 +532,7 @@ export function loadTranscript(filePath: string): { words: Word[]; format: Trans } // JSON formats — parse once, detect, then extract words - const parsed = JSON.parse(content); + const parsed = parseWhisperJson(content); const format = detectJsonFormat(parsed); // JSON parsers never set id — assign w{index} like the srt/vtt branches above diff --git a/packages/cli/src/whisper/transcribe.ts b/packages/cli/src/whisper/transcribe.ts index 311044372e..a4af0093eb 100644 --- a/packages/cli/src/whisper/transcribe.ts +++ b/packages/cli/src/whisper/transcribe.ts @@ -22,7 +22,7 @@ import { sha256File } from "./modelFile.js"; import { initialModelForLanguage } from "./modelForLanguage.js"; import { TRANSCRIPT_FILE } from "./transcriptFile.js"; import { findWavChunk } from "./wav.js"; -import type { Word } from "./normalize.js"; +import { parseWhisperJson, type Word } from "./normalize.js"; import { emitWords } from "./progress.js"; import { probeAudio } from "../audio/analysis/decode.js"; import { decodableAudioStream } from "@hyperframes/parsers/media-streams"; @@ -578,7 +578,7 @@ export async function transcribe( throw new Error("Whisper did not produce output. Check the input file."); } - const transcript = JSON.parse(readFileSync(transcriptPath, "utf-8")); + const transcript = parseWhisperJson(readFileSync(transcriptPath, "utf-8")); const reportedLanguage: unknown = transcript.result?.language; const detectedLanguage = automaticLanguage && typeof reportedLanguage === "string" && reportedLanguage.length > 0