Skip to content

Commit e21ab02

Browse files
fix(cli): preserve transcribed words before speech onset (#5164)
1 parent 9f09861 commit e21ab02

4 files changed

Lines changed: 30 additions & 66 deletions

File tree

‎packages/cli/src/commands/transcribe.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,34 @@ describe("transcribe command", () => {
112112
vi.unstubAllEnvs();
113113
});
114114

115+
it("keeps quiet Spanish words before the relative speech onset in transcript and captions", async () => {
116+
const { dir, input } = dummyAudio();
117+
dirs.push(dir);
118+
const words = [
119+
{ id: "w0", text: "Siempre", start: 0.2, end: 0.49 },
120+
{ id: "w1", text: "que", start: 0.49, end: 0.7 },
121+
{ id: "w2", text: "los", start: 0.7, end: 0.91 },
122+
{ id: "w3", text: "nietos", start: 0.91, end: 1.33 },
123+
{ id: "w4", text: "vecindario", start: 10.03, end: 11.02 },
124+
];
125+
const htmlPath = join(dir, "index.html");
126+
writeFileSync(htmlPath, "<script>const TRANSCRIPT = [];</script>");
127+
transcribeMock.mockImplementation(async (_input, outputDir) => {
128+
const result = fakeTranscript(outputDir, "whisper");
129+
writeFileSync(result.transcriptPath, JSON.stringify(words));
130+
return { ...result, wordCount: 5, durationSeconds: 50, speechOnsetSeconds: 10 };
131+
});
132+
133+
await runCommand(transcribeCmd, {
134+
rawArgs: [input, "--dir", dir, "--engine", "whisper", "--language", "es", "--json"],
135+
});
136+
137+
expect(JSON.parse(readFileSync(join(dir, "transcript.json"), "utf8"))).toEqual(words);
138+
const captions = readFileSync(htmlPath, "utf8").match(/const TRANSCRIPT = ([\s\S]*?);/)![1]!;
139+
expect(JSON.parse(captions)).toEqual(words);
140+
expect(lastJson()).toMatchObject({ ok: true, wordCount: 5, speechOnsetSeconds: 10 });
141+
});
142+
115143
it("keeps typed progress on stderr and reports resolved metadata in one stdout result", async () => {
116144
const { dir, input } = dummyAudio();
117145
dirs.push(dir);

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ async function transcribeAudio(
309309
},
310310
): Promise<void> {
311311
const { transcribe } = await import("../whisper/transcribe.js");
312-
const { loadTranscript, patchCaptionHtml, stripBeforeOnset } =
313-
await import("../whisper/normalize.js");
312+
const { loadTranscript, patchCaptionHtml } = await import("../whisper/normalize.js");
314313

315314
const {
316315
DecodeCancelled,
@@ -411,18 +410,7 @@ async function transcribeAudio(
411410
}
412411
}
413412

414-
let { words } = loadTranscript(result.transcriptPath);
415-
416-
if (result.speechOnsetSeconds != null) {
417-
const before = words.length;
418-
words = stripBeforeOnset(words, result.speechOnsetSeconds);
419-
const stripped = before - words.length;
420-
if (stripped > 0 && !opts.json) {
421-
spin?.message(
422-
`Stripped ${stripped} words before speech onset at ${result.speechOnsetSeconds.toFixed(1)}s`,
423-
);
424-
}
425-
}
413+
const { words } = loadTranscript(result.transcriptPath);
426414

427415
writeFileSync(result.transcriptPath, JSON.stringify(words, null, 2));
428416
patchCaptionHtml(dir, words);

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
loadTranscript,
77
detectFormat,
88
patchCaptionHtml,
9-
stripBeforeOnset,
109
formatSrt,
1110
formatVtt,
1211
wordsToCues,
@@ -671,44 +670,3 @@ describe("detectSpeechOnset", () => {
671670
expect(onset).toBeNull();
672671
});
673672
});
674-
675-
describe("stripBeforeOnset", () => {
676-
it("removes words before onset time", () => {
677-
const words = [
678-
{ text: "ghost", start: 0.5, end: 2.0 },
679-
{ text: "alone", start: 3.0, end: 5.0 },
680-
{ text: "Given", start: 19.0, end: 19.5 },
681-
{ text: "the", start: 19.5, end: 20.0 },
682-
];
683-
const result = stripBeforeOnset(words, 18.5);
684-
expect(result).toHaveLength(2);
685-
expect(result[0]!.text).toBe("Given");
686-
});
687-
688-
it("keeps words within 0.5s tolerance of onset", () => {
689-
const words = [
690-
{ text: "hello", start: 18.2, end: 18.8 },
691-
{ text: "world", start: 19.0, end: 19.5 },
692-
];
693-
const result = stripBeforeOnset(words, 18.5);
694-
expect(result).toHaveLength(2);
695-
});
696-
697-
it("keeps everything when onset is 0", () => {
698-
const words = [
699-
{ text: "hello", start: 0.1, end: 0.5 },
700-
{ text: "world", start: 0.6, end: 1.0 },
701-
];
702-
const result = stripBeforeOnset(words, 0);
703-
expect(result).toHaveLength(2);
704-
});
705-
706-
it("returns empty array when all words are before onset", () => {
707-
const words = [
708-
{ text: "ghost", start: 0.5, end: 2.0 },
709-
{ text: "alone", start: 3.0, end: 5.0 },
710-
];
711-
const result = stripBeforeOnset(words, 20.0);
712-
expect(result).toHaveLength(0);
713-
});
714-
});

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -525,16 +525,6 @@ export function loadTranscript(filePath: string): { words: Word[]; format: Trans
525525
return { words, format };
526526
}
527527

528-
/**
529-
* Remove words that fall before the detected speech onset.
530-
* Whisper can hallucinate words over non-speech sections at the start of audio.
531-
*/
532-
export function stripBeforeOnset(words: Word[], onsetSeconds: number): Word[] {
533-
// 0.5s tolerance: keep words whose timestamps straddle the onset boundary,
534-
// since whisper may assign a slightly early start to the first spoken word.
535-
return words.filter((w) => w.start >= onsetSeconds - 0.5);
536-
}
537-
538528
export function patchCaptionHtml(dir: string, words: Word[]): void {
539529
if (words.length === 0) return;
540530

0 commit comments

Comments
 (0)