Skip to content

🎥 Make videos programmatically with React

`@remotion/captions`: Add silence-based caption page breaks (#10543)

Brayden86285256c9c0
diff --git a/packages/captions/src/create-tiktok-style-captions.ts b/packages/captions/src/create-tiktok-style-captions.ts
index 67cfbbefe4..5b96e8e7bd 100644
--- a/packages/captions/src/create-tiktok-style-captions.ts
+++ b/packages/captions/src/create-tiktok-style-captions.ts
@@ -16,6 +16,7 @@ export type TikTokPage = {
export type CreateTikTokStyleCaptionsInput = {
captions: Caption[];
combineTokensWithinMilliseconds: number;
+ breakOnSilenceAfterMilliseconds?: number;
};
export type CreateTikTokStyleCaptionsOutput = {
@@ -25,6 +26,7 @@ export type CreateTikTokStyleCaptionsOutput = {
export const createTikTokStyleCaptions = ({
captions,
combineTokensWithinMilliseconds,
+ breakOnSilenceAfterMilliseconds,
}: CreateTikTokStyleCaptionsInput): CreateTikTokStyleCaptionsOutput => {
const tikTokStyleCaptions: TikTokPage[] = [];
let currentText = '';
@@ -48,11 +50,16 @@ export const createTikTokStyleCaptions = ({
captions.forEach((item, index) => {
const {text} = item;
+ const exceedsDuration =
+ currentTo - currentFrom > combineTokensWithinMilliseconds;
+ // A pause between the previous caption and this one
+ const shouldBreakOnSilence =
+ breakOnSilenceAfterMilliseconds !== undefined &&
+ currentText !== '' &&
+ item.startMs - currentTo >= breakOnSilenceAfterMilliseconds;
+
// If text starts with a space, push the currentText (if it exists) and start a new one
- if (
- text.startsWith(' ') &&
- currentTo - currentFrom > combineTokensWithinMilliseconds
- ) {
+ if (text.startsWith(' ') && (exceedsDuration || shouldBreakOnSilence)) {
if (currentText !== '') {
add();
}
diff --git a/packages/captions/src/test/tiktok.test.ts b/packages/captions/src/test/tiktok.test.ts
index e6ee33ceb5..4e51d75eff 100644
--- a/packages/captions/src/test/tiktok.test.ts
+++ b/packages/captions/src/test/tiktok.test.ts
@@ -119,3 +119,144 @@ test('Should finalize the duration when captions end in whitespace', () => {
},
]);
});
+
+test('Should start a new page after the configured silence duration', () => {
+ const {pages} = createTikTokStyleCaptions({
+ captions: [
+ {
+ text: 'hello',
+ startMs: 0,
+ endMs: 200,
+ timestampMs: 100,
+ confidence: null,
+ },
+ {
+ text: ' there',
+ startMs: 200,
+ endMs: 400,
+ timestampMs: 300,
+ confidence: null,
+ },
+ {
+ text: ' again',
+ startMs: 5000,
+ endMs: 5200,
+ timestampMs: 5100,
+ confidence: null,
+ },
+ ],
+ combineTokensWithinMilliseconds: 10000,
+ breakOnSilenceAfterMilliseconds: 1000,
+ });
+
+ expect(pages).toEqual([
+ {
+ text: 'hello there',
+ startMs: 0,
+ durationMs: 5000,
+ tokens: [
+ {text: 'hello', fromMs: 0, toMs: 200},
+ {text: ' there', fromMs: 200, toMs: 400},
+ ],
+ },
+ {
+ text: 'again',
+ startMs: 5000,
+ durationMs: 200,
+ tokens: [{text: 'again', fromMs: 5000, toMs: 5200}],
+ },
+ ]);
+});
+
+test('Should not break when the silence is shorter than the configured duration', () => {
+ const {pages} = createTikTokStyleCaptions({
+ captions: [
+ {
+ text: 'hello',
+ startMs: 0,
+ endMs: 200,
+ timestampMs: 100,
+ confidence: null,
+ },
+ {
+ text: ' there',
+ startMs: 900,
+ endMs: 1100,
+ timestampMs: 1000,
+ confidence: null,
+ },
+ ],
+ combineTokensWithinMilliseconds: 10000,
+ breakOnSilenceAfterMilliseconds: 1000,
+ });
+
+ expect(pages.map((p) => p.text)).toEqual(['hello there']);
+});
+
+test('Should never break inside a word, even across a silence', () => {
+ const {pages} = createTikTokStyleCaptions({
+ captions: [
+ {
+ text: 'Dr.',
+ startMs: 0,
+ endMs: 200,
+ timestampMs: 100,
+ confidence: null,
+ },
+ // No leading space means continuation, so never a page break
+ {
+ text: 'Strange',
+ startMs: 5000,
+ endMs: 5300,
+ timestampMs: 5150,
+ confidence: null,
+ },
+ ],
+ combineTokensWithinMilliseconds: 100,
+ breakOnSilenceAfterMilliseconds: 500,
+ });
+
+ expect(pages).toEqual([
+ {
+ text: 'Dr.Strange',
+ startMs: 0,
+ durationMs: 5300,
+ tokens: [
+ {text: 'Dr.', fromMs: 0, toMs: 200},
+ {text: 'Strange', fromMs: 5000, toMs: 5300},
+ ],
+ },
+ ]);
+});
+
+test('Should break at every word boundary when the silence duration is 0', () => {
+ const {pages} = createTikTokStyleCaptions({
+ captions: [
+ {
+ text: 'one',
+ startMs: 0,
+ endMs: 200,
+ timestampMs: 100,
+ confidence: null,
+ },
+ {
+ text: ' two',
+ startMs: 200,
+ endMs: 400,
+ timestampMs: 300,
+ confidence: null,
+ },
+ {
+ text: ' three',
+ startMs: 400,
+ endMs: 600,
+ timestampMs: 500,
+ confidence: null,
+ },
+ ],
+ combineTokensWithinMilliseconds: 10000,
+ breakOnSilenceAfterMilliseconds: 0,
+ });
+
+ expect(pages.map((p) => p.text)).toEqual(['one', 'two', 'three']);
+});
diff --git a/packages/docs/docs/captions/create-tiktok-style-captions.mdx b/packages/docs/docs/captions/create-tiktok-style-captions.mdx
index 90c2f3be50..8ba8bda60c 100644
--- a/packages/docs/docs/captions/create-tiktok-style-captions.mdx
+++ b/packages/docs/docs/captions/create-tiktok-style-captions.mdx
@@ -104,6 +104,20 @@ An array of [`Caption`](/docs/captions/caption) objects.
Controls how long captions are combined into a page. Once the current page's duration exceeds this value, the next caption that starts with a space begins a new page.
+### `breakOnSilenceAfterMilliseconds?`<AvailableFrom v="4.0.514" />
+
+_number_
+
+If set, a gap of at least this many milliseconds between the end of one caption and the start of the next begins a new page, even if `combineTokensWithinMilliseconds` has not been exceeded yet. By default, gaps do not cause an early page break.
+
+This compares caption timestamps and does not analyze the audio track.
+
+As with all page breaks, the previous page's `durationMs` extends until the next page starts, so the caption stays on screen through the pause.
+
+A value of `0` starts a new page at every word boundary, resulting in word-by-word pages.
+
+This option only adds earlier page breaks: `combineTokensWithinMilliseconds` remains the upper bound, and pages can only get shorter, never longer.
+
## Return value
An object with the following properties:
diff --git a/packages/example/e2e/studio.test.mts b/packages/example/e2e/studio.test.mts
index a20ce33378..0f8fd1511d 100644
--- a/packages/example/e2e/studio.test.mts
+++ b/packages/example/e2e/studio.test.mts
@@ -1,7 +1,7 @@
-import fs from 'fs';
-import path from 'path';
import {expect, test, type Page} from '@playwright/test';
import {StudioProtocolInternals} from '@remotion/studio-protocol';
+import fs from 'fs';
+import path from 'path';
import {
STUDIO_URL,
effectKeyframeE2eFile,
@@ -425,20 +425,12 @@ test.describe('visual mode', () => {
).toBeVisible();
const canvasItem = page.getByText('Performance overview', {exact: true});
- const canvasItemBox = await canvasItem.boundingBox();
- if (canvasItemBox === null) {
- throw new Error('Canvas item has no bounding box');
- }
-
- const canvasItemCenter = {
- x: canvasItemBox.x + canvasItemBox.width / 2,
- y: canvasItemBox.y + canvasItemBox.height / 2,
- };
- await page.mouse.move(canvasItemCenter.x, canvasItemCenter.y);
- await page.waitForTimeout(100);
- await page.mouse.click(canvasItemCenter.x, canvasItemCenter.y, {
- button: 'right',
- });
+ await canvasItem.hover();
+ const canvasItemOutline = page.locator(
+ 'polygon[data-remotion-prevent-selection-clear="true"][stroke-opacity="1"]',
+ );
+ await expect(canvasItemOutline).toHaveCount(1);
+ await canvasItemOutline.click({button: 'right'});
const duplicateButton = page.getByRole('button', {
name: 'Duplicate',