Skip to content

Commit f5aeecd

Browse files
fix(cli): retain WebVTT timestamps before cue settings (#5224)
1 parent 2295ed5 commit f5aeecd

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ describe("detectFormat", () => {
8383
});
8484

8585
describe("loadTranscript", () => {
86+
it.each([
87+
["00:00:01.000", "00:00:03.500", "align:start"],
88+
["00:01.000", "00:03.500", "align:start"],
89+
["00:00:01.000", "00:00:03.500", "line:90% position:50%,center size:80% align:center"],
90+
["00:01.000", "00:03.500", "\tvertical:rl\tline:0"],
91+
["00:00:01.000", "00:00:03.500", "region:captions"],
92+
])("retains VTT end time before cue settings: %s --> %s %s", (start, end, settings) => {
93+
const source = `WEBVTT\n\nfirst\n${start} --> ${end} ${settings}\nFirst phrase\n\nsecond\n00:04.000 --> 00:06.000\nSecond phrase\n`;
94+
const { words } = loadTranscript(tmpFile("settings.vtt", source));
95+
expect(words).toEqual([
96+
{ text: "First phrase", start: 1, end: 3.5, id: "w0" },
97+
{ text: "Second phrase", start: 4, end: 6, id: "w1" },
98+
]);
99+
expect(formatSrt(words, { preGrouped: true })).toBe(
100+
"1\n00:00:01,000 --> 00:00:03,500\nFirst phrase\n\n2\n00:00:04,000 --> 00:00:06,000\nSecond phrase\n",
101+
);
102+
});
103+
86104
it("reads an empty word list as a transcript with no words", () => {
87105
expect(loadTranscript(tmpFile("transcript.json", "[]"))).toEqual({
88106
words: [],

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ function parseSrtTimestamp(ts: string): number {
266266

267267
/** Parse VTT timestamp: 00:01:23.456 or 01:23.456 → seconds */
268268
function parseVttTimestamp(ts: string): number {
269-
const parts = ts.split(":");
270-
if (parts.length === 3) return parseSrtTimestamp(ts);
269+
const timestamp = ts.split(/[ \t]+/, 1)[0] ?? "";
270+
const parts = timestamp.split(":");
271+
if (parts.length === 3) return parseSrtTimestamp(timestamp);
271272
// MM:SS.mmm
272273
if (parts.length === 2) {
273274
const [min, secMs] = parts;

0 commit comments

Comments
 (0)