Skip to content

🎥 Make videos programmatically with React

implement rest of fields

Jonny Burgerd7c0ecf59fbc
diff --git a/packages/media-parser/src/get-container.ts b/packages/media-parser/src/get-container.ts
index 66b2fb20b7..3172813d90 100644
--- a/packages/media-parser/src/get-container.ts
+++ b/packages/media-parser/src/get-container.ts
@@ -1,3 +1,4 @@
+import {isRiffAvi} from './boxes/riff/traversal';
import type {ParseMediaContainer} from './options';
import type {Structure} from './parse-result';
@@ -12,6 +13,12 @@ export const getContainer = (
return 'webm';
}
+ if (segments.type === 'riff') {
+ if (isRiffAvi(segments)) {
+ return 'avi';
+ }
+ }
+
throw new Error('Unknown container');
};
diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.ts
index 560a167496..60b0703170 100644
--- a/packages/media-parser/src/get-duration.ts
+++ b/packages/media-parser/src/get-duration.ts
@@ -5,9 +5,15 @@ import {
getMoovBox,
getMvhdBox,
} from './boxes/iso-base-media/traversal';
+import {getStrhBox, getStrlBoxes} from './boxes/riff/traversal';
import type {DurationSegment} from './boxes/webm/segments/all-segments';
import {getTracks} from './get-tracks';
-import type {AnySegment, Structure} from './parse-result';
+import type {
+ AnySegment,
+ IsoBaseMediaStructure,
+ RiffStructure,
+ Structure,
+} from './parse-result';
import type {ParserState} from './parser-state';
const getDurationFromMatroska = (segments: AnySegment[]): number | null => {
@@ -48,6 +54,71 @@ export const isMatroska = (boxes: AnySegment[]) => {
return matroskaBox;
};
+const getDurationFromIsoBaseMedia = (
+ structure: IsoBaseMediaStructure,
+ parserState: ParserState,
+) => {
+ const moovBox = getMoovBox(structure.boxes);
+ if (!moovBox) {
+ return null;
+ }
+
+ const moofBox = getMoofBox(structure.boxes);
+ const mvhdBox = getMvhdBox(moovBox);
+
+ if (!mvhdBox) {
+ return null;
+ }
+
+ if (mvhdBox.type !== 'mvhd-box') {
+ throw new Error('Expected mvhd-box');
+ }
+
+ if (mvhdBox.durationInSeconds > 0) {
+ return mvhdBox.durationInSeconds;
+ }
+
+ const tracks = getTracks(structure, parserState);
+ const allTracks = [
+ ...tracks.videoTracks,
+ ...tracks.audioTracks,
+ ...tracks.otherTracks,
+ ];
+ const allSamples = allTracks.map((t) => {
+ const {timescale: ts} = t;
+ const samplePositions = getSamplePositionsFromTrack(
+ t.trakBox as TrakBox,
+ moofBox,
+ );
+
+ const highest = samplePositions
+ ?.map((sp) => (sp.cts + sp.duration) / ts)
+ .reduce((a, b) => Math.max(a, b), 0);
+ return highest ?? 0;
+ });
+ const highestTimestamp = Math.max(...allSamples);
+ return highestTimestamp;
+};
+
+const getDurationFromAvi = (structure: RiffStructure) => {
+ const strl = getStrlBoxes(structure);
+
+ const lengths: number[] = [];
+ for (const s of strl) {
+ const strh = getStrhBox(s.children);
+ if (!strh) {
+ throw new Error('No strh box');
+ }
+
+ const samplesPerSecond = strh.rate / strh.scale;
+
+ const streamLength = strh.length / samplesPerSecond;
+ lengths.push(streamLength);
+ }
+
+ return Math.max(...lengths);
+};
+
export const getDuration = (
structure: Structure,
parserState: ParserState,
@@ -57,46 +128,11 @@ export const getDuration = (
}
if (structure.type === 'iso-base-media') {
- const moovBox = getMoovBox(structure.boxes);
- if (!moovBox) {
- return null;
- }
-
- const moofBox = getMoofBox(structure.boxes);
- const mvhdBox = getMvhdBox(moovBox);
-
- if (!mvhdBox) {
- return null;
- }
-
- if (mvhdBox.type !== 'mvhd-box') {
- throw new Error('Expected mvhd-box');
- }
-
- if (mvhdBox.durationInSeconds > 0) {
- return mvhdBox.durationInSeconds;
- }
+ return getDurationFromIsoBaseMedia(structure, parserState);
+ }
- const tracks = getTracks(structure, parserState);
- const allTracks = [
- ...tracks.videoTracks,
- ...tracks.audioTracks,
- ...tracks.otherTracks,
- ];
- const allSamples = allTracks.map((t) => {
- const {timescale: ts} = t;
- const samplePositions = getSamplePositionsFromTrack(
- t.trakBox as TrakBox,
- moofBox,
- );
-
- const highest = samplePositions
- ?.map((sp) => (sp.cts + sp.duration) / ts)
- .reduce((a, b) => Math.max(a, b), 0);
- return highest ?? 0;
- });
- const highestTimestamp = Math.max(...allSamples);
- return highestTimestamp;
+ if (structure.type === 'riff') {
+ return getDurationFromAvi(structure);
}
throw new Error('Has no duration');
diff --git a/packages/media-parser/src/get-fps.ts b/packages/media-parser/src/get-fps.ts
index 7b3f87eb54..4ca0a51803 100644
--- a/packages/media-parser/src/get-fps.ts
+++ b/packages/media-parser/src/get-fps.ts
@@ -7,7 +7,12 @@ import {
getSttsBox,
getTraks,
} from './boxes/iso-base-media/traversal';
-import type {Structure} from './parse-result';
+import {getStrhBox, getStrlBoxes} from './boxes/riff/traversal';
+import type {
+ IsoBaseMediaStructure,
+ RiffStructure,
+ Structure,
+} from './parse-result';
const calculateFps = ({
sttsBox,
@@ -92,21 +97,48 @@ export const getFpsFromMp4TrakBox = (trakBox: TrakBox) => {
});
};
-export const getFps = (segments: Structure) => {
- if (segments.type === 'iso-base-media') {
- const moovBox = getMoovBox(segments.boxes);
- if (!moovBox) {
- return null;
- }
+const getFpsFromIsoMaseMedia = (structure: IsoBaseMediaStructure) => {
+ const moovBox = getMoovBox(structure.boxes);
+ if (!moovBox) {
+ return null;
+ }
+
+ const trackBoxes = getTraks(moovBox);
+
+ const trackBox = trackBoxes.find(trakBoxContainsVideo);
+ if (!trackBox) {
+ return null;
+ }
+
+ return getFpsFromMp4TrakBox(trackBox);
+};
- const trackBoxes = getTraks(moovBox);
+const getFpsFromAvi = (structure: RiffStructure) => {
+ const strl = getStrlBoxes(structure);
- const trackBox = trackBoxes.find(trakBoxContainsVideo);
- if (!trackBox) {
- return null;
+ for (const s of strl) {
+ const strh = getStrhBox(s.children);
+ if (!strh) {
+ throw new Error('No strh box');
}
- return getFpsFromMp4TrakBox(trackBox);
+ if (strh.fccType === 'auds') {
+ continue;
+ }
+
+ return strh.rate;
+ }
+
+ return null;
+};
+
+export const getFps = (segments: Structure) => {
+ if (segments.type === 'iso-base-media') {
+ return getFpsFromIsoMaseMedia(segments);
+ }
+
+ if (segments.type === 'riff') {
+ return getFpsFromAvi(segments);
}
// TODO: Matroska doesn't have Matroska
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index 923c190460..237e81c8d2 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -4,6 +4,7 @@ import {
getStsdBox,
getTraks,
} from './boxes/iso-base-media/traversal';
+import {getStrhBox, getStrlBoxes} from './boxes/riff/traversal';
import {parseAv1PrivateData} from './boxes/webm/av1-codec-private';
import type {MatroskaSegment} from './boxes/webm/segments';
import {trakBoxContainsVideo} from './get-fps';
@@ -19,7 +20,7 @@ import {
type MediaParserVideoCodec,
type VideoTrackColorParams,
} from './get-tracks';
-import type {Structure} from './parse-result';
+import type {RiffStructure, Structure} from './parse-result';
import type {ParserState} from './parser-state';
export const getVideoCodecFromIsoTrak = (trakBox: TrakBox) => {
@@ -125,6 +126,27 @@ const getVideoCodecFromMatroska = (boxes: MatroskaSegment[]) => {
throw new Error('Could not find video codec');
};
+const getVideoCodecFromAvi = (structure: RiffStructure): 'h264' => {
+ const strl = getStrlBoxes(structure);
+
+ for (const s of strl) {
+ const strh = getStrhBox(s.children);
+ if (!strh) {
+ throw new Error('No strh box');
+ }
+
+ if (strh.fccType === 'auds') {
+ continue;
+ }
+
+ if (strh.handler === 'H264') {
+ return 'h264';
+ }
+ }
+
+ throw new Error('Unsupported codec');
+};
+
export const getVideoCodec = (
boxes: Structure,
): MediaParserVideoCodec | null => {
@@ -140,6 +162,10 @@ export const getVideoCodec = (
}
}
+ if (boxes.type === 'riff') {
+ return getVideoCodecFromAvi(boxes);
+ }
+
if (boxes.type === 'matroska') {
return getVideoCodecFromMatroska(boxes.boxes);
}
diff --git a/packages/media-parser/src/options.ts b/packages/media-parser/src/options.ts
index d8fd97c912..50fa54a13f 100644
--- a/packages/media-parser/src/options.ts
+++ b/packages/media-parser/src/options.ts
@@ -74,7 +74,7 @@ export type TracksField = {
audioTracks: AudioTrack[];
};
-export type ParseMediaContainer = 'mp4' | 'webm';
+export type ParseMediaContainer = 'mp4' | 'webm' | 'avi';
export type ParseMediaCallbacks<Fields extends Options<ParseMediaFields>> =
(Fields['dimensions'] extends true
diff --git a/packages/media-parser/src/test/parse-avi-file.test.ts b/packages/media-parser/src/test/parse-avi-file.test.ts
index f5fb3a9730..f511e53ea3 100644
--- a/packages/media-parser/src/test/parse-avi-file.test.ts
+++ b/packages/media-parser/src/test/parse-avi-file.test.ts
@@ -8,12 +8,34 @@ test('AVI file', async () => {
let videoTrackCount = 0;
let audioSamples = 0;
let videoSamples = 0;
- const {structure, audioTracks, videoTracks} = await parseMedia({
+ const {
+ structure,
+ audioTracks,
+ videoTracks,
+ size,
+ container,
+ audioCodec,
+ dimensions,
+ durationInSeconds,
+ fps,
+ name,
+ rotation,
+ videoCodec,
+ } = await parseMedia({
src: exampleVideos.avi,
reader: nodeReader,
fields: {
structure: true,
tracks: true,
+ size: true,
+ container: true,
+ audioCodec: true,
+ dimensions: true,
+ durationInSeconds: true,
+ fps: true,
+ name: true,
+ rotation: true,
+ videoCodec: true,
},
onAudioTrack: () => {
audioTrackCount++;
@@ -32,10 +54,22 @@ test('AVI file', async () => {
};
},
});
+ expect(container).toBe('avi');
+ expect(dimensions).toEqual({
+ height: 270,
+ width: 480,
+ });
+ expect(rotation).toBe(0);
+ expect(name).toBe('example.avi');
+ expect(fps).toBe(30);
+ expect(durationInSeconds).toEqual(30.613333333333333);
+ expect(audioCodec).toBe('aac');
+ expect(size).toBe(742478);
expect(audioTrackCount).toBe(1);
expect(videoTrackCount).toBe(1);
expect(audioSamples).toBe(1435);
expect(videoSamples).toBe(901);
+ expect(videoCodec).toBe('h264');
expect(audioTracks).toEqual([
{
codec: 'mp4a.40.2',