Skip to content

🎥 Make videos programmatically with React

support uint8array

Jonny Burgercff3cc991c69
diff --git a/packages/cli/src/on-artifact.ts b/packages/cli/src/on-artifact.ts
index b8e7659426..595d3d946f 100644
--- a/packages/cli/src/on-artifact.ts
+++ b/packages/cli/src/on-artifact.ts
@@ -15,17 +15,12 @@ export const handleOnArtifact = (
artifact.filename.replace('/', path.sep),
);
- const stringOrUintArray =
- typeof artifact.content === 'string'
- ? artifact.content
- : new Uint8Array(Object.values(artifact.content));
-
- writeFileSync(artifact.filename, stringOrUintArray);
+ writeFileSync(artifact.filename, artifact.content);
initialProgress.received.push({
absoluteOutputDestination,
filename: artifact.filename,
- sizeInBytes: stringOrUintArray.length,
+ sizeInBytes: artifact.content.length,
});
};
diff --git a/packages/lambda/src/functions/helpers/serialize-artifact.ts b/packages/lambda/src/functions/helpers/serialize-artifact.ts
new file mode 100644
index 0000000000..f65a856928
--- /dev/null
+++ b/packages/lambda/src/functions/helpers/serialize-artifact.ts
@@ -0,0 +1,51 @@
+import type {EmittedArtifact} from '@remotion/renderer';
+
+export type SerializedArtifact = {
+ filename: string;
+ stringContent: string;
+ frame: number;
+ binary: boolean;
+};
+
+export const deserializeArtifact = (
+ serializedArtifact: SerializedArtifact,
+): EmittedArtifact => {
+ if (serializedArtifact.binary) {
+ const encoder = new TextEncoder();
+ const content = encoder.encode(atob(serializedArtifact.stringContent));
+
+ return {
+ filename: serializedArtifact.filename,
+ content,
+ frame: serializedArtifact.frame,
+ };
+ }
+
+ return {
+ filename: serializedArtifact.filename,
+ content: serializedArtifact.stringContent,
+ frame: serializedArtifact.frame,
+ };
+};
+
+export const serializeArtifact = (
+ artifact: EmittedArtifact,
+): SerializedArtifact => {
+ if (artifact.content instanceof Uint8Array) {
+ const decoder = new TextDecoder('utf8');
+ const b64encoded = btoa(decoder.decode(artifact.content));
+ return {
+ filename: artifact.filename,
+ stringContent: b64encoded,
+ frame: artifact.frame,
+ binary: true,
+ };
+ }
+
+ return {
+ filename: artifact.filename,
+ stringContent: artifact.content,
+ frame: artifact.frame,
+ binary: false,
+ };
+};
diff --git a/packages/lambda/src/functions/helpers/stream-renderer.ts b/packages/lambda/src/functions/helpers/stream-renderer.ts
index e7a041aaaa..ea99ff99f6 100644
--- a/packages/lambda/src/functions/helpers/stream-renderer.ts
+++ b/packages/lambda/src/functions/helpers/stream-renderer.ts
@@ -1,4 +1,4 @@
-import type {EmittedAsset, LogLevel} from '@remotion/renderer';
+import type {EmittedArtifact, LogLevel} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {writeFileSync} from 'fs';
import {join} from 'path';
@@ -8,6 +8,7 @@ import {callLambdaWithStreaming} from '../../shared/call-lambda';
import type {OnMessage} from '../streaming/streaming';
import {getCurrentRegionInFunction} from './get-current-region';
import type {OverallProgressHelper} from './overall-render-progress';
+import {deserializeArtifact} from './serialize-artifact';
type StreamRendererResponse =
| {
@@ -34,7 +35,7 @@ const streamRenderer = ({
overallProgress: OverallProgressHelper;
files: string[];
logLevel: LogLevel;
- onArtifact: (asset: EmittedAsset) => {alreadyExisted: boolean};
+ onArtifact: (asset: EmittedArtifact) => {alreadyExisted: boolean};
}) => {
if (payload.type !== LambdaRoutines.renderer) {
throw new Error('Expected renderer type');
@@ -100,13 +101,14 @@ const streamRenderer = ({
}
if (message.type === 'artifact-emitted') {
+ const artifact = deserializeArtifact(message.payload.artifact);
RenderInternals.Log.info(
{indent: false, logLevel},
`Received artifact on frame ${message.payload.artifact.frame}:`,
- message.payload.artifact.filename,
- message.payload.artifact.content.length + 'bytes.',
+ artifact.filename,
+ artifact.content.length + 'bytes.',
);
- const {alreadyExisted} = onArtifact(message.payload.artifact);
+ const {alreadyExisted} = onArtifact(artifact);
if (alreadyExisted) {
return resolve({
type: 'error',
@@ -191,7 +193,7 @@ export const streamRendererFunctionWithRetry = async ({
overallProgress: OverallProgressHelper;
files: string[];
logLevel: LogLevel;
- onArtifact: (asset: EmittedAsset) => {alreadyExisted: boolean};
+ onArtifact: (asset: EmittedArtifact) => {alreadyExisted: boolean};
}): Promise<unknown> => {
if (payload.type !== LambdaRoutines.renderer) {
throw new Error('Expected renderer type');
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index f42a3873c2..2a4932f1d1 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
-import type {EmittedAsset, LogOptions} from '@remotion/renderer';
+import type {EmittedArtifact, LogOptions} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {existsSync, mkdirSync, rmSync} from 'fs';
import {join} from 'path';
@@ -356,7 +356,7 @@ const innerLaunchHandler = async ({
const files: string[] = [];
- const onArtifact = (artifact: EmittedAsset): {alreadyExisted: boolean} => {
+ const onArtifact = (artifact: EmittedArtifact): {alreadyExisted: boolean} => {
if (
overallProgress
.getReceivedAssets()
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 439c63136d..f6ac91c2b0 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -28,6 +28,7 @@ import {getCurrentRegionInFunction} from './helpers/get-current-region';
import {startLeakDetection} from './helpers/leak-detection';
import {onDownloadsHelper} from './helpers/on-downloads-logger';
import type {RequestContext} from './helpers/request-context';
+import {serializeArtifact} from './helpers/serialize-artifact';
import {timer} from './helpers/timer';
import {getTmpDirStateIfENoSp} from './helpers/write-lambda-error';
import type {OnStream} from './streaming/streaming';
@@ -183,7 +184,7 @@ const renderHandler = async ({
onStream({
type: 'artifact-emitted',
payload: {
- artifact,
+ artifact: serializeArtifact(artifact),
},
})
.then(() => {
diff --git a/packages/lambda/src/functions/streaming/streaming.ts b/packages/lambda/src/functions/streaming/streaming.ts
index 71966376ef..d9215838de 100644
--- a/packages/lambda/src/functions/streaming/streaming.ts
+++ b/packages/lambda/src/functions/streaming/streaming.ts
@@ -1,5 +1,5 @@
-import type {EmittedAsset} from '@remotion/renderer';
import {makeStreamPayloadMessage} from '@remotion/streaming';
+import type {SerializedArtifact} from '../helpers/serialize-artifact';
import type {LambdaErrorInfo} from '../helpers/write-lambda-error';
import type {RenderStillLambdaResponsePayload} from '../still';
@@ -90,7 +90,7 @@ export type StreamingPayload =
| {
type: typeof artifactEmitted;
payload: {
- artifact: EmittedAsset;
+ artifact: SerializedArtifact;
};
};
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 9b3973414e..912d26d792 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -108,12 +108,7 @@ export {X264Preset} from './options/x264-preset';
export {PixelFormat} from './pixel-format';
export {RemotionServer} from './prepare-server';
export {ProResProfile} from './prores-profile';
-export {
- EmittedArtifact as EmittedAsset,
- OnArtifact,
- RenderFramesOptions,
- renderFrames,
-} from './render-frames';
+export {OnArtifact, RenderFramesOptions, renderFrames} from './render-frames';
export {
InternalRenderMediaOptions,
RenderMediaOnProgress,
@@ -127,6 +122,7 @@ export {
SelectCompositionOptions,
selectComposition,
} from './select-composition';
+export {EmittedArtifact} from './serialize-artifact';
export {
StitchFramesToVideoOptions,
stitchFramesToVideo,
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index 5b8dc82023..5bdc261ed5 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -53,6 +53,7 @@ import {puppeteerEvaluateWithCatch} from './puppeteer-evaluate';
import type {BrowserReplacer} from './replace-browser';
import {handleBrowserCrash} from './replace-browser';
import {seekToFrame} from './seek-to-frame';
+import type {EmittedArtifact} from './serialize-artifact';
import {setPropsAndEnv} from './set-props-and-env';
import {takeFrameAndCompose} from './take-frame-and-compose';
import {truthy} from './truthy';
@@ -106,12 +107,6 @@ type InternalRenderFramesOptions = {
onArtifact: OnArtifact | null;
} & ToOptions<typeof optionsMap.renderFrames>;
-export type EmittedArtifact = {
- filename: string;
- content: string | Uint8Array;
- frame: number;
-};
-
type InnerRenderFramesOptions = {
onStart: null | ((data: OnStartData) => void);
onFrameUpdate:
@@ -531,7 +526,15 @@ const innerRenderFrames = async ({
}
}
- onArtifact?.(artifact);
+ const stringOrUintArray =
+ typeof artifact.content === 'string'
+ ? artifact.content
+ : new Uint8Array(Object.values(artifact.content));
+
+ onArtifact?.({
+ ...artifact,
+ content: stringOrUintArray,
+ });
}
const compressedAssets = audioAndVideoAssets.map((asset) => {
diff --git a/packages/renderer/src/serialize-artifact.ts b/packages/renderer/src/serialize-artifact.ts
new file mode 100644
index 0000000000..45b067da23
--- /dev/null
+++ b/packages/renderer/src/serialize-artifact.ts
@@ -0,0 +1,5 @@
+export type EmittedArtifact = {
+ filename: string;
+ content: string | Uint8Array;
+ frame: number;
+};