Skip to content

🎥 Make videos programmatically with React

handle assets correctly

Jonny Burger127a28ace988
diff --git a/packages/renderer/src/assets/map-localhost-to-file.ts b/packages/renderer/src/assets/map-localhost-to-file.ts
new file mode 100644
index 0000000000..1cbbe38839
--- /dev/null
+++ b/packages/renderer/src/assets/map-localhost-to-file.ts
@@ -0,0 +1,17 @@
+import path from 'path';
+import {TAsset} from 'remotion';
+
+export const mapLocalhostAssetToFile = ({
+ localhostAsset,
+ webpackBundle,
+}: {
+ localhostAsset: TAsset;
+ webpackBundle: string;
+}): TAsset => {
+ const {pathname} = new URL(localhostAsset.src);
+ const newSrc = path.join(webpackBundle, pathname);
+ return {
+ ...localhostAsset,
+ src: 'file://' + newSrc,
+ };
+};
diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.ts
index 1cd940e633..87abff6207 100644
--- a/packages/renderer/src/render.ts
+++ b/packages/renderer/src/render.ts
@@ -1,7 +1,8 @@
import path from 'path';
import {Browser, FrameRange, Internals, VideoConfig} from 'remotion';
import {openBrowser, provideScreenshot} from '.';
-import {Assets, calculateAssetsPosition} from './assets';
+import {Assets, calculateAssetsPosition} from './assets/assets';
+import {mapLocalhostAssetToFile} from './assets/map-localhost-to-file';
import {getActualConcurrency} from './get-concurrency';
import {getFrameCount} from './get-frame-range';
import {getFrameToRender} from './get-frame-to-render';
@@ -96,7 +97,7 @@ export const renderFrames = async ({
const freePage = await pool.acquire();
const paddedIndex = String(frame).padStart(filePadLength, '0');
- const screenshot = await provideScreenshot({
+ await provideScreenshot({
page: freePage,
imageFormat,
quality,
@@ -111,9 +112,15 @@ export const renderFrames = async ({
pool.release(freePage);
framesRendered++;
onFrameUpdate(framesRendered);
- return await freePage.evaluate(() => {
+ const assets = await freePage.evaluate(() => {
return window.remotion_collectAssets();
});
+ return assets.map((a) => {
+ return mapLocalhostAssetToFile({
+ localhostAsset: a,
+ webpackBundle,
+ });
+ });
})
);
await Promise.all([browserInstance.close(), close()]);
diff --git a/packages/renderer/src/test/map-localhost-to-file.test.ts b/packages/renderer/src/test/map-localhost-to-file.test.ts
new file mode 100644
index 0000000000..f3ba1a7766
--- /dev/null
+++ b/packages/renderer/src/test/map-localhost-to-file.test.ts
@@ -0,0 +1,22 @@
+import {mapLocalhostAssetToFile} from '../assets/map-localhost-to-file';
+
+test('Should correctly map a localhost URL to an asset', () => {
+ expect(
+ mapLocalhostAssetToFile({
+ localhostAsset: {
+ type: 'video',
+ src: 'http://localhost:3000/5f25ba62771d1f8195f858ec5ff8e8d6.mp4',
+ id: '0.8331499681195862',
+ sequenceFrame: 2045,
+ },
+ webpackBundle:
+ '/var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7',
+ })
+ ).toEqual({
+ type: 'video',
+ src:
+ 'file:///var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7/5f25ba62771d1f8195f858ec5ff8e8d6.mp4',
+ id: '0.8331499681195862',
+ sequenceFrame: 2045,
+ });
+});