Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Match HTML-in-canvas wrapper timing

JonnyBurgerb6f61f479fda
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.ts
index 9338200a2e..d435ebe8e8 100644
--- a/packages/browser-studio/src/browser-studio-operations.ts
+++ b/packages/browser-studio/src/browser-studio-operations.ts
@@ -1547,6 +1547,7 @@ export const createBrowserStudioOperations = ({
wrapper,
width,
height,
+ timing,
}) => {
try {
const project = getProject();
@@ -1574,7 +1575,11 @@ export const createBrowserStudioOperations = ({
: 'remotion',
props:
wrapper === 'HtmlInCanvas' || wrapper === 'HtmlInCanvasMotionBlur'
- ? {width: width ?? 0, height: height ?? 0}
+ ? {
+ width: width ?? 0,
+ height: height ?? 0,
+ ...(timing ?? {}),
+ }
: {},
}),
});
diff --git a/packages/browser-studio/src/test/browser-studio-operations.test.ts b/packages/browser-studio/src/test/browser-studio-operations.test.ts
index 8601ffec30..2071305803 100644
--- a/packages/browser-studio/src/test/browser-studio-operations.test.ts
+++ b/packages/browser-studio/src/test/browser-studio-operations.test.ts
@@ -988,6 +988,7 @@ export const Component = () => <AbsoluteFill><div /></AbsoluteFill>;`;
wrapper: 'Sequence',
width: null,
height: null,
+ timing: null,
});
expect(result.success).toBe(true);
expect(getProject().files[fileName]).toContain('<Sequence>');
diff --git a/packages/studio-server/src/preview-server/routes/wrap-node.ts b/packages/studio-server/src/preview-server/routes/wrap-node.ts
index 786a6e57dc..6bb8b36004 100644
--- a/packages/studio-server/src/preview-server/routes/wrap-node.ts
+++ b/packages/studio-server/src/preview-server/routes/wrap-node.ts
@@ -18,7 +18,7 @@ import {
} from './source-file-write-queue';
export const wrapNodeHandler: ApiHandler<WrapNodeRequest, WrapNodeResponse> = ({
- input: {fileName, nodePath, wrapper, width, height},
+ input: {fileName, nodePath, wrapper, width, height, timing},
remotionRoot,
logLevel,
}) => {
@@ -58,7 +58,11 @@ export const wrapNodeHandler: ApiHandler<WrapNodeRequest, WrapNodeResponse> = ({
: 'remotion',
props:
wrapper === 'HtmlInCanvas' || wrapper === 'HtmlInCanvasMotionBlur'
- ? {width: width ?? 0, height: height ?? 0}
+ ? {
+ width: width ?? 0,
+ height: height ?? 0,
+ ...(timing ?? {}),
+ }
: {},
}),
});
diff --git a/packages/studio-server/src/test/wrap-node.test.ts b/packages/studio-server/src/test/wrap-node.test.ts
index 169c34fdbe..e468c47805 100644
--- a/packages/studio-server/src/test/wrap-node.test.ts
+++ b/packages/studio-server/src/test/wrap-node.test.ts
@@ -48,6 +48,11 @@ export const Comp = () => {
wrapper: 'HtmlInCanvas',
width: 2560,
height: 1248,
+ timing: {
+ from: 10,
+ durationInFrames: 30,
+ trimBefore: 10,
+ },
},
entryPoint,
remotionRoot,
@@ -69,7 +74,13 @@ export const Comp = () => {
expect(result.success).toBe(true);
expect(readFileSync(entryPoint, 'utf-8')).toContain(
[
- ' <HtmlInCanvas width={2560} height={1248}>',
+ ' <HtmlInCanvas',
+ ' width={2560}',
+ ' height={1248}',
+ ' from={10}',
+ ' durationInFrames={30}',
+ ' trimBefore={10}',
+ ' >',
' <AbsoluteFill',
' style={{width: 2560, height: 1248}}',
' >',
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index e88ed19533..5dd59e701a 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -903,6 +903,11 @@ export type WrapNodeRequest = {
wrapper: NodeWrapper | null;
width: number | null;
height: number | null;
+ timing: {
+ from: number;
+ durationInFrames: number;
+ trimBefore: number;
+ } | null;
};
export type WrapNodeResponse =
diff --git a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
index d1d05e8245..0834699b70 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
@@ -365,7 +365,7 @@ const SequenceSourceQuickActions: React.FC<{
{mediaSequence === null ? (
<SequenceWrapAction
nodePathInfo={selection.nodePathInfo}
- sequence={track.sequence}
+ track={track}
sourceActionsDisabled={sourceActionsDisabled}
sourceLocation={validatedLocation}
/>
diff --git a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
index 5285079923..adc0206d96 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
@@ -7,11 +7,13 @@ import React, {
useState,
} from 'react';
import {Internals, isHtmlInCanvasSupported, useVideoConfig} from 'remotion';
+import {calculateTimeline} from '../../helpers/calculate-timeline';
import type {
SequenceNodePathInfo,
TimelineTrackData,
} from '../../helpers/get-timeline-sequence-sort-key';
import {installRequiredPackages} from '../../helpers/install-required-package';
+import {timelineSequenceNodePathToKey} from '../../helpers/timeline-node-path-key';
import {HtmlInCanvasIcon} from '../../icons/html-in-canvas';
import {MotionBlurIcon} from '../../icons/motion-blur';
import {SetSelectedModalContext} from '../../state/modals';
@@ -35,21 +37,99 @@ const htmlInCanvasComponentIdentities = new Set([
export const SequenceWrapAction: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo;
- readonly sequence: TimelineTrackData['sequence'];
+ readonly track: TimelineTrackData;
readonly sourceActionsDisabled: boolean;
readonly sourceLocation: {
readonly source: string;
readonly line: number;
};
-}> = ({nodePathInfo, sequence, sourceActionsDisabled, sourceLocation}) => {
+}> = ({nodePathInfo, track, sourceActionsDisabled, sourceLocation}) => {
const {width, height} = useVideoConfig();
const {setSelectedModal} = useContext(SetSelectedModalContext);
const {sequences} = useContext(Internals.SequenceManager);
+ const {overrideIdToNodePathMappings} = useContext(
+ Internals.OverrideIdsToNodePathsGettersContext,
+ );
+ const {sequence} = track;
const nodePathKey = JSON.stringify(nodePathInfo.sequenceSubscriptionKey);
const sequenceStack = sequence.getStack();
const eligibilityKey = JSON.stringify([nodePathKey, sequenceStack]);
const [busy, setBusy] = useState(false);
const [eligibleNodeKey, setEligibleNodeKey] = useState<string | null>(null);
+ const timing = useMemo(() => {
+ const selectedNodePathKey = timelineSequenceNodePathToKey(
+ nodePathInfo.sequenceSubscriptionKey,
+ );
+ const matchingTracks = calculateTimeline({
+ sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ }).filter(
+ (candidate) =>
+ candidate.nodePathInfo !== null &&
+ timelineSequenceNodePathToKey(
+ candidate.nodePathInfo.sequenceSubscriptionKey,
+ ) === selectedNodePathKey,
+ );
+ const timings = matchingTracks.map((candidate) => {
+ const from = Math.max(
+ 0,
+ candidate.localStart +
+ (candidate.sequence.from - candidate.cascadedStart) *
+ candidate.keyframePlaybackRate,
+ );
+ return {
+ from,
+ durationInFrames:
+ candidate.sequence.duration * candidate.keyframePlaybackRate,
+ trimBefore: from,
+ };
+ });
+ const firstTiming = timings[0];
+ if (
+ !firstTiming ||
+ !Number.isFinite(firstTiming.from) ||
+ !Number.isFinite(firstTiming.durationInFrames) ||
+ firstTiming.durationInFrames <= 0
+ ) {
+ return null;
+ }
+
+ for (const candidate of timings.slice(1)) {
+ if (
+ !Number.isFinite(candidate.from) ||
+ !Number.isFinite(candidate.durationInFrames) ||
+ candidate.durationInFrames <= 0
+ ) {
+ return null;
+ }
+
+ const fromTolerance =
+ Number.EPSILON *
+ Math.max(1, Math.abs(firstTiming.from), Math.abs(candidate.from)) *
+ 16;
+ const durationTolerance =
+ Number.EPSILON *
+ Math.max(
+ 1,
+ Math.abs(firstTiming.durationInFrames),
+ Math.abs(candidate.durationInFrames),
+ ) *
+ 16;
+ if (
+ Math.abs(firstTiming.from - candidate.from) > fromTolerance ||
+ Math.abs(firstTiming.durationInFrames - candidate.durationInFrames) >
+ durationTolerance
+ ) {
+ return null;
+ }
+ }
+
+ return firstTiming;
+ }, [
+ nodePathInfo.sequenceSubscriptionKey,
+ overrideIdToNodePathMappings,
+ sequences,
+ ]);
const wouldNestAtRuntime = useMemo(() => {
const sequencesById = new Map(
sequences.map((registeredSequence) => [
@@ -99,7 +179,7 @@ export const SequenceWrapAction: React.FC<{
}, [sequence.id, sequences]);
useEffect(() => {
- if (sourceActionsDisabled || wouldNestAtRuntime) {
+ if (sourceActionsDisabled || wouldNestAtRuntime || timing === null) {
setEligibleNodeKey(null);
return;
}
@@ -115,6 +195,7 @@ export const SequenceWrapAction: React.FC<{
wrapper: null,
width: null,
height: null,
+ timing: null,
})
.then((eligibility) => {
if (!cancelled) {
@@ -134,7 +215,13 @@ export const SequenceWrapAction: React.FC<{
return () => {
cancelled = true;
};
- }, [eligibilityKey, nodePathKey, sourceActionsDisabled, wouldNestAtRuntime]);
+ }, [
+ eligibilityKey,
+ nodePathKey,
+ sourceActionsDisabled,
+ timing,
+ wouldNestAtRuntime,
+ ]);
const onWrap = useCallback(
async (wrapper: HtmlInCanvasWrapper) => {
@@ -142,6 +229,7 @@ export const SequenceWrapAction: React.FC<{
busy ||
sourceActionsDisabled ||
wouldNestAtRuntime ||
+ timing === null ||
eligibleNodeKey !== eligibilityKey
) {
return;
@@ -167,6 +255,7 @@ export const SequenceWrapAction: React.FC<{
wrapper: null,
width: null,
height: null,
+ timing: null,
});
if (!eligibility.success) {
showNotification(eligibility.reason, 4000);
@@ -202,6 +291,7 @@ export const SequenceWrapAction: React.FC<{
wrapper,
width,
height,
+ timing,
});
if (!result.success) {
showNotification(result.reason, 4000);
@@ -223,6 +313,7 @@ export const SequenceWrapAction: React.FC<{
setSelectedModal,
sourceActionsDisabled,
sourceLocation,
+ timing,
width,
wouldNestAtRuntime,
],
@@ -231,6 +322,7 @@ export const SequenceWrapAction: React.FC<{
if (
sourceActionsDisabled ||
eligibleNodeKey !== eligibilityKey ||
+ timing === null ||
wouldNestAtRuntime
) {
return null;