π₯ Make videos programmatically with React
Docs: New article about Media Fragment Hashes
Jonny Burger0cbbdb9b8a07
diff --git a/packages/docs/components/AcceleratedVideoPlayerExample.tsx b/packages/docs/components/AcceleratedVideoPlayerExample.tsxindex af895c41cb..28592e2de0 100644--- a/packages/docs/components/AcceleratedVideoPlayerExample.tsx+++ b/packages/docs/components/AcceleratedVideoPlayerExample.tsx@@ -1,57 +1,57 @@-import { Player } from "@remotion/player";-import React from "react";-import { interpolate, Sequence, useCurrentFrame, Video } from "remotion";+import {Player} from '@remotion/player';+import React from 'react';+import {interpolate, Sequence, useCurrentFrame, Video} from 'remotion';const remapSpeed = ({- frame,- speed,+ frame,+ speed,}: {- frame: number;- speed: (fr: number) => number;+ frame: number;+ speed: (fr: number) => number;}) => {- let framesPassed = 0;+ let framesPassed = 0;- for (let i = 0; i <= frame; i++) {- framesPassed += speed(i);- }+ for (let i = 0; i <= frame; i++) {+ framesPassed += speed(i);+ }- return framesPassed;+ return framesPassed;};const AcceleratedVideo: React.FC = () => {- const frame = useCurrentFrame();-- const speedFunction = (f: number) =>- Math.min(16, interpolate(f, [0, 500], [1, 5]));-- const remappedFrame = remapSpeed({- frame,- speed: speedFunction,- });-- return (- <Sequence from={frame}>- <Video- startFrom={Math.round(remappedFrame)}- playbackRate={speedFunction(frame)}- src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"- />- </Sequence>- );+ const frame = useCurrentFrame();++ const speedFunction = (f: number) =>+ Math.min(16, interpolate(f, [0, 500], [1, 5]));++ const remappedFrame = remapSpeed({+ frame,+ speed: speedFunction,+ });++ return (+ <Sequence from={frame}>+ <Video+ startFrom={Math.round(remappedFrame)}+ playbackRate={speedFunction(frame)}+ src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4#disable"+ />+ </Sequence>+ );};export const AcceleratedVideoExample: React.FC = () => {- return (- <Player- component={AcceleratedVideo}- compositionHeight={720}- compositionWidth={1280}- durationInFrames={1500}- fps={30}- controls- style={{- width: "100%",- }}- />- );+ return (+ <Player+ component={AcceleratedVideo}+ compositionHeight={720}+ compositionWidth={1280}+ durationInFrames={1500}+ fps={30}+ controls+ style={{+ width: '100%',+ }}+ />+ );};diff --git a/packages/docs/docs/media-fragments.mdx b/packages/docs/docs/media-fragments.mdxnew file mode 100644index 0000000000..90000b2d8f--- /dev/null+++ b/packages/docs/docs/media-fragments.mdx@@ -0,0 +1,78 @@+---+image: /generated/articles-docs-media-fragments.png+crumb: 'Video behavior'+title: '#t= Media Fragments'+slug: media-fragments+---++Remotion by default adds [Media fragment](https://www.w3.org/TR/media-frags/) hashes to video URLs.++## Behavior++For example, the following Remotion code:++```tsx twoslash title="MyComp.tsx"+import {Sequence, OffthreadVideo, useVideoConfig} from 'remotion';++export const MyComp: React.FC = () => {+ const {fps} = useVideoConfig();++ return (+ <Sequence from={2 * fps} durationInFrames={4 * fps}>+ <OffthreadVideo src="https://example.com/bbb.mp4" />+ </Sequence>+ );+};+```++results in the following `<video>` tag being mounted:++```tsx+<video src="https://example.com/bbb.mp4#t=2.0,4.0" />+```++This instructs browsers to only load the section of 00:02 to 00:04 of the video.++## Upsides++It improves playback performance and reduces bandwidth usage.++On Safari, these hints have higher importance. We find that without these hints, Safari videos behave badly on mobile.++## How to disable media fragment hints++You can disable Remotion appending media fragments by appending your own hash:++```tsx twoslash title="MyComp.tsx" {8}+import {Sequence, OffthreadVideo, useVideoConfig} from 'remotion';++export const MyComp: React.FC = () => {+ const {fps} = useVideoConfig();++ return (+ <Sequence from={2 * fps} durationInFrames={4 * fps}>+ <OffthreadVideo src="https://example.com/bbb.mp4#disable" />+ </Sequence>+ );+};+```++## When to disable media fragment hints++If your `<Sequence>`'s change value over time, the media fragments will change too.+Anytime they change, the browser considers this a new source and re-loads the video.++There are valid scenarios where we recommend disabling the media fragment hints:++- Changing the `from` and `durationInFrames` values of a `<Sequence>` dynamically+- Changing the `startFrom` and `endAt` values of a `<Video>` or `<OffthreadVideo>` dynamically+- Changing the `playbackRate` of a `<Video>` over time.++Normally you would not do this, but there are exceptions, like: [Changing the speed of a video over time](/docs/miscellaneous/snippets/accelerated-video)++## Recommendation++Use the automatic media fragment hints as a default because they are helpful with playback performance.+If you find a valid scenario where you need to disable the media fragment hints, you can do so.++If the playback performance suffers, you can also provide your own media fragment hint to the URL.diff --git a/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdx b/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdxindex 462fd60862..7b08892c99 100644--- a/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdx+++ b/packages/docs/docs/miscellaneous/snippets/accelerated-video.mdx@@ -1,17 +1,17 @@---image: /generated/articles-docs-miscellaneous-snippets-accelerated-video.png-title: "Change the speed of a video over time"-crumb: "Snippets"+title: 'Change the speed of a video over time'+crumb: 'Snippets'----import {AcceleratedVideoExample} from "../../../components/AcceleratedVideoPlayerExample.tsx"+import {AcceleratedVideoExample} from '../../../components/AcceleratedVideoPlayerExample.tsx';To speed up a video over time - for example to start with regular speed and then increasingly make it faster - we need to write some code to ensure we stay within Remotion's rules.It is not as easy as interpolating the [`playbackRate`](/docs/video#playbackrate):```tsx twoslash title="β Does not work"-import { interpolate, Video } from "remotion";+import {interpolate, Video} from 'remotion';let frame = 0;// ---cut---<Video playbackRate={interpolate(frame, [0, 100], [1, 5])} />;@@ -22,8 +22,8 @@ This is because Remotion will evaluate each frame independently from the other fThe correct way is to calculate the total time that has elapsed until the current frame, by accumulating the playback rates of the previous frames. Then we start the video from that frame and set the playback rate of that current frame to ensure the video plays smoothly.```tsx twoslash title="β AcceleratedVideo.tsx"-import React from "react";-import { interpolate, Sequence, useCurrentFrame, Video } from "remotion";+import React from 'react';+import {interpolate, Sequence, useCurrentFrame, Video} from 'remotion';const remapSpeed = (frame: number, speed: (fr: number) => number) => {let framesPassed = 0;@@ -46,13 +46,17 @@ export const AcceleratedVideo: React.FC = () => {<VideostartFrom={Math.round(remappedFrame)}playbackRate={speedFunction(frame)}- src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"+ src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4#disable"/></Sequence>);};```+:::note+We are [disabling the media fragment hints](/docs/media-fragments) being appended to the video URL by adding `#disable` to the end of the URL.+:::+Note that the timeline in the Remotion Studio might move as you play the video because we reposition the sequence over time. This is okay as long as we calculate the frame in an idempotent way.## Demodiff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 60a72c1ccc..fea230836b 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -997,6 +997,7 @@ module.exports = {'bun','standalone','miscellaneous/emojis',+ 'media-fragments',],},{diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex e2437f4255..f018230897 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -482,6 +482,13 @@ export const articles = [compId: 'articles-docs-media-playback-error',crumb: 'Troubleshooting',},+ {+ id: 'media-fragments',+ title: '#t= Media Fragments',+ relativePath: 'docs/media-fragments.mdx',+ compId: 'articles-docs-media-fragments',+ crumb: 'Video behavior',+ },{id: 'staticfile-relative-paths',title: 'staticFile() does not support relative paths',diff --git a/packages/docs/static/generated/articles-docs-media-fragments.png b/packages/docs/static/generated/articles-docs-media-fragments.pngnew file mode 100644index 0000000000..609cffc8d7Binary files /dev/null and b/packages/docs/static/generated/articles-docs-media-fragments.png differ