Remotion: Video with React!
Hi there! It has been a while since I've made my last post, and now I will share about an interesting library I found called Remotion - a library promising to "Create real MP4 videos using React".
Remotion is very simple to use. To begin learning, I'd recommend setting up and starting off with React (make sure to learn React JS, not React Native: there are two React types!).
Setup
Assuming you have NodeJS, you can open a terminal (I use PowerShell: it's better than Command Prompt) and type npm init video
. Select a template (I use the JavaScript Hello World template) and project name, and wait until the command finishes (NOTE: It may ask to install the "create-video" package - just press enter to confirm the installation and proceed).
Once done, open a code editor on the created folder (I use VS Code - a cross-platform lightweight code editor), making sure the right plugin(s) are installed (if needed)! On VS Code, you may need to trust the folder before starting.
To test the code, type in npm start
in the terminal - it will work similarly to a typical React development setup. Then, it'll open a browser to load a video previewer containing all compositions you have made (each composition is a single video to be rendered by Remotion) and show a nice hello world video. Keep this command running in the background - as any typical React development flow would - and then start to the next section, remembering to start the command each time you want to preview your video. Now we'll make a circle show as shown below:
Code
First, create two files called Circle.js and CircleShow.js - these are for one circle and a set of circles respectively. First we'll edit the Circle.js and continue on with CircleShow.js later on.
Coding an Individual Circle
To begin, import React for using JSX (the markup system of React):
Then import the required things from Remotion (hooks and components):
Then define and export our main circle component using functional components and arrow functions (not forgetting to accept input props):
I am using a feature called destructuring assignment. It is a feature allowing us to write code like this to extract certain object values without using any variables - a very handy tool to kow!
The input props are:
- delay - when the circle begins animating
- x - the x coordinate of the circle
- y - the y coordinate of the circle
- maxOpacity - the peak opacity of the circle (all circles have this set to 1)
- r - the radius of the circle
- fill - the fill color of the circle
Following that, extract video configurations from the useVideoConfig hook provided by Remotion, while also extracting the current frame using the useCurrentFrame hook:
Afterwards, compute the opacity:
Finally, make the actual circle:
There are quite some code here, but basically it means to center an SVG circle with x-coordinate x and y-coordinate y. The opacity is set to the opacity variable above, the radius is set to r, and the fill is set to the fill prop.
That's it for the individual circle!
Coding a circle show
To begin, import the necessary things as before:
Then, create the CircleShow function:
After that, extract the video configurations to be used:
And define functions for a random hexadecimal character and a random 6-letter color code (respectively):
As you may notice, I am using the Remotion random function. This utility function takes in a seed, making sure that the PRNG (PseudoRandom Number Generator) returns the same result wherever, whenever, and however we view the video - thus making it consistent everywhere.
Then we initialize the data for each circle - using useMemo from React to optimize the code:
Then we return the collection of circles:
Final Steps
Import the CircleShow.js file, replacing the imports in lines 2 and 3. Then, replace HelloWorld in line 13, and tweak the FPS, frames, and resolution to your needs (I settled with 25 FPS with 1500 frames to generate a 1 minute video). Remove the Composition component underneath, preview the video, and it's ready!
Rendering
Except that, the video isn't an actual video just yet. We need to render the video to get the real MP4 (or other format) files to share to other people. To do this, run the command npm run build
, and wait until the build finishes.
You can try passing the -- --gl=angle
flag (the four hypens are not a typo!) to make the render faster by using your GPU, but it's not really that helpful in a simple use case like this. 3D rendering and things like that may benefit more from this, but it's a useful trick to know - in case it helps.
Then check the out directory, and the video is finally ready to view! You can check it out, share it, or do anything you like with it.
Summary
Remotion is a great library, and I personally would like to try and make some good videos using it. One side note still needs to be said about it's (ratherly unique) licence: if you are using Remotion for an organization with 4 or more people, you'll need to get a licence (see this). Overall, it's a very nice (and creative) way of using a web development tool to create video.