Skip to content

Commit 9c1d3fb

Browse files
fix(cli): preserve subtitle cues across line endings (#5223)
* fix(cli): preserve subtitle cues across line endings * refactor(cli): preserve newline handling while reconciling subtitle tests * refactor(cli): normalize WebVTT line endings at the parser * style(cli): format the retained subtitle test block
1 parent 9e69821 commit 9c1d3fb

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,47 @@ Short format
280280
});
281281
});
282282

283+
describe("subtitle line endings", () => {
284+
it.each([
285+
["srt", "\r\n"],
286+
["srt", "\r"],
287+
["vtt", "\r\n"],
288+
["vtt", "\r"],
289+
])("keeps separate %s cues with %j line endings", (ext, newline) => {
290+
const timestamp = ext === "srt" ? "," : ".";
291+
const lines = [
292+
...(ext === "vtt" ? ["WEBVTT", ""] : []),
293+
"1",
294+
`00:00:01${timestamp}000 --> 00:00:03${timestamp}500`,
295+
"<b>Hello</b> world",
296+
"Again",
297+
"",
298+
"2",
299+
`00:00:04${timestamp}000 --> 00:00:06${timestamp}000`,
300+
"How are you",
301+
"",
302+
];
303+
const { words, format } = loadTranscript(tmpFile(`captions.${ext}`, lines.join(newline)));
304+
expect(format).toBe(ext);
305+
expect(words).toEqual([
306+
{ text: "Hello world Again", start: 1, end: 3.5, id: "w0" },
307+
{ text: "How are you", start: 4, end: 6, id: "w1" },
308+
]);
309+
expect(formatSrt(words, { preGrouped: true })).toBe(
310+
"1\n00:00:01,000 --> 00:00:03,500\nHello world Again\n\n2\n00:00:04,000 --> 00:00:06,000\nHow are you\n",
311+
);
312+
});
313+
314+
it("preserves WebVTT cues when newline forms are mixed", () => {
315+
const source =
316+
"WEBVTT\r\n\r\n00:01.000 --> 00:02.000\rFirst phrase\r\r00:03.000 --> 00:04.000\nSecond phrase\n";
317+
expect(loadTranscript(tmpFile("mixed.vtt", source)).words).toEqual([
318+
{ text: "First phrase", start: 1, end: 2, id: "w0" },
319+
{ text: "Second phrase", start: 3, end: 4, id: "w1" },
320+
]);
321+
});
322+
});
323+
283324
describe("caption formatting", () => {
284325
it.each([
285326
["R&D <config> next", "R&amp;D &lt;config&gt; next"],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function parseOpenAI(data: Record<string, unknown>): Word[] {
190190
function parseSrt(content: string): Word[] {
191191
// SRT doesn't have word-level timestamps — parse as phrase-level entries.
192192
// Each cue becomes one "word" entry (the full phrase).
193-
const blocks = content.trim().split(/\n\n+/);
193+
const blocks = content.replace(/\r\n?/g, "\n").trim().split(/\n\n+/);
194194
const words: Word[] = [];
195195

196196
for (const block of blocks) {
@@ -227,7 +227,7 @@ function decodeVttText(text: string): string {
227227
}
228228

229229
function parseVtt(content: string): Word[] {
230-
const body = content.replace(/^WEBVTT[^\n]*\n/, "");
230+
const body = content.replace(/\r\n?/g, "\n").replace(/^WEBVTT[^\n]*\n/, "");
231231
// VTT is structurally similar to SRT (without numeric indices)
232232
const blocks = body.trim().split(/\n\n+/);
233233
const words: Word[] = [];

0 commit comments

Comments
 (0)