Skip to content

🎥 Make videos programmatically with React

`@remotion/captions`: Add forced line breaks

Jonny Burger4796cd3291ff
diff --git a/packages/brand/src/Compose/Captions.tsx b/packages/brand/src/Compose/Captions.tsx
index b05bbbda52..8e8b58ca24 100644
--- a/packages/brand/src/Compose/Captions.tsx
+++ b/packages/brand/src/Compose/Captions.tsx
@@ -12,7 +12,8 @@ import {
const Token: React.FC<{
readonly token: TikTokToken;
readonly pageStart: number;
-}> = ({token, pageStart}) => {
+ readonly isLast: boolean;
+}> = ({token, pageStart, isLast}) => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
const start = ((token.fromMs - pageStart) / 1000) * 30;
@@ -49,10 +50,13 @@ const Token: React.FC<{
}, [active, jump]);
return (
- <span>
- {' '}
- <span style={style}>{token.text.trim()}</span>
- </span>
+ <>
+ <span>
+ {' '}
+ <span style={style}>{token.text.trim()}</span>
+ </span>
+ {token.lineBreakAfter && !isLast ? <br /> : null}
+ </>
);
};
@@ -95,9 +99,14 @@ export const Captions: React.FC = () => {
layout="none"
>
<div key={page.startMs}>
- {page.tokens.map((t) => {
+ {page.tokens.map((t, index) => {
return (
- <Token key={t.fromMs} pageStart={page.startMs} token={t} />
+ <Token
+ key={t.fromMs}
+ pageStart={page.startMs}
+ token={t}
+ isLast={index === page.tokens.length - 1}
+ />
);
})}
</div>
diff --git a/packages/captions/src/caption.ts b/packages/captions/src/caption.ts
index 28c5d0def1..a892b9d4cb 100644
--- a/packages/captions/src/caption.ts
+++ b/packages/captions/src/caption.ts
@@ -4,4 +4,5 @@ export type Caption = {
endMs: number;
timestampMs: number | null;
confidence: number | null;
+ lineBreakAfter?: boolean;
};
diff --git a/packages/captions/src/create-tiktok-style-captions.ts b/packages/captions/src/create-tiktok-style-captions.ts
index 5b96e8e7bd..adf0eac333 100644
--- a/packages/captions/src/create-tiktok-style-captions.ts
+++ b/packages/captions/src/create-tiktok-style-captions.ts
@@ -4,6 +4,7 @@ export type TikTokToken = {
text: string;
fromMs: number;
toMs: number;
+ lineBreakAfter?: boolean;
};
export type TikTokPage = {
@@ -35,8 +36,11 @@ export const createTikTokStyleCaptions = ({
let currentTo = 0;
const add = () => {
+ const text = currentText.endsWith('\n')
+ ? currentText.slice(0, -1)
+ : currentText;
tikTokStyleCaptions.push({
- text: currentText.trimStart(),
+ text: text.trimStart(),
startMs: currentFrom,
tokens: currentTokens,
durationMs: Infinity,
@@ -65,9 +69,14 @@ export const createTikTokStyleCaptions = ({
}
// Start a new sentence
- currentText = text.trimStart();
+ currentText = text.trimStart() + (item.lineBreakAfter ? '\n' : '');
currentTokens = [
- {text: currentText, fromMs: item.startMs, toMs: item.endMs},
+ {
+ text: text.trimStart(),
+ fromMs: item.startMs,
+ toMs: item.endMs,
+ ...(item.lineBreakAfter ? {lineBreakAfter: true} : {}),
+ },
].filter((t) => t.text !== '');
currentFrom = item.startMs;
currentTo = item.endMs;
@@ -78,16 +87,24 @@ export const createTikTokStyleCaptions = ({
currentFrom = item.startMs;
}
- currentText += text;
+ const followsLineBreak = currentText.endsWith('\n');
+ const textToAppend = followsLineBreak ? text.trimStart() : text;
+ currentText += textToAppend;
currentText = currentText.trimStart();
if (text.trim() !== '') {
currentTokens.push({
- text: currentTokens.length === 0 ? currentText.trimStart() : text,
+ text:
+ currentTokens.length === 0 ? currentText.trimStart() : textToAppend,
fromMs: item.startMs,
toMs: item.endMs,
+ ...(item.lineBreakAfter ? {lineBreakAfter: true} : {}),
});
}
+ if (item.lineBreakAfter) {
+ currentText += '\n';
+ }
+
currentTo = item.endMs;
}
diff --git a/packages/captions/src/ensure-max-characters-per-line.ts b/packages/captions/src/ensure-max-characters-per-line.ts
index 34dd36507d..e105910f41 100644
--- a/packages/captions/src/ensure-max-characters-per-line.ts
+++ b/packages/captions/src/ensure-max-characters-per-line.ts
@@ -15,6 +15,9 @@ const splitWords = (inputCaptions: Caption[]): Caption[] => {
endMs: w.endMs,
confidence: w.confidence,
timestampMs: w.timestampMs,
+ ...(j === words.length - 1 && w.lineBreakAfter
+ ? {lineBreakAfter: true}
+ : {}),
});
}
}
@@ -59,8 +62,15 @@ export const ensureMaxCharactersPerLine = ({
}
currentSegment.push(w);
+ if (w.lineBreakAfter && i < splitted.length - 1) {
+ segments.push(currentSegment);
+ currentSegment = [];
+ }
+ }
+
+ if (currentSegment.length > 0) {
+ segments.push(currentSegment);
}
- segments.push(currentSegment);
return {segments};
};
diff --git a/packages/captions/src/serialize-srt.ts b/packages/captions/src/serialize-srt.ts
index 94402a99d3..d241994052 100644
--- a/packages/captions/src/serialize-srt.ts
+++ b/packages/captions/src/serialize-srt.ts
@@ -35,7 +35,14 @@ export const serializeSrt = ({lines}: SerializeSrtInput) => {
currentIndex,
formatSrtTimestamp(firstTimestamp, lastTimestamp),
// Text
- s.map((caption) => caption.text).join(''),
+ s
+ .map((caption, index) => {
+ return (
+ caption.text +
+ (caption.lineBreakAfter && index < s.length - 1 ? '\n' : '')
+ );
+ })
+ .join(''),
].join('\n');
})
.filter(Boolean)
diff --git a/packages/captions/src/test/ensure-max-characters.test.ts b/packages/captions/src/test/ensure-max-characters.test.ts
index a71952fda2..5477485c6a 100644
--- a/packages/captions/src/test/ensure-max-characters.test.ts
+++ b/packages/captions/src/test/ensure-max-characters.test.ts
@@ -183,3 +183,42 @@ test('Does not emit standalone whitespace captions', () => {
],
});
});
+
+test('Preserves a forced line break on the final split word', () => {
+ const firstCaption: Caption = {
+ confidence: 1,
+ endMs: 1000,
+ startMs: 0,
+ text: ' Hello there',
+ timestampMs: 500,
+ lineBreakAfter: true,
+ };
+ const secondCaption: Caption = {
+ confidence: 1,
+ endMs: 2000,
+ startMs: 1000,
+ text: ' Remotion',
+ timestampMs: 1500,
+ };
+
+ expect(
+ ensureMaxCharactersPerLine({
+ captions: [firstCaption, secondCaption],
+ maxCharsPerLine: 100,
+ }),
+ ).toEqual({
+ segments: [
+ [
+ {
+ confidence: 1,
+ endMs: 1000,
+ startMs: 0,
+ text: ' Hello',
+ timestampMs: 500,
+ },
+ {...firstCaption, text: 'there'},
+ ],
+ [{...secondCaption, text: ' Remotion'}],
+ ],
+ });
+});
diff --git a/packages/captions/src/test/srt.test.ts b/packages/captions/src/test/srt.test.ts
index 883e1ed40d..4264070ba0 100644
--- a/packages/captions/src/test/srt.test.ts
+++ b/packages/captions/src/test/srt.test.ts
@@ -46,3 +46,31 @@ test('Should create captions', () => {
const serialized = serializeSrt({lines: captions.map((c) => [c])});
expect(serialized).toEqual(input);
});
+
+test('Should serialize forced line breaks within an SRT cue', () => {
+ const serialized = serializeSrt({
+ lines: [
+ [
+ {
+ text: 'First line',
+ startMs: 0,
+ endMs: 500,
+ timestampMs: 250,
+ confidence: 1,
+ lineBreakAfter: true,
+ },
+ {
+ text: 'Second line',
+ startMs: 500,
+ endMs: 1000,
+ timestampMs: 750,
+ confidence: 1,
+ },
+ ],
+ ],
+ });
+
+ expect(serialized).toBe(
+ '1\n00:00:00,000 --> 00:00:01,000\nFirst line\nSecond line',
+ );
+});
diff --git a/packages/captions/src/test/tiktok.test.ts b/packages/captions/src/test/tiktok.test.ts
index 4e51d75eff..74e941906c 100644
--- a/packages/captions/src/test/tiktok.test.ts
+++ b/packages/captions/src/test/tiktok.test.ts
@@ -76,6 +76,46 @@ test('Should create captions', () => {
]);
});
+test('Should preserve forced line breaks without creating a new page', () => {
+ const {pages} = createTikTokStyleCaptions({
+ captions: [
+ {
+ text: 'hello',
+ startMs: 0,
+ endMs: 200,
+ timestampMs: 100,
+ confidence: null,
+ lineBreakAfter: true,
+ },
+ {
+ text: ' there',
+ startMs: 200,
+ endMs: 400,
+ timestampMs: 300,
+ confidence: null,
+ },
+ ],
+ combineTokensWithinMilliseconds: 1000,
+ });
+
+ expect(pages).toEqual([
+ {
+ text: 'hello\nthere',
+ startMs: 0,
+ durationMs: 400,
+ tokens: [
+ {
+ text: 'hello',
+ fromMs: 0,
+ toMs: 200,
+ lineBreakAfter: true,
+ },
+ {text: 'there', fromMs: 200, toMs: 400},
+ ],
+ },
+ ]);
+});
+
test('Should finalize the duration when captions end in whitespace', () => {
const {pages} = createTikTokStyleCaptions({
captions: [
diff --git a/packages/convert/app/components/transcribe/display.tsx b/packages/convert/app/components/transcribe/display.tsx
index 01fe3f4405..ed240fa858 100644
--- a/packages/convert/app/components/transcribe/display.tsx
+++ b/packages/convert/app/components/transcribe/display.tsx
@@ -1,7 +1,7 @@
/* eslint-disable react/no-array-index-key */
import type {Caption} from '@remotion/captions';
import {Switch} from '@remotion/design';
-import {useEffect, useMemo, useRef, useState} from 'react';
+import {Fragment, useEffect, useMemo, useRef, useState} from 'react';
import {Card} from '../ui/card';
const SingleToken: React.FC<{
@@ -95,14 +95,16 @@ export default function Display({
}
return (
- <span
- key={`${c.text}-${c.startMs}-${index}`}
- data-active={time * 1000 >= c.startMs}
- data-last-active={index === lastActiveIndex}
- className="data-[active=true]:font-bold data-[last-active=true]:text-brand"
- >
- {c.text}
- </span>
+ <Fragment key={`${c.text}-${c.startMs}-${index}`}>
+ <span
+ data-active={time * 1000 >= c.startMs}
+ data-last-active={index === lastActiveIndex}
+ className="data-[active=true]:font-bold data-[last-active=true]:text-brand"
+ >
+ {c.text}
+ </span>
+ {c.lineBreakAfter && index < result.length - 1 ? <br /> : null}
+ </Fragment>
);
})}
</div>
diff --git a/packages/docs/docs/captions/caption.mdx b/packages/docs/docs/captions/caption.mdx
index 8e28ac6b2f..ad3e941bf6 100644
--- a/packages/docs/docs/captions/caption.mdx
+++ b/packages/docs/docs/captions/caption.mdx
@@ -45,6 +45,14 @@ Otherwise, it is not defined, but may be the average of the start and end timest
A number between 0 and 1 that indicates how confident the transcription is.
+### `lineBreakAfter?`<AvailableFrom v="4.0.517" />
+
+_boolean_
+
+When `true`, forces a visual line break after this caption if another caption follows in the same displayed page or subtitle cue.
+
+This field does not change caption timing or start a new caption page. When it is `false` or omitted, renderers may wrap the text automatically.
+
## Whitespace sensitivity
The [`text`](#text) field is whitespace sensitive. You should include spaces in it, ideally before each word.
diff --git a/packages/docs/docs/captions/create-tiktok-style-captions.mdx b/packages/docs/docs/captions/create-tiktok-style-captions.mdx
index 8ba8bda60c..4d3b462be0 100644
--- a/packages/docs/docs/captions/create-tiktok-style-captions.mdx
+++ b/packages/docs/docs/captions/create-tiktok-style-captions.mdx
@@ -135,6 +135,9 @@ A page consists of:
- `text`: The text of the token.
- `fromMs`: The absolute start time of the token in milliseconds.
- `toMs`: The absolute end time of the token in milliseconds.
+ - `lineBreakAfter`: Whether a line break should be rendered after the token (_from v4.0.517_).
+
+If a [`Caption`](/docs/captions/caption) has `lineBreakAfter: true`, the corresponding token preserves the marker and the page's `text` contains a newline. A line break does not start a new page.
## Whitespace sensitivity
diff --git a/packages/docs/docs/captions/displaying.mdx b/packages/docs/docs/captions/displaying.mdx
index 4567c76e80..0a644eae43 100644
--- a/packages/docs/docs/captions/displaying.mdx
+++ b/packages/docs/docs/captions/displaying.mdx
@@ -149,18 +149,22 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {
whiteSpace: 'pre',
}}
>
- {page.tokens.map((token) => {
+ {page.tokens.map((token, tokenIndex) => {
const isActive = token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;
return (
- <span
- key={token.fromMs}
- style={{
- color: isActive ? HIGHLIGHT_COLOR : 'white',
- }}
- >
- {token.text}
- </span>
+ <React.Fragment key={`${token.fromMs}-${tokenIndex}`}>
+ <span
+ style={{
+ color: isActive ? HIGHLIGHT_COLOR : 'white',
+ }}
+ >
+ {token.text}
+ </span>
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
+ </React.Fragment>
);
})}
</div>
@@ -204,18 +208,22 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {
whiteSpace: 'pre',
}}
>
- {page.tokens.map((token) => {
+ {page.tokens.map((token, tokenIndex) => {
const isActive = token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;
return (
- <span
- key={token.fromMs}
- style={{
- color: isActive ? HIGHLIGHT_COLOR : 'white',
- }}
- >
- {token.text}
- </span>
+ <React.Fragment key={`${token.fromMs}-${tokenIndex}`}>
+ <span
+ style={{
+ color: isActive ? HIGHLIGHT_COLOR : 'white',
+ }}
+ >
+ {token.text}
+ </span>
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
+ </React.Fragment>
);
})}
</div>
diff --git a/packages/docs/docs/captions/ensure-max-characters-per-line.mdx b/packages/docs/docs/captions/ensure-max-characters-per-line.mdx
index 889f32ef1c..82bb7d3f38 100644
--- a/packages/docs/docs/captions/ensure-max-characters-per-line.mdx
+++ b/packages/docs/docs/captions/ensure-max-characters-per-line.mdx
@@ -8,6 +8,8 @@ crumb: '@remotion/captions'
This is an internal, undocumented API for now.
+Captions with `lineBreakAfter: true` always end the current segment, even if the segment has not reached the maximum number of characters.
+
## See also
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/captions/src/ensure-max-characters-per-line.ts)
diff --git a/packages/docs/docs/captions/exporting.mdx b/packages/docs/docs/captions/exporting.mdx
index a6cd9a61bd..1b16226027 100644
--- a/packages/docs/docs/captions/exporting.mdx
+++ b/packages/docs/docs/captions/exporting.mdx
@@ -92,6 +92,7 @@ const srtContent = serializeSrt({
endMs: token.toMs,
timestampMs: (token.fromMs + token.toMs) / 2,
confidence: null,
+ lineBreakAfter: token.lineBreakAfter,
}));
}),
});
diff --git a/packages/docs/docs/captions/serialize-srt.mdx b/packages/docs/docs/captions/serialize-srt.mdx
index e92ee275ae..75a15be0e3 100644
--- a/packages/docs/docs/captions/serialize-srt.mdx
+++ b/packages/docs/docs/captions/serialize-srt.mdx
@@ -64,6 +64,7 @@ Each top-level item represents a line in the SubRip file.
The second-level items represent the words in that line.
Words get concatenated together during serialization. No spaces are added between the words.
+If a word has `lineBreakAfter: true`, a newline is inserted after it unless it is the final word in the subtitle cue.
The start timestamp is determined from the `startMs` value of the first word in the line.
The end timestamp is determined from the `endMs` value of the last word in the line.
diff --git a/packages/docs/docs/interactivity-schema.mdx b/packages/docs/docs/interactivity-schema.mdx
index 67ba5a8071..379070a581 100644
--- a/packages/docs/docs/interactivity-schema.mdx
+++ b/packages/docs/docs/interactivity-schema.mdx
@@ -119,6 +119,8 @@ Shows a list of values. Array fields are not keyframable.
Marks a prop containing [`Caption[]`](/docs/captions/caption) data for the captions inspector. Caption fields are not keyframable.
+The inspector can edit caption text and toggle [`lineBreakAfter`](/docs/captions/caption#linebreakafter) for each caption.
+
### `rotation-css`
Shows a rotation control backed by a CSS rotation string such as `"15deg"`.
diff --git a/packages/docs/elements/captions/moving-pill-captions/moving-pill-captions.tsx b/packages/docs/elements/captions/moving-pill-captions/moving-pill-captions.tsx
index cffc69a941..0c0037d157 100644
--- a/packages/docs/elements/captions/moving-pill-captions/moving-pill-captions.tsx
+++ b/packages/docs/elements/captions/moving-pill-captions/moving-pill-captions.tsx
@@ -337,6 +337,9 @@ const CaptionPage: React.FC<{
{visibleText}
</span>
{trailingWhitespace}
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
</React.Fragment>
);
})}
diff --git a/packages/docs/elements/captions/popping-word-captions/popping-word-captions.tsx b/packages/docs/elements/captions/popping-word-captions/popping-word-captions.tsx
index c73e077110..6a5b90e19e 100644
--- a/packages/docs/elements/captions/popping-word-captions/popping-word-captions.tsx
+++ b/packages/docs/elements/captions/popping-word-captions/popping-word-captions.tsx
@@ -247,6 +247,9 @@ const CaptionPage: React.FC<{
{visibleText}
</span>
{trailingWhitespace}
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
</React.Fragment>
);
})}
diff --git a/packages/docs/elements/captions/word-highlight-captions/word-highlight-captions.tsx b/packages/docs/elements/captions/word-highlight-captions/word-highlight-captions.tsx
index adac3d873b..eb92e9dbb5 100644
--- a/packages/docs/elements/captions/word-highlight-captions/word-highlight-captions.tsx
+++ b/packages/docs/elements/captions/word-highlight-captions/word-highlight-captions.tsx
@@ -206,6 +206,9 @@ const CaptionPage: React.FC<{
{visibleText}
</span>
{trailingWhitespace}
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
</React.Fragment>
);
})}
diff --git a/packages/example/e2e/captions-inspector.test.mts b/packages/example/e2e/captions-inspector.test.mts
new file mode 100644
index 0000000000..46c662de06
--- /dev/null
+++ b/packages/example/e2e/captions-inspector.test.mts
@@ -0,0 +1,58 @@
+import fs from 'fs';
+import path from 'path';
+import {expect, test} from '@playwright/test';
+import {EXPANDED_SIDEBAR_STATE, STUDIO_URL, exampleDir} from './constants.mts';
+import {startStudio, stopStudio} from './studio-server.mts';
+
+test.use({storageState: EXPANDED_SIDEBAR_STATE});
+
+const inlineCaptionsFile = path.join(
+ exampleDir,
+ 'src',
+ 'CaptionsTester',
+ 'InlineAnimatedCaptions.tsx',
+);
+
+test.describe('captions inspector', () => {
+ let sourceBefore: string;
+
+ test.beforeEach(async () => {
+ sourceBefore = fs.readFileSync(inlineCaptionsFile, 'utf-8');
+ await startStudio();
+ });
+
+ test.afterEach(async () => {
+ await stopStudio();
+ fs.writeFileSync(inlineCaptionsFile, sourceBefore);
+ });
+
+ test('persists a forced line break to an inline caption', async ({page}) => {
+ await page.goto(`${STUDIO_URL}/captions-inspector-e2e`);
+ await expect(page).toHaveURL(/captions-inspector-e2e/, {timeout: 15_000});
+ await page.waitForFunction(
+ () => !document.body.innerText.includes('Loading...'),
+ {timeout: 30_000},
+ );
+
+ const captionsSequence = page
+ .getByText('<AnimatedCaptions>', {exact: true})
+ .first();
+ const lineBreakAfterFirstCaption = page.getByRole('checkbox', {
+ name: 'Line break after caption 1',
+ exact: true,
+ });
+ await expect(async () => {
+ await captionsSequence.click();
+ await expect(lineBreakAfterFirstCaption).toBeVisible({timeout: 1_000});
+ }).toPass({timeout: 30_000});
+
+ await lineBreakAfterFirstCaption.check();
+ await expect
+ .poll(() => {
+ return fs
+ .readFileSync(inlineCaptionsFile, 'utf-8')
+ .includes('lineBreakAfter: true');
+ })
+ .toBe(true);
+ });
+});
diff --git a/packages/example/src/CaptionsTester/AnimatedCaptions.tsx b/packages/example/src/CaptionsTester/AnimatedCaptions.tsx
index d8ab890953..f0b0b10217 100644
--- a/packages/example/src/CaptionsTester/AnimatedCaptions.tsx
+++ b/packages/example/src/CaptionsTester/AnimatedCaptions.tsx
@@ -92,20 +92,26 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {
{page.tokens.map((token, tokenIndex) => {
const isActive =
token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;
+ const startsLine =
+ tokenIndex === 0 || page.tokens[tokenIndex - 1]?.lineBreakAfter;
return (
- <span
- key={`${token.fromMs}-${tokenIndex}`}
- style={{
- color: isActive ? HIGHLIGHT_COLOR : '#ffffff',
- display: 'inline-block',
- marginLeft: tokenIndex === 0 ? 0 : 26,
- WebkitTextStroke: isActive
- ? '3px rgba(0, 20, 28, 0.95)'
- : '2px rgba(0, 0, 0, 0.85)',
- }}
- >
- {token.text.trimStart()}
- </span>
+ <React.Fragment key={`${token.fromMs}-${tokenIndex}`}>
+ <span
+ style={{
+ color: isActive ? HIGHLIGHT_COLOR : '#ffffff',
+ display: 'inline-block',
+ marginLeft: startsLine ? 0 : 26,
+ WebkitTextStroke: isActive
+ ? '3px rgba(0, 20, 28, 0.95)'
+ : '2px rgba(0, 0, 0, 0.85)',
+ }}
+ >
+ {token.text.trimStart()}
+ </span>
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
+ </React.Fragment>
);
})}
</div>
diff --git a/packages/example/src/Compose/Captions.tsx b/packages/example/src/Compose/Captions.tsx
index 356f652cf6..dd772131c8 100644
--- a/packages/example/src/Compose/Captions.tsx
+++ b/packages/example/src/Compose/Captions.tsx
@@ -15,7 +15,8 @@ import {
const Token: React.FC<{
token: TikTokToken;
pageStart: number;
-}> = ({token, pageStart}) => {
+ isLast: boolean;
+}> = ({token, pageStart, isLast}) => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
const start = ((token.fromMs - pageStart) / 1000) * 30;
@@ -52,10 +53,13 @@ const Token: React.FC<{
}, [active, jump]);
return (
- <span>
- {' '}
- <span style={style}>{token.text.trim()}</span>
- </span>
+ <>
+ <span>
+ {' '}
+ <span style={style}>{token.text.trim()}</span>
+ </span>
+ {token.lineBreakAfter && !isLast ? <br /> : null}
+ </>
);
};
@@ -96,9 +100,14 @@ export const Captions: React.FC = () => {
key={page.startMs}
>
<div key={page.startMs}>
- {page.tokens.map((t) => {
+ {page.tokens.map((t, index) => {
return (
- <Token pageStart={page.startMs} key={t.fromMs} token={t} />
+ <Token
+ pageStart={page.startMs}
+ key={t.fromMs}
+ token={t}
+ isLast={index === page.tokens.length - 1}
+ />
);
})}
</div>
diff --git a/packages/example/src/E2eTestRoot.tsx b/packages/example/src/E2eTestRoot.tsx
index 2cd5d275e0..88f9ef52dd 100644
--- a/packages/example/src/E2eTestRoot.tsx
+++ b/packages/example/src/E2eTestRoot.tsx
@@ -1,6 +1,11 @@
import React from 'react';
import {AbsoluteFill, Composition, Folder, useCurrentScale} from 'remotion';
import {BarChart} from './BarChart';
+import {
+ CAPTIONS_DURATION_IN_FRAMES,
+ CAPTIONS_HEIGHT,
+} from './CaptionsTester/AnimatedCaptions';
+import {AnimatedCaptionsComposition} from './CaptionsTester/AnimatedCaptionsComposition';
import {EffectKeyframeE2e} from './EffectKeyframeE2e';
import {
ErrorOverlayRepro,
@@ -96,6 +101,14 @@ export const E2eTestRoot: React.FC = () => {
width={1280}
height={720}
/>
+ <Composition
+ id="captions-inspector-e2e"
+ component={AnimatedCaptionsComposition}
+ durationInFrames={CAPTIONS_DURATION_IN_FRAMES}
+ fps={30}
+ width={1080}
+ height={CAPTIONS_HEIGHT}
+ />
<Folder name="visual-controls">
<Composition
id="visual-controls"
diff --git a/packages/jonnys-videos/src/components/AnimatedCaptions.tsx b/packages/jonnys-videos/src/components/AnimatedCaptions.tsx
index a37b2ddfad..87393cba37 100644
--- a/packages/jonnys-videos/src/components/AnimatedCaptions.tsx
+++ b/packages/jonnys-videos/src/components/AnimatedCaptions.tsx
@@ -51,20 +51,26 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {
{page.tokens.map((token, tokenIndex) => {
const isActive =
token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;
+ const startsLine =
+ tokenIndex === 0 || page.tokens[tokenIndex - 1]?.lineBreakAfter;
return (
- <span
- key={`${token.fromMs}-${tokenIndex}`}
- style={{
- color: isActive ? HIGHLIGHT_COLOR : '#ffffff',
- display: 'inline-block',
- marginLeft: tokenIndex === 0 ? 0 : 26,
- WebkitTextStroke: isActive
- ? '3px rgba(38, 4, 0, 0.95)'
- : '2px rgba(0, 0, 0, 0.85)',
- }}
- >
- {token.text.trimStart()}
- </span>
+ <React.Fragment key={`${token.fromMs}-${tokenIndex}`}>
+ <span
+ style={{
+ color: isActive ? HIGHLIGHT_COLOR : '#ffffff',
+ display: 'inline-block',
+ marginLeft: startsLine ? 0 : 26,
+ WebkitTextStroke: isActive
+ ? '3px rgba(38, 4, 0, 0.95)'
+ : '2px rgba(0, 0, 0, 0.85)',
+ }}
+ >
+ {token.text.trimStart()}
+ </span>
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
+ </React.Fragment>
);
})}
</div>
diff --git a/packages/jonnys-videos/src/components/paginate-captions.ts b/packages/jonnys-videos/src/components/paginate-captions.ts
index 1e02164a24..96a0918283 100644
--- a/packages/jonnys-videos/src/components/paginate-captions.ts
+++ b/packages/jonnys-videos/src/components/paginate-captions.ts
@@ -27,6 +27,7 @@ const toToken = (caption: Caption, isFirst: boolean): TikTokToken => ({
text: isFirst ? caption.text.trimStart() : caption.text,
fromMs: caption.startMs,
toMs: caption.endMs,
+ ...(caption.lineBreakAfter ? {lineBreakAfter: true} : {}),
});
const rebalanceSentence = ({
@@ -49,15 +50,18 @@ const rebalanceSentence = ({
const endMs = nextCaption
? nextCaption.startMs
: pageCaptions[pageCaptions.length - 1].endMs;
- const tokens = pageCaptions.map((caption, tokenIndex) =>
- toToken(caption, tokenIndex === 0),
- );
+ const tokens = pageCaptions.map((caption, tokenIndex) => {
+ const startsLine =
+ tokenIndex === 0 || pageCaptions[tokenIndex - 1]?.lineBreakAfter;
+ return toToken(caption, Boolean(startsLine));
+ });
pages.push({
text: tokens
- .map((token) => token.text)
+ .map((token) => token.text + (token.lineBreakAfter ? '\n' : ''))
.join('')
- .trimStart(),
+ .trimStart()
+ .replace(/\n$/, ''),
startMs,
tokens,
durationMs: endMs - startMs,
diff --git a/packages/skills/skills/remotion-captions/SKILL.md b/packages/skills/skills/remotion-captions/SKILL.md
index da072ac18a..aa245f24c6 100644
--- a/packages/skills/skills/remotion-captions/SKILL.md
+++ b/packages/skills/skills/remotion-captions/SKILL.md
@@ -19,6 +19,7 @@ type Caption = {
endMs: number;
timestampMs: number | null;
confidence: number | null;
+ lineBreakAfter?: boolean;
};
```
diff --git a/packages/skills/skills/remotion-captions/display-captions.md b/packages/skills/skills/remotion-captions/display-captions.md
index d1fa6a2057..0b60b94ec8 100644
--- a/packages/skills/skills/remotion-captions/display-captions.md
+++ b/packages/skills/skills/remotion-captions/display-captions.md
@@ -156,17 +156,19 @@ const CaptionPage: React.FC<{ page: TikTokPage }> = ({ page }) => {
return (
<AbsoluteFill style={{ justifyContent: "center", alignItems: "center" }}>
<div style={{ fontSize: 80, fontWeight: "bold", whiteSpace: "pre" }}>
- {page.tokens.map((token) => {
+ {page.tokens.map((token, tokenIndex) => {
const isActive =
token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;
return (
- <span
- key={token.fromMs}
- style={{ color: isActive ? HIGHLIGHT_COLOR : "white" }}
- >
- {token.text}
- </span>
+ <React.Fragment key={`${token.fromMs}-${tokenIndex}`}>
+ <span style={{ color: isActive ? HIGHLIGHT_COLOR : "white" }}>
+ {token.text}
+ </span>
+ {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
+ </React.Fragment>
);
})}
</div>
diff --git a/packages/studio-codemods/src/update-inline-caption-patches.ts b/packages/studio-codemods/src/update-inline-caption-patches.ts
index 30b8e9840a..6507655c79 100644
--- a/packages/studio-codemods/src/update-inline-caption-patches.ts
+++ b/packages/studio-codemods/src/update-inline-caption-patches.ts
@@ -10,6 +10,7 @@ const captionKeys = [
'endMs',
'timestampMs',
'confidence',
+ 'lineBreakAfter',
] as const;
type CaptionKey = (typeof captionKeys)[number];
@@ -27,11 +28,17 @@ const getPropertyKey = (property: ObjectProperty): string | null => {
return null;
};
-const getStaticValue = (value: Expression): string | number | null => {
+const getStaticValue = (
+ value: Expression,
+): string | number | boolean | null => {
if (value.type === 'StringLiteral' || value.type === 'NumericLiteral') {
return value.value;
}
+ if (value.type === 'BooleanLiteral') {
+ return value.value;
+ }
+
if (value.type === 'NullLiteral') {
return null;
}
@@ -40,7 +47,7 @@ const getStaticValue = (value: Expression): string | number | null => {
};
const getStaticCaption = (expression: ObjectExpression): StaticCaption => {
- const values = new Map<string, string | number | null>();
+ const values = new Map<string, string | number | boolean | null>();
for (const property of expression.properties) {
if (property.type !== 'ObjectProperty' || property.computed) {
throw new Error(
@@ -59,19 +66,28 @@ const getStaticCaption = (expression: ObjectExpression): StaticCaption => {
const endMs = values.get('endMs');
const timestampMs = values.get('timestampMs');
const confidence = values.get('confidence');
+ const lineBreakAfter = values.get('lineBreakAfter');
if (
typeof text !== 'string' ||
typeof startMs !== 'number' ||
typeof endMs !== 'number' ||
(timestampMs !== null && typeof timestampMs !== 'number') ||
- (confidence !== null && typeof confidence !== 'number')
+ (confidence !== null && typeof confidence !== 'number') ||
+ (lineBreakAfter !== undefined && typeof lineBreakAfter !== 'boolean')
) {
throw new Error(
'Captions must have the standard static caption shape to edit',
);
}
- return {text, startMs, endMs, timestampMs, confidence};
+ return {
+ text,
+ startMs,
+ endMs,
+ timestampMs,
+ confidence,
+ ...(typeof lineBreakAfter === 'boolean' ? {lineBreakAfter} : {}),
+ };
};
const isCaptionKey = (key: string): key is CaptionKey =>
@@ -81,10 +97,14 @@ const getReplacementValue = ({
value,
previous,
}: {
- value: string | number | null;
+ value: string | number | boolean | null;
previous: Expression;
}): string => {
- if (value === null || typeof value === 'number') {
+ if (
+ value === null ||
+ typeof value === 'number' ||
+ typeof value === 'boolean'
+ ) {
return String(value);
}
@@ -142,6 +162,101 @@ const getSourceValueRange = ({
};
};
+const getPropertyInsertionReplacements = ({
+ input,
+ caption,
+ key,
+ value,
+}: {
+ input: string;
+ caption: ObjectExpression;
+ key: CaptionKey;
+ value: string | number | boolean | null;
+}): SourceReplacement[] => {
+ const firstProperty = caption.properties.find(
+ (property): property is ObjectProperty =>
+ property.type === 'ObjectProperty',
+ );
+ const lastProperty = caption.properties.findLast(
+ (property): property is ObjectProperty =>
+ property.type === 'ObjectProperty',
+ );
+ if (!firstProperty || !lastProperty) {
+ throw new Error('Captions must have static object properties to be edited');
+ }
+
+ const firstPropertyKey = getPropertyKey(firstProperty);
+ const lastPropertyKey = getPropertyKey(lastProperty);
+ if (firstPropertyKey === null || lastPropertyKey === null) {
+ throw new Error('Captions must have static object properties to be edited');
+ }
+
+ const firstPropertyValue = getSourceValueRange({
+ input,
+ property: firstProperty,
+ key: firstPropertyKey,
+ });
+ const lastPropertyValue = getSourceValueRange({
+ input,
+ property: lastProperty,
+ key: lastPropertyKey,
+ });
+ const openingBrace = input.lastIndexOf('{', firstPropertyValue.start);
+ const closingBrace = input.indexOf('}', lastPropertyValue.end);
+ if (openingBrace === -1 || closingBrace === -1) {
+ throw new Error(`Could not locate caption ${key} in the source file`);
+ }
+
+ const objectSource = input.slice(openingBrace, closingBrace + 1);
+ if (!objectSource.includes('\n')) {
+ const currentProperties = input.slice(openingBrace + 1, closingBrace);
+ const separator =
+ currentProperties.trim() === ''
+ ? ''
+ : currentProperties.trimEnd().endsWith(',')
+ ? ' '
+ : ', ';
+ return [
+ {
+ start: closingBrace,
+ end: closingBrace,
+ value: `${separator}${key}: ${String(value)}`,
+ },
+ ];
+ }
+
+ const propertyLineStart =
+ input.lastIndexOf('\n', lastPropertyValue.start - 1) + 1;
+ const propertyLine = input.slice(
+ propertyLineStart,
+ input.indexOf('\n', propertyLineStart),
+ );
+ const keyOffset = propertyLine.indexOf(lastPropertyKey);
+ if (keyOffset === -1) {
+ throw new Error(`Could not locate caption ${key} in the source file`);
+ }
+
+ const propertyIndent = propertyLine.slice(0, keyOffset);
+ const closingLineStart = input.lastIndexOf('\n', closingBrace - 1) + 1;
+ const replacements: SourceReplacement[] = [
+ {
+ start: closingLineStart,
+ end: closingLineStart,
+ value: `${propertyIndent}${key}: ${String(value)},\n`,
+ },
+ ];
+
+ if (!input.slice(lastPropertyValue.end, closingBrace).includes(',')) {
+ replacements.push({
+ start: lastPropertyValue.end,
+ end: lastPropertyValue.end,
+ value: ',',
+ });
+ }
+
+ return replacements;
+};
+
const updateCaption = ({
input,
caption,
@@ -172,7 +287,8 @@ const updateCaption = ({
(typeof value !== 'number' || !Number.isFinite(value))) ||
((key === 'timestampMs' || key === 'confidence') &&
value !== null &&
- (typeof value !== 'number' || !Number.isFinite(value)))
+ (typeof value !== 'number' || !Number.isFinite(value))) ||
+ (key === 'lineBreakAfter' && typeof value !== 'boolean')
) {
throw new Error(`Caption ${key} has an invalid value`);
}
@@ -185,6 +301,18 @@ const updateCaption = ({
);
});
if (!property || property.type !== 'ObjectProperty') {
+ if (key === 'lineBreakAfter' && typeof value === 'boolean') {
+ replacements.push(
+ ...getPropertyInsertionReplacements({
+ input,
+ caption,
+ key,
+ value,
+ }),
+ );
+ continue;
+ }
+
throw new Error(`Caption ${patch.index} is missing ${key}`);
}
@@ -193,7 +321,7 @@ const updateCaption = ({
start,
end,
value: getReplacementValue({
- value: value as string | number | null,
+ value: value as string | number | boolean | null,
previous: property.value as Expression,
}),
});
diff --git a/packages/studio-server/src/test/save-sequence-props.test.ts b/packages/studio-server/src/test/save-sequence-props.test.ts
index bfc72dc445..8b44e0c84a 100644
--- a/packages/studio-server/src/test/save-sequence-props.test.ts
+++ b/packages/studio-server/src/test/save-sequence-props.test.ts
@@ -276,7 +276,7 @@ test('saveSequenceProps saves inline caption patches as an undoable source edit'
timestampMs: 500,
confidence: null,
},
- changes: {text: 'Updated'},
+ changes: {text: 'Updated', lineBreakAfter: true},
},
],
},
@@ -305,6 +305,7 @@ test('saveSequenceProps saves inline caption patches as an undoable source edit'
});
expect(readFileSync(filePath, 'utf-8')).toContain("text: 'Updated'");
+ expect(readFileSync(filePath, 'utf-8')).toContain('lineBreakAfter: true');
expect(getUndoStack()).toHaveLength(1);
expect(getUndoStack()[0]?.suppressHmrOnFileRestore).toBe(false);
diff --git a/packages/studio-server/src/test/update-inline-caption-patches.test.ts b/packages/studio-server/src/test/update-inline-caption-patches.test.ts
index c913ae2c58..e8f3e05f69 100644
--- a/packages/studio-server/src/test/update-inline-caption-patches.test.ts
+++ b/packages/studio-server/src/test/update-inline-caption-patches.test.ts
@@ -57,6 +57,78 @@ test('updates only the patched inline caption fields', () => {
);
});
+test('adds and updates lineBreakAfter on inline captions', () => {
+ const added = updateInlineCaptionPatches({
+ input,
+ nodePath: lineColumnToNodePath(input, 5),
+ patches: [
+ {
+ index: 0,
+ before: {
+ text: 'First',
+ startMs: 0,
+ endMs: 1000,
+ timestampMs: 500,
+ confidence: null,
+ },
+ changes: {lineBreakAfter: true},
+ },
+ ],
+ });
+
+ expect(added.changedFields).toEqual([['lineBreakAfter']]);
+ expect(added.output).toContain(
+ '\t\t\t\t\tconfidence: null,\n\t\t\t\t\tlineBreakAfter: true,',
+ );
+
+ const updated = updateInlineCaptionPatches({
+ input: added.output,
+ nodePath: lineColumnToNodePath(added.output, 5),
+ patches: [
+ {
+ index: 0,
+ before: {
+ text: 'First',
+ startMs: 0,
+ endMs: 1000,
+ timestampMs: 500,
+ confidence: null,
+ lineBreakAfter: true,
+ },
+ changes: {lineBreakAfter: false},
+ },
+ ],
+ });
+
+ expect(updated.output).toContain('lineBreakAfter: false,');
+});
+
+test('adds lineBreakAfter to a single-line inline caption', () => {
+ const singleLineInput = `export const Example = () => {
+ return <TimedCaptions captions={[{text: 'First', startMs: 0, endMs: 1000, timestampMs: 500, confidence: null}]} />;
+};
+`;
+ const {output} = updateInlineCaptionPatches({
+ input: singleLineInput,
+ nodePath: lineColumnToNodePath(singleLineInput, 2),
+ patches: [
+ {
+ index: 0,
+ before: {
+ text: 'First',
+ startMs: 0,
+ endMs: 1000,
+ timestampMs: 500,
+ confidence: null,
+ },
+ changes: {lineBreakAfter: true},
+ },
+ ],
+ });
+
+ expect(output).toContain('confidence: null, lineBreakAfter: true}]}');
+});
+
test('rejects a queued patch whose source caption has changed', () => {
expect(() =>
updateInlineCaptionPatches({
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 3eb7321358..78a584fa6d 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -410,6 +410,7 @@ export type CaptionPatch = {
endMs: number;
timestampMs: number | null;
confidence: number | null;
+ lineBreakAfter?: boolean;
};
changes: Partial<{
text: string;
@@ -417,6 +418,7 @@ export type CaptionPatch = {
endMs: number;
timestampMs: number | null;
confidence: number | null;
+ lineBreakAfter: boolean;
}>;
};
diff --git a/packages/studio/src/components/CaptionTextEditor.tsx b/packages/studio/src/components/CaptionTextEditor.tsx
index d06bb0ce08..a564ee22c7 100644
--- a/packages/studio/src/components/CaptionTextEditor.tsx
+++ b/packages/studio/src/components/CaptionTextEditor.tsx
@@ -1,6 +1,7 @@
import type {Caption} from '@remotion/captions';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import {BACKGROUND, LIGHT_TEXT, LINE_COLOR, WHITE} from '../helpers/colors';
+import {Checkbox} from './Checkbox';
import {RemotionInput} from './NewComposition/RemInput';
const container: React.CSSProperties = {
@@ -21,11 +22,29 @@ const row: React.CSSProperties = {
alignItems: 'center',
borderBottom: `1px solid ${LINE_COLOR}`,
display: 'grid',
- gap: 12,
- gridTemplateColumns: '100px minmax(0, 1fr)',
+ gap: 8,
+ gridTemplateColumns: '100px minmax(0, 1fr) 18px',
padding: '5px 12px',
};
+const lineBreakControl: React.CSSProperties = {
+ display: 'flex',
+ height: 18,
+ position: 'relative',
+ width: 18,
+};
+
+const visuallyHidden: React.CSSProperties = {
+ clip: 'rect(0, 0, 0, 0)',
+ height: 1,
+ margin: -1,
+ overflow: 'hidden',
+ padding: 0,
+ position: 'absolute',
+ whiteSpace: 'nowrap',
+ width: 1,
+};
+
const timing: React.CSSProperties = {
color: LIGHT_TEXT,
fontFamily: 'monospace',
@@ -89,15 +108,19 @@ export const CaptionTextEditor: React.FC<{
return commitPending;
}, [commitPending]);
- const updateText = useCallback(
- (index: number, text: string) => {
- if (latestRef.current.captions[index]?.text === text) {
+ const updateCaption = useCallback(
+ (
+ index: number,
+ changes: Partial<Pick<Caption, 'text' | 'lineBreakAfter'>>,
+ ) => {
+ const currentCaption = latestRef.current.captions[index];
+ if (!currentCaption) {
return;
}
const nextCaptions = latestRef.current.captions.map(
(caption, captionIndex) => {
- return captionIndex === index ? {...caption, text} : caption;
+ return captionIndex === index ? {...caption, ...changes} : caption;
},
);
latestRef.current.captions = nextCaptions;
@@ -107,6 +130,31 @@ export const CaptionTextEditor: React.FC<{
[onChange],
);
+ const updateText = useCallback(
+ (index: number, text: string) => {
+ if (latestRef.current.captions[index]?.text === text) {
+ return;
+ }
+
+ updateCaption(index, {text});
+ },
+ [updateCaption],
+ );
+
+ const updateLineBreakAfter = useCallback(
+ (index: number, lineBreakAfter: boolean) => {
+ if (
+ Boolean(latestRef.current.captions[index]?.lineBreakAfter) ===
+ lineBreakAfter
+ ) {
+ return;
+ }
+
+ updateCaption(index, {lineBreakAfter});
+ },
+ [updateCaption],
+ );
+
const focusSibling = useCallback((index: number) => {
const input = listRef.current?.querySelector<HTMLInputElement>(
`[data-caption-index="${index}"]`,
@@ -171,6 +219,21 @@ export const CaptionTextEditor: React.FC<{
}}
value={caption.text}
/>
+ <label style={lineBreakControl} title="Line break after">
+ <span style={visuallyHidden}>
+ Line break after caption {index + 1}
+ </span>
+ <Checkbox
+ checked={Boolean(caption.lineBreakAfter)}
+ disabled={readOnly}
+ name={`caption-line-break-after-${index}`}
+ onChange={(event) => {
+ updateLineBreakAfter(index, event.target.checked);
+ commitPending();
+ }}
+ variant="small"
+ />
+ </label>
</div>
);
})}
diff --git a/packages/studio/src/components/InlineCaptionInspector.tsx b/packages/studio/src/components/InlineCaptionInspector.tsx
index 63b461c36a..c26b032a3f 100644
--- a/packages/studio/src/components/InlineCaptionInspector.tsx
+++ b/packages/studio/src/components/InlineCaptionInspector.tsx
@@ -39,12 +39,17 @@ const getCaptionPatches = ({
return null;
}
+ const changes: CaptionPatch['changes'] = {};
if (before.text !== after.text) {
- patches.push({
- index,
- before,
- changes: {text: after.text},
- });
+ changes.text = after.text;
+ }
+
+ if (Boolean(before.lineBreakAfter) !== Boolean(after.lineBreakAfter)) {
+ changes.lineBreakAfter = Boolean(after.lineBreakAfter);
+ }
+
+ if (Object.keys(changes).length > 0) {
+ patches.push({index, before, changes});
}
}
diff --git a/packages/template-audiogram/src/Audiogram/get-number-of-lines-for-text.ts b/packages/template-audiogram/src/Audiogram/get-number-of-lines-for-text.ts
index 1de8e4dfc0..35fff6b39b 100644
--- a/packages/template-audiogram/src/Audiogram/get-number-of-lines-for-text.ts
+++ b/packages/template-audiogram/src/Audiogram/get-number-of-lines-for-text.ts
@@ -14,17 +14,29 @@ export const layoutText = ({
fontFamily: string;
fontSize: number;
}) => {
- const box = fillTextBox({
+ let box = fillTextBox({
maxBoxWidth: textBoxWidth,
maxLines: 1_000,
});
const lines: Caption[][] = [[]];
- for (const caption of captions) {
- const isFirstCaption = captions.indexOf(caption) === 0;
+ for (const [index, caption] of captions.entries()) {
+ const isFirstCaption = index === 0;
+ const previousCaption = captions[index - 1];
+ const forcedLineBreak = Boolean(previousCaption?.lineBreakAfter);
+ if (forcedLineBreak) {
+ lines.push([]);
+ box = fillTextBox({
+ maxBoxWidth: textBoxWidth,
+ maxLines: 1_000,
+ });
+ }
const { newLine } = box.add({
- text: isFirstCaption ? caption.text.trimStart() : caption.text,
+ text:
+ isFirstCaption || forcedLineBreak
+ ? caption.text.trimStart()
+ : caption.text,
fontFamily,
fontSize,
});
@@ -34,7 +46,7 @@ export const layoutText = ({
}
const newCaption = { ...caption };
- if (newLine || isFirstCaption) {
+ if (newLine || isFirstCaption || forcedLineBreak) {
newCaption.text = newCaption.text.trimStart();
box.add({
text: " ".repeat(caption.text.length - newCaption.text.length),
diff --git a/packages/template-recorder/remotion/captions/boxed/components/SquareSubtitles.tsx b/packages/template-recorder/remotion/captions/boxed/components/SquareSubtitles.tsx
index 1638773808..a36bbc6a5e 100644
--- a/packages/template-recorder/remotion/captions/boxed/components/SquareSubtitles.tsx
+++ b/packages/template-recorder/remotion/captions/boxed/components/SquareSubtitles.tsx
@@ -1,4 +1,4 @@
-import React, { useMemo } from "react";
+import React, { Fragment, useMemo } from "react";
import type { Theme } from "../../../../config/themes";
import { getHorizontalPaddingForSubtitles } from "../../processing/layout-captions";
import type { CaptionPage } from "../../types";
@@ -37,13 +37,17 @@ export const SquareSubtitles: React.FC<{
<span style={style}>
{segment.captions.map((caption, index) => {
return (
- <BoxedSingleCaption
- key={caption.startMs + caption.text + index}
- isLast={index === segment.captions.length - 1}
- caption={caption}
- theme={theme}
- startFrame={startFrame}
- />
+ <Fragment key={caption.startMs + caption.text + index}>
+ <BoxedSingleCaption
+ isLast={index === segment.captions.length - 1}
+ caption={caption}
+ theme={theme}
+ startFrame={startFrame}
+ />
+ {caption.lineBreakAfter && index < segment.captions.length - 1 ? (
+ <br />
+ ) : null}
+ </Fragment>
);
})}
</span>
diff --git a/packages/template-recorder/remotion/captions/processing/fix-backticks.ts b/packages/template-recorder/remotion/captions/processing/fix-backticks.ts
index 5951cfdc71..a97beb0509 100644
--- a/packages/template-recorder/remotion/captions/processing/fix-backticks.ts
+++ b/packages/template-recorder/remotion/captions/processing/fix-backticks.ts
@@ -14,16 +14,23 @@ export const fixBackticks = (_captions: Caption[]): Caption[] => {
const caption = captions[i] as Caption;
const previousCaption = captions[i - 1] ?? null;
- if (caption.text.startsWith(". ")) {
+ if (caption.text.startsWith(". ") && !previousCaption?.lineBreakAfter) {
const lastAddedCaption = newCaptions[newCaptions.length - 1] as Caption;
lastAddedCaption.text += ".";
caption.text = caption.text.slice(1);
}
- if (!caption.text.startsWith(" ") && previousCaption) {
+ if (
+ !caption.text.startsWith(" ") &&
+ previousCaption &&
+ !previousCaption.lineBreakAfter
+ ) {
const lastAddedCaption = newCaptions[newCaptions.length - 1] as Caption;
lastAddedCaption.text += caption.text;
lastAddedCaption.endMs = caption.endMs;
+ if (caption.lineBreakAfter) {
+ lastAddedCaption.lineBreakAfter = true;
+ }
} else {
newCaptions.push(caption);
}
diff --git a/packages/template-recorder/remotion/captions/processing/layout-captions.ts b/packages/template-recorder/remotion/captions/processing/layout-captions.ts
index 1895d553c7..34f771644a 100644
--- a/packages/template-recorder/remotion/captions/processing/layout-captions.ts
+++ b/packages/template-recorder/remotion/captions/processing/layout-captions.ts
@@ -79,17 +79,18 @@ const cutCaptions = ({
maxLines: number;
fontSize: number;
}): CaptionPage[] => {
- const { add } = fillTextBox({ maxBoxWidth: boxWidth, maxLines });
+ let textBox = fillTextBox({ maxBoxWidth: boxWidth, maxLines });
let captionsFitted = 0;
+ let linesUsed = 1;
- for (const caption of captions) {
+ for (const [index, caption] of captions.entries()) {
const { fontFamily, fontWeight, ...additionalStyles } = isCaptionMonospace(
caption,
)
? MONOSPACE_FONT
: REGULAR_FONT;
- const { exceedsBox } = add({
+ const { exceedsBox, newLine } = textBox.add({
text: removeMonospaceTicks(caption).text,
fontFamily: fontFamily as string,
fontWeight: fontWeight as string,
@@ -103,6 +104,22 @@ const cutCaptions = ({
} else {
captionsFitted++;
}
+
+ if (newLine) {
+ linesUsed++;
+ }
+
+ if (caption.lineBreakAfter && index < captions.length - 1) {
+ linesUsed++;
+ if (linesUsed > maxLines) {
+ break;
+ }
+
+ textBox = fillTextBox({
+ maxBoxWidth: boxWidth,
+ maxLines: maxLines - linesUsed + 1,
+ });
+ }
}
if (captionsFitted === captions.length) {
diff --git a/packages/template-recorder/remotion/captions/processing/split-caption-into-monospace-segments.ts b/packages/template-recorder/remotion/captions/processing/split-caption-into-monospace-segments.ts
index d8a96c24d7..43a607eb9e 100644
--- a/packages/template-recorder/remotion/captions/processing/split-caption-into-monospace-segments.ts
+++ b/packages/template-recorder/remotion/captions/processing/split-caption-into-monospace-segments.ts
@@ -4,6 +4,7 @@ export const splitCaptionIntoMonospaceSegments = (
caption: Caption,
): Caption[] => {
const result: Caption[] = [];
+ const { lineBreakAfter, ...captionWithoutLineBreak } = caption;
const regex = /`([^`]+)`/g; // regex pattern to find text enclosed in backticks
let lastIndex = 0;
@@ -12,20 +13,28 @@ export const splitCaptionIntoMonospaceSegments = (
while ((match = regex.exec(caption.text)) !== null) {
if (match.index > lastIndex) {
result.push({
- ...caption,
+ ...captionWithoutLineBreak,
text: caption.text.slice(lastIndex, match.index),
});
}
result.push({
- ...caption,
+ ...captionWithoutLineBreak,
text: `${("`" + match[1]) as string}\``,
});
lastIndex = regex.lastIndex;
}
if (lastIndex < caption.text.length) {
- result.push({ ...caption, text: caption.text.slice(lastIndex) });
+ result.push({
+ ...captionWithoutLineBreak,
+ text: caption.text.slice(lastIndex),
+ });
+ }
+
+ const lastCaption = result[result.length - 1];
+ if (lineBreakAfter && lastCaption) {
+ lastCaption.lineBreakAfter = true;
}
return result;
diff --git a/packages/template-recorder/remotion/captions/srt/SrtPreviewAndEditor/SrtPreviewLine.tsx b/packages/template-recorder/remotion/captions/srt/SrtPreviewAndEditor/SrtPreviewLine.tsx
index caa4ba478e..3ea5c99630 100644
--- a/packages/template-recorder/remotion/captions/srt/SrtPreviewAndEditor/SrtPreviewLine.tsx
+++ b/packages/template-recorder/remotion/captions/srt/SrtPreviewAndEditor/SrtPreviewLine.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { Fragment } from "react";
import { UnserializedSrt } from "../helpers/serialize-srt";
import { SrtSingleCaption } from "./SingleCaption";
@@ -30,12 +30,14 @@ export const SrtPreviewLine: React.FC<{
return (
<div style={container}>
<div style={inner}>
- {segment.captions.map((caption) => {
+ {segment.captions.map((caption, index) => {
return (
- <SrtSingleCaption
- caption={caption}
- key={caption.startMs}
- ></SrtSingleCaption>
+ <Fragment key={caption.startMs}>
+ <SrtSingleCaption caption={caption}></SrtSingleCaption>
+ {caption.lineBreakAfter && index < segment.captions.length - 1 ? (
+ <br />
+ ) : null}
+ </Fragment>
);
})}
</div>
diff --git a/packages/template-recorder/remotion/captions/srt/helpers/calculate-srt.ts b/packages/template-recorder/remotion/captions/srt/helpers/calculate-srt.ts
index efd40ef114..bf2f67fd35 100644
--- a/packages/template-recorder/remotion/captions/srt/helpers/calculate-srt.ts
+++ b/packages/template-recorder/remotion/captions/srt/helpers/calculate-srt.ts
@@ -86,8 +86,12 @@ export const calculateSrt = ({
firstTimestampMs,
lastTimestampMs,
text: segment
- .map((s) => s.text.trim())
- .join(" ")
+ .map((s, captionIndex) => {
+ const separator =
+ s.lineBreakAfter && captionIndex < segment.length - 1 ? "\n" : " ";
+ return s.text.trim() + separator;
+ })
+ .join("")
.trim(),
captions: segment,
};
diff --git a/packages/template-recorder/tests/fix-backticks.test.ts b/packages/template-recorder/tests/fix-backticks.test.ts
index bd3cd42a9e..e70162ce0f 100644
--- a/packages/template-recorder/tests/fix-backticks.test.ts
+++ b/packages/template-recorder/tests/fix-backticks.test.ts
@@ -82,3 +82,41 @@ test("join captions correctly", () => {
},
]);
});
+
+test("does not join captions across a forced line break", () => {
+ const captions = fixBackticks([
+ {
+ text: " `dev`",
+ endMs: 6.96,
+ startMs: 0,
+ confidence: null,
+ timestampMs: 0,
+ lineBreakAfter: true,
+ },
+ {
+ text: ". It",
+ endMs: 8.36,
+ startMs: 0,
+ confidence: null,
+ timestampMs: 0,
+ },
+ ]);
+
+ expect(captions).toEqual([
+ {
+ text: " `dev`",
+ endMs: 6.96,
+ startMs: 0,
+ confidence: null,
+ timestampMs: 0,
+ lineBreakAfter: true,
+ },
+ {
+ text: ". It",
+ endMs: 8.36,
+ startMs: 0,
+ confidence: null,
+ timestampMs: 0,
+ },
+ ]);
+});
diff --git a/packages/template-recorder/tests/make-monospace-captions.test.ts b/packages/template-recorder/tests/make-monospace-captions.test.ts
index d99c88ea25..9982c64468 100644
--- a/packages/template-recorder/tests/make-monospace-captions.test.ts
+++ b/packages/template-recorder/tests/make-monospace-captions.test.ts
@@ -37,3 +37,22 @@ test("Should split up into monospace words", () => {
},
]);
});
+
+test("preserves a forced line break on the final segment", () => {
+ const caption: Caption = {
+ text: "This is a `monospace` word",
+ startMs: 0,
+ endMs: 0,
+ confidence: null,
+ timestampMs: 0,
+ lineBreakAfter: true,
+ };
+
+ const segments = splitCaptionIntoMonospaceSegments(caption);
+
+ expect(segments.map((segment) => segment.lineBreakAfter)).toEqual([
+ undefined,
+ undefined,
+ true,
+ ]);
+});
diff --git a/packages/template-tiktok/src/CaptionedVideo/Page.tsx b/packages/template-tiktok/src/CaptionedVideo/Page.tsx
index d81ad8b18c..42537cdd59 100644
--- a/packages/template-tiktok/src/CaptionedVideo/Page.tsx
+++ b/packages/template-tiktok/src/CaptionedVideo/Page.tsx
@@ -64,7 +64,7 @@ export const Page: React.FC<{
]),
}}
>
- {page.tokens.map((t) => {
+ {page.tokens.map((t, index) => {
const startRelativeToSequence = t.fromMs - page.startMs;
const endRelativeToSequence = t.toMs - page.startMs;
@@ -73,16 +73,20 @@ export const Page: React.FC<{
endRelativeToSequence > timeInMs;
return (
- <span
- key={t.fromMs}
- style={{
- display: "inline",
- whiteSpace: "pre",
- color: active ? HIGHLIGHT_COLOR : "white",
- }}
- >
- {t.text}
- </span>
+ <React.Fragment key={`${t.fromMs}-${index}`}>
+ <span
+ style={{
+ display: "inline",
+ whiteSpace: "pre",
+ color: active ? HIGHLIGHT_COLOR : "white",
+ }}
+ >
+ {t.text}
+ </span>
+ {t.lineBreakAfter && index < page.tokens.length - 1 ? (
+ <br />
+ ) : null}
+ </React.Fragment>
);
})}
</span>