Skip to content

🎥 Make videos programmatically with React

Merge remote-tracking branch 'origin/main' into fix/srt-arrow-caption-text

JonnyBurger046c64c29288
diff --cc packages/captions/src/parse-srt.ts
index 942330c846,6e9872a736..bf6dce3281
--- a/packages/captions/src/parse-srt.ts
+++ b/packages/captions/src/parse-srt.ts
@@@ -52,16 -52,17 +52,16 @@@ export const parseSrt = ({input}: Parse
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;
+ i++;
} else if (line?.trim() === '') {
if (captions.length > 0) {
(captions[captions.length - 1] as Caption).text = (
diff --cc packages/captions/src/test/srt.test.ts
index 116933caca,7b50a2a8c8..fa4c2ff82b
--- a/packages/captions/src/test/srt.test.ts
+++ b/packages/captions/src/test/srt.test.ts
@@@ -47,18 -47,18 +47,30 @@@ test('Should create captions', () =>
expect(serialized).toEqual(input);
});
+test.each(['Left --> right\nAnother line', '00:00:03,000 --> 00:00:04,000'])(
+ 'Keeps arrows in SRT caption text: %s',
+ (text) => {
+ const srt = `1\n00:00:00,000 --> 00:00:01,000\n${text}`;
+ const {captions} = parseSrt({input: srt});
+
+ expect(captions).toHaveLength(1);
+ expect(captions[0].text).toBe(text);
+ expect(serializeSrt({lines: [captions]})).toBe(srt);
+ },
+);
+
+ 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: [