Skip to content

🎥 Make videos programmatically with React

get video metadata improvement and docs

Jonny Burger500a291c08e6
diff --git a/packages/docs/docs/get-audio-data.md b/packages/docs/docs/get-audio-data.md
index ae20fbaabe..15570b64e6 100644
--- a/packages/docs/docs/get-audio-data.md
+++ b/packages/docs/docs/get-audio-data.md
@@ -26,7 +26,7 @@ A string pointing to an audio asset.
- `durationInSeconds`: `number` The duration of the audio in seconds.
- `numberOfChannels`: `number` The number of channels contained in the audio file. This corresponds to the length of the `channelWaveforms` array.
- `resultId`: `string` Unique identifier of this audio data fetching call. Other functions can cache expensive operations if they get called with the same resultId multiple times.
-- `isRemote`: Whether the audio was imported locally or from a different origin.
+- `isRemote`: `boolean` Whether the audio was imported locally or from a different origin.
## Example
@@ -65,6 +65,6 @@ Use the [`useAudioData()`](use-audio-data) helper hook to not have to do state m
## See also
-- [Using audio](using-audio)
-- [Audio visualization](audio-visualization)
-- [`<Audio/>`](audio)
+- [Using audio](/docs/using-audio)
+- [Audio visualization](/docs/audio-visualization)
+- [`<Audio/>`](/docs/audio)
diff --git a/packages/docs/docs/get-video-metadata.md b/packages/docs/docs/get-video-metadata.md
index 5c6e7710f2..18e81be134 100644
--- a/packages/docs/docs/get-video-metadata.md
+++ b/packages/docs/docs/get-video-metadata.md
@@ -9,4 +9,47 @@ This function is part of Remotion 2.0 which is not yet released. The information
_Part of the `@remotion/media-utils`_ package of helper functions.
-TODO
+Takes a `src` to a video, loads it and returns metadata for the specified source.
+
+## Arguments
+
+### `src`
+
+A string pointing to an asset.
+
+## Return value
+
+`Promise<VideoMetadata>` - object with information about the video data:
+
+- `durationInSeconds`: `number` The duration of the video in seconds.
+- `width`: `number` The width of the video in pixels.
+- `height`: `number` The height of the video in pixels.
+- `aspectRatio`: `number` Video width divided by video height.
+- `isRemote`: `boolean` Whether the video was imported locally or from a different origin.
+
+## Example
+
+```tsx
+import {getVideoMetadata} from '@remotion/media-utils';
+import video from '../video.mp4';
+
+await getVideoMetadata(video); /* {
+ durationInSeconds: 100.00,
+ width: 1280,
+ height: 720,
+ aspectRatio: 1.77777778,
+ isRemote: false
+} */
+await getVideoMetadata('https://example.com/remote-audio.webm'); /* {
+ durationInSeconds: 40.213,
+ width: 1920,
+ height: 1080,
+ aspectRatio: 1.77777778,
+ isRemote: true
+} */
+```
+
+## See also
+
+- [Using videos](/docs/assets#using-videos)
+- [`<Video/>`](/docs/video)
diff --git a/packages/docs/docs/importing-assets.md b/packages/docs/docs/importing-assets.md
index c2f5de3330..4e5d46869b 100644
--- a/packages/docs/docs/importing-assets.md
+++ b/packages/docs/docs/importing-assets.md
@@ -12,14 +12,15 @@ Remotion allows you to include several types of files in your project:
## Using images
-Require images using an `import` statement
+Require images using an `import` statement and pass them to the [`<Img/>`](/docs/img) tag.
```tsx
+import {Img} from 'remotion';
import logo from './logo.png'
export const MyComp: React.FC = () => {
return (
- <img src={logo} />
+ <Img src={logo} />
)
}
```
@@ -73,7 +74,7 @@ Be aware that if you are rendering using Chromium (as opposed to Chrome), the co
## Using Audio
-Import your audio using an `@import` statement and pass it to the [`<Audio/ >`](/docs/audio) component.
+Import your audio using an `import` statement and pass it to the [`<Audio/ >`](/docs/audio) component.
```tsx
import {Audio} from 'remotion';
@@ -86,9 +87,7 @@ export const MyComp: React.FC = () => {
}
```
-:::info
-Audio is experimental and is currently only playing in the preview, not in the final video.
-:::
+See the [audio guide](/docs/using-audio) for guidance on including audio.
## Using fonts
diff --git a/packages/media-utils/src/get-video-metadata.ts b/packages/media-utils/src/get-video-metadata.ts
index 114204963f..e704008d4e 100644
--- a/packages/media-utils/src/get-video-metadata.ts
+++ b/packages/media-utils/src/get-video-metadata.ts
@@ -1,22 +1,31 @@
import {isRemoteAsset} from './is-remote-asset';
-import {VideoData} from './types';
+import {VideoMetadata} from './types';
+
+const cache: {[key: string]: VideoMetadata} = {};
+
+export const getVideoMetadata = async (src: string): Promise<VideoMetadata> => {
+ if (cache[src]) {
+ return cache[src];
+ }
-export const getVideoMetadata = async (src: string): Promise<VideoData> => {
const video = document.createElement('video');
video.src = src;
- return new Promise<VideoData>((resolve, reject) => {
+ return new Promise<VideoMetadata>((resolve, reject) => {
const onError = (ev: ErrorEvent) => {
reject(ev.error);
cleanup();
};
const onLoadedMetadata = () => {
- resolve({
+ const metadata: VideoMetadata = {
durationInSeconds: video.duration,
width: video.videoWidth,
height: video.videoHeight,
aspectRatio: video.videoWidth / video.videoHeight,
isRemote: isRemoteAsset(src),
- });
+ };
+ resolve(metadata);
+ cache[src] = metadata;
+
cleanup();
};
const cleanup = () => {
diff --git a/packages/media-utils/src/index.ts b/packages/media-utils/src/index.ts
index f94831755a..c707d64da0 100644
--- a/packages/media-utils/src/index.ts
+++ b/packages/media-utils/src/index.ts
@@ -2,6 +2,6 @@ export {getAudioData} from './get-audio-data';
export {getAudioDuration} from './get-audio-duration';
export {getVideoMetadata} from './get-video-metadata';
export {getWaveformPortion} from './get-waveform-portion';
-export type {AudioData, VideoData} from './types';
+export type {AudioData, VideoMetadata as VideoData} from './types';
export {useAudioData} from './use-audio-metadata';
export {visualizeAudio} from './visualize-audio';
diff --git a/packages/media-utils/src/types.ts b/packages/media-utils/src/types.ts
index 5ab4378c09..b04cbf419d 100644
--- a/packages/media-utils/src/types.ts
+++ b/packages/media-utils/src/types.ts
@@ -7,7 +7,7 @@ export type AudioData = {
isRemote: boolean;
};
-export type VideoData = {
+export type VideoMetadata = {
durationInSeconds: number;
width: number;
height: number;