Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Update example for looping an OffthreadVideo #4113

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions packages/docs/docs/offthreadvideo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -279,49 +279,71 @@ You can use the following snippet that uses [`@remotion/media-utils`](/docs/medi
Note that this will mount a `<video>` tag in the browser, meaning only codecs supported by the browser can be used.

```tsx twoslash title="LoopedOffthreadVideo.tsx"
import { getVideoMetadata } from "@remotion/media-utils";
import React, { useEffect, useState } from "react";
import {
cancelRender,
continueRender,
delayRender,
Loop,
OffthreadVideo,
staticFile,
useVideoConfig,
} from "remotion";

const src = staticFile("myvideo.mp4");

export const LoopedOffthreadVideo: React.FC = () => {
const [duration, setDuration] = useState<null | number>(null);
const [handle] = useState(() => delayRender());
export const LoopedOffthreadVideo: React.FC<{
durationInSeconds: number | null;
src: string;
}> = ({durationInSeconds, src}) => {
const { fps } = useVideoConfig();

useEffect(() => {
getVideoMetadata(src)
.then(({ durationInSeconds }) => {
setDuration(durationInSeconds);
continueRender(handle);
})
.catch((err) => {
cancelRender(handle);
console.log(err);
});
}, [handle]);

if (duration === null) {
if (durationInSeconds === null) {
return null;
}

return (
<Loop durationInFrames={Math.floor(fps * duration)}>
<Loop durationInFrames={Math.floor(fps * durationInSeconds)}>
<OffthreadVideo src={src} />
</Loop>
);
};
```

```tsx twoslash title="Root.tsx"
const LoopedOffthreadVideo: React.FC<{
durationInSeconds: number | null;
src: string;
}> = () => null;

// ---cut---

import React, { useState } from "react";
import { Composition, staticFile } from "remotion";
import { getVideoMetadata } from "@remotion/media-utils";

export const RemotionRoot: React.FC = () => {
return (
<Composition
id="MyComp"
component={LoopedOffthreadVideo}
defaultProps={{
durationInSeconds: null,
src: staticFile("myvideo.mp4"),
}}
calculateMetadata={async ({props}) => {
const { durationInSeconds, width, height } = await getVideoMetadata(props.src);
const fps = 30;

return {
durationInFrames: Math.floor(durationInSeconds * fps),
fps,
width,
height,
props: {
...props,
durationInSeconds,
}
};
}}
/>
)
}
```

## Supported codecs by `<OffthreadVideo>`

The following codecs can be read by `<OffthreadVideo>`:
Expand Down
Loading