Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Add Copy frame canvas action

Jonny Burger16c199aea9e7
diff --git a/packages/studio/src/components/SelectedOutlineElement.tsx b/packages/studio/src/components/SelectedOutlineElement.tsx
index 62d1abad0c..e7c51b4e72 100644
--- a/packages/studio/src/components/SelectedOutlineElement.tsx
+++ b/packages/studio/src/components/SelectedOutlineElement.tsx
@@ -32,7 +32,10 @@ import {SelectedOutlineCanvasRotation} from './SelectedOutlineCanvasRotation';
import {SelectedOutlinePolygon} from './SelectedOutlinePolygon';
import {disableSequenceInteractivity} from './Timeline/disable-sequence-interactivity';
import {duplicateSequencesFromSource} from './Timeline/duplicate-selected-timeline-item';
-import {getSequenceContextMenuItems} from './Timeline/get-sequence-context-menu-items';
+import {
+ findCopyableFrameElement,
+ getSequenceContextMenuItems,
+} from './Timeline/get-sequence-context-menu-items';
import {getTimelineAssetLinkInfo} from './Timeline/timeline-asset-link';
import {
getTimelineSequenceSelectionKey,
@@ -238,14 +241,16 @@ const SelectedOutlineElementUnmemoized: React.FC<
previewServerState.type === 'connected';
const canCrop = contextMenuTarget.canCrop && !sourceEditDisabled;
const canRotate = !sourceEditDisabled;
+ const outlineElement =
+ contextMenuTarget.sequence.refForOutline?.current ?? null;
return getSequenceContextMenuItems({
assetLinkInfo,
canOpenInEditor,
codingAgentInfo,
copyImageElement:
contextMenuTarget.sequence.type === 'image'
- ? (contextMenuTarget.sequence.refForOutline?.current ?? null)
- : null,
+ ? outlineElement
+ : findCopyableFrameElement(outlineElement),
deleteDisabled: sourceEditDisabled,
disableInteractivityDisabled,
duplicateDisabled: sourceEditDisabled || isProgrammaticallyDuplicated,
diff --git a/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts b/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts
index 8ac8c38e5b..d773e596df 100644
--- a/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts
+++ b/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts
@@ -44,6 +44,16 @@ const normalizeMenuDividers = (items: ComboboxValue[]): ComboboxValue[] => {
const interactiveSvgComponentIdentity = 'dev.remotion.remotion.Interactive.Svg';
+export const findCopyableFrameElement = (
+ element: Element | null,
+): Element | null => {
+ if (element?.tagName === 'CANVAS' || element?.tagName === 'VIDEO') {
+ return element;
+ }
+
+ return element?.querySelector('canvas, video') ?? null;
+};
+
const copyImageToClipboard = async (element: Element): Promise<void> => {
let canvas: HTMLCanvasElement;
@@ -64,8 +74,23 @@ const copyImageToClipboard = async (element: Element): Promise<void> => {
}
context.drawImage(image, 0, 0);
+ } else if (element.tagName === 'VIDEO') {
+ const video = element as HTMLVideoElement;
+ if (video.videoWidth === 0 || video.videoHeight === 0) {
+ throw new Error('Video frame has not loaded');
+ }
+
+ canvas = document.createElement('canvas');
+ canvas.width = video.videoWidth;
+ canvas.height = video.videoHeight;
+ const context = canvas.getContext('2d');
+ if (!context) {
+ throw new Error('Could not create canvas context');
+ }
+
+ context.drawImage(video, 0, 0);
} else {
- throw new Error('Expected an image or canvas element');
+ throw new Error('Expected an image, video, or canvas element');
}
const blob = new Promise<Blob>((resolve, reject) => {
@@ -189,6 +214,8 @@ export const getSequenceContextMenuItems = ({
: null;
const defaultOpenInName =
defaultOpenInTarget === 'editor' ? editorName : gitSourceName;
+ const copyImageLabel =
+ copyImageElement && sequence.type === 'image' ? 'Copy image' : 'Copy frame';
const defaultCodingAgent = codingAgentInfo?.installedCodingAgents.find(
(codingAgent) => codingAgent.id === codingAgentInfo.defaultCodingAgent,
);
@@ -321,26 +348,29 @@ export const getSequenceContextMenuItems = ({
copyImageElement
? {
type: 'item' as const,
- id: 'copy-image',
+ id: copyImageLabel === 'Copy image' ? 'copy-image' : 'copy-frame',
keyHint: null,
- label: 'Copy image',
+ label: copyImageLabel,
leftItem: null,
disabled: false,
onClick: () => {
copyImageToClipboard(copyImageElement)
.then(() => {
- showNotification('Copied image to clipboard', 1000);
+ showNotification(
+ `Copied ${copyImageLabel === 'Copy image' ? 'image' : 'frame'} to clipboard`,
+ 1000,
+ );
})
.catch((err) => {
showNotification(
- `Could not copy image: ${(err as Error).message}`,
+ `Could not copy ${copyImageLabel === 'Copy image' ? 'image' : 'frame'}: ${(err as Error).message}`,
2000,
);
});
},
quickSwitcherLabel: null,
subMenu: null,
- value: 'copy-image',
+ value: copyImageLabel === 'Copy image' ? 'copy-image' : 'copy-frame',
}
: null,
assetLinkInfo || copyImageElement
diff --git a/packages/studio/src/test/sequence-context-menu-items.test.ts b/packages/studio/src/test/sequence-context-menu-items.test.ts
index 00cf497040..6be490e818 100644
--- a/packages/studio/src/test/sequence-context-menu-items.test.ts
+++ b/packages/studio/src/test/sequence-context-menu-items.test.ts
@@ -4,7 +4,10 @@ import type {TSequence} from 'remotion';
import {getOpenInMenuItems} from '../components/get-open-in-menu-items';
import type {ComboboxValue} from '../components/NewComposition/ComboBox';
import {getCopyContextForAgentsMenuItem} from '../components/Timeline/get-copy-context-for-agents-menu-item';
-import {getSequenceContextMenuItems} from '../components/Timeline/get-sequence-context-menu-items';
+import {
+ findCopyableFrameElement,
+ getSequenceContextMenuItems,
+} from '../components/Timeline/get-sequence-context-menu-items';
import {getTimelineMediaStartFrame} from '../components/Timeline/get-timeline-media-start-frame';
import {
calculateSequenceFreezeFrame,
@@ -74,6 +77,23 @@ const renameSequenceItem: ComboboxValue = {
const noop = () => undefined;
+test('finds direct and nested elements that can copy a frame', () => {
+ const canvas = {tagName: 'CANVAS'} as Element;
+ const video = {tagName: 'VIDEO'} as Element;
+ const wrapper = {
+ tagName: 'DIV',
+ querySelector: (selector: string) => {
+ expect(selector).toBe('canvas, video');
+ return video;
+ },
+ } as unknown as Element;
+
+ expect(findCopyableFrameElement(canvas)).toBe(canvas);
+ expect(findCopyableFrameElement(video)).toBe(video);
+ expect(findCopyableFrameElement(wrapper)).toBe(video);
+ expect(findCopyableFrameElement(null)).toBeNull();
+});
+
test('copy context menu items write their agent context to the clipboard', () => {
const copiedTexts: string[] = [];
Object.defineProperty(globalThis, 'navigator', {
@@ -144,14 +164,14 @@ const expectNoExtraDividers = (items: ComboboxValue[]) => {
}
};
-test('sequence context menu normalizes dividers before source actions', () => {
+test('sequence context menu adds frame copying and normalizes dividers', () => {
installTestWindow();
const items = getSequenceContextMenuItems({
assetLinkInfo: null,
canOpenInEditor: false,
codingAgentInfo: null,
- copyImageElement: null,
+ copyImageElement: {tagName: 'CANVAS'} as Element,
deleteDisabled: false,
disableInteractivityDisabled: false,
duplicateDisabled: false,
@@ -168,6 +188,7 @@ test('sequence context menu normalizes dividers before source actions', () => {
selectAsset: noop,
sequence: {
documentationLink: 'https://www.remotion.dev/docs/sequence',
+ type: 'sequence',
} as TSequence,
sourceActions: [renameSequenceItem],
});
@@ -177,10 +198,15 @@ test('sequence context menu normalizes dividers before source actions', () => {
const copyContextIndex = items.findIndex(
(item) => item.id === 'copy-context-for-agents',
);
+ const copyFrameIndex = items.findIndex((item) => item.id === 'copy-frame');
const renameIndex = items.findIndex((item) => item.id === 'rename-sequence');
+ expect(copyFrameIndex).toBe(copyContextIndex + 1);
+ expect(
+ items[copyFrameIndex]?.type === 'item' ? items[copyFrameIndex].label : null,
+ ).toBe('Copy frame');
expect(
- items.slice(copyContextIndex + 1, renameIndex).map((item) => item.type),
+ items.slice(copyFrameIndex + 1, renameIndex).map((item) => item.type),
).toEqual(['divider']);
});