Skip to content

Commit 1e52c90

Browse files
committed
fix(cli): transcribe also reads a Windows whisper's -nan(ind) as no value
1 parent d53a053 commit 1e52c90

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe("loadTranscript", () => {
124124
);
125125
});
126126

127-
it("reads whisper-cli output whose floats are nan, -nan or inf", () => {
127+
it("reads whisper-cli output whose floats are nan, -nan, inf or MSVC's -nan(ind)", () => {
128128
// Token objects as whisper.cpp's cli.cpp writes them: tab-indented, floats via iostream.
129129
const token = (text: string, from: number, to: number, p: string) =>
130130
[
@@ -147,13 +147,15 @@ describe("loadTranscript", () => {
147147
token(" nan", 0, 500, "nan"),
148148
token(" world", 600, 1200, "-nan"),
149149
token(" again", 1300, 1800, "inf"),
150+
token(" later", 1900, 2400, "-nan(ind)"),
150151
].join(",\n")}\n\t\t\t]\n\t\t}\n\t]\n}\n`;
151152
const { words, format } = loadTranscript(tmpFile("transcript.json", raw));
152153
expect(format).toBe("whisper-cpp");
153154
expect(words).toEqual([
154155
{ text: "nan", start: 0, end: 0.5, id: "w0" },
155156
{ text: "world", start: 0.6, end: 1.2, id: "w1" },
156157
{ text: "again", start: 1.3, end: 1.8, id: "w2" },
158+
{ text: "later", start: 1.9, end: 2.4, id: "w3" },
157159
]);
158160
});
159161

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function detectFormat(filePath: string): TranscriptFormat {
4141
const ext = extname(filePath).toLowerCase();
4242
if (ext === ".srt") return "srt";
4343
if (ext === ".vtt") return "vtt";
44-
if (ext === ".json") return detectJsonFormat(JSON.parse(readJsonFileText(filePath)));
44+
if (ext === ".json") return detectJsonFormat(parseWhisperJson(readJsonFileText(filePath)));
4545
throw new Error(`Unsupported transcript file extension: ${ext}. Use .json, .srt, or .vtt`);
4646
}
4747

@@ -499,11 +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;
502+
const BARE_NAN_OR_INF_OUTSIDE_STRINGS =
503+
/"(?:[^"\\]|\\.)*"|-?\b(?:nan(?:\([^)]*\))?|inf(?:inity)?)(?!\w)/gi;
503504

504-
/** whisper-cli prints a NaN or infinite float as bare `nan`/`-nan`/`inf`; read those as null. */
505505
export function parseWhisperJson(text: string): any {
506-
return JSON.parse(text.replace(NON_FINITE, (m) => (m.startsWith('"') ? m : "null")));
506+
return JSON.parse(
507+
text.replace(BARE_NAN_OR_INF_OUTSIDE_STRINGS, (m) => (m.startsWith('"') ? m : "null")),
508+
);
507509
}
508510

509511
/**

0 commit comments

Comments
 (0)