Skip to content

Commit d53a053

Browse files
committed
fix(cli): transcribe reads whisper output with nan or inf values
1 parent 341fdd3 commit d53a053

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎packages/cli/src/whisper/normalize.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,39 @@ describe("loadTranscript", () => {
124124
);
125125
});
126126

127+
it("reads whisper-cli output whose floats are nan, -nan or inf", () => {
128+
// Token objects as whisper.cpp's cli.cpp writes them: tab-indented, floats via iostream.
129+
const token = (text: string, from: number, to: number, p: string) =>
130+
[
131+
"\t\t\t{",
132+
`\t\t\t\t"text": "${text}",`,
133+
`\t\t\t\t"timestamps": {`,
134+
`\t\t\t\t\t"from": "00:00:00.000",`,
135+
`\t\t\t\t\t"to": "00:00:00.500"`,
136+
`\t\t\t\t},`,
137+
`\t\t\t\t"offsets": {`,
138+
`\t\t\t\t\t"from": ${from},`,
139+
`\t\t\t\t\t"to": ${to}`,
140+
`\t\t\t\t},`,
141+
`\t\t\t\t"id": 50364,`,
142+
`\t\t\t\t"p": ${p},`,
143+
`\t\t\t\t"t_dtw": -1`,
144+
"\t\t\t}",
145+
].join("\n");
146+
const raw = `{\n\t"transcription": [\n\t\t{\n\t\t\t"tokens": [\n${[
147+
token(" nan", 0, 500, "nan"),
148+
token(" world", 600, 1200, "-nan"),
149+
token(" again", 1300, 1800, "inf"),
150+
].join(",\n")}\n\t\t\t]\n\t\t}\n\t]\n}\n`;
151+
const { words, format } = loadTranscript(tmpFile("transcript.json", raw));
152+
expect(format).toBe("whisper-cpp");
153+
expect(words).toEqual([
154+
{ text: "nan", start: 0, end: 0.5, id: "w0" },
155+
{ text: "world", start: 0.6, end: 1.2, id: "w1" },
156+
{ text: "again", start: 1.3, end: 1.8, id: "w2" },
157+
]);
158+
});
159+
127160
it("parses whisper-cpp JSON with punctuation merging", () => {
128161
const path = tmpFile(
129162
"transcript.json",

‎packages/cli/src/whisper/normalize.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ export function formatVtt(words: Word[], opts?: WordsToCuesOptions): string {
499499
// Public API
500500
// ---------------------------------------------------------------------------
501501

502+
const NON_FINITE = /"(?:[^"\\]|\\.)*"|-?\b(?:nan(?:\([^)]*\))?|inf(?:inity)?)\b/gi;
503+
504+
/** whisper-cli prints a NaN or infinite float as bare `nan`/`-nan`/`inf`; read those as null. */
505+
export function parseWhisperJson(text: string): any {
506+
return JSON.parse(text.replace(NON_FINITE, (m) => (m.startsWith('"') ? m : "null")));
507+
}
508+
502509
/**
503510
* Load and normalize a transcript file to a standard word array.
504511
*
@@ -523,7 +530,7 @@ export function loadTranscript(filePath: string): { words: Word[]; format: Trans
523530
}
524531

525532
// JSON formats — parse once, detect, then extract words
526-
const parsed = JSON.parse(content);
533+
const parsed = parseWhisperJson(content);
527534
const format = detectJsonFormat(parsed);
528535

529536
// JSON parsers never set id — assign w{index} like the srt/vtt branches above

‎packages/cli/src/whisper/transcribe.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { sha256File } from "./modelFile.js";
2222
import { initialModelForLanguage } from "./modelForLanguage.js";
2323
import { TRANSCRIPT_FILE } from "./transcriptFile.js";
2424
import { findWavChunk } from "./wav.js";
25-
import type { Word } from "./normalize.js";
25+
import { parseWhisperJson, type Word } from "./normalize.js";
2626
import { emitWords } from "./progress.js";
2727
import { probeAudio } from "../audio/analysis/decode.js";
2828
import { decodableAudioStream } from "@hyperframes/parsers/media-streams";
@@ -578,7 +578,7 @@ export async function transcribe(
578578
throw new Error("Whisper did not produce output. Check the input file.");
579579
}
580580

581-
const transcript = JSON.parse(readFileSync(transcriptPath, "utf-8"));
581+
const transcript = parseWhisperJson(readFileSync(transcriptPath, "utf-8"));
582582
const reportedLanguage: unknown = transcript.result?.language;
583583
const detectedLanguage =
584584
automaticLanguage && typeof reportedLanguage === "string" && reportedLanguage.length > 0

0 commit comments

Comments
 (0)