Skip to content

🎥 Make videos programmatically with React

get audio codec

Jonny Burger400358c79924
diff --git a/packages/media-parser/src/get-audio-codec.ts b/packages/media-parser/src/get-audio-codec.ts
new file mode 100644
index 0000000000..f47f6a4e7d
--- /dev/null
+++ b/packages/media-parser/src/get-audio-codec.ts
@@ -0,0 +1,82 @@
+/* eslint-disable max-depth */
+import {trakBoxContainsAudio} from './get-fps';
+import type {KnownAudioCodecs} from './options';
+import type {AnySegment} from './parse-result';
+
+export const hasAudioCodec = (boxes: AnySegment[]): boolean => {
+ try {
+ return getAudioCodec(boxes) !== null;
+ } catch (e) {
+ return false;
+ }
+};
+
+export const getAudioCodec = (boxes: AnySegment[]): KnownAudioCodecs | null => {
+ const moovBox = boxes.find((b) => b.type === 'moov-box');
+ if (moovBox && moovBox.type === 'moov-box') {
+ const trakBox = moovBox.children.find(
+ (b) => b.type === 'trak-box' && trakBoxContainsAudio(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 === 'audio',
+ );
+ if (videoSample && videoSample.type === 'audio') {
+ if (videoSample.format === 'mp4a') {
+ return 'aac';
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ const mainSegment = boxes.find((b) => b.type === 'main-segment');
+ if (!mainSegment || mainSegment.type !== 'main-segment') {
+ return null;
+ }
+
+ const tracksSegment = mainSegment.children.find(
+ (b) => b.type === 'tracks-segment',
+ );
+ if (!tracksSegment || tracksSegment.type !== 'tracks-segment') {
+ return null;
+ }
+
+ for (const track of tracksSegment.children) {
+ if (track.type === 'track-entry-segment') {
+ const trackType = track.children.find((b) => b.type === 'codec-segment');
+ if (trackType && trackType.type === 'codec-segment') {
+ // TODO: Add more codecs
+ }
+ }
+ }
+
+ return null;
+};
diff --git a/packages/media-parser/src/get-fps.ts b/packages/media-parser/src/get-fps.ts
index 5696f5d81f..ec550593a9 100644
--- a/packages/media-parser/src/get-fps.ts
+++ b/packages/media-parser/src/get-fps.ts
@@ -27,6 +27,54 @@ type TimescaleAndDuration = {
duration: number;
};
+export const trakBoxContainsAudio = (trakBox: AnySegment): boolean => {
+ if (trakBox.type !== 'trak-box') {
+ return false;
+ }
+
+ const {children} = trakBox;
+ const mediaBoxes = children.filter(
+ (c) => c.type === 'regular-box' && c.boxType === 'mdia',
+ );
+ if (!mediaBoxes || mediaBoxes.length === 0) {
+ return false;
+ }
+
+ const firstMediaBox = mediaBoxes[0];
+ if (
+ firstMediaBox.type !== 'regular-box' ||
+ firstMediaBox.boxType !== 'mdia'
+ ) {
+ return false;
+ }
+
+ const minf = firstMediaBox.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'minf',
+ );
+ if (!minf || minf.type !== 'regular-box' || minf.boxType !== 'minf') {
+ return false;
+ }
+
+ const stbl = minf.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'stbl',
+ );
+ if (!stbl || stbl.type !== 'regular-box' || stbl.boxType !== 'stbl') {
+ return false;
+ }
+
+ const stsd = stbl.children.find((c) => c.type === 'stsd-box');
+ if (!stsd || stsd.type !== 'stsd-box') {
+ return false;
+ }
+
+ const videoSample = stsd.samples.find((s) => s.type === 'audio');
+ if (!videoSample || videoSample.type !== 'audio') {
+ return false;
+ }
+
+ return true;
+};
+
export const trakBoxContainsVideo = (trakBox: AnySegment): boolean => {
if (trakBox.type !== 'trak-box') {
return false;
diff --git a/packages/media-parser/src/has-all-info.ts b/packages/media-parser/src/has-all-info.ts
index cf4471299f..db84d50c18 100644
--- a/packages/media-parser/src/has-all-info.ts
+++ b/packages/media-parser/src/has-all-info.ts
@@ -1,3 +1,4 @@
+import {hasAudioCodec} from './get-audio-codec';
import {hasDimensions} from './get-dimensions';
import {hasDuration} from './get-duration';
import {hasFps} from './get-fps';
@@ -6,12 +7,19 @@ import type {Options} from './options';
import type {ParseResult} from './parse-result';
export const hasAllInfo = (
- options: Options<boolean, boolean, boolean, boolean, boolean>,
+ options: Options<boolean, boolean, boolean, boolean, boolean, boolean>,
parseResult: ParseResult,
) => {
const keys = Object.entries(options)
.filter(([, value]) => value)
- .map(([key]) => key) as (keyof Options<true, true, true, true, true>)[];
+ .map(([key]) => key) as (keyof Options<
+ true,
+ true,
+ true,
+ true,
+ true,
+ true
+ >)[];
return keys.every((key) => {
if (key === 'boxes') {
@@ -34,6 +42,10 @@ export const hasAllInfo = (
return hasVideoCodec(parseResult.segments) !== null;
}
+ if (key === 'audioCodec') {
+ return hasAudioCodec(parseResult.segments) !== null;
+ }
+
throw new Error(`Unknown key: ${key satisfies never}`);
});
};
diff --git a/packages/media-parser/src/options.ts b/packages/media-parser/src/options.ts
index 4e5a858079..4344670613 100644
--- a/packages/media-parser/src/options.ts
+++ b/packages/media-parser/src/options.ts
@@ -10,18 +10,22 @@ export type KnownVideoCodecs =
| 'av1'
| 'prores';
+export type KnownAudioCodecs = 'aac';
+
export type Options<
EnableDimensions extends boolean,
EnableDuration extends boolean,
EnableBoxes extends boolean,
EnableFps extends boolean,
EnableVideoCodec extends boolean,
+ EnableAudioCodec extends boolean,
> = {
dimensions?: EnableDimensions;
durationInSeconds?: EnableDuration;
boxes?: EnableBoxes;
fps?: EnableFps;
videoCodec?: EnableVideoCodec;
+ audioCodec?: EnableAudioCodec;
};
export type Metadata<
@@ -30,11 +34,13 @@ export type Metadata<
EnableBoxes extends boolean,
EnableFps extends boolean,
EnableVideoCodec extends boolean,
+ EnableAudioCodec extends boolean,
> = (EnableDimensions extends true ? {dimensions: Dimensions} : {}) &
(EnableDuration extends true ? {durationInSeconds: number | null} : {}) &
(EnableBoxes extends true ? {boxes: AnySegment[]} : {}) &
(EnableFps extends true ? {fps: number | null} : {}) &
- (EnableVideoCodec extends true ? {videoCodec: KnownVideoCodecs | null} : {});
+ (EnableVideoCodec extends true ? {videoCodec: KnownVideoCodecs | null} : {}) &
+ (EnableAudioCodec extends true ? {audioCodec: KnownAudioCodecs | null} : {});
export type ParseMedia = <
EnableDimensions extends boolean,
@@ -42,6 +48,7 @@ export type ParseMedia = <
EnableBoxes extends boolean,
EnableFps extends boolean,
EnableVideoCodec extends boolean,
+ EnableAudioCodec extends boolean,
>(
src: string,
options: Options<
@@ -49,7 +56,8 @@ export type ParseMedia = <
EnableDuration,
EnableBoxes,
EnableFps,
- EnableVideoCodec
+ EnableVideoCodec,
+ EnableAudioCodec
>,
readerInterface?: ReaderInterface,
) => Promise<
@@ -58,6 +66,7 @@ export type ParseMedia = <
EnableDuration,
EnableBoxes,
EnableFps,
- EnableVideoCodec
+ EnableVideoCodec,
+ EnableAudioCodec
>
>;
diff --git a/packages/media-parser/src/parse-media.ts b/packages/media-parser/src/parse-media.ts
index c8a862e6fa..2a4241d59b 100644
--- a/packages/media-parser/src/parse-media.ts
+++ b/packages/media-parser/src/parse-media.ts
@@ -1,5 +1,6 @@
import {getArrayBufferIterator} from './buffer-iterator';
import {webReader} from './from-web';
+import {getAudioCodec} from './get-audio-codec';
import {getDimensions} from './get-dimensions';
import {getDuration} from './get-duration';
import {getFps} from './get-fps';
@@ -15,7 +16,7 @@ export const parseMedia: ParseMedia = async (
) => {
const reader = await readerInterface.read(src, null);
- const returnValue = {} as Metadata<true, true, true, true, true>;
+ const returnValue = {} as Metadata<true, true, true, true, true, true>;
const iterator = getArrayBufferIterator(new Uint8Array([]));
let parseResult = parseVideo(iterator);
@@ -54,6 +55,10 @@ export const parseMedia: ParseMedia = async (
returnValue.videoCodec = getVideoCodec(parseResult.segments);
}
+ if (options.audioCodec) {
+ returnValue.audioCodec = getAudioCodec(parseResult.segments);
+ }
+
if (options.boxes) {
returnValue.boxes = parseResult.segments;
}
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index 312ab91d09..2421e7cddc 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -10,12 +10,15 @@ test('Should stream ISO base media', async () => {
durationInSeconds: true,
fps: true,
videoCodec: true,
+ audioCodec: true,
+ boxes: true,
},
nodeReader,
);
expect(result.durationInSeconds).toBe(12.568333333333333);
expect(result.fps).toBe(29.99602174777881);
expect(result.videoCodec).toBe('h265');
+ expect(result.audioCodec).toBe('aac');
});
test('Should stream WebM with no duration', async () => {