Skip to content

🎥 Make videos programmatically with React

transparent videos

Jonny Burger7754ba1e484b
diff --git a/packages/docs/components/TransparentVideoDemo.tsx b/packages/docs/components/TransparentVideoDemo.tsx
new file mode 100644
index 0000000000..3b422a7ed2
--- /dev/null
+++ b/packages/docs/components/TransparentVideoDemo.tsx
@@ -0,0 +1,23 @@
+import React, { useCallback, useRef } from "react";
+import "./transparent-video.css";
+
+export const TransparentVideoDemo: React.FC = () => {
+ const ref = useRef<HTMLVideoElement>(null);
+ const onClick = useCallback(() => {
+ ref.current?.classList.toggle("transparent");
+ }, []);
+ return (
+ <div>
+ <video
+ ref={ref}
+ src="/static/img/transparent-video.webm"
+ controls
+ autoPlay
+ loop
+ ></video>
+ <p className="tr-centered" onClick={onClick}>
+ <button>Toggle transparency</button>
+ </p>
+ </div>
+ );
+};
diff --git a/packages/docs/components/transparent-video.css b/packages/docs/components/transparent-video.css
new file mode 100644
index 0000000000..458add5b21
--- /dev/null
+++ b/packages/docs/components/transparent-video.css
@@ -0,0 +1,16 @@
+.transparent {
+ background: linear-gradient(45deg, rgba(0, 0, 0, 0.1) 25%, transparent 25%),
+ linear-gradient(135deg, rgba(0, 0, 0, 0.1) 25%, transparent 25%),
+ linear-gradient(45deg, transparent 75%, rgba(0, 0, 0, 0.1) 75%),
+ linear-gradient(135deg, transparent 75%, rgba(0, 0, 0, 0.1) 75%), white;
+ background-size: 49px 49px;
+ background-position: 0 0, 24.5px 0, 24.5px -24.5px, 0px 24.5px;
+}
+
+video {
+ max-width: 100%;
+}
+
+p.tr-centered {
+ text-align: center;
+}
diff --git a/packages/docs/docs/transparent-videos.mdx b/packages/docs/docs/transparent-videos.mdx
new file mode 100644
index 0000000000..3bf753faed
--- /dev/null
+++ b/packages/docs/docs/transparent-videos.mdx
@@ -0,0 +1,85 @@
+---
+id: transparent-videos
+title: Transparent videos
+---
+
+import { TransparentVideoDemo } from "../components/TransparentVideoDemo";
+
+Chrome and Firefox support WebM videos with alpha channels. That means that on these browsers, you can embed videos with transparency.
+
+If you are lucky enough to visit the website in one of those supported web browsers, check it out:
+
+<TransparentVideoDemo />
+
+## Creating a video with Alpha channel
+
+In order to create a transparent video, you need at least version 1.4 of Remotion. Make sure to not set any background (use the checkerboard button to ensure your video is transparent). In order to render your videos transparent, you need to change 3 settings:
+
+- Render each frame as PNG.
+- Use the VP8 or VP9 codec
+- Use the `yuva420p` pixel format.
+
+If you want to set these options and persist them, add this to your `remotion.config.ts` file (create it if you don't yet have one);
+
+```tsx
+import {Config} from 'remotion';
+
+Config.Rendering.setImageFormat('png')
+Config.Output.setPixelFormat('yuva420p')
+Config.Output.setCodec('vp8')
+```
+
+You can also set the settings on the command line:
+
+```console
+--image-format=png --pixel-format=yuva420p --codec=vp8
+```
+
+## Creating a fallback version
+
+Given the poor browser support, consider making two versions of a video, one with alpha channel, and an opaque video as a fallback. You can achieve this in Remotion using standard React props:
+
+```tsx {2}
+const MyVideo: React.FC<{
+ transparent: boolean;
+}> = ({transparent}) => {
+ return (
+ <div style={{backgroundColor: transparent ? undefined : 'white', flex: 1}}>
+ {/* Omit opaque background based on `transparent` prop */}
+ </div>
+ )
+}
+```
+
+It's a good practice to set a default when defining the composition:
+
+```tsx {8-10}
+<Composition
+ id="my-video"
+ component={MyVideo}
+ width={1920}
+ height={1080}
+ fps={30}
+ durationInFrames={150}
+ defaultProps={{
+ transparent: true,
+ }}
+/>
+```
+
+You can then have separate render commands in your package.json:
+
+```json
+"scripts": {
+ "render": "remotion render src/index.tsx my-video video.mp4",
+ "render-transparent": "remotion render src/index.tsx --image-format=png --pixel-format=yuva420p --codec=vp8 my-video video-transparent.webm"
+}
+```
+
+Now you can render two variants of the same video and serve a different one
+based on browser support. You can use APIs like [`<source>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source) or the [`canplay`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/canplay_event) event to determine programmatically if a browser is able to play a video.
+
+## See also
+
+- [CLI options](/docs/cli)
+- [Configuration option](/docs/config)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index fb029bfeb0..23b717bdf0 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -25,6 +25,7 @@ module.exports = {
"javascript",
"using-randomness",
"encoding",
+ "transparent-videos",
],
},
{
diff --git a/packages/docs/static/img/transparent-video.webm b/packages/docs/static/img/transparent-video.webm
new file mode 100644
index 0000000000..7538ad295c
Binary files /dev/null and b/packages/docs/static/img/transparent-video.webm differ