Skip to content

🎥 Make videos programmatically with React

nice

Jonny Burger6fc3a62bb7d8
diff --git a/packages/media-parser/src/get-dimensions.ts b/packages/media-parser/src/get-dimensions.ts
index dedc1b381a..d3429c34e2 100644
--- a/packages/media-parser/src/get-dimensions.ts
+++ b/packages/media-parser/src/get-dimensions.ts
@@ -1,6 +1,7 @@
import type {MainSegment} from './boxes/webm/segments/main';
+import {trakBoxContainsVideo} from './get-fps';
import type {AnySegment} from './parse-result';
-import {getMoovBox} from './traversal';
+import {getMoovBox, getStsdBox, getTkhdBox, getTraks} from './traversal';
export type Dimensions = {
width: number;
@@ -68,69 +69,26 @@ export const getDimensions = (boxes: AnySegment[]): Dimensions => {
throw new Error('Expected moov box');
}
- const {children} = moovBox;
- if (!children) {
- throw new Error('Expected moov box children');
- }
-
- const t = children.find((b) => b.type === 'trak-box');
+ const trakBox = getTraks(moovBox).filter((t) => trakBoxContainsVideo(t))[0];
- if (!t || t.type !== 'trak-box') {
+ if (!trakBox) {
throw new Error('Expected trak box');
}
- const mdiaBox = t.children.find(
- (c) => c.type === 'regular-box' && c.boxType === 'mdia',
- );
- const tkhdBox = t.children.find((c) => c.type === 'tkhd-box');
- if (tkhdBox && tkhdBox.type === 'tkhd-box') {
+ const tkhdBox = getTkhdBox(trakBox);
+ if (tkhdBox) {
return {
width: tkhdBox.width,
height: tkhdBox.height,
};
}
- if (!mdiaBox) {
- throw new Error('Expected mdia box');
- }
-
- if (mdiaBox.type !== 'regular-box') {
- throw new Error('Expected mdia box');
- }
-
- const minfBox = mdiaBox.children.find(
- (c) => c.type === 'regular-box' && c.boxType === 'minf',
- );
- if (!minfBox) {
- throw new Error('Expected minf box');
- }
-
- if (minfBox.type !== 'regular-box') {
- throw new Error('Expected minf box');
- }
-
- const stblBox = minfBox.children.find(
- (c) => c.type === 'regular-box' && c.boxType === 'stbl',
- );
-
- if (!stblBox) {
- throw new Error('Expected stbl box');
- }
-
- if (stblBox.type !== 'regular-box') {
- throw new Error('Expected stbl box');
- }
-
- const stsdBox = stblBox.children.find((c) => c.type === 'stsd-box');
+ const stsdBox = getStsdBox(trakBox);
if (!stsdBox) {
throw new Error('Expected stsd box');
}
- if (stsdBox.type !== 'stsd-box') {
- throw new Error('Expected stsd box');
- }
-
const videoSamples = stsdBox.samples.filter((s) => s.type === 'video');
if (videoSamples.length === 0) {
throw new Error('Has no video stream');
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index c0dfd36af0..e8038ca392 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -2,7 +2,7 @@
import {trakBoxContainsVideo} from './get-fps';
import type {KnownVideoCodecs} from './options';
import type {AnySegment} from './parse-result';
-import {getMoovBox} from './traversal';
+import {getMoovBox, getStsdBox, getTraks} from './traversal';
export const hasVideoCodec = (boxes: AnySegment[]): boolean => {
try {
@@ -15,51 +15,22 @@ export const hasVideoCodec = (boxes: AnySegment[]): boolean => {
export const getVideoCodec = (boxes: AnySegment[]): KnownVideoCodecs | null => {
const moovBox = getMoovBox(boxes);
if (moovBox) {
- const trakBox = moovBox.children.find(
- (b) => b.type === 'trak-box' && trakBoxContainsVideo(b),
- );
- if (trakBox && trakBox.type === 'trak-box') {
- const mdiaBox = trakBox.children.find(
- (b) => b.type === 'regular-box' && b.boxType === 'mdia',
- );
- if (
- mdiaBox &&
- mdiaBox.type === 'regular-box' &&
- mdiaBox.boxType === 'mdia'
- ) {
- const minfBox = mdiaBox?.children.find(
- (b) => b.type === 'regular-box' && b.boxType === 'minf',
- );
- if (
- minfBox &&
- minfBox.type === 'regular-box' &&
- minfBox.boxType === 'minf'
- ) {
- const stblBox = minfBox?.children.find(
- (b) => b.type === 'regular-box' && b.boxType === 'stbl',
- );
- if (stblBox && stblBox.type === 'regular-box') {
- const stsdBox = stblBox?.children.find(
- (b) => b.type === 'stsd-box',
- );
- if (stsdBox && stsdBox.type === 'stsd-box') {
- const videoSample = stsdBox.samples.find(
- (s) => s.type === 'video',
- );
- if (videoSample && videoSample.type === 'video') {
- if (videoSample.format === 'hvc1') {
- return 'h265';
- }
+ const trakBox = getTraks(moovBox).filter((t) => trakBoxContainsVideo(t))[0];
+ if (trakBox) {
+ const stsdBox = getStsdBox(trakBox);
+ if (stsdBox && stsdBox.type === 'stsd-box') {
+ const videoSample = stsdBox.samples.find((s) => s.type === 'video');
+ if (videoSample && videoSample.type === 'video') {
+ if (videoSample.format === 'hvc1') {
+ return 'h265';
+ }
- if (videoSample.format === 'avc1') {
- return 'h264';
- }
+ if (videoSample.format === 'avc1') {
+ return 'h264';
+ }
- if (videoSample.format === 'ap4h') {
- return 'prores';
- }
- }
- }
+ if (videoSample.format === 'ap4h') {
+ return 'prores';
}
}
}
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index 367556b2ff..d93d470adb 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -38,7 +38,7 @@ export const getTkhdBox = (trakBox: TrakBox): TkhdBox | null => {
return tkhdBox;
};
-export const getStblBox = (trakBox: TrakBox): RegularBox | null => {
+export const getMdiaBox = (trakBox: TrakBox): RegularBox | null => {
const mdiaBox = trakBox.children.find(
(s) => s.type === 'regular-box' && s.boxType === 'mdia',
);
@@ -47,6 +47,16 @@ export const getStblBox = (trakBox: TrakBox): RegularBox | null => {
return null;
}
+ return mdiaBox;
+};
+
+export const getStblBox = (trakBox: TrakBox): RegularBox | null => {
+ const mdiaBox = getMdiaBox(trakBox);
+
+ if (!mdiaBox) {
+ return null;
+ }
+
const minfBox = mdiaBox.children.find(
(s) => s.type === 'regular-box' && s.boxType === 'minf',
);