Skip to content

🎥 Make videos programmatically with React

Preserve spaces when splitting multi-word captions

Muhammed Furkan Akıncıe69145f32a0b
diff --git a/packages/captions/src/ensure-max-characters-per-line.ts b/packages/captions/src/ensure-max-characters-per-line.ts
index 5c4e933324..c21bbd9a2c 100644
--- a/packages/captions/src/ensure-max-characters-per-line.ts
+++ b/packages/captions/src/ensure-max-characters-per-line.ts
@@ -10,7 +10,7 @@ const splitWords = (inputCaptions: Caption[]): Caption[] => {
for (let j = 0; j < words.length; j++) {
const word = words[j];
captions.push({
- text: j === 0 ? ` ${word}` : word,
+ text: ` ${word}`,
startMs: w.startMs,
endMs: w.endMs,
confidence: w.confidence,
diff --git a/packages/captions/src/test/ensure-max-characters.test.ts b/packages/captions/src/test/ensure-max-characters.test.ts
index 099de3582c..0590957595 100644
--- a/packages/captions/src/test/ensure-max-characters.test.ts
+++ b/packages/captions/src/test/ensure-max-characters.test.ts
@@ -20,145 +20,50 @@ test('Ensure max characters per line', () => {
},
];
- // TODO: This creates captions that are not nice!
expect(ensureMaxCharactersPerLine({captions, maxCharsPerLine: 42})).toEqual({
segments: [
[
- {
- text: ' This',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: 'is',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: 'a',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: 'demonstration',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: 'of',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: 'SRT',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: 'subtitles.',
- startMs: 3000,
- endMs: 6000,
- confidence: 1,
- timestampMs: 4500,
- },
- {
- text: ' You',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- ],
- [
- {
- text: 'can',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'use',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'SRT',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'files',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'to',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'add',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'subtitles',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- ],
- [
- {
- text: 'to',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'your',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- {
- text: 'videos.',
- startMs: 7000,
- endMs: 10500,
- confidence: 1,
- timestampMs: 8750,
- },
- ],
+ ' This',
+ ' is',
+ ' a',
+ ' demonstration',
+ ' of',
+ ' SRT',
+ ' subtitles.',
+ ].map((text) => ({...captions[0], text})),
+ [' You', ' can', ' use', ' SRT', ' files', ' to', ' add'].map((text) => ({
+ ...captions[1],
+ text,
+ })),
+ [' subtitles', ' to', ' your', ' videos.'].map((text) => ({
+ ...captions[1],
+ text,
+ })),
],
});
});
+test('Preserves spaces between words within a caption', () => {
+ const caption: Caption = {
+ text: 'Hello world again',
+ startMs: 0,
+ endMs: 1000,
+ timestampMs: 500,
+ confidence: 1,
+ };
+ const {segments} = ensureMaxCharactersPerLine({
+ captions: [caption],
+ maxCharsPerLine: 100,
+ });
+
+ expect(
+ segments[0]
+ .map((word) => word.text)
+ .join('')
+ .trim(),
+ ).toBe(caption.text);
+});
+
test('Does not emit standalone whitespace captions', () => {
const captions: Caption[] = [
{
@@ -174,11 +79,11 @@ test('Does not emit standalone whitespace captions', () => {
segments: [
[
{...captions[0], text: ' Using'},
- {...captions[0], text: "Remotion's"},
+ {...captions[0], text: " Remotion's"},
],
[
- {...captions[0], text: 'TikTok'},
- {...captions[0], text: 'template,'},
+ {...captions[0], text: ' TikTok'},
+ {...captions[0], text: ' template,'},
],
],
});
@@ -216,7 +121,7 @@ test('Preserves a forced page break on the final split word', () => {
text: ' Hello',
timestampMs: 500,
},
- {...firstCaption, text: 'there'},
+ {...firstCaption, text: ' there'},
],
[{...secondCaption, text: ' Remotion'}],
],