Skip to content

🎥 Make videos programmatically with React

deserialize

Jonny Burgerb34c044e5c97
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
index 6a43564c8c..47a9f2e2aa 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
@@ -24,12 +24,12 @@ import {SegmentedControl} from '../SegmentedControl';
import type {TypeCanSaveState} from './get-render-modal-warnings';
import {getRenderModalWarnings} from './get-render-modal-warnings';
import {RenderModalJSONPropsEditor} from './RenderModalJSONPropsEditor';
-import type {SerializedJSONWithDate} from './SchemaEditor/date-serialization';
+import {extractEnumJsonPaths} from './SchemaEditor/extract-enum-json-paths';
+import type {SerializedJSONWithCustomFields} from './SchemaEditor/input-props-serialization';
import {
deserializeJSONWithDate,
serializeJSONWithDate,
-} from './SchemaEditor/date-serialization';
-import {extractEnumJsonPaths} from './SchemaEditor/extract-enum-json-paths';
+} from './SchemaEditor/input-props-serialization';
import {SchemaEditor} from './SchemaEditor/SchemaEditor';
import {
NoDefaultProps,
@@ -139,7 +139,7 @@ export const RenderModalData: React.FC<{
);
const inJSONEditor = mode === 'json';
- const serializedJSON: SerializedJSONWithDate | null = useMemo(() => {
+ const serializedJSON: SerializedJSONWithCustomFields | null = useMemo(() => {
if (!inJSONEditor) {
return null;
}
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModalJSONPropsEditor.tsx b/packages/cli/src/editor/components/RenderModal/RenderModalJSONPropsEditor.tsx
index 1c4f384eb6..eefca47b76 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModalJSONPropsEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModalJSONPropsEditor.tsx
@@ -5,7 +5,7 @@ import {Row, Spacing} from '../layout';
import {RemTextarea} from '../NewComposition/RemTextarea';
import {ValidationMessage} from '../NewComposition/ValidationMessage';
import type {State} from './RenderModalData';
-import type {SerializedJSONWithDate} from './SchemaEditor/date-serialization';
+import type {SerializedJSONWithCustomFields} from './SchemaEditor/input-props-serialization';
const style: React.CSSProperties = {
fontFamily: 'monospace',
@@ -38,7 +38,7 @@ export const RenderModalJSONPropsEditor: React.FC<{
valBeforeSafe: unknown;
showSaveButton: boolean;
parseJSON: (str: string) => State;
- serializedJSON: SerializedJSONWithDate | null;
+ serializedJSON: SerializedJSONWithCustomFields | null;
}> = ({
setValue,
value,
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts b/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts
index 8dd9623b54..77cd44018b 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/input-props-serialization.ts
@@ -1,32 +1,56 @@
-export type SerializedJSONWithDate = {
+import {staticFile} from 'remotion';
+
+export type SerializedJSONWithCustomFields = {
serializedString: string;
customDateUsed: boolean;
+ customFileUsed: boolean;
};
+const DATE_TOKEN = 'remotion-date:';
+const FILE_TOKEN = 'remotion-file:';
+
export const serializeJSONWithDate = (
data: unknown,
indent: number | undefined
-): SerializedJSONWithDate => {
+): SerializedJSONWithCustomFields => {
let customDateUsed = false;
+ let customFileUsed = false;
+
const serializedString = JSON.stringify(
data,
function (key, value) {
- if (this[key] instanceof Date) {
+ const item = this[key];
+ if (item instanceof Date) {
customDateUsed = true;
- return `remotion-date:${this[key].toISOString()}`;
+ return `${DATE_TOKEN}${item.toISOString()}`;
+ }
+
+ if (
+ typeof item === 'string' &&
+ item.startsWith(window.remotion_staticBase)
+ ) {
+ customFileUsed = true;
+ return `${FILE_TOKEN}${item.replace(
+ window.remotion_staticBase + '/',
+ ''
+ )}`;
}
return value;
},
indent
);
- return {serializedString, customDateUsed};
+ return {serializedString, customDateUsed, customFileUsed};
};
export const deserializeJSONWithDate = (data: string) => {
return JSON.parse(data, (_, value) => {
- if (typeof value === 'string' && value.startsWith('remotion-date:')) {
- return new Date(value.replace('remotion-date:', ''));
+ if (typeof value === 'string' && value.startsWith(DATE_TOKEN)) {
+ return new Date(value.replace(DATE_TOKEN, ''));
+ }
+
+ if (typeof value === 'string' && value.startsWith(FILE_TOKEN)) {
+ return staticFile(value.replace(FILE_TOKEN, ''));
}
return value;
diff --git a/packages/cli/src/editor/components/RenderQueue/actions.ts b/packages/cli/src/editor/components/RenderQueue/actions.ts
index 5e189a0fc6..7b66f4741c 100644
--- a/packages/cli/src/editor/components/RenderQueue/actions.ts
+++ b/packages/cli/src/editor/components/RenderQueue/actions.ts
@@ -12,8 +12,8 @@ import type {
RenderJob,
} from '../../../preview-server/render-queue/job';
import type {RequiredChromiumOptions} from '../../../required-chromium-options';
-import {serializeJSONWithDate} from '../RenderModal/SchemaEditor/date-serialization';
import type {EnumPath} from '../RenderModal/SchemaEditor/extract-enum-json-paths';
+import {serializeJSONWithDate} from '../RenderModal/SchemaEditor/input-props-serialization';
const callApi = <Endpoint extends keyof ApiRoutes>(
endpoint: Endpoint,
diff --git a/packages/cli/src/preview-server/routes/update-default-props.ts b/packages/cli/src/preview-server/routes/update-default-props.ts
index c914b12461..3c72359bb8 100644
--- a/packages/cli/src/preview-server/routes/update-default-props.ts
+++ b/packages/cli/src/preview-server/routes/update-default-props.ts
@@ -1,6 +1,6 @@
import {readFileSync, writeFileSync} from 'node:fs';
import {updateDefaultProps} from '../../codemods/update-default-props';
-import {deserializeJSONWithDate} from '../../editor/components/RenderModal/SchemaEditor/date-serialization';
+import {deserializeJSONWithDate} from '../../editor/components/RenderModal/SchemaEditor/input-props-serialization';
import type {ApiHandler} from '../api-types';
import {getProjectInfo} from '../project-info';
import type {UpdateDefaultPropsRequest} from '../render-queue/job';
diff --git a/packages/cli/src/test/date-serialization.test.ts b/packages/cli/src/test/date-serialization.test.ts
index abba1d702c..2f258dbf13 100644
--- a/packages/cli/src/test/date-serialization.test.ts
+++ b/packages/cli/src/test/date-serialization.test.ts
@@ -2,7 +2,7 @@ import {expect, test} from 'vitest';
import {
deserializeJSONWithDate,
serializeJSONWithDate,
-} from '../editor/components/RenderModal/SchemaEditor/date-serialization';
+} from '../editor/components/RenderModal/SchemaEditor/input-props-serialization';
test('date serialization', () => {
const date = {data: new Date(), hi: 'there'};
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index f1642ddc00..020eac87d0 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -784,7 +784,7 @@ export const Index: React.FC = () => {
color: '#eb3a60',
nullable: null,
optional: 'hihi',
- filePath: staticFile('logo.png'),
+ filePath: 'remotion-file:jpeg.png',
}}
durationInFrames={150}
/>