Skip to content

🎥 Make videos programmatically with React

Fix editing default Element captions

Igor Samokhovets8bdcf082b0cb
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 a80f5808fc..05c6495762 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
@@ -51,10 +51,64 @@ const pillVerticalPadding = 12;
const pillBorderRadius = 10;
const pillMoveDurationInFrames = 5;
const defaultCombineTokensWithinMilliseconds = 800;
+const defaultCaptions: Caption[] = [
+ {
+ text: 'Captions',
+ startMs: 0,
+ endMs: 800,
+ timestampMs: 400,
+ confidence: null,
+ },
+ {
+ text: ' can',
+ startMs: 800,
+ endMs: 1500,
+ timestampMs: 1150,
+ confidence: null,
+ },
+ {
+ text: ' move',
+ startMs: 1500,
+ endMs: 2300,
+ timestampMs: 1900,
+ confidence: null,
+ },
+ {
+ text: ' with',
+ startMs: 2300,
+ endMs: 3100,
+ timestampMs: 2700,
+ confidence: null,
+ },
+ {
+ text: ' every',
+ startMs: 3100,
+ endMs: 4000,
+ timestampMs: 3550,
+ confidence: null,
+ },
+ {
+ text: ' spoken',
+ startMs: 4000,
+ endMs: 5100,
+ timestampMs: 4550,
+ confidence: null,
+ },
+ {
+ text: ' word.',
+ startMs: 5100,
+ endMs: 6500,
+ timestampMs: 5800,
+ confidence: null,
+ },
+];
const movingPillCaptionsSchema = {
...Interactive.baseSchema,
- ...Interactive.captionsSchema,
+ captions: {
+ ...Interactive.captionsSchema.captions,
+ default: defaultCaptions,
+ },
width: {
type: 'number',
min: 1,
@@ -504,57 +558,7 @@ const MovingPillCaptionsWithControls: React.FC<
<MovingPillCaptionsInner
{...props}
callerStyle={style ?? null}
- captions={[
- {
- text: 'Captions',
- startMs: 0,
- endMs: 800,
- timestampMs: 400,
- confidence: null,
- },
- {
- text: ' can',
- startMs: 800,
- endMs: 1500,
- timestampMs: 1150,
- confidence: null,
- },
- {
- text: ' move',
- startMs: 1500,
- endMs: 2300,
- timestampMs: 1900,
- confidence: null,
- },
- {
- text: ' with',
- startMs: 2300,
- endMs: 3100,
- timestampMs: 2700,
- confidence: null,
- },
- {
- text: ' every',
- startMs: 3100,
- endMs: 4000,
- timestampMs: 3550,
- confidence: null,
- },
- {
- text: ' spoken',
- startMs: 4000,
- endMs: 5100,
- timestampMs: 4550,
- confidence: null,
- },
- {
- text: ' word.',
- startMs: 5100,
- endMs: 6500,
- timestampMs: 5800,
- confidence: null,
- },
- ]}
+ captions={defaultCaptions}
controls={controls}
width={props.width ?? 681}
height={props.height ?? 252}
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 b5a4a5f704..6e0cf57c09 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
@@ -47,10 +47,64 @@ const textColor = '#ffffff';
const highlightColor = '#18ff0e';
const activeWordScale = 1.03;
const defaultCombineTokensWithinMilliseconds = 800;
+const defaultCaptions: Caption[] = [
+ {
+ text: 'Captions',
+ startMs: 0,
+ endMs: 800,
+ timestampMs: 400,
+ confidence: null,
+ },
+ {
+ text: ' can',
+ startMs: 800,
+ endMs: 1500,
+ timestampMs: 1150,
+ confidence: null,
+ },
+ {
+ text: ' move',
+ startMs: 1500,
+ endMs: 2300,
+ timestampMs: 1900,
+ confidence: null,
+ },
+ {
+ text: ' with',
+ startMs: 2300,
+ endMs: 3100,
+ timestampMs: 2700,
+ confidence: null,
+ },
+ {
+ text: ' every',
+ startMs: 3100,
+ endMs: 4000,
+ timestampMs: 3550,
+ confidence: null,
+ },
+ {
+ text: ' spoken',
+ startMs: 4000,
+ endMs: 5100,
+ timestampMs: 4550,
+ confidence: null,
+ },
+ {
+ text: ' word.',
+ startMs: 5100,
+ endMs: 6500,
+ timestampMs: 5800,
+ confidence: null,
+ },
+];
const poppingWordCaptionsSchema = {
...Interactive.baseSchema,
- ...Interactive.captionsSchema,
+ captions: {
+ ...Interactive.captionsSchema.captions,
+ default: defaultCaptions,
+ },
width: {
type: 'number',
min: 1,
@@ -414,57 +468,7 @@ const PoppingWordCaptionsWithControls: React.FC<
<PoppingWordCaptionsInner
{...props}
callerStyle={style ?? null}
- captions={[
- {
- text: 'Captions',
- startMs: 0,
- endMs: 800,
- timestampMs: 400,
- confidence: null,
- },
- {
- text: ' can',
- startMs: 800,
- endMs: 1500,
- timestampMs: 1150,
- confidence: null,
- },
- {
- text: ' move',
- startMs: 1500,
- endMs: 2300,
- timestampMs: 1900,
- confidence: null,
- },
- {
- text: ' with',
- startMs: 2300,
- endMs: 3100,
- timestampMs: 2700,
- confidence: null,
- },
- {
- text: ' every',
- startMs: 3100,
- endMs: 4000,
- timestampMs: 3550,
- confidence: null,
- },
- {
- text: ' spoken',
- startMs: 4000,
- endMs: 5100,
- timestampMs: 4550,
- confidence: null,
- },
- {
- text: ' word.',
- startMs: 5100,
- endMs: 6500,
- timestampMs: 5800,
- confidence: null,
- },
- ]}
+ captions={defaultCaptions}
controls={controls}
width={props.width ?? 681}
height={props.height ?? 252}
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 d6b54bfa01..458aa26435 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
@@ -44,10 +44,64 @@ const fontWeight = '700';
const textColor = '#ffffff';
const highlightColor = '#18ff0e';
const defaultCombineTokensWithinMilliseconds = 800;
+const defaultCaptions: Caption[] = [
+ {
+ text: 'Captions',
+ startMs: 0,
+ endMs: 800,
+ timestampMs: 400,
+ confidence: null,
+ },
+ {
+ text: ' can',
+ startMs: 800,
+ endMs: 1500,
+ timestampMs: 1150,
+ confidence: null,
+ },
+ {
+ text: ' move',
+ startMs: 1500,
+ endMs: 2300,
+ timestampMs: 1900,
+ confidence: null,
+ },
+ {
+ text: ' with',
+ startMs: 2300,
+ endMs: 3100,
+ timestampMs: 2700,
+ confidence: null,
+ },
+ {
+ text: ' every',
+ startMs: 3100,
+ endMs: 4000,
+ timestampMs: 3550,
+ confidence: null,
+ },
+ {
+ text: ' spoken',
+ startMs: 4000,
+ endMs: 5100,
+ timestampMs: 4550,
+ confidence: null,
+ },
+ {
+ text: ' word.',
+ startMs: 5100,
+ endMs: 6500,
+ timestampMs: 5800,
+ confidence: null,
+ },
+];
const wordHighlightCaptionsSchema = {
...Interactive.baseSchema,
- ...Interactive.captionsSchema,
+ captions: {
+ ...Interactive.captionsSchema.captions,
+ default: defaultCaptions,
+ },
width: {
type: 'number',
min: 1,
@@ -372,57 +426,7 @@ const WordHighlightCaptionsWithControls: React.FC<
<WordHighlightCaptionsInner
{...props}
callerStyle={style ?? null}
- captions={[
- {
- text: 'Captions',
- startMs: 0,
- endMs: 800,
- timestampMs: 400,
- confidence: null,
- },
- {
- text: ' can',
- startMs: 800,
- endMs: 1500,
- timestampMs: 1150,
- confidence: null,
- },
- {
- text: ' move',
- startMs: 1500,
- endMs: 2300,
- timestampMs: 1900,
- confidence: null,
- },
- {
- text: ' with',
- startMs: 2300,
- endMs: 3100,
- timestampMs: 2700,
- confidence: null,
- },
- {
- text: ' every',
- startMs: 3100,
- endMs: 4000,
- timestampMs: 3550,
- confidence: null,
- },
- {
- text: ' spoken',
- startMs: 4000,
- endMs: 5100,
- timestampMs: 4550,
- confidence: null,
- },
- {
- text: ' word.',
- startMs: 5100,
- endMs: 6500,
- timestampMs: 5800,
- confidence: null,
- },
- ]}
+ captions={defaultCaptions}
controls={controls}
width={props.width ?? 681}
height={props.height ?? 252}
diff --git a/packages/example/e2e/captions-inspector.test.mts b/packages/example/e2e/captions-inspector.test.mts
index 4431cb3c56..e8c7d39d44 100644
--- a/packages/example/e2e/captions-inspector.test.mts
+++ b/packages/example/e2e/captions-inspector.test.mts
@@ -12,21 +12,29 @@ const inlineCaptionsFile = path.join(
'CaptionsTester',
'InlineAnimatedCaptions.tsx',
);
+const defaultCaptionsFile = path.join(
+ exampleDir,
+ 'src',
+ 'MovingPillCaptionsComposition.tsx',
+);
test.describe('captions inspector', () => {
- let sourceBefore: string;
+ let inlineSourceBefore: string;
+ let defaultSourceBefore: string;
test.beforeEach(async () => {
- sourceBefore = fs.readFileSync(inlineCaptionsFile, 'utf-8');
+ inlineSourceBefore = fs.readFileSync(inlineCaptionsFile, 'utf-8');
+ defaultSourceBefore = fs.readFileSync(defaultCaptionsFile, 'utf-8');
await startStudio();
});
test.afterEach(async () => {
await stopStudio();
- fs.writeFileSync(inlineCaptionsFile, sourceBefore);
+ fs.writeFileSync(inlineCaptionsFile, inlineSourceBefore);
+ fs.writeFileSync(defaultCaptionsFile, defaultSourceBefore);
});
- test('persists a forced page break to an inline caption', async ({page}) => {
+ test('persists inline and default caption edits', async ({page}) => {
await page.goto(`${STUDIO_URL}/captions-inspector-e2e`);
await expect(page).toHaveURL(/captions-inspector-e2e/, {timeout: 15_000});
await page.waitForFunction(
@@ -64,5 +72,34 @@ test.describe('captions inspector', () => {
.includes('pageBreakAfter: true');
})
.toBe(true);
+
+ await page.goto(`${STUDIO_URL}/default-captions-inspector-e2e`);
+ await expect(page).toHaveURL(/default-captions-inspector-e2e/, {
+ timeout: 15_000,
+ });
+ await page.waitForFunction(
+ () => !document.body.innerText.includes('Loading...'),
+ {timeout: 30_000},
+ );
+
+ const defaultCaptionsSequence = page
+ .getByTitle('Moving Pill Captions', {exact: true})
+ .first();
+ const defaultCaption = page.getByRole('textbox', {name: 'Caption 1'});
+ await expect(async () => {
+ await defaultCaptionsSequence.click();
+ await expect(defaultCaption).toBeVisible({timeout: 1_000});
+ }).toPass({timeout: 30_000});
+ await expect(defaultCaption).toBeEnabled();
+
+ await defaultCaption.fill('Editable captions');
+ await defaultCaption.blur();
+ await expect
+ .poll(() => {
+ return /text:\s*['"]Editable captions['"]/.test(
+ fs.readFileSync(defaultCaptionsFile, 'utf-8'),
+ );
+ })
+ .toBe(true);
});
});
diff --git a/packages/example/src/E2eTestRoot.tsx b/packages/example/src/E2eTestRoot.tsx
index 88f9ef52dd..3ed1c623de 100644
--- a/packages/example/src/E2eTestRoot.tsx
+++ b/packages/example/src/E2eTestRoot.tsx
@@ -17,6 +17,7 @@ import {Issue8216} from './Issue8216/Issue8216';
import {LightLeakExample} from './LightLeak';
import {LostNodePathRepro} from './LostNodePathE2e/LostNodePathRepro';
import {MacCursorsExample} from './MacCursors';
+import {MovingPillCaptionsComposition} from './MovingPillCaptionsComposition';
import {NewVideoComp} from './NewVideo';
import {RotationKeyframeE2e} from './RotationKeyframeE2e';
import {SchemaTest, schemaTestSchema} from './SchemaTest';
@@ -109,6 +110,14 @@ export const E2eTestRoot: React.FC = () => {
width={1080}
height={CAPTIONS_HEIGHT}
/>
+ <Composition
+ id="default-captions-inspector-e2e"
+ component={MovingPillCaptionsComposition}
+ durationInFrames={210}
+ fps={30}
+ width={1920}
+ height={1080}
+ />
<Folder name="visual-controls">
<Composition
id="visual-controls"
diff --git a/packages/example/src/moving-pill-captions.element.tsx b/packages/example/src/moving-pill-captions.element.tsx
index a80f5808fc..05c6495762 100644
--- a/packages/example/src/moving-pill-captions.element.tsx
+++ b/packages/example/src/moving-pill-captions.element.tsx
@@ -51,10 +51,64 @@ const pillVerticalPadding = 12;
const pillBorderRadius = 10;
const pillMoveDurationInFrames = 5;
const defaultCombineTokensWithinMilliseconds = 800;
+const defaultCaptions: Caption[] = [
+ {
+ text: 'Captions',
+ startMs: 0,
+ endMs: 800,
+ timestampMs: 400,
+ confidence: null,
+ },
+ {
+ text: ' can',
+ startMs: 800,
+ endMs: 1500,
+ timestampMs: 1150,
+ confidence: null,
+ },
+ {
+ text: ' move',
+ startMs: 1500,
+ endMs: 2300,
+ timestampMs: 1900,
+ confidence: null,
+ },
+ {
+ text: ' with',
+ startMs: 2300,
+ endMs: 3100,
+ timestampMs: 2700,
+ confidence: null,
+ },
+ {
+ text: ' every',
+ startMs: 3100,
+ endMs: 4000,
+ timestampMs: 3550,
+ confidence: null,
+ },
+ {
+ text: ' spoken',
+ startMs: 4000,
+ endMs: 5100,
+ timestampMs: 4550,
+ confidence: null,
+ },
+ {
+ text: ' word.',
+ startMs: 5100,
+ endMs: 6500,
+ timestampMs: 5800,
+ confidence: null,
+ },
+];
const movingPillCaptionsSchema = {
...Interactive.baseSchema,
- ...Interactive.captionsSchema,
+ captions: {
+ ...Interactive.captionsSchema.captions,
+ default: defaultCaptions,
+ },
width: {
type: 'number',
min: 1,
@@ -504,57 +558,7 @@ const MovingPillCaptionsWithControls: React.FC<
<MovingPillCaptionsInner
{...props}
callerStyle={style ?? null}
- captions={[
- {
- text: 'Captions',
- startMs: 0,
- endMs: 800,
- timestampMs: 400,
- confidence: null,
- },
- {
- text: ' can',
- startMs: 800,
- endMs: 1500,
- timestampMs: 1150,
- confidence: null,
- },
- {
- text: ' move',
- startMs: 1500,
- endMs: 2300,
- timestampMs: 1900,
- confidence: null,
- },
- {
- text: ' with',
- startMs: 2300,
- endMs: 3100,
- timestampMs: 2700,
- confidence: null,
- },
- {
- text: ' every',
- startMs: 3100,
- endMs: 4000,
- timestampMs: 3550,
- confidence: null,
- },
- {
- text: ' spoken',
- startMs: 4000,
- endMs: 5100,
- timestampMs: 4550,
- confidence: null,
- },
- {
- text: ' word.',
- startMs: 5100,
- endMs: 6500,
- timestampMs: 5800,
- confidence: null,
- },
- ]}
+ captions={defaultCaptions}
controls={controls}
width={props.width ?? 681}
height={props.height ?? 252}
diff --git a/packages/studio/src/components/CaptionTextEditor.tsx b/packages/studio/src/components/CaptionTextEditor.tsx
index 346567b81e..bd5a7187cb 100644
--- a/packages/studio/src/components/CaptionTextEditor.tsx
+++ b/packages/studio/src/components/CaptionTextEditor.tsx
@@ -170,6 +170,7 @@ export const CaptionTextEditor: React.FC<{
{formatMilliseconds(caption.endMs)} ms
</div>
<RemotionInput
+ aria-label={`Caption ${index + 1}`}
data-caption-index={index}
disabled={readOnly}
onBlur={(event) => {
diff --git a/packages/studio/src/components/InlineCaptionInspector.tsx b/packages/studio/src/components/InlineCaptionInspector.tsx
index 2591d1fc1e..35cd528fb5 100644
--- a/packages/studio/src/components/InlineCaptionInspector.tsx
+++ b/packages/studio/src/components/InlineCaptionInspector.tsx
@@ -15,7 +15,10 @@ import {Internals} from 'remotion';
import type {CodePosition} from '../error-overlay/react-overlay/utils/get-source-map';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {CaptionInspector} from './CaptionInspector';
-import {saveInlineCaptionPatches} from './Timeline/save-sequence-prop';
+import {
+ saveInlineCaptionPatches,
+ saveSequenceProps,
+} from './Timeline/save-sequence-prop';
const serializeCaptions = (captions: Caption[]): string => {
return JSON.stringify(captions);
@@ -95,6 +98,8 @@ export const InlineCaptionInspector: React.FC<{
: null;
const canSave =
!readOnlyStudio && clientId !== null && captionStatus?.status === 'static';
+ const captionsAreOmitted =
+ captionStatus?.status === 'static' && captionStatus.codeValue === undefined;
const [draftCaptions, setDraftCaptions] = useState(captions);
const savedCaptions = useRef(captions);
const runtimeSignature = serializeCaptions(captions);
@@ -147,21 +152,51 @@ export const InlineCaptionInspector: React.FC<{
}
savedCaptions.current = nextCaptions;
- saveInlineCaptionPatches({
- fileName: validatedLocation.source,
- nodePath,
- schema: controls.schema,
- patches,
- nextCaptions,
- setPropStatuses,
- clientId,
- undoLabel: 'Update captions',
- redoLabel: 'Update captions again',
- });
+ if (captionsAreOmitted) {
+ const defaultCaptions =
+ controls.schema.captions?.type === 'remotion-captions'
+ ? controls.schema.captions.default
+ : undefined;
+ saveSequenceProps({
+ changes: [
+ {
+ fileName: validatedLocation.source,
+ nodePath,
+ fieldKey: 'captions',
+ value: nextCaptions,
+ defaultValue:
+ defaultCaptions === undefined
+ ? null
+ : JSON.stringify(defaultCaptions),
+ schema: controls.schema,
+ },
+ ],
+ addedKeyframes: null,
+ movedKeyframes: null,
+ setPropStatuses,
+ clientId,
+ undoLabel: 'Update captions',
+ redoLabel: 'Update captions again',
+ });
+ } else {
+ saveInlineCaptionPatches({
+ fileName: validatedLocation.source,
+ nodePath,
+ schema: controls.schema,
+ patches,
+ nextCaptions,
+ setPropStatuses,
+ clientId,
+ undoLabel: 'Update captions',
+ redoLabel: 'Update captions again',
+ });
+ }
+
clearDragOverrides(nodePath);
},
[
canSave,
+ captionsAreOmitted,
clearDragOverrides,
clientId,
controls.schema,
diff --git a/packages/studio/src/components/InspectorSequenceSection.tsx b/packages/studio/src/components/InspectorSequenceSection.tsx
index 2c821bb69b..3afb086e77 100644
--- a/packages/studio/src/components/InspectorSequenceSection.tsx
+++ b/packages/studio/src/components/InspectorSequenceSection.tsx
@@ -636,7 +636,7 @@ export const InspectorSequenceSection: React.FC<{
);
const inlineCaptionValue =
schema.captions?.type === 'remotion-captions'
- ? runtimeValues.captions
+ ? (runtimeValues.captions ?? schema.captions.default)
: null;
const inlineCaptions = Array.isArray(inlineCaptionValue)
? (inlineCaptionValue as Caption[])