🎥 Make videos programmatically with React
@remotion/captions: Start a new page after forced breaks
Jonny Burger20a26f7b2292
diff --git a/packages/captions/src/create-tiktok-style-captions.ts b/packages/captions/src/create-tiktok-style-captions.tsindex adf0eac333..5c1b23023c 100644--- a/packages/captions/src/create-tiktok-style-captions.ts+++ b/packages/captions/src/create-tiktok-style-captions.ts@@ -69,7 +69,7 @@ export const createTikTokStyleCaptions = ({}// Start a new sentence- currentText = text.trimStart() + (item.lineBreakAfter ? '\n' : '');+ currentText = text.trimStart();currentTokens = [{text: text.trimStart(),@@ -87,8 +87,7 @@ export const createTikTokStyleCaptions = ({currentFrom = item.startMs;}- const followsLineBreak = currentText.endsWith('\n');- const textToAppend = followsLineBreak ? text.trimStart() : text;+ const textToAppend = text;currentText += textToAppend;currentText = currentText.trimStart();if (text.trim() !== '') {@@ -101,13 +100,15 @@ export const createTikTokStyleCaptions = ({});}- if (item.lineBreakAfter) {- currentText += '\n';- }-currentTo = item.endMs;}+ if (item.lineBreakAfter && currentText !== '') {+ add();+ currentText = '';+ currentTokens = [];+ }+// Ensure the last sentence is addedif (index === captions.length - 1 && currentText !== '') {add();diff --git a/packages/captions/src/test/tiktok.test.ts b/packages/captions/src/test/tiktok.test.tsindex 74e941906c..2bf77bd6cb 100644--- a/packages/captions/src/test/tiktok.test.ts+++ b/packages/captions/src/test/tiktok.test.ts@@ -76,7 +76,7 @@ test('Should create captions', () => {]);});-test('Should preserve forced line breaks without creating a new page', () => {+test('Should start a new page after a forced line break', () => {const {pages} = createTikTokStyleCaptions({captions: [{@@ -100,9 +100,9 @@ test('Should preserve forced line breaks without creating a new page', () => {expect(pages).toEqual([{- text: 'hello\nthere',+ text: 'hello',startMs: 0,- durationMs: 400,+ durationMs: 200,tokens: [{text: 'hello',@@ -110,9 +110,14 @@ test('Should preserve forced line breaks without creating a new page', () => {toMs: 200,lineBreakAfter: true,},- {text: 'there', fromMs: 200, toMs: 400},],},+ {+ text: 'there',+ startMs: 200,+ durationMs: 200,+ tokens: [{text: 'there', fromMs: 200, toMs: 400}],+ },]);});diff --git a/packages/docs/docs/captions/caption.mdx b/packages/docs/docs/captions/caption.mdxindex d16948d003..9d1e4ba26f 100644--- a/packages/docs/docs/captions/caption.mdx+++ b/packages/docs/docs/captions/caption.mdx@@ -59,9 +59,11 @@ A number between 0 and 1 that indicates how confident the transcription is, or `_boolean_-When `true`, forces a visual line break after this caption if another caption follows in the same displayed page or subtitle cue.+When `true`, forces a break after this caption.-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.+[`createTikTokStyleCaptions()`](/docs/captions/create-tiktok-style-captions) ends the current page after this caption, so the next caption starts a new page. Other consumers, such as [`serializeSrt()`](/docs/captions/serialize-srt), may represent the break as a newline.++This field does not change caption timing. When it is `false` or omitted, page breaks are determined automatically.## Whitespace sensitivitydiff --git a/packages/docs/docs/captions/create-tiktok-style-captions.mdx b/packages/docs/docs/captions/create-tiktok-style-captions.mdxindex 4d3b462be0..e5d1c7d714 100644--- a/packages/docs/docs/captions/create-tiktok-style-captions.mdx+++ b/packages/docs/docs/captions/create-tiktok-style-captions.mdx@@ -135,9 +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_).+ - `lineBreakAfter`: Whether the source caption forced the page to end after this 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.+If a [`Caption`](/docs/captions/caption) has `lineBreakAfter: true`, the current page ends after the corresponding token and the next caption starts a new page. The token preserves the marker, while the page's `text` does not include a trailing newline.## Whitespace sensitivitydiff --git a/packages/docs/docs/captions/displaying.mdx b/packages/docs/docs/captions/displaying.mdxindex 07ab5e07be..1c7fa759dc 100644--- a/packages/docs/docs/captions/displaying.mdx+++ b/packages/docs/docs/captions/displaying.mdx@@ -119,7 +119,6 @@ const CaptionedContent: React.FC = () => {A caption page contains `tokens` which you can use to highlight the currently spoken word. Here's an example that highlights words as they are spoken:```tsx twoslash title="Rendering a caption page with word highlighting"-import {Fragment} from 'react';import {AbsoluteFill, useCurrentFrame, useVideoConfig} from 'remotion';import type {TikTokPage} from '@remotion/captions';@@ -154,18 +153,14 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {const isActive = token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;return (- <Fragment key={`${token.fromMs}-${tokenIndex}`}>- <span- style={{- color: isActive ? HIGHLIGHT_COLOR : 'white',- }}- >- {token.text}- </span>- {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (- <br />- ) : null}- </Fragment>+ <span+ key={`${token.fromMs}-${tokenIndex}`}+ style={{+ color: isActive ? HIGHLIGHT_COLOR : 'white',+ }}+ >+ {token.text}+ </span>);})}</div>@@ -180,7 +175,7 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {<summary>Show full example</summary>```tsx twoslash title="Full captioned video example"-import {Fragment, useState, useEffect, useCallback, useMemo} from 'react';+import {useState, useEffect, useCallback, useMemo} from 'react';import {AbsoluteFill, Sequence, staticFile, useCurrentFrame, useDelayRender, useVideoConfig} from 'remotion';import {createTikTokStyleCaptions} from '@remotion/captions';import type {Caption, TikTokPage} from '@remotion/captions';@@ -213,18 +208,14 @@ const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {const isActive = token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;return (- <Fragment key={`${token.fromMs}-${tokenIndex}`}>- <span- style={{- color: isActive ? HIGHLIGHT_COLOR : 'white',- }}- >- {token.text}- </span>- {token.lineBreakAfter && tokenIndex < page.tokens.length - 1 ? (- <br />- ) : null}- </Fragment>+ <span+ key={`${token.fromMs}-${tokenIndex}`}+ style={{+ color: isActive ? HIGHLIGHT_COLOR : 'white',+ }}+ >+ {token.text}+ </span>);})}</div>diff --git a/packages/skills/skills/remotion-captions/display-captions.md b/packages/skills/skills/remotion-captions/display-captions.mdindex 0b60b94ec8..a28b7f907f 100644--- a/packages/skills/skills/remotion-captions/display-captions.md+++ b/packages/skills/skills/remotion-captions/display-captions.md@@ -84,6 +84,8 @@ const { pages } = useMemo(() => {}, [captions]);```+If a caption has `lineBreakAfter: true`, the current page ends after that caption and the next caption starts a new page.+## Rendering with SequencesMap over the pages and render each one in a `<Sequence>`. Calculate the start frame and duration from the page timing:@@ -161,14 +163,12 @@ const CaptionPage: React.FC<{ page: TikTokPage }> = ({ page }) => {token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;return (- <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>+ <span+ key={`${token.fromMs}-${tokenIndex}`}+ style={{ color: isActive ? HIGHLIGHT_COLOR : "white" }}+ >+ {token.text}+ </span>);})}</div>