🎥 Make videos programmatically with React
Captions: Treat forced breaks as page boundaries in promo videos
Jonny Burger7d7b9df3cfab
diff --git a/packages/jonnys-videos/package.json b/packages/jonnys-videos/package.jsonindex cb078dd5dd..5d793b9f3e 100644--- a/packages/jonnys-videos/package.json+++ b/packages/jonnys-videos/package.json@@ -11,6 +11,7 @@"bundle": "remotion bundle","render": "remotion render","lint": "eslint src && tsgo --noEmit",+ "test": "bun test src","formatting": "oxfmt src remotion.config.ts --check","format": "oxfmt src remotion.config.ts"},diff --git a/packages/jonnys-videos/src/components/paginate-captions.test.ts b/packages/jonnys-videos/src/components/paginate-captions.test.tsnew file mode 100644index 0000000000..fdfefa0315--- /dev/null+++ b/packages/jonnys-videos/src/components/paginate-captions.test.ts@@ -0,0 +1,51 @@+import {expect, test} from 'bun:test';+import type {Caption} from '@remotion/captions';+import {createSentenceAwareCaptionPages} from './paginate-captions';++test('treats a forced line break as a page boundary', () => {+ const captions = [+ {+ text: 'This',+ startMs: 0,+ endMs: 200,+ timestampMs: 100,+ confidence: null,+ },+ {+ text: ' should',+ startMs: 200,+ endMs: 400,+ timestampMs: 300,+ confidence: null,+ lineBreakAfter: true,+ },+ {+ text: ' start',+ startMs: 400,+ endMs: 600,+ timestampMs: 500,+ confidence: null,+ },+ {+ text: ' a new page',+ startMs: 600,+ endMs: 800,+ timestampMs: 700,+ confidence: null,+ },+ ] satisfies Caption[];++ const pages = createSentenceAwareCaptionPages({+ captions,+ combineTokensWithinMilliseconds: 10_000,+ layout: null,+ });++ expect(+ pages.map((page) => page.tokens.map((token) => token.text.trim())),+ ).toEqual([+ ['This', 'should'],+ ['start', 'a new page'],+ ]);+ expect(pages.map((page) => page.startMs)).toEqual([0, 400]);+});diff --git a/packages/jonnys-videos/src/components/paginate-captions.ts b/packages/jonnys-videos/src/components/paginate-captions.tsindex 4bd013a50f..0724c159ac 100644--- a/packages/jonnys-videos/src/components/paginate-captions.ts+++ b/packages/jonnys-videos/src/components/paginate-captions.ts@@ -173,24 +173,24 @@ const countAwkwardPageBreaks = (pages: TikTokPage[]) => {return awkwardBreaks;};-const splitIntoSentences = (captions: Caption[]): Caption[][] => {- const sentences: Caption[][] = [];- let sentence: Caption[] = [];+const splitIntoPageGroups = (captions: Caption[]): Caption[][] => {+ const pageGroups: Caption[][] = [];+ let pageGroup: Caption[] = [];for (const caption of captions) {- sentence.push(caption);+ pageGroup.push(caption);- if (SENTENCE_END.test(caption.text.trim())) {- sentences.push(sentence);- sentence = [];+ if (SENTENCE_END.test(caption.text.trim()) || caption.lineBreakAfter) {+ pageGroups.push(pageGroup);+ pageGroup = [];}}- if (sentence.length > 0) {- sentences.push(sentence);+ if (pageGroup.length > 0) {+ pageGroups.push(pageGroup);}- return sentences;+ return pageGroups;};const toToken = (caption: Caption, isFirst: boolean): TikTokToken => ({@@ -436,11 +436,11 @@ const rebalanceSentence = ({};/**- * Never crosses a sentence boundary. When layout information is supplied, the- * page count is derived from rendered width and a duration ceiling. Dynamic- * programming then chooses boundaries based on line balance, timing and- * natural phrase boundaries. Without layout information, the library's- * time-based cadence is retained.+ * Never crosses a sentence or forced line break boundary. When layout+ * information is supplied, the page count is derived from rendered width and+ * a duration ceiling. Dynamic programming then chooses boundaries based on+ * line balance, timing and natural phrase boundaries. Without layout+ * information, the library's time-based cadence is retained.*/export const createSentenceAwareCaptionPages = ({captions,@@ -451,8 +451,8 @@ export const createSentenceAwareCaptionPages = ({combineTokensWithinMilliseconds: number;layout: CaptionPageLayout | null;}): TikTokPage[] => {- return splitIntoSentences(captions).flatMap((sentence) => {- const visibleCaptions = sentence.filter(+ return splitIntoPageGroups(captions).flatMap((pageGroup) => {+ const visibleCaptions = pageGroup.filter((caption) => caption.text.trim().length > 0,);