π₯ Make videos programmatically with React
rewrite dynamic imports
Jonny Burger51127fe3bd9e
diff --git a/packages/bundler/src/webpack-config.ts b/packages/bundler/src/webpack-config.tsindex 251711c268..02ca7afcfa 100644--- a/packages/bundler/src/webpack-config.ts+++ b/packages/bundler/src/webpack-config.ts@@ -124,7 +124,7 @@ export const webpackConfig = ({path: outDir,devtoolModuleFilenameTemplate: '[resource-path]',assetModuleFilename:- environment === 'development' ? '[path][name].[ext]' : '[hash].[ext]',+ environment === 'development' ? '[path][name][ext]' : '[hash][ext]',},devServer: {contentBase: path.resolve(__dirname, '..', 'web'),@@ -172,9 +172,6 @@ export const webpackConfig = ({{test: /\.(woff(2)?|otf|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,type: 'asset/resource',- generator: {- filename: 'static/[hash][ext][query]',- },},{test: /\.jsx?$/,diff --git a/packages/docs/docs/dynamic-import.md b/packages/docs/docs/dynamic-import.mdindex 8987015561..51d2b18720 100644--- a/packages/docs/docs/dynamic-import.md+++ b/packages/docs/docs/dynamic-import.md@@ -7,23 +7,7 @@ A common need in Remotion is to import assets from dynamic paths. This means thaThis can become an unexpected hurdle in Webpack. On this page we collect some tips how to handle dynamic assets.-## Write dynamic expressions correctly--Consider you have a PNG sequence of images with the following file structure:--```bash-my-video/-ββ src/-β ββ assets/-β β ββ image0.png-β β ββ image1.png-β β ββ image2.png-β β ββ ...-β β ββ image99.png-β ββ index.tsx-```--Note that the following pattern doesn't work:+Take this scenario for an example:```tsx twoslashimport { Img, useCurrentFrame } from "remotion";@@ -35,11 +19,26 @@ export const DynamicImports: React.FC = () => {};```+may result in:+```bashError: Cannot find module './image0.png'```-However the following example **does** work:+even if the file exists. This is because Webpack needs to figure out using static code analysis which assets it should bundle and cannot do so.++## Recommended: Use `staticFile()` instead++We recommend that you put the asset inside the `public/` folder and use `staticFile()` to reference it. This new way does not suffer from the underlying problem.++- [Importing assets](/docs/assets)+- [`staticFile()`](/docs/staticfile)++## Write dynamic expressions correctly++While the example at the top did not work, Webpck is smart enough to do so if you place your expression inside the `require()` or `import()` statement. In this case, Webpack will automatically bundle all `.png` files in the `assets/image` folder even if the asset is never used.++The following **does** work:```tsx twoslashimport { Img, useCurrentFrame } from "remotion";@@ -50,8 +49,6 @@ export const DynamicImports: React.FC = () => {};```-Webpack needs to figure out which assets it should bundle and cannot do it in the first example. However, it is smart enough to do so if you place your expression inside the `require()` or `import()` statement. In this case, Webpack will automatically bundle all `.png` files in the `assets/image` folder even if the asset is never used. Therefore you have to be careful to not bundle too many assets.-Please read [the Webpack documentation page](https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import) about this behavior if you would like to learn more.## Import assets at runtime@@ -80,51 +77,6 @@ const DynamicAsset: React.FC = () => {};```-## Adding assets after bundling--If you server-side render your video using the Node.JS APIs, you might find yourself in a situation where you want to add new assets after you have already bundled the Remotion application. In this scenario, it is simply impossible for Webpack to interpret a `require()` statement correctly.--You can use the following workaround that relies on the fact that Remotion creates a static web server while rendering:--1. [`bundle()`](/docs/bundle) the video - the return value is a promise resolving to the folder where the bundle got saved.-2. Copy a new asset into this folder.-3. Rely on the fact that the asset is available via the static HTTP server. Since you only want this during rendering, use the `process.env.NODE_ENV === 'production'` statement to differentiate between development and rendering.--```tsx twoslash-import { getInputProps, Img } from "remotion";--export const DynamicAsset: React.FC = () => {- const inputProps = getInputProps(); // {"imageSrc": "assets/img0.png"}- return (- <Img- src={- process.env.NODE_ENV === "production"- ? `/${inputProps.imageSrc}`- : require("./assets/default-asset.png")- }- />- );-};-```--## Set up an HTTP server--As a last resort, you can spin up your own static HTTP server and import the assets via HTTP:--```sh-# From any directory-npx serve --cors ./src-```--```tsx twoslash-import { getInputProps, Img } from "remotion";--const HttpAsset: React.FC = () => {- const inputProps = getInputProps(); // {"imageSrc": "assets/img0.png"}- return <Img src={`http://localhost:5000/${inputProps.imageSrc}`} />;-};-```-## Struggling?If you still have troubles importing your assets, hit us up on [Discord](https://discord.gg/6VzzNDwUwV) or file an issue. We'd love to hear your input about how we can improve and will help you out.diff --git a/packages/docs/docs/importing-assets.md b/packages/docs/docs/importing-assets.mdindex 9d808c2da8..ddf0aec608 100644--- a/packages/docs/docs/importing-assets.md+++ b/packages/docs/docs/importing-assets.md@@ -3,65 +3,72 @@ id: assetstitle: Importing assets----Remotion allows you to include several types of files in your project:+To import assets in Remotion, create a `public/` folder in your project and use `staticFile()` to import it.++```txt+my-video/+ββ node_modules/+ββ public/+β ββ logo.png+ββ src/+β ββ MyComp.tsx+β ββ Video.tsx+β ββ index.tsx+ββ package.json+```-- Images (`.png`, `.svg`, `.jpg`, `.jpeg`, `.webp`, `.gif`, `.bmp`)-- Videos (`.webm`, `.mov`, `.mp4`)-- Audio (`.mp3`, `.wav`, `.aac`)-- [Webfonts - read the separate page for fonts](/docs/fonts)-- Arbitrary files using the `staticFile()` API+```tsx twoslash title="src/MyComp.tsx"+import { Img, staticFile } from "remotion";++export const MyComp: React.FC = () => {+ return <Img src={staticFile("logo.png")} />;+};+```## Using images-Require images using an `import` statement and pass them to the [`<Img/>`](/docs/img) tag.+Use the [`<Img/>`](/docs/img) tag from Remotion.-```tsx twoslash-import { Img } from "remotion";-import logo from "./logo.png";+```tsx twoslash title="MyComp.tsx"+import { Img, staticFile } from "remotion";export const MyComp: React.FC = () => {- return <Img src={logo} />;+ return <Img src={staticFile("logo.png")} />;};```## Using image sequences-If you have a series of images, for example exported from another program like After Effects or Rotato, you can use a dynamic `require` statement to import the images as they are needed.+If you have a series of images, for example exported from another program like After Effects or Rotato, you can interpolate the path to create a dynamic import.-```tsx twoslash-import { useCurrentFrame } from "remotion";+```txt+my-video/+ββ public/+β ββ frame1.png+β ββ frame2.png+β ββ frame3.png+ββ package.json+```-/*- Assuming your file structure is:- assets/- frame1.png- frame2.png- frame3.png- ...-*/+```tsx twoslash+import { Img, useCurrentFrame, staticFile } from "remotion";const MyComp: React.FC = () => {const frame = useCurrentFrame();- const src = require("./assets/frame" + frame + ".png");- return <img src={src} />;+ return <Img src={staticFile(`/${frame}.png`)} />;};```-:::tip-Avoid writing a require statement that requires a file that doesn't exist. If your project throws an error because your composition is longer than than your image sequence, clamp the file name using `Math.min()` or Remotion's `interpolate()`.-:::-## Using videos-Import your files using an import statement. Use the [`<Video />`](/docs/video) component to keep the timeline and your video in sync.+Use the [`<Video />`](/docs/video) component to keep the timeline and your video in sync.```tsx twoslash-import { Video } from "remotion";-import vid from "./vid.webm";+import { staticFile, Video } from "remotion";export const MyComp: React.FC = () => {- return <Video src={vid} />;+ return <Video src={staticFile("vid.webm")} />;};```@@ -69,23 +76,71 @@ Be aware that if you are rendering using Chromium (as opposed to Chrome), the co## Using Audio-Import your audio using an `import` statement and pass it to the [`<Audio/ >`](/docs/audio) component.+Use the [`<Audio/ >`](/docs/audio) component.```tsx twoslash-import { Audio } from "remotion";-import tune from "./tune.mp3";+import { Audio, staticFile } from "remotion";export const MyComp: React.FC = () => {- return <Audio src={tune} />;+ return <Audio src={staticFile("tune.mp3")} />;};```See the [audio guide](/docs/using-audio) for guidance on including audio.-## Using fonts+## Using CSS++Put the .css file alongside your JavaScript source files and use an `import` statement.++```txt+my-video/+ββ node_modules/+ββ src/+β ββ style.css+β ββ MyComp.tsx+β ββ Video.tsx+β ββ index.tsx+ββ package.json+```++```tsx twoslash title="MyComp.tsx"+import "./style.css";+```++:::note+Want to use SASS, Tailwind or similar? [See examples on how to override the Webpack configuration](/docs/webpack).+:::++## Using Fonts++[Read the separate page for fonts.](/docs/fonts)++## `import` statements++As an alternative way to import files, Remotion allows you to `import` or `require()` several types of files in your project:++- Images (`.png`, `.svg`, `.jpg`, `.jpeg`, `.webp`, `.gif`, `.bmp`)+- Videos (`.webm`, `.mov`, `.mp4`)+- Audio (`.mp3`, `.wav`, `.aac`, `m4a`)+- Fonts (`.woff`, `.woff2`, `otf`, `ttf`, `eot`)++For example:++```tsx twoslash title="MyComp.tsx"+import { Img } from "remotion";+import logo from "./logo.png";++export const MyComp: React.FC = () => {+ return <Img src={logo} />;+};+```++### Caveats-See the [dedicated page about fonts](/docs/fonts).+While this was previously the main way of importing files, we recommend against it because of the following reasons:-## Importing arbitrary files+- Only the above listed file extensions are supported.+- The maximum file size is 2GB.+- Dynamic imports such as `require('img' + frame + '.png')` are [funky](/docs/webpack-dynamic-imports).-Create a `public/` folder in your project and use the [`staticFile()`](/docs/staticfile) API to get a URL reference of the asset that you can load into your project.+Prefer importing using `staticFile()` is possible.