🎥 Make videos programmatically with React
`@remotion/media-parser`: `sampleRate` and `numberOfAudioChannels` fields
Jonny Burger2d959dab8c39
diff --git a/.github/workflows/push.yml b/.github/workflows/push.ymlindex 5ed0781e4f..5473555562 100644--- a/.github/workflows/push.yml+++ b/.github/workflows/push.yml@@ -40,6 +40,7 @@ jobs:run: |pnpm build- name: Test Lambda IT+ timeout-minutes: 12run: |pnpm run testlambdassr-tests:diff --git a/packages/docs/docs/media-parser/fast-and-slow.mdx b/packages/docs/docs/media-parser/fast-and-slow.mdxindex 4a33c0e362..3e8c4e5537 100644--- a/packages/docs/docs/media-parser/fast-and-slow.mdx+++ b/packages/docs/docs/media-parser/fast-and-slow.mdx@@ -48,6 +48,8 @@ The following [`fields`](/docs/media-parser#fields) require only the metadata se- [`rotation`](/docs/media-parser/parse-media#rotation)- [`location`](/docs/media-parser/parse-media#location)- [`keyframes`](/docs/media-parser/parse-media#keyframes)+- [`sampleRate`](/docs/media-parser/parse-media#sampleRate)+- [`numberOfAudioChannels`](/docs/media-parser/parse-media#numberofaudiochannels)Also, if an [`onVideoTrack`](/docs/media-parser/parse-media#onvideotrack) or [`onAudioTrack`](/docs/media-parser/parse-media#onvideotrack) handler is passed, only the parsing of the metadata section is required if `null` is returned from the handler function.diff --git a/packages/docs/docs/media-parser/parse-media.mdx b/packages/docs/docs/media-parser/parse-media.mdxindex 97259e6789..0a4c9bfb34 100644--- a/packages/docs/docs/media-parser/parse-media.mdx+++ b/packages/docs/docs/media-parser/parse-media.mdx@@ -63,10 +63,100 @@ If you pass a `File` object, you must also pass [`webFileReader`](/docs/media-pa### `fields?`An object specifying which fields you'd like to receive.-If you like to receive the field, pass `true` as the value.-Possible fields are:+If you like to receive the field, pass `true` as the value.-#### `dimensions`+See [Available Fields](#available-fields) for a list of available fields.++### `reader?`++A reader interface.+Default value: `fetchReader`, which uses `fetch()` to read the video.+If you pass [`nodeReader`](/docs/media-parser/node-reader), you must also pass a local file path as the `src` argument.+If you pass [`webFileReader`](/docs/media-parser/web-file-reader), you must also pass a `File` as the `src` argument.++### `onVideoTrack?`++A callback that is called when a video track is detected.+It receives an object with `track` and `container` (API not yet stable).+You must return either `null` or a callback that is called for each sample that corresponds to the video track.++The `sample` has the type `VideoSample` and while not all fields are stable, it has all the mandatory fields for the [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) constructor.++```tsx twoslash title="Reading video frames"+// @module: es2022+// @target: es2017+import {parseMedia, OnVideoTrack} from '@remotion/media-parser';++const onVideoTrack: OnVideoTrack = ({track}) => {+ console.log(track);++ return (sample) => {+ console.log(new EncodedVideoChunk(sample));+ };+};+```++### `onAudioTrack?`++A callback that is called when an audio track is detected.+It receives an object with `track` and `container` (API not yet stable).+You must return either `null` or a callback that is called for each sample that corresponds to the audio track.++The `sample` has the type `AudioSample` and while not all fields are stable, it has all the mandatory fields for the [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk) constructor.++```tsx twoslash title="Reading audio frames"+// @module: es2022+// @target: es2017+// @noErrors+import {parseMedia, OnAudioTrack} from '@remotion/media-parser';++const onAudioTrack: OnAudioTrack = ({track}) => {+ console.log(track);++ return (sample) => {+ console.log(new EncodedAudioChunk(sample));+ };+};+```++### `onParseProgress?`++A callback that is called from time to time when bytes have been read from the media.+It includes the following data:++```tsx twoslash+import {ParseMediaProgress} from '@remotion/media-parser';+// ^?+```++:::note+You may make this an async function, and while it is not resolved, **the parsing process will be paused**.+This is useful if you want to add a small delay inbetween progress steps to keep the UI interactive.+:::++### `progressIntervalInMs?`++_number_++The interval in milliseconds at which the [`onParseProgress`](#onparseprogress) callback is called.+Default `100`. Set to `0` for unthrottled updates.+Note that updates are fired very often and updating the DOM often may slow down the conversion process.++### `logLevel?`++One of `"error"`, `"warn"`, `"info"`, `"debug"`, `"trace"`.+Default value: `"info"`, which logs only important information.++### `signal?`++An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) instance.+If the signal is aborted, the parser will stop reading the media and stop the decoding process and throw an error.++## Available fields++The following fields are queryable:++### `dimensions`_`{width: number, height: number} | null`_@@ -76,81 +166,81 @@ Use `unrotatedDimensions` to get the dimensions before rotation.If the media passed is an audio file, this will return `null`.-#### `durationInSeconds`+### `durationInSeconds`_number | null_The duration of the video in seconds.Only returns a non-null value if the duration is stored in the metadata.-#### `slowDurationInSeconds`+### `slowDurationInSeconds`_number_The duration of the video in seconds, but it is guaranteed to return a value.If needed, the entire video file is read to determine the duration.-#### `name`+### `name`_string_The name of the file.-#### `container`+### `container`_"mp4" | "webm" | "avi" | "transport-stream" | "mp3" | "aac" | "wav" | "flac"_The container of the file.-#### `size`+### `size`_number | null_The size of the input in bytes.-#### `mimeType`+### `mimeType`_string | null_The MIME type of the file that was returned when the file was fetched.Only available if using the `fetchReader` or `webFileReader`.-#### `structure`+### `structure`The internal structure of the video. Unstable, internal data structure, refer to the TypeScript types to see what's inside.-#### `fps`+### `fps`_number | null_The frame rate of the video.Only returns a non-null value if the frame rate is stored in the metadata.-#### `slowFps`+### `slowFps`_number_The frame rate of the video, but it is guaranteed to return a value.If needed, the entire video file is read to determine the frame rate.-#### `videoCodec`+### `videoCodec`The video codec of the file.If multiple video tracks are present, this will be the first video track.One of `"h264"`, `"h265"`, `"vp8"`, `"vp9"`, `"av1"`, `"prores"` or `null` (in case of an unknown codec).-#### `audioCodec`+### `audioCodec`The audio codec of the file.If multiple audio tracks are present, this will be the first audio track.One of `'aac'`, `'mp3'`, `'aiff'`, `'opus'`, `'pcm'`, `'flac'`, `'unknown'` (audio is there but not recognized) or `null` (in case of no audio detected).-#### `metadata`+### `metadata`Metadata fields such as ID3 tags or EXIF data.See [metadata](/docs/media-parser/tags) for more information.-#### `location`+### `location`The location of the video was shot. Either `null` if not available or:@@ -159,12 +249,12 @@ The location of the video was shot. Either `null` if not available or:- `altitude`: The altitude of the location (can be `null`)- `horizontalAccuracy`: The horizontal accuracy of the location (can be `null`)-#### `tracks`+### `tracks`Returns an object of two two arrays `videoTracks` and `audioTracks`.The data structure of them is not yet stable.-#### `keyframes`+### `keyframes`Return type: <TsType type="MediaParserKeyframe[] | null" source="@remotion/media-parser" />@@ -178,7 +268,7 @@ An array of keyframes. Each keyframe has the following structure:Only being returned if the keyframe information are stored in the metadata, otherwise `null`.-#### `slowKeyframes`+### `slowKeyframes`Return type: <TsType type="MediaParserKeyframe[]" source="@remotion/media-parser" />@@ -186,32 +276,32 @@ An array of keyframes, same as [`keyframes`](#keyframes), but it is guaranteed tWill read the entire video file to determine the keyframes.-#### `slowNumberOfFrames`+### `slowNumberOfFrames`_number_The number of video frames in the media.Will read the entire video file to determine the number of frames.-#### `unrotatedDimensions`+### `unrotatedDimensions`_`{width: number, height: number}`_The dimensions of the video before rotation.-#### `isHdr`+### `isHdr`_`boolean`_Whether the video is in HDR (High dynamic range).-#### `rotation`+### `rotation`_number_The rotation of the video in degrees (e.g. `-90` for a 90° counter-clockwise rotation).-#### `images`+### `images`Return type: <TsType type="MediaParserEmbeddedImage[]" source="@remotion/media-parser" />@@ -222,92 +312,19 @@ Each array element has the following fields:- `description`: A description of the image, or `null`- `data`: The image data as a `Uint8Array`-### `reader?`--A reader interface.-Default value: `fetchReader`, which uses `fetch()` to read the video.-If you pass [`nodeReader`](/docs/media-parser/node-reader), you must also pass a local file path as the `src` argument.-If you pass [`webFileReader`](/docs/media-parser/web-file-reader), you must also pass a `File` as the `src` argument.--### `onVideoTrack?`--A callback that is called when a video track is detected.-It receives an object with `track` and `container` (API not yet stable).-You must return either `null` or a callback that is called for each sample that corresponds to the video track.--The `sample` has the type `VideoSample` and while not all fields are stable, it has all the mandatory fields for the [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) constructor.--```tsx twoslash title="Reading video frames"-// @module: es2022-// @target: es2017-import {parseMedia, OnVideoTrack} from '@remotion/media-parser';--const onVideoTrack: OnVideoTrack = ({track}) => {- console.log(track);-- return (sample) => {- console.log(new EncodedVideoChunk(sample));- };-};-```--### `onAudioTrack?`--A callback that is called when an audio track is detected.-It receives an object with `track` and `container` (API not yet stable).-You must return either `null` or a callback that is called for each sample that corresponds to the audio track.--The `sample` has the type `AudioSample` and while not all fields are stable, it has all the mandatory fields for the [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk) constructor.--```tsx twoslash title="Reading audio frames"-// @module: es2022-// @target: es2017-// @noErrors-import {parseMedia, OnAudioTrack} from '@remotion/media-parser';--const onAudioTrack: OnAudioTrack = ({track}) => {- console.log(track);-- return (sample) => {- console.log(new EncodedAudioChunk(sample));- };-};-```--### `onParseProgress?`--A callback that is called from time to time when bytes have been read from the media.-It includes the following data:--```tsx twoslash-import {ParseMediaProgress} from '@remotion/media-parser';-// ^?-```--:::note-You may make this an async function, and while it is not resolved, **the parsing process will be paused**.-This is useful if you want to add a small delay inbetween progress steps to keep the UI interactive.-:::+### `sampleRate`-### `progressIntervalInMs?`--_number_--The interval in milliseconds at which the [`onParseProgress`](#onparseprogress) callback is called.-Default `100`. Set to `0` for unthrottled updates.-Note that updates are fired very often and updating the DOM often may slow down the conversion process.+_number | null_-### `logLevel?`+The audio sample rate, if there is an audio track.-One of `"error"`, `"warn"`, `"info"`, `"debug"`, `"trace"`.-Default value: `"info"`, which logs only important information.+### `numberOfAudioChannels`-### `signal?`+_number | null_-An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) instance.-If the signal is aborted, the parser will stop reading the media and stop the decoding process and throw an error.+The number of audio channels, if there is an audio track.-### Callbacks+## CallbacksEach field also has a callback that allows you to retrieve the value as soon as it is obtained without waiting for the function to resolve.diff --git a/packages/media-parser/src/boxes/riff/get-duration.ts b/packages/media-parser/src/boxes/riff/get-duration.tsnew file mode 100644index 0000000000..d6e6ea678a--- /dev/null+++ b/packages/media-parser/src/boxes/riff/get-duration.ts@@ -0,0 +1,38 @@+import type {RiffStructure} from './riff-box';+import {getStrhBox, getStrlBoxes} from './traversal';++export 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 getSampleRateFromAvi = (structure: RiffStructure) => {+ const strl = getStrlBoxes(structure);++ for (const s of strl) {+ const strh = getStrhBox(s.children);+ if (!strh) {+ throw new Error('No strh box');+ }++ if (strh.strf.type === 'strf-box-audio') {+ return strh.strf.sampleRate;+ }+ }++ return null;+};diff --git a/packages/media-parser/src/emit-available-info.ts b/packages/media-parser/src/emit-available-info.tsindex 4218751e10..bd51a6e6f2 100644--- a/packages/media-parser/src/emit-available-info.ts+++ b/packages/media-parser/src/emit-available-info.ts@@ -7,6 +7,8 @@ import {getFps} from './get-fps';import {getIsHdr} from './get-is-hdr';import {getKeyframes} from './get-keyframes';import {getLocation} from './get-location';+import {getNumberOfAudioChannels} from './get-number-of-audio-channels';+import {getSampleRate} from './get-sample-rate';import {getTracks} from './get-tracks';import {getVideoCodec} from './get-video-codec';import {getMetadata} from './metadata/get-metadata';@@ -452,6 +454,38 @@ export const emitAvailableInfo = ({continue;}+ if (key === 'sampleRate') {+ if (!emittedFields.sampleRate && hasInfo.sampleRate && parseResult) {+ const sampleRate = getSampleRate(state);+ callbacks.onSampleRate?.(sampleRate);+ if (fieldsInReturnValue.sampleRate) {+ returnValue.sampleRate = sampleRate;+ }++ emittedFields.sampleRate = true;+ }++ continue;+ }++ if (key === 'numberOfAudioChannels') {+ if (+ !emittedFields.numberOfAudioChannels &&+ hasInfo.numberOfAudioChannels &&+ parseResult+ ) {+ const numberOfAudioChannels = getNumberOfAudioChannels(state);+ callbacks.onNumberOfAudioChannels?.(numberOfAudioChannels);+ if (fieldsInReturnValue.numberOfAudioChannels) {+ returnValue.numberOfAudioChannels = numberOfAudioChannels;+ }++ emittedFields.numberOfAudioChannels = true;+ }++ continue;+ }+throw new Error(`Unhandled key: ${key satisfies never}`);}};diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.tsindex 14e85c4d44..4e6e0c633e 100644--- a/packages/media-parser/src/get-duration.ts+++ b/packages/media-parser/src/get-duration.ts@@ -7,8 +7,7 @@ import {getMvhdBox,} from './boxes/iso-base-media/traversal';import {getDurationFromMp3} from './boxes/mp3/get-duration';-import type {RiffStructure} from './boxes/riff/riff-box';-import {getStrhBox, getStrlBoxes} from './boxes/riff/traversal';+import {getDurationFromAvi} from './boxes/riff/get-duration';import {getDurationFromWav} from './boxes/wav/get-duration-from-wav';import type {DurationSegment} from './boxes/webm/segments/all-segments';import {getHasTracks, getTracks} from './get-tracks';@@ -103,25 +102,6 @@ const getDurationFromIsoBaseMedia = (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,diff --git a/packages/media-parser/src/get-fields-from-callbacks.ts b/packages/media-parser/src/get-fields-from-callbacks.tsindex 59e84a2829..c1357f3bbe 100644--- a/packages/media-parser/src/get-fields-from-callbacks.ts+++ b/packages/media-parser/src/get-fields-from-callbacks.ts@@ -36,6 +36,8 @@ export const getFieldsFromCallback = <F extends Options<ParseMediaFields>>({slowNumberOfFrames: Boolean(callbacks.onSlowNumberOfFrames),keyframes: Boolean(callbacks.onKeyframes),images: Boolean(callbacks.onImages),+ numberOfAudioChannels: Boolean(callbacks.onNumberOfAudioChannels),+ sampleRate: Boolean(callbacks.onSampleRate),...fields,};return newFields;diff --git a/packages/media-parser/src/get-number-of-audio-channels.ts b/packages/media-parser/src/get-number-of-audio-channels.tsnew file mode 100644index 0000000000..54b8036019--- /dev/null+++ b/packages/media-parser/src/get-number-of-audio-channels.ts@@ -0,0 +1,13 @@+import type {ParserState} from './state/parser-state';++export const getNumberOfAudioChannels = (state: ParserState) => {+ return (+ state.callbacks.tracks.getTracks().find((track) => {+ return track.type === 'audio';+ })?.numberOfChannels ?? null+ );+};++export const hasNumberOfAudioChannels = (state: ParserState) => {+ return state.callbacks.tracks.hasAllTracks();+};diff --git a/packages/media-parser/src/get-sample-rate.ts b/packages/media-parser/src/get-sample-rate.tsnew file mode 100644index 0000000000..222b6cd2da--- /dev/null+++ b/packages/media-parser/src/get-sample-rate.ts@@ -0,0 +1,13 @@+import type {ParserState} from './state/parser-state';++export const getSampleRate = (state: ParserState) => {+ return (+ state.callbacks.tracks.getTracks().find((track) => {+ return track.type === 'audio';+ })?.sampleRate ?? null+ );+};++export const hasSampleRate = (state: ParserState) => {+ return state.callbacks.tracks.hasAllTracks();+};diff --git a/packages/media-parser/src/has-all-info.ts b/packages/media-parser/src/has-all-info.tsindex ac4c57c66b..85d8a8d900 100644--- a/packages/media-parser/src/has-all-info.ts+++ b/packages/media-parser/src/has-all-info.ts@@ -5,6 +5,8 @@ import {hasDuration, hasSlowDuration} from './get-duration';import {hasFps, hasFpsSuitedForSlowFps} from './get-fps';import {hasHdr} from './get-is-hdr';import {hasKeyframes} from './get-keyframes';+import {hasNumberOfAudioChannels} from './get-number-of-audio-channels';+import {hasSampleRate} from './get-sample-rate';import {getHasTracks} from './get-tracks';import {hasVideoCodec} from './get-video-codec';import {maySkipVideoData} from './may-skip-video-data/may-skip-video-data';@@ -109,6 +111,14 @@ export const getAvailableInfo = ({return false;}+ if (key === 'numberOfAudioChannels') {+ return hasNumberOfAudioChannels(state);+ }++ if (key === 'sampleRate') {+ return hasSampleRate(state);+ }+throw new Error(`Unknown key: ${key satisfies never}`);});diff --git a/packages/media-parser/src/may-skip-video-data/need-samples-for-fields.ts b/packages/media-parser/src/may-skip-video-data/need-samples-for-fields.tsindex c0e7bfaead..d336465ddb 100644--- a/packages/media-parser/src/may-skip-video-data/need-samples-for-fields.ts+++ b/packages/media-parser/src/may-skip-video-data/need-samples-for-fields.ts@@ -24,6 +24,8 @@ const needsSamples: Record<keyof Options<ParseMediaFields>, boolean> = {mimeType: false,keyframes: false,images: false,+ numberOfAudioChannels: false,+ sampleRate: false,};export const needsToIterateOverSamples = ({diff --git a/packages/media-parser/src/options.ts b/packages/media-parser/src/options.tsindex 0c2007ce1f..788df7f1fe 100644--- a/packages/media-parser/src/options.ts+++ b/packages/media-parser/src/options.ts@@ -47,6 +47,8 @@ export type ParseMediaFields = {slowKeyframes: boolean;slowNumberOfFrames: boolean;images: boolean;+ sampleRate: boolean;+ numberOfAudioChannels: boolean;};export type AllParseMediaFields = {@@ -73,6 +75,8 @@ export type AllParseMediaFields = {keyframes: true;slowKeyframes: true;images: true;+ sampleRate: true;+ numberOfAudioChannels: true;};export type AllOptions<Fields extends ParseMediaFields> = {@@ -99,6 +103,8 @@ export type AllOptions<Fields extends ParseMediaFields> = {slowKeyframes: Fields['slowKeyframes'];slowNumberOfFrames: Fields['slowNumberOfFrames'];images: Fields['images'];+ sampleRate: Fields['sampleRate'];+ numberOfAudioChannels: Fields['numberOfAudioChannels'];};export type Options<Fields extends ParseMediaFields> = Partial<@@ -152,6 +158,8 @@ export interface ParseMediaCallbacks {onSlowKeyframes?: (keyframes: MediaParserKeyframe[]) => void;onSlowNumberOfFrames?: (samples: number) => void;onImages?: (images: MediaParserEmbeddedImage[]) => void;+ onSampleRate?: (sampleRate: number | null) => void;+ onNumberOfAudioChannels?: (numberOfChannels: number | null) => void;}export interface ParseMediaData {@@ -178,6 +186,8 @@ export interface ParseMediaData {slowKeyframes: MediaParserKeyframe[];slowNumberOfFrames: number;images: MediaParserEmbeddedImage[];+ sampleRate: number | null;+ numberOfAudioChannels: number | null;}export type ParseMediaResult<T extends Partial<ParseMediaFields>> = {diff --git a/packages/media-parser/src/state/can-skip-tracks.ts b/packages/media-parser/src/state/can-skip-tracks.tsindex e384757cd6..528432ab8d 100644--- a/packages/media-parser/src/state/can-skip-tracks.ts+++ b/packages/media-parser/src/state/can-skip-tracks.ts@@ -34,7 +34,9 @@ export const needsTracksForField = ({field === 'slowKeyframes' ||field === 'slowNumberOfFrames' ||field === 'keyframes' ||- field === 'images'+ field === 'images' ||+ field === 'sampleRate' ||+ field === 'numberOfAudioChannels') {return true;}diff --git a/packages/media-parser/src/state/emitted-fields.ts b/packages/media-parser/src/state/emitted-fields.tsindex 6981ce61f3..7933c4c236 100644--- a/packages/media-parser/src/state/emitted-fields.ts+++ b/packages/media-parser/src/state/emitted-fields.ts@@ -25,6 +25,8 @@ export const emittedState = () => {slowNumberOfFrames: false,keyframes: false,images: false,+ numberOfAudioChannels: false,+ sampleRate: false,};return emittedFields;diff --git a/packages/media-parser/src/test/flac.test.ts b/packages/media-parser/src/test/flac.test.tsindex a5767bf0f8..5cb34afec8 100644--- a/packages/media-parser/src/test/flac.test.ts+++ b/packages/media-parser/src/test/flac.test.ts@@ -29,6 +29,8 @@ test('parse flac', async () => {slowNumberOfFrames,structure,unrotatedDimensions,+ numberOfAudioChannels,+ sampleRate,} = await parseMedia({src: exampleVideos.flac,reader: nodeReader,@@ -56,6 +58,8 @@ test('parse flac', async () => {slowNumberOfFrames: true,structure: true,unrotatedDimensions: true,+ numberOfAudioChannels: true,+ sampleRate: true,},onAudioTrack: () => {return () => {@@ -203,6 +207,8 @@ test('parse flac', async () => {],});expect(unrotatedDimensions).toBe(null);+ expect(sampleRate).toBe(44100);+ expect(numberOfAudioChannels).toBe(2);});test('parse flac minimal seek', async () => {diff --git a/packages/media-parser/src/test/iso-base-media.test.ts b/packages/media-parser/src/test/iso-base-media.test.tsindex cda23b63f8..ae0cb4d020 100644--- a/packages/media-parser/src/test/iso-base-media.test.ts+++ b/packages/media-parser/src/test/iso-base-media.test.ts@@ -21,6 +21,8 @@ if (process.platform !== 'win32') {dimensions: true,rotation: true,unrotatedDimensions: true,+ numberOfAudioChannels: true,+ sampleRate: true,},reader: nodeReader,onVideoTrack: ({track}) => {@@ -52,6 +54,8 @@ if (process.platform !== 'win32') {height: 2160,width: 3840,});+ expect(result.sampleRate).toBe(44100);+ expect(result.numberOfAudioChannels).toBe(2);expect(videoTracks).toBe(1);expect(audioTracks).toBe(1);expect(videoSamples).toBe(377);diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.tsindex 5b2a30faf7..6365a51aa2 100644--- a/packages/media-parser/src/test/matroska.test.ts+++ b/packages/media-parser/src/test/matroska.test.ts@@ -13,6 +13,8 @@ test('Should get duration of AV1 video', async () => {fps: true,slowFps: true,slowNumberOfFrames: true,+ numberOfAudioChannels: true,+ sampleRate: true,},reader: nodeReader,});@@ -25,6 +27,8 @@ test('Should get duration of AV1 video', async () => {width: 1920,height: 1080,});+ expect(parsed.numberOfAudioChannels).toBe(null);+ expect(parsed.sampleRate).toBe(null);expect(parsed.structure.boxes).toEqual([{diff --git a/packages/media-parser/src/test/parse-aac.test.ts b/packages/media-parser/src/test/parse-aac.test.tsindex 5c2ef10b51..8d3b9e4796 100644--- a/packages/media-parser/src/test/parse-aac.test.ts+++ b/packages/media-parser/src/test/parse-aac.test.ts@@ -29,6 +29,8 @@ test('should be able to parse aac', async () => {tracks,unrotatedDimensions,videoCodec,+ numberOfAudioChannels,+ sampleRate,} = await parseMedia({src: exampleVideos.aac,reader: nodeReader,@@ -56,6 +58,8 @@ test('should be able to parse aac', async () => {structure: true,tracks: true,unrotatedDimensions: true,+ numberOfAudioChannels: true,+ sampleRate: true,},onAudioTrack: ({track}) => {expect(track).toEqual({@@ -107,9 +111,11 @@ test('should be able to parse aac', async () => {});expect(tracks.audioTracks.length).toBe(1);expect(tracks.videoTracks.length).toBe(0);+ expect(numberOfAudioChannels).toBe(2);+ expect(sampleRate).toBe(44100);});-test.only('should be able to get basics without parsing all', async () => {+test('should be able to get basics without parsing all', async () => {const audioSamples = 0;const {durationInSeconds,diff --git a/packages/media-parser/src/test/parse-avi-file.test.ts b/packages/media-parser/src/test/parse-avi-file.test.tsindex 7e26e9b789..a578186d51 100644--- a/packages/media-parser/src/test/parse-avi-file.test.ts+++ b/packages/media-parser/src/test/parse-avi-file.test.ts@@ -20,6 +20,8 @@ test('AVI file', async () => {name,rotation,videoCodec,+ numberOfAudioChannels,+ sampleRate,} = await parseMedia({src: exampleVideos.avi,reader: nodeReader,@@ -35,6 +37,8 @@ test('AVI file', async () => {name: true,rotation: true,videoCodec: true,+ sampleRate: true,+ numberOfAudioChannels: true,},onAudioTrack: () => {audioTrackCount++;@@ -297,4 +301,6 @@ test('AVI file', async () => {},],});+ expect(sampleRate).toBe(48000);+ expect(numberOfAudioChannels).toBe(2);});diff --git a/packages/media-parser/src/test/parse-mp3.test.ts b/packages/media-parser/src/test/parse-mp3.test.tsindex 29fd34b2a1..cfa0c6837a 100644--- a/packages/media-parser/src/test/parse-mp3.test.ts+++ b/packages/media-parser/src/test/parse-mp3.test.ts@@ -28,6 +28,8 @@ test('should read MP3 file', async () => {structure,unrotatedDimensions,videoCodec,+ numberOfAudioChannels,+ sampleRate,} = await parseMedia({src: exampleVideos.music,reader: nodeReader,@@ -54,6 +56,8 @@ test('should read MP3 file', async () => {structure: true,unrotatedDimensions: true,videoCodec: true,+ numberOfAudioChannels: true,+ sampleRate: true,},onAudioTrack: () => {let lastSample = -1;@@ -158,6 +162,8 @@ test('should read MP3 file', async () => {expect(slowNumberOfFrames).toBe(0);expect(structure.boxes.length).toEqual(1);expect(unrotatedDimensions).toBe(null);+ expect(numberOfAudioChannels).toBe(2);+ expect(sampleRate).toBe(44100);});test('should read only metadata', async () => {diff --git a/packages/media-parser/src/test/parse-transport-stream-partially.test.ts b/packages/media-parser/src/test/parse-transport-stream-partially.test.tsindex c120677b5b..11b4846566 100644--- a/packages/media-parser/src/test/parse-transport-stream-partially.test.ts+++ b/packages/media-parser/src/test/parse-transport-stream-partially.test.ts@@ -32,6 +32,8 @@ test('Should be able to parse only tracks of Transport Stream', async () => {tracks: true,durationInSeconds: true,fps: true,+ sampleRate: true,+ numberOfAudioChannels: true,},reader: nodeReader,});@@ -41,4 +43,6 @@ test('Should be able to parse only tracks of Transport Stream', async () => {finalCursorOffset: 80652,skippedBytes: 1832812,});+ expect(parsed.sampleRate).toBe(48000);+ expect(parsed.numberOfAudioChannels).toBe(2);});diff --git a/packages/media-parser/src/test/parse-webm-partially.test.ts b/packages/media-parser/src/test/parse-webm-partially.test.tsindex c5dc63b6b7..e24435fb0c 100644--- a/packages/media-parser/src/test/parse-webm-partially.test.ts+++ b/packages/media-parser/src/test/parse-webm-partially.test.ts@@ -22,11 +22,13 @@ test('Parse only header of WebM', async () => {});test('Parse WebM partially', async () => {- const {internalStats} = await parseMedia({+ const {internalStats, numberOfAudioChannels, sampleRate} = await parseMedia({src: exampleVideos.stretchedVp8,fields: {tracks: true,internalStats: true,+ sampleRate: true,+ numberOfAudioChannels: true,},reader: nodeReader,});@@ -35,6 +37,8 @@ test('Parse WebM partially', async () => {finalCursorOffset: 4540,skippedBytes: 13190819,});+ expect(numberOfAudioChannels).toBe(2);+ expect(sampleRate).toBe(44100);});test('Parse WebM fully', async () => {diff --git a/packages/media-parser/src/test/wav.test.ts b/packages/media-parser/src/test/wav.test.tsindex 2dc168bc73..fc46548d02 100644--- a/packages/media-parser/src/test/wav.test.ts+++ b/packages/media-parser/src/test/wav.test.ts@@ -28,6 +28,8 @@ test('parse full wav', async () => {structure,unrotatedDimensions,videoCodec,+ numberOfAudioChannels,+ sampleRate,} = await parseMedia({src: exampleVideos.chirp,reader: nodeReader,@@ -55,6 +57,8 @@ test('parse full wav', async () => {structure: true,unrotatedDimensions: true,videoCodec: true,+ sampleRate: true,+ numberOfAudioChannels: true,},});expect(dimensions).toBe(null);@@ -136,6 +140,8 @@ test('parse full wav', async () => {});expect(unrotatedDimensions).toBe(null);expect(videoCodec).toBe(null);+ expect(numberOfAudioChannels).toBe(1);+ expect(sampleRate).toBe(44100);});test('should be fast to only get duration', async () => {