Skip to content

🎥 Make videos programmatically with React

Docs: Update metadata snippet to use createInputFrom and async accessors

Jonny Burgera6e8096e70b6
diff --git a/packages/docs/docs/mediabunny/metadata.mdx b/packages/docs/docs/mediabunny/metadata.mdx
index e97b74834d..20c169ad23 100644
--- a/packages/docs/docs/mediabunny/metadata.mdx
+++ b/packages/docs/docs/mediabunny/metadata.mdx
@@ -16,22 +16,19 @@ then [Mediabunny](https://mediabunny.dev) is the ideal library for doing so.
## Getting metadata from a URL
```tsx twoslash title="get-media-metadata.ts"
-import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';
+import {ALL_FORMATS, createInputFrom} from 'mediabunny';
export const getMediaMetadata = async (src: string) => {
- const input = new Input({
- formats: ALL_FORMATS,
- source: new UrlSource(src, {
- getRetryDelay: () => null,
- }),
+ const input = createInputFrom(src, ALL_FORMATS, {
+ getRetryDelay: () => null,
});
const durationInSeconds = await input.computeDuration();
const videoTrack = await input.getPrimaryVideoTrack();
const dimensions = videoTrack
? {
- width: videoTrack.displayWidth,
- height: videoTrack.displayHeight,
+ width: await videoTrack.getDisplayWidth(),
+ height: await videoTrack.getDisplayHeight(),
}
: null;
const packetStats = await videoTrack?.computePacketStats(50);
@@ -52,26 +49,24 @@ export const getMediaMetadata = async (src: string) => {
<p>By setting `getRetryDelay` to `() => null`, we prevent Mediabunny from infinitely retrying if the request fails.</p>
<p>We compute the duration, the dimensions and the frame rate. If the media file is an audio file, we return `null` for the dimensions and the frame rate.</p>
<p>To avoid reading the entire file, we at most read 50 packets to compute the frame rate.</p>
+ <p>`createInputFrom` also supports HLS (`.m3u8`) URLs - it automatically handles the multi-file nature of HLS playlists.</p>
</details>
```tsx twoslash title="Usage example"
// @filename: get-media-metadata.ts
-import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';
+import {ALL_FORMATS, createInputFrom} from 'mediabunny';
export const getMediaMetadata = async (src: string) => {
- const input = new Input({
- formats: ALL_FORMATS,
- source: new UrlSource(src, {
- getRetryDelay: () => null,
- }),
+ const input = createInputFrom(src, ALL_FORMATS, {
+ getRetryDelay: () => null,
});
const durationInSeconds = await input.computeDuration();
const videoTrack = await input.getPrimaryVideoTrack();
const dimensions = videoTrack
? {
- width: videoTrack.displayWidth,
- height: videoTrack.displayHeight,
+ width: await videoTrack.getDisplayWidth(),
+ height: await videoTrack.getDisplayHeight(),
}
: null;
const packetStats = await videoTrack?.computePacketStats(50);