Skip to content

🎥 Make videos programmatically with React

get fps from base media

Jonny Burger3530600f30b6
diff --git a/packages/video-parser/src/get-fps.ts b/packages/video-parser/src/get-fps.ts
new file mode 100644
index 0000000000..5161a53a76
--- /dev/null
+++ b/packages/video-parser/src/get-fps.ts
@@ -0,0 +1,92 @@
+import type {SttsBox} from './boxes/iso-base-media/stts/stts';
+import type {AnySegment} from './parse-result';
+
+const calculateFps = (sttsBox: SttsBox, timeScale: number) => {
+ let sum = 0;
+ let totalSamples = 0;
+ for (const sample of sttsBox.sampleDistribution) {
+ sum += sample.sampleCount * sample.sampleDelta;
+ totalSamples += sample.sampleCount;
+ }
+
+ return timeScale / (sum / totalSamples);
+};
+
+export const getFps = (segments: AnySegment[]) => {
+ const moovBox = segments.find((s) => s.type === 'moov-box');
+ if (!moovBox || moovBox.type !== 'moov-box') {
+ return null;
+ }
+
+ const mvhdBox = moovBox.children.find((c) => c.type === 'mvhd-box');
+ if (!mvhdBox || mvhdBox.type !== 'mvhd-box') {
+ return null;
+ }
+
+ const {timeScale} = mvhdBox;
+
+ const {children} = moovBox;
+ const trackBoxes = children.filter((c) => c.type === 'trak-box');
+ if (!trackBoxes || trackBoxes.length === 0) {
+ return null;
+ }
+
+ // TODO: What if the video track is not the first track?
+ const trackBox = trackBoxes[0];
+ if (!trackBox || trackBox.type !== 'trak-box') {
+ return null;
+ }
+
+ const trackBoxChildren = trackBox.children;
+ if (!trackBoxChildren || trackBoxChildren.length === 0) {
+ return null;
+ }
+
+ const mdiaBox = trackBoxChildren.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'mdia',
+ );
+ if (
+ !mdiaBox ||
+ mdiaBox.type !== 'regular-box' ||
+ mdiaBox.boxType !== 'mdia'
+ ) {
+ return null;
+ }
+
+ const minfBox = mdiaBox.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'minf',
+ );
+ if (
+ !minfBox ||
+ minfBox.type !== 'regular-box' ||
+ minfBox.boxType !== 'minf'
+ ) {
+ return null;
+ }
+
+ const stblBox = minfBox.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'stbl',
+ );
+ if (
+ !stblBox ||
+ stblBox.type !== 'regular-box' ||
+ stblBox.boxType !== 'stbl'
+ ) {
+ return null;
+ }
+
+ const sttsBox = stblBox.children.find((c) => c.type === 'stts-box');
+ if (!sttsBox || sttsBox.type !== 'stts-box') {
+ return null;
+ }
+
+ return calculateFps(sttsBox, timeScale);
+};
+
+export const hasFps = (boxes: AnySegment[]): boolean => {
+ try {
+ return getFps(boxes) !== null;
+ } catch (err) {
+ return false;
+ }
+};
diff --git a/packages/video-parser/src/get-video-metadata.ts b/packages/video-parser/src/get-video-metadata.ts
index d929a46ffb..5d62e39774 100644
--- a/packages/video-parser/src/get-video-metadata.ts
+++ b/packages/video-parser/src/get-video-metadata.ts
@@ -2,6 +2,7 @@ import {getArrayBufferIterator} from './buffer-iterator';
import {webReader} from './from-web';
import {getDimensions} from './get-dimensions';
import {getDuration} from './get-duration';
+import {getFps} from './get-fps';
import {hasAllInfo} from './has-all-info';
import type {Metadata, ParseMedia} from './options';
import {parseVideo} from './parse-video';
@@ -13,7 +14,7 @@ export const parseMedia: ParseMedia = async (
) => {
const reader = await readerInterface.read(src, null);
- const returnValue = {} as Metadata<true, true, true>;
+ const returnValue = {} as Metadata<true, true, true, true>;
const iterator = getArrayBufferIterator(new Uint8Array([]));
let parseResult = parseVideo(iterator);
@@ -44,6 +45,10 @@ export const parseMedia: ParseMedia = async (
returnValue.durationInSeconds = getDuration(parseResult.segments);
}
+ if (options.fps) {
+ returnValue.fps = getFps(parseResult.segments);
+ }
+
if (options.boxes) {
returnValue.boxes = parseResult.segments;
}
diff --git a/packages/video-parser/src/has-all-info.ts b/packages/video-parser/src/has-all-info.ts
index 9623ef4366..e5f2b92005 100644
--- a/packages/video-parser/src/has-all-info.ts
+++ b/packages/video-parser/src/has-all-info.ts
@@ -1,15 +1,16 @@
import {hasDimensions} from './get-dimensions';
import {hasDuration} from './get-duration';
+import {hasFps} from './get-fps';
import type {Options} from './options';
import type {ParseResult} from './parse-result';
export const hasAllInfo = (
- options: Options<boolean, boolean, boolean>,
+ options: Options<boolean, boolean, boolean, boolean>,
parseResult: ParseResult,
) => {
const keys = Object.entries(options)
.filter(([, value]) => value)
- .map(([key]) => key) as (keyof Options<true, true, true>)[];
+ .map(([key]) => key) as (keyof Options<true, true, true, true>)[];
return keys.every((key) => {
if (key === 'boxes') {
@@ -24,6 +25,10 @@ export const hasAllInfo = (
return hasDimensions(parseResult.segments);
}
+ if (key === 'fps') {
+ return hasFps(parseResult.segments) !== null;
+ }
+
throw new Error(`Unknown key: ${key satisfies never}`);
});
};
diff --git a/packages/video-parser/src/options.ts b/packages/video-parser/src/options.ts
index a3abd4017c..81a851d362 100644
--- a/packages/video-parser/src/options.ts
+++ b/packages/video-parser/src/options.ts
@@ -6,26 +6,33 @@ export type Options<
EnableDimensions extends boolean,
EnableDuration extends boolean,
EnableBoxes extends boolean,
+ EnableFps extends boolean,
> = {
dimensions?: EnableDimensions;
durationInSeconds?: EnableDuration;
boxes?: EnableBoxes;
+ fps?: EnableFps;
};
export type Metadata<
EnableDimensions extends boolean,
EnableDuration extends boolean,
EnableBoxes extends boolean,
+ EnableFps extends boolean,
> = (EnableDimensions extends true ? {dimensions: Dimensions} : {}) &
(EnableDuration extends true ? {durationInSeconds: number | null} : {}) &
- (EnableBoxes extends true ? {boxes: AnySegment[]} : {});
+ (EnableBoxes extends true ? {boxes: AnySegment[]} : {}) &
+ (EnableFps extends true ? {fps: number | null} : {});
export type ParseMedia = <
EnableDimensions extends boolean,
EnableDuration extends boolean,
EnableBoxes extends boolean,
+ EnableFps extends boolean,
>(
src: string,
- options: Options<EnableDimensions, EnableDuration, EnableBoxes>,
+ options: Options<EnableDimensions, EnableDuration, EnableBoxes, EnableFps>,
readerInterface?: ReaderInterface,
-) => Promise<Metadata<EnableDimensions, EnableDuration, EnableBoxes>>;
+) => Promise<
+ Metadata<EnableDimensions, EnableDuration, EnableBoxes, EnableFps>
+>;
diff --git a/packages/video-parser/src/test/stream-local.test.ts b/packages/video-parser/src/test/stream-local.test.ts
index 12c9b446fb..9e7ef05b3a 100644
--- a/packages/video-parser/src/test/stream-local.test.ts
+++ b/packages/video-parser/src/test/stream-local.test.ts
@@ -9,9 +9,10 @@ test('Should stream', async () => {
{
durationInSeconds: true,
boxes: true,
+ fps: true,
},
nodeReader,
);
- await Bun.write('boxes.json', JSON.stringify(result.boxes, null, 2));
expect(result.durationInSeconds).toBe(12.568333333333333);
+ expect(result.fps).toBe(29.99602174777881);
});