π₯ Make videos programmatically with React
new docs section: Embedding videos in Remotion
Jonny Burger9ca67307e4bb
diff --git a/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdx b/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdxindex f1500de56c..fe95f59c5e 100644--- a/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdx+++ b/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdx@@ -1,6 +1,7 @@---image: /generated/articles-docs-miscellaneous-snippets-accelerated-video.pngtitle: 'Change the speed of a video over time'+sidebar_label: Changing speed over timecrumb: 'Snippets'---@@ -11,10 +12,13 @@ To speed up a video over time - for example to start with regular speed and thenIt is not as easy as interpolating the [`playbackRate`](/docs/video#playbackrate):```tsx twoslash title="β Does not work"-import {interpolate, Video} from 'remotion';+import {interpolate, OffthreadVideo} from 'remotion';let frame = 0;// ---cut----<Video playbackRate={interpolate(frame, [0, 100], [1, 5])} />;+<OffthreadVideo+ playbackRate={interpolate(frame, [0, 100], [1, 5])}+ src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4#disable"+/>;```This is because Remotion will evaluate each frame independently from the other frames. If `frame` is 100, the `playbackRate` evaluates as 5 and Remotion will render the 500th frame of the video, which is undesired because it does not take into account that the speed has been building up to 5 until now.diff --git a/packages/docs/docs/miscellaneous/snippets/align-duration.mdx b/packages/docs/docs/miscellaneous/snippets/align-duration.mdxindex 849e4085b9..6f8d22176f 100644--- a/packages/docs/docs/miscellaneous/snippets/align-duration.mdx+++ b/packages/docs/docs/miscellaneous/snippets/align-duration.mdx@@ -1,32 +1,32 @@---image: /generated/articles-docs-miscellaneous-snippets-align-duration.pngtitle: How do I make the composition the same duration as my video?-sidebar_label: Same composition duration as video-crumb: "FAQ"+sidebar_label: Align timeline duration to video+crumb: 'FAQ'---If you have a component rendering a video:```tsx twoslash title="MyComp.tsx"-import React from "react";-import { OffthreadVideo, staticFile } from "remotion";+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';export const MyComp: React.FC = () => {- return <OffthreadVideo src={staticFile("video.mp4")} />;+ return <OffthreadVideo src={staticFile('video.mp4')} />;};```and you want to make the composition the same duration as the video, first make the video source a React prop:```tsx twoslash title="MyComp.tsx"-import React from "react";-import { OffthreadVideo, staticFile } from "remotion";+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';type MyCompProps = {src: string;};-export const MyComp: React.FC<MyCompProps> = ({ src }) => {+export const MyComp: React.FC<MyCompProps> = ({src}) => {return <OffthreadVideo src={src} />;};```@@ -41,12 +41,12 @@ type MyCompProps = {// ---cut----import { CalculateMetadataFunction } from "remotion";-import { getVideoMetadata } from "@remotion/media-utils";+import {CalculateMetadataFunction} from 'remotion';+import {getVideoMetadata} from '@remotion/media-utils';export const calculateMetadata: CalculateMetadataFunction<MyCompProps-> = async ({ props }) => {+> = async ({props}) => {const data = await getVideoMetadata(props.src);const fps = 30;@@ -61,9 +61,9 @@ Finally, pass the `calculateMetadata` function to the `Composition` component an```tsx twoslash title="Root.tsx"// @filename: MyComp.tsx-import React from "react";-import { CalculateMetadataFunction } from "remotion";-import { getVideoMetadata } from "@remotion/media-utils";+import React from 'react';+import {CalculateMetadataFunction} from 'remotion';+import {getVideoMetadata} from '@remotion/media-utils';export const MyComp: React.FC<MyCompProps> = () => {return null;@@ -74,7 +74,7 @@ type MyCompProps = {export const calculateMetadata: CalculateMetadataFunction<MyCompProps-> = async ({ props }) => {+> = async ({props}) => {const data = await getVideoMetadata(props.src);const fps = 30;@@ -87,9 +87,9 @@ export const calculateMetadata: CalculateMetadataFunction<// @filename: Root.tsx// ---cut----import React from "react";-import { Composition } from "remotion";-import { MyComp, calculateMetadata } from "./MyComp";+import React from 'react';+import {Composition} from 'remotion';+import {MyComp, calculateMetadata} from './MyComp';export const Root: React.FC = () => {return (@@ -101,7 +101,7 @@ export const Root: React.FC = () => {width={1920}height={1080}defaultProps={{- src: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',}}calculateMetadata={calculateMetadata}/>diff --git a/packages/docs/docs/miscellaneous/snippets/hls.mdx b/packages/docs/docs/miscellaneous/snippets/hls.mdxindex 4ef1008302..28ac3eb7f2 100644--- a/packages/docs/docs/miscellaneous/snippets/hls.mdx+++ b/packages/docs/docs/miscellaneous/snippets/hls.mdx@@ -1,8 +1,9 @@---image: /generated/articles-docs-miscellaneous-snippets-hls.png-title: "HLS support (HTTP Live Streaming)"+title: 'HLS support (HTTP Live Streaming)'+sidebar_label: 'HTTP Live Streaming'id: hls-crumb: "Video"+crumb: 'Video'---## No native support@@ -21,21 +22,29 @@ You can play HLS videos during preview in the [`<Player>`](/docs/player) and inNote the following caveats:-<Step>1</Step> This code only shows how to connect the video tag to the HLS stream, it has not been tested on a real project. <br/>-<Step>2</Step> Audio will not work when rendering a video to an MP4 using this code. Use an alternative source during rendering. See <a href="/docs/miscellaneous/snippets/offthread-video-while-rendering"><OffthreadVideo> while rendering-</a> and <a href="/docs/get-remotion-environment"><code>getRemotionEnvironment()</code></a> for how to use different components based on whether you are rendering or previewing.<br/><br/>+<Step>1</Step> This code only shows how to connect the video tag to the HLS+stream, it has not been tested on a real project. <br />+<Step>2</Step> Audio will not work when rendering a video to an MP4 using this+code. Use an alternative source during rendering. See <a href="/docs/miscellaneous/snippets/offthread-video-while-rendering">+ <OffthreadVideo> while rendering+</a> and <a href="/docs/get-remotion-environment">+ <code>getRemotionEnvironment()</code>+</a> for how to use different components based on whether you are rendering or+previewing.+<br />+<br />```tsx twoslash title="HlsDemo.tsx"-import Hls from "hls.js";-import React, { useEffect, useRef } from "react";-import { AbsoluteFill, RemotionVideoProps, Video } from "remotion";+import Hls from 'hls.js';+import React, {useEffect, useRef} from 'react';+import {AbsoluteFill, RemotionVideoProps, Video} from 'remotion';-const HlsVideo: React.FC<RemotionVideoProps> = ({ src }) => {+const HlsVideo: React.FC<RemotionVideoProps> = ({src}) => {const videoRef = useRef<HTMLVideoElement>(null);useEffect(() => {if (!src) {- throw new Error("src is required");+ throw new Error('src is required');}const startFrom = 0;diff --git a/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx b/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdxindex 611384038b..bcfeba87c4 100644--- a/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx+++ b/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx@@ -1,9 +1,13 @@---image: /generated/articles-docs-miscellaneous-snippets-jumpcuts.pngtitle: 'Jump Cutting'+sidebar_label: Jump Cutscrumb: 'Snippets'---+You might wanna use a "jump cut" to skip parts of a video.+Use the following snippet to skip certain sections of a video, without re-mounting it.+```tsx twoslashimport React, {useMemo} from 'react';import {diff --git a/packages/docs/docs/video-manipulation.mdx b/packages/docs/docs/video-manipulation.mdxindex 3413fd082c..9baa076a6f 100644--- a/packages/docs/docs/video-manipulation.mdx+++ b/packages/docs/docs/video-manipulation.mdx@@ -2,10 +2,11 @@image: /generated/articles-docs-video-manipulation.pngid: video-manipulationtitle: Video manipulation-crumb: "How To"+sidebar_label: Manipulating pixels+crumb: 'How To'----import { VideoCanvasExamples } from "../components/GreenscreenExamples/index";+import {VideoCanvasExamples} from '../components/GreenscreenExamples/index';You can draw frames of an [`<OffthreadVideo>`](/docs/offthreadvideo) or a [`<Video>`](/docs/video) onto a `<canvas>` element using the [`drawImage()`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage) API.@@ -19,39 +20,42 @@ Browser support: Firefox 130 (August 2024), Chrome 83, Safari 15.4.In this example, an [`<OffthreadVideo>`](/docs/offthreadvideo) is rendered and made invisible.Every frame that is emitted is drawn to a Canvas and a grayscale [`filter`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter) is applied.-<VideoCanvasExamples type="base"/>-<br/>+<VideoCanvasExamples type="base" />+<br />```tsx twoslash-import React, { useCallback, useEffect, useRef } from "react";-import { AbsoluteFill, useVideoConfig, OffthreadVideo } from "remotion";+import React, {useCallback, useEffect, useRef} from 'react';+import {AbsoluteFill, useVideoConfig, OffthreadVideo} from 'remotion';// ---cut---export const VideoOnCanvas: React.FC = () => {const video = useRef<HTMLVideoElement>(null);const canvas = useRef<HTMLCanvasElement>(null);- const { width, height } = useVideoConfig();+ const {width, height} = useVideoConfig();// Process a frame- const onVideoFrame = useCallback((frame: CanvasImageSource) => {- if (!canvas.current ) {- return;- }- const context = canvas.current.getContext("2d");+ const onVideoFrame = useCallback(+ (frame: CanvasImageSource) => {+ if (!canvas.current) {+ return;+ }+ const context = canvas.current.getContext('2d');- if (!context) {- return;- }+ if (!context) {+ return;+ }- context.filter = "grayscale(100%)";- context.drawImage(frame, 0, 0, width, height);- }, [height, width]);+ context.filter = 'grayscale(100%)';+ context.drawImage(frame, 0, 0, width, height);+ },+ [height, width],+ );return (<AbsoluteFill><AbsoluteFill><OffthreadVideo// Hide the original video tag- style={{ opacity: 0 }}+ style={{opacity: 0}}onVideoFrame={onVideoFrame}src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"/>@@ -68,8 +72,8 @@ export const VideoOnCanvas: React.FC = () => {In this example, we loop over each pixel in the image buffer and if it's green, we transparentize it. Drag the slider below to turn the video transparent.-<VideoCanvasExamples type="greenscreen"/>-<br/>+<VideoCanvasExamples type="greenscreen" />+<br />```tsx twoslashdeclare global {@@ -88,28 +92,28 @@ declare global {type VideoFrameRequestCallbackId = number;interface HTMLVideoElement extends HTMLMediaElement {requestVideoFrameCallback(- callback: (now: DOMHighResTimeStamp, metadata: VideoFrameMetadata) => any+ callback: (now: DOMHighResTimeStamp, metadata: VideoFrameMetadata) => any,): VideoFrameRequestCallbackId;cancelVideoFrameCallback(handle: VideoFrameRequestCallbackId): void;}}-import React, { useCallback, useEffect, useRef } from "react";-import { AbsoluteFill, useVideoConfig, OffthreadVideo } from "remotion";+import React, {useCallback, useEffect, useRef} from 'react';+import {AbsoluteFill, useVideoConfig, OffthreadVideo} from 'remotion';// ---cut---export const Greenscreen: React.FC<{opacity: number;-}> = ({ opacity }) => {+}> = ({opacity}) => {const canvas = useRef<HTMLCanvasElement>(null);- const { width, height } = useVideoConfig();+ const {width, height} = useVideoConfig();// Process a frameconst onVideoFrame = useCallback((frame: CanvasImageSource) => {- if (!canvas.current ) {+ if (!canvas.current) {return;}- const context = canvas.current.getContext("2d");+ const context = canvas.current.getContext('2d');if (!context) {return;@@ -117,7 +121,7 @@ export const Greenscreen: React.FC<{context.drawImage(frame, 0, 0, width, height);const imageFrame = context.getImageData(0, 0, width, height);- const { length } = imageFrame.data;+ const {length} = imageFrame.data;// If the pixel is very green, reduce the alpha channelfor (let i = 0; i < length; i += 4) {@@ -130,15 +134,14 @@ export const Greenscreen: React.FC<{}context.putImageData(imageFrame, 0, 0);},- [height, width]+ [height, width],);-return (<AbsoluteFill><AbsoluteFill><OffthreadVideo- style={{ opacity: 0 }}+ style={{opacity: 0}}onVideoFrame={onVideoFrame}src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it-short.mp4"/>diff --git a/packages/docs/docs/video-vs-offthreadvideo.mdx b/packages/docs/docs/video-vs-offthreadvideo.mdxindex 831b491c67..76d934f2f0 100644--- a/packages/docs/docs/video-vs-offthreadvideo.mdx+++ b/packages/docs/docs/video-vs-offthreadvideo.mdx@@ -1,8 +1,8 @@---image: /generated/articles-docs-video-vs-offthreadvideo.pngid: video-vs-offthreadvideo-title: "<Video> vs. <OffthreadVideo>"-crumb: "Comparison"+title: '<OffthreadVideo> vs. <Video>'+crumb: 'Comparison'---We offer two components for including other videos in your video: [`<Video />`](/docs/video) and [`<OffthreadVideo />`](/docs/offthreadvideo).@@ -40,7 +40,7 @@ Is based on the native HTML5 `<video>` element and therefore behaves similar to**Pros**β Can render a video without having it to be downloaded fully (if you don't pass the [`muted`](/docs/video/#muted) prop, the video will still be downloaded fully to extract its audio).-β You can attach a ref to the `<video>` element.+β You can attach a ref to the `<video>` element.**Cons**@@ -48,5 +48,4 @@ Is based on the native HTML5 `<video>` element and therefore behaves similar toβ Chrome may throttle video tags if the page is heavy.β If too many video tags get rendered simultaneously, a timeout may occur.β If the input video framerate does not match with the output framerate, some duplicate frames may occur in the output.-β A Google Chrome build with proprietary codecs is required.-+β A Google Chrome build with proprietary codecs is required.diff --git a/packages/docs/docs/videos/index.mdx b/packages/docs/docs/videos/index.mdxnew file mode 100644index 0000000000..fce4148716--- /dev/null+++ b/packages/docs/docs/videos/index.mdx@@ -0,0 +1,156 @@+---+image: /generated/articles-docs-videos-index.png+title: Embedding videos into Remotion+sidebar_label: Adding a video+crumb: 'How To'+---++You can embed existing videos into Remotion by using the [`<OffthreadVideo>`](/docs/offthreadvideo) component.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return (+ <OffthreadVideo src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" />+ );+};+```++## Using a local file++Put a file into the [`public` folder](/docs/terminology/public-dir) and reference it using [`staticFile()`](/docs/static-file).++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return <OffthreadVideo src={staticFile('video.mp4')} />;+};+```++## Trimming++By using the [`startAt`](/docs/offthreadvideo#startat) prop, you can remove the first few seconds of the video.+In the example below, the first two seconds of the video are skipped (assuming a composition FPS of 30).++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return <OffthreadVideo src={staticFile('video.mp4')} startFrom={60} />;+};+```++Similarly, you can use [`endAt`](/docs/offthreadvideo#endat) to trim the end of the video.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return (+ <OffthreadVideo src={staticFile('video.mp4')} startFrom={60} endAt={120} />+ );+};+```++## Delaying++Use the [`<Sequence>`](/docs/sequence) component to delay the appearance of a video.+In the example below, the video will start playing at frame 60.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile, Sequence} from 'remotion';++export const MyComp: React.FC = () => {+ return (+ <Sequence from={60}>+ <OffthreadVideo src={staticFile('video.mp4')} />+ </Sequence>+ );+};+```++## Size and Position++You can size and position the video using CSS.+You'll find the properties `width`, `height`, `position`, `left`, `top`, `right`, `bottom`, `margin`, `aspectRatio` and `objectFit` useful.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return (+ <OffthreadVideo+ src={staticFile('video.mp4')}+ style={{+ width: 640,+ height: 360,+ position: 'absolute',+ top: 100,+ left: 100,+ }}+ />+ );+};+```++## Volume++You can set the volume of the video using the [`volume`](/docs/offthreadvideo#volume) prop.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return <OffthreadVideo src={staticFile('video.mp4')} volume={0.5} />;+};+```++You can also mute a video using the [`muted`](/docs/offthreadvideo#muted) prop.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return <OffthreadVideo src={staticFile('video.mp4')} muted />;+};+```++See [Using Audio](/docs/using-audio#controlling-volume) for more ways you can control volume.++## Speed++You can use the [`playbackRate`](/docs/offthreadvideo#playbackrate) prop to change the speed of the video.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return <OffthreadVideo src={staticFile('video.mp4')} playbackRate={2} />;+};+```++This only works if the speed is constant. See also: [Changing the speed of a video over time](/docs/miscellaneous/snippets/accelerated-video).++## Looping++See: [Looping an `<OffthreadVideo>`](/docs/offthreadvideo#looping-a-video)++## GIFs++See: [Using GIFs](/docs/gif)++## See also++- [`<OffthreadVideo>`](/docs/offthreadvideo)+- [Using Audio](/docs/using-audio)diff --git a/packages/docs/docs/videos/transparency.mdx b/packages/docs/docs/videos/transparency.mdxnew file mode 100644index 0000000000..ff98283298--- /dev/null+++ b/packages/docs/docs/videos/transparency.mdx@@ -0,0 +1,49 @@+---+image: /generated/articles-docs-videos-transparency.png+title: Embedding transparent videos+sidebar_label: Adding a transparent video+crumb: 'How To'+---++You can embed transparent videos in Remotion.++## With an alpha channel++To embed a video which has an alpha channel, use the [`<OffthreadVideo>`](/docs/offthreadvideo) component with the [`transparent`](/docs/offthreadvideo#transparent) prop.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return <OffthreadVideo src={staticFile('transparent.webm')} transparent />;+};+```++## Without an alpha channel++To embed a video which does not have an alpha channel but just a black background, add `mixBlendMode: "screen"` to the [`style`](/docs/offthreadvideo#style) prop of the [`<OffthreadVideo>`](/docs/offthreadvideo) component.++```tsx twoslash+import React from 'react';+import {OffthreadVideo, staticFile} from 'remotion';++export const MyComp: React.FC = () => {+ return (+ <OffthreadVideo+ src={staticFile('nottransparent.mp4')}+ style={{+ mixBlendMode: 'screen',+ }}+ />+ );+};+```++## Greenscreen effect++To remove a background based on the color of the pixels, see the [Greenscreen example](/docs/video-manipulation#greenscreen-example).++## Rendering transparent videos++To export a video with transparency, see [Transparent videos](/docs/transparent-videos/)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 290978ce0c..2a160ddc49 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -662,10 +662,23 @@ module.exports = {'using-randomness','audio-visualization','noise-visualization',- 'video-manipulation','animation-math',],},+ {+ type: 'category',+ label: 'Embedding videos',+ items: [+ 'videos/index',+ 'videos/transparency',+ 'miscellaneous/snippets/align-duration',+ 'video-manipulation',+ 'miscellaneous/snippets/accelerated-video',+ 'miscellaneous/snippets/jumpcuts',+ 'miscellaneous/snippets/hls',+ 'video-vs-offthreadvideo',+ ],+ },{type: 'category',label: 'Parameterized videos',@@ -701,7 +714,6 @@ module.exports = {'dataset-render','render-all','miscellaneous/video-formats',- 'video-vs-offthreadvideo','artifacts','metadata','hardware-acceleration',@@ -928,15 +940,11 @@ module.exports = {type: 'category',label: 'Snippets',items: [- 'miscellaneous/snippets/accelerated-video','miscellaneous/snippets/different-segments-at-different-speeds',- 'miscellaneous/snippets/jumpcuts','miscellaneous/snippets/player-in-iframe','miscellaneous/snippets/use-delay-render','miscellaneous/snippets/offthread-video-while-rendering',- 'miscellaneous/snippets/hls','miscellaneous/snippets/combine-compositions',- 'miscellaneous/snippets/align-duration',],},{diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex a3d9efe76d..cd1495f50d 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -3888,7 +3888,7 @@ export const articles = [},{id: 'video-vs-offthreadvideo',- title: '<Video> vs. <OffthreadVideo>',+ title: '<OffthreadVideo> vs. <Video>',relativePath: 'docs/video-vs-offthreadvideo.mdx',compId: 'articles-docs-video-vs-offthreadvideo',crumb: 'Comparison',@@ -3900,6 +3900,20 @@ export const articles = [compId: 'articles-docs-video',crumb: 'API',},+ {+ id: 'videos/index',+ title: 'Embedding videos into Remotion',+ relativePath: 'docs/videos/index.mdx',+ compId: 'articles-docs-videos-index',+ crumb: 'How To',+ },+ {+ id: 'videos/transparency',+ title: 'Embedding transparent videos',+ relativePath: 'docs/videos/transparency.mdx',+ compId: 'articles-docs-videos-transparency',+ crumb: 'How To',+ },{id: 'visual-editing',title: 'Edit default props visually',diff --git a/packages/docs/static/generated/articles-docs-videos-index.png b/packages/docs/static/generated/articles-docs-videos-index.pngnew file mode 100644index 0000000000..e673c16a15Binary files /dev/null and b/packages/docs/static/generated/articles-docs-videos-index.png differdiff --git a/packages/docs/static/generated/articles-docs-videos-transparency.png b/packages/docs/static/generated/articles-docs-videos-transparency.pngnew file mode 100644index 0000000000..004d822dabBinary files /dev/null and b/packages/docs/static/generated/articles-docs-videos-transparency.png differ