mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user