Skip to content

🎥 Make videos programmatically with React

tiktok captions

Jonny Burgerc532af61251a
diff --git a/packages/captions/src/create-tiktok-style-captions.ts b/packages/captions/src/create-tiktok-style-captions.ts
index cfd21a146c..ca4e2d154c 100644
--- a/packages/captions/src/create-tiktok-style-captions.ts
+++ b/packages/captions/src/create-tiktok-style-captions.ts
@@ -1,8 +1,15 @@
import {Caption} from './captions';
+export type TikTokToken = {
+ text: string;
+ fromMs: number;
+ toMs: number;
+};
+
export type TikTokPage = {
text: string;
- startInSeconds: number;
+ startMs: number;
+ tokens: TikTokToken[];
};
export function createTikTokStyleCaptions({
@@ -14,9 +21,9 @@ export function createTikTokStyleCaptions({
}): {captions: TikTokPage[]} {
const merged: TikTokPage[] = [];
let currentText = '';
+ let currentTokens: TikTokToken[] = [];
let currentFrom = 0;
let currentTo = 0;
- let currentTokenLevelTimestamp = 0;
transcription.forEach((item, index) => {
const {text} = item;
@@ -27,26 +34,36 @@ export function createTikTokStyleCaptions({
) {
if (currentText !== '') {
merged.push({
- text: currentText,
- startInSeconds: currentTokenLevelTimestamp / 1000,
+ text: currentText.trimStart(),
+ startMs: currentFrom,
+ tokens: currentTokens,
});
}
// Start a new sentence
currentText = text.trimStart();
+ currentTokens = [
+ {text: currentText, fromMs: item.startMs, toMs: item.endMs},
+ ].filter((t) => t.text !== '');
currentFrom = item.startMs;
currentTo = item.endMs;
- currentTokenLevelTimestamp = item.timestamp;
} else {
// Continuation or start of a new sentence without leading space
if (currentText === '') {
// It's the start of the document or after a sentence that started with a space
currentFrom = item.startMs;
- currentTokenLevelTimestamp = item.timestamp;
}
currentText += text;
currentText = currentText.trimStart();
+ if (text.trim() !== '') {
+ currentTokens.push({
+ text: currentTokens.length === 0 ? currentText.trimStart() : text,
+ fromMs: item.startMs,
+ toMs: item.endMs,
+ });
+ }
+
currentTo = item.endMs;
}
@@ -54,7 +71,8 @@ export function createTikTokStyleCaptions({
if (index === transcription.length - 1 && currentText !== '') {
merged.push({
text: currentText,
- startInSeconds: currentTokenLevelTimestamp / 1000,
+ startMs: currentFrom,
+ tokens: currentTokens,
});
}
});
diff --git a/packages/captions/src/index.ts b/packages/captions/src/index.ts
index ead8db4666..aa44cbe73e 100644
--- a/packages/captions/src/index.ts
+++ b/packages/captions/src/index.ts
@@ -1,2 +1,6 @@
export {Caption} from './captions';
-export {createTikTokStyleCaptions} from './create-tiktok-style-captions';
+export {
+ TikTokPage,
+ TikTokToken,
+ createTikTokStyleCaptions,
+} from './create-tiktok-style-captions';
diff --git a/packages/template-tiktok/src/CaptionedVideo/Page.tsx b/packages/template-tiktok/src/CaptionedVideo/Page.tsx
new file mode 100644
index 0000000000..e06dbe3883
--- /dev/null
+++ b/packages/template-tiktok/src/CaptionedVideo/Page.tsx
@@ -0,0 +1,91 @@
+import React from "react";
+import {
+ AbsoluteFill,
+ interpolate,
+ useCurrentFrame,
+ useVideoConfig,
+} from "remotion";
+import { TheBoldFont } from "../load-font";
+import { fitText } from "@remotion/layout-utils";
+import { makeTransform, scale, translateY } from "@remotion/animation-utils";
+import { TikTokPage } from "@remotion/captions";
+
+const fontFamily = TheBoldFont;
+
+const container: React.CSSProperties = {
+ justifyContent: "center",
+ alignItems: "center",
+ top: undefined,
+ bottom: 350,
+ height: 150,
+};
+
+const DESIRED_FONT_SIZE = 120;
+const HIGHLIGHT_COLOR = "#39E508";
+
+export const Page: React.FC<{
+ enterProgress: number;
+ page: TikTokPage;
+}> = ({ enterProgress, page }) => {
+ const frame = useCurrentFrame();
+ const { width, fps } = useVideoConfig();
+ const timeInMs = (frame / fps) * 1000;
+
+ const fittedText = fitText({
+ fontFamily,
+ text: page.text,
+ withinWidth: width * 0.9,
+ });
+
+ const fontSize = Math.min(DESIRED_FONT_SIZE, fittedText.fontSize);
+
+ return (
+ <AbsoluteFill style={container}>
+ <div
+ style={{
+ fontSize,
+ color: "white",
+ WebkitTextStroke: "20px black",
+ paintOrder: "stroke",
+ transform: makeTransform([
+ scale(interpolate(enterProgress, [0, 1], [0.8, 1])),
+ translateY(interpolate(enterProgress, [0, 1], [50, 0])),
+ ]),
+ fontFamily,
+ textTransform: "uppercase",
+ }}
+ >
+ <span
+ style={{
+ transform: makeTransform([
+ scale(interpolate(enterProgress, [0, 1], [0.8, 1])),
+ translateY(interpolate(enterProgress, [0, 1], [50, 0])),
+ ]),
+ }}
+ >
+ {page.tokens.map((t) => {
+ const startRelativeToSequence = t.fromMs - page.startMs;
+ const endRelativeToSequence = t.toMs - page.startMs;
+
+ const active =
+ startRelativeToSequence <= timeInMs &&
+ endRelativeToSequence > timeInMs;
+
+ return (
+ <span
+ key={t.fromMs}
+ style={{
+ display: "inline-block",
+ whiteSpace: "pre",
+ color: active ? HIGHLIGHT_COLOR : "white",
+ }}
+ >
+ {t.text}
+ </span>
+ );
+ })}
+ </span>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx b/packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx
index 6574d6aef5..9c00274d0b 100644
--- a/packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx
+++ b/packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx
@@ -5,9 +5,10 @@ import {
useCurrentFrame,
useVideoConfig,
} from "remotion";
-import { Word } from "./Word";
+import { Page } from "./Page";
+import { TikTokPage } from "@remotion/captions";
-const Subtitle: React.FC<{ text: string }> = ({ text }) => {
+const Subtitle: React.FC<{ page: TikTokPage }> = ({ page }) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
@@ -20,15 +21,9 @@ const Subtitle: React.FC<{ text: string }> = ({ text }) => {
durationInFrames: 5,
});
- // Overlay stroked text with normal text to create an effect where the stroke is outside
return (
<AbsoluteFill>
- <AbsoluteFill>
- <Word stroke enterProgress={enter} text={text} />
- </AbsoluteFill>
- <AbsoluteFill>
- <Word enterProgress={enter} text={text} stroke={false} />
- </AbsoluteFill>
+ <Page enterProgress={enter} page={page} />
</AbsoluteFill>
);
};
diff --git a/packages/template-tiktok/src/CaptionedVideo/Word.tsx b/packages/template-tiktok/src/CaptionedVideo/Word.tsx
deleted file mode 100644
index c6523a19d2..0000000000
--- a/packages/template-tiktok/src/CaptionedVideo/Word.tsx
+++ /dev/null
@@ -1,52 +0,0 @@
-import React from "react";
-import { AbsoluteFill, interpolate, useVideoConfig } from "remotion";
-import { TheBoldFont } from "../load-font";
-import { fitText } from "@remotion/layout-utils";
-import { makeTransform, scale, translateY } from "@remotion/animation-utils";
-
-const fontFamily = TheBoldFont;
-
-export const Word: React.FC<{
- enterProgress: number;
- text: string;
- stroke: boolean;
-}> = ({ enterProgress, text, stroke }) => {
- const { width } = useVideoConfig();
- const desiredFontSize = 120;
-
- const fittedText = fitText({
- fontFamily,
- text,
- withinWidth: width * 0.8,
- });
-
- const fontSize = Math.min(desiredFontSize, fittedText.fontSize);
-
- return (
- <AbsoluteFill
- style={{
- justifyContent: "center",
- alignItems: "center",
- top: undefined,
- bottom: 350,
- height: 150,
- }}
- >
- <div
- style={{
- fontSize,
- color: "white",
- WebkitTextStroke: stroke ? "20px black" : undefined,
- transform: makeTransform([
- scale(interpolate(enterProgress, [0, 1], [0.8, 1])),
- translateY(interpolate(enterProgress, [0, 1], [50, 0])),
- ]),
- fontFamily,
- textTransform: "uppercase",
- }}
- >
- {text}
- </div>
- </AbsoluteFill>
- );
-};
diff --git a/packages/template-tiktok/src/CaptionedVideo/index.tsx b/packages/template-tiktok/src/CaptionedVideo/index.tsx
index 66b8f85837..777ca6124e 100644
--- a/packages/template-tiktok/src/CaptionedVideo/index.tsx
+++ b/packages/template-tiktok/src/CaptionedVideo/index.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useMemo, useState } from "react";
import {
AbsoluteFill,
CalculateMetadataFunction,
@@ -90,10 +90,12 @@ export const CaptionedVideo: React.FC<{
};
}, [fetchSubtitles, src, subtitlesFile]);
- const { captions } = createTikTokStyleCaptions({
- combineTokensWithinMilliseconds: SWITCH_CAPTIONS_EVERY_MS,
- transcription: subtitles ?? [],
- });
+ const { captions } = useMemo(() => {
+ return createTikTokStyleCaptions({
+ combineTokensWithinMilliseconds: SWITCH_CAPTIONS_EVERY_MS,
+ transcription: subtitles ?? [],
+ });
+ }, [subtitles]);
return (
<AbsoluteFill style={{ backgroundColor: "white" }}>
@@ -107,9 +109,9 @@ export const CaptionedVideo: React.FC<{
</AbsoluteFill>
{captions.map((subtitle, index) => {
const nextSubtitle = captions[index + 1] ?? null;
- const subtitleStartFrame = subtitle.startInSeconds * fps;
+ const subtitleStartFrame = (subtitle.startMs / 1000) * fps;
const subtitleEndFrame = Math.min(
- nextSubtitle ? nextSubtitle.startInSeconds * fps : Infinity,
+ nextSubtitle ? (nextSubtitle.startMs / 1000) * fps : Infinity,
subtitleStartFrame + SWITCH_CAPTIONS_EVERY_MS,
);
const durationInFrames = subtitleEndFrame - subtitleStartFrame;
@@ -119,10 +121,11 @@ export const CaptionedVideo: React.FC<{
return (
<Sequence
+ key={index}
from={subtitleStartFrame}
durationInFrames={durationInFrames}
>
- <Subtitle key={index} text={subtitle.text} />;
+ <Subtitle key={index} page={subtitle} />;
</Sequence>
);
})}