Skip to content

🎥 Make videos programmatically with React

`docs`: Clarify Mediabunny FPS estimates (#8847)

Igor Samokhovetsb70d133465df
diff --git a/packages/docs/docs/mediabunny/metadata.mdx b/packages/docs/docs/mediabunny/metadata.mdx
index 563bd7af72..bf5f06af30 100644
--- a/packages/docs/docs/mediabunny/metadata.mdx
+++ b/packages/docs/docs/mediabunny/metadata.mdx
@@ -18,6 +18,21 @@ then [Mediabunny](https://mediabunny.dev) is the ideal library for doing so.
```tsx twoslash title="get-media-metadata.ts"
import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';
+const commonFpsValues = [
+ 24000 / 1001,
+ 24,
+ 25,
+ 30000 / 1001,
+ 30,
+ 50,
+ 60000 / 1001,
+ 60,
+];
+
+const snapToCommonFps = (fps: number) => {
+ return commonFpsValues.find((commonFps) => Math.abs(fps - commonFps) < 0.01) ?? fps;
+};
+
export const getMediaMetadata = async (src: string) => {
const input = new Input({
formats: ALL_FORMATS,
@@ -35,7 +50,9 @@ export const getMediaMetadata = async (src: string) => {
}
: null;
const packetStats = await videoTrack?.computePacketStats(50);
- const fps = packetStats?.averagePacketRate ?? null;
+ const fps = packetStats
+ ? snapToCommonFps(packetStats.averagePacketRate)
+ : null;
return {
durationInSeconds,
@@ -52,12 +69,28 @@ 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>The FPS is an estimate based on packet timestamps. We snap it to common FPS values because container timestamp rounding can return values such as `29.999` or `30.006` for videos that are nominally 30 FPS. Values like `29.97` and `59.94` are represented exactly as `30000 / 1001` and `60000 / 1001`.</p>
</details>
```tsx twoslash title="Usage example"
// @filename: get-media-metadata.ts
import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';
+const commonFpsValues = [
+ 24000 / 1001,
+ 24,
+ 25,
+ 30000 / 1001,
+ 30,
+ 50,
+ 60000 / 1001,
+ 60,
+];
+
+const snapToCommonFps = (fps: number) => {
+ return commonFpsValues.find((commonFps) => Math.abs(fps - commonFps) < 0.01) ?? fps;
+};
+
export const getMediaMetadata = async (src: string) => {
const input = new Input({
formats: ALL_FORMATS,
@@ -75,7 +108,9 @@ export const getMediaMetadata = async (src: string) => {
}
: null;
const packetStats = await videoTrack?.computePacketStats(50);
- const fps = packetStats?.averagePacketRate ?? null;
+ const fps = packetStats
+ ? snapToCommonFps(packetStats.averagePacketRate)
+ : null;
return {
durationInSeconds,