🎥 Make videos programmatically with React
Preserve exact milliseconds when parsing SRT timestamps
Muhammed Furkan Akıncı2e0ceacb916b
diff --git a/packages/captions/src/parse-srt.ts b/packages/captions/src/parse-srt.tsindex 31066b7e67..6e9872a736 100644--- a/packages/captions/src/parse-srt.ts+++ b/packages/captions/src/parse-srt.ts@@ -1,6 +1,6 @@import type {Caption} from './caption';-function toSeconds(time: string) {+function toMilliseconds(time: string) {const [first, second, third] = time.split(':');if (!first) {throw new Error(`Invalid timestamp:${time}`);@@ -24,10 +24,10 @@ function toSeconds(time: string) {}return (- parseInt(first, 10) * 3600 +- parseInt(second, 10) * 60 +- parseInt(seconds, 10) +- parseInt(millis, 10) / 1000+ parseInt(first, 10) * 3600000 ++ parseInt(second, 10) * 60000 ++ parseInt(seconds, 10) * 1000 ++ parseInt(millis, 10));}@@ -52,14 +52,14 @@ export const parseSrt = ({input}: ParseSrtInput): ParseSrtOutput => {const nextLine = inputLines[i + 1];if (line?.match(/^\s*\d+\s*$/) && nextLine?.includes(' --> ')) {const nextLineSplit = nextLine.split(' --> ');- const start = toSeconds((nextLineSplit[0] as string).trim());- const end = toSeconds((nextLineSplit[1] as string).trim());+ const start = toMilliseconds((nextLineSplit[0] as string).trim());+ const end = toMilliseconds((nextLineSplit[1] as string).trim());captions.push({text: '',- startMs: start * 1000,- endMs: end * 1000,+ startMs: start,+ endMs: end,confidence: 1,- timestampMs: ((start + end) / 2) * 1000,+ timestampMs: (start + end) / 2,});} else if (line?.includes(' --> ')) {continue;diff --git a/packages/captions/src/test/srt.test.ts b/packages/captions/src/test/srt.test.tsindex 7754a5b844..7b50a2a8c8 100644--- a/packages/captions/src/test/srt.test.ts+++ b/packages/captions/src/test/srt.test.ts@@ -47,6 +47,18 @@ test('Should create captions', () => {expect(serialized).toEqual(input);});+test('Preserves exact milliseconds when parsing and serializing SRT', () => {+ const srt = '1\n00:00:01,001 --> 00:00:01,003\nHello';+ const {captions} = parseSrt({input: srt});++ expect(captions[0]).toMatchObject({+ startMs: 1001,+ endMs: 1003,+ timestampMs: 1002,+ });+ expect(serializeSrt({lines: [captions]})).toBe(srt);+});+test('Should start a new SRT cue after a forced page break', () => {const serialized = serializeSrt({lines: [