Skip to content

🎥 Make videos programmatically with React

Parse WAVE form extensible

Jonny Burgerae1f3eb277f6
diff --git a/packages/example-videos/src/index.ts b/packages/example-videos/src/index.ts
index 5f180e44d6..a45e8aed19 100644
--- a/packages/example-videos/src/index.ts
+++ b/packages/example-videos/src/index.ts
@@ -130,6 +130,12 @@ export const exampleVideos = {
'videos',
'problematic-fragmented.m4a',
),
+ waveFormatExtensible: path.join(
+ __dirname,
+ '..',
+ 'videos',
+ 'wave-format-extensible.wav',
+ ),
};
export {
diff --git a/packages/example-videos/videos/wave-format-extensible.wav b/packages/example-videos/videos/wave-format-extensible.wav
new file mode 100644
index 0000000000..80529978f4
Binary files /dev/null and b/packages/example-videos/videos/wave-format-extensible.wav differ
diff --git a/packages/media-parser/src/containers/wav/parse-fmt.ts b/packages/media-parser/src/containers/wav/parse-fmt.ts
index 9db1f1f617..dc901e2a73 100644
--- a/packages/media-parser/src/containers/wav/parse-fmt.ts
+++ b/packages/media-parser/src/containers/wav/parse-fmt.ts
@@ -4,6 +4,44 @@ import type {ParserState} from '../../state/parser-state';
import {WEBCODECS_TIMESCALE} from '../../webcodecs-timescale';
import type {WavFmt} from './types';
+const CHANNELS: {[bit: number]: string} = {
+ 0: 'Front Left',
+ 1: 'Front Right',
+ 2: 'Front Center',
+ 3: 'Low Frequency',
+ 4: 'Back Left',
+ 5: 'Back Right',
+ 6: 'Front Left of Center',
+ 7: 'Front Right of Center',
+ 8: 'Back Center',
+ 9: 'Side Left',
+ 10: 'Side Right',
+ 11: 'Top Center',
+ 12: 'Top Front Left',
+ 13: 'Top Front Center',
+ 14: 'Top Front Right',
+ 15: 'Top Back Left',
+ 16: 'Top Back Center',
+ 17: 'Top Back Right',
+ // Add more if needed as per the spec
+};
+
+export function getChannelsFromMask(channelMask: number): string[] {
+ const channels: string[] = [];
+ for (let bit = 0; bit < 18; bit++) {
+ if ((channelMask & (1 << bit)) !== 0) {
+ const channelName = CHANNELS[bit];
+ if (channelName) {
+ channels.push(channelName);
+ } else {
+ channels.push(`Unknown Channel (bit ${bit})`);
+ }
+ }
+ }
+
+ return channels;
+}
+
export const parseFmt = async ({
state,
}: {
@@ -13,7 +51,7 @@ export const parseFmt = async ({
const ckSize = iterator.getUint32Le(); // chunkSize
const box = iterator.startBox(ckSize);
const audioFormat = iterator.getUint16Le();
- if (audioFormat !== 1) {
+ if (audioFormat !== 1 && audioFormat !== 65534) {
throw new Error(
`Only supporting WAVE with PCM audio format, but got ${audioFormat}`,
);
@@ -47,6 +85,40 @@ export const parseFmt = async ({
};
state.structure.getWavStructure().boxes.push(wavHeader);
+
+ if (audioFormat === 65534) {
+ const extraSize = iterator.getUint16Le();
+ if (extraSize !== 22) {
+ throw new Error(
+ `Only supporting WAVE with 22 extra bytes, but got ${extraSize} bytes extra size`,
+ );
+ }
+
+ iterator.getUint16Le(); // valid bits per sample
+ const channelMask = iterator.getUint32Le();
+ const subFormat = iterator.getSlice(16);
+ // check if same as [ 1, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 ]
+ if (subFormat.length !== 16) {
+ throw new Error(
+ `Only supporting WAVE with PCM audio format, but got ${subFormat.length}`,
+ );
+ }
+
+ for (let i = 0; i < 16; i++) {
+ if (
+ subFormat[i] !==
+ [1, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113][i]
+ ) {
+ throw new Error(
+ `Only supporting WAVE with PCM audio format, but got subformat ${subFormat[i]}`,
+ );
+ }
+ }
+
+ const channels = getChannelsFromMask(channelMask);
+ wavHeader.numberOfChannels = channels.length;
+ }
+
await registerAudioTrack({
track: {
type: 'audio',
diff --git a/packages/media-parser/src/containers/wav/parse-junk.ts b/packages/media-parser/src/containers/wav/parse-junk.ts
index 2424164a54..02951f4e25 100644
--- a/packages/media-parser/src/containers/wav/parse-junk.ts
+++ b/packages/media-parser/src/containers/wav/parse-junk.ts
@@ -1,6 +1,5 @@
import {Log} from '../../log';
import type {ParseResult} from '../../parse-result';
-import {makeSkip} from '../../skip';
import type {ParserState} from '../../state/parser-state';
export const parseJunk = ({
@@ -13,6 +12,7 @@ export const parseJunk = ({
Log.trace(state.logLevel, `Skipping JUNK chunk of size ${ckSize}`);
- // Skip over the JUNK chunk data
- return Promise.resolve(makeSkip(iterator.counter.getOffset() + ckSize));
+ iterator.discard(ckSize);
+
+ return Promise.resolve(null);
};
diff --git a/packages/media-parser/src/containers/wav/parse-wav.ts b/packages/media-parser/src/containers/wav/parse-wav.ts
index 4e4faea489..cd9f515981 100644
--- a/packages/media-parser/src/containers/wav/parse-wav.ts
+++ b/packages/media-parser/src/containers/wav/parse-wav.ts
@@ -40,7 +40,7 @@ export const parseWav = (state: ParserState): Promise<ParseResult> => {
return parseId3({state});
}
- if (type === 'JUNK') {
+ if (type === 'JUNK' || type === 'FLLR') {
return parseJunk({state});
}
diff --git a/packages/media-parser/src/test/wav-junk.test.ts b/packages/media-parser/src/test/wav-junk.test.ts
deleted file mode 100644
index 67b950bb53..0000000000
--- a/packages/media-parser/src/test/wav-junk.test.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import {expect, test} from 'bun:test';
-import {parseMedia} from '../parse-media';
-import {nodeReader} from '../readers/from-node';
-
-test('parse WAV with JUNK box type', async () => {
- // Test file from the issue that contains JUNK box
- // Downloaded from: https://remotion-video-submissions.s3.ap-northeast-1.amazonaws.com/f1f10e14-f82d-4a72-87bb-51127091319b
- const testFilePath = '/tmp/junk_test.wav';
-
- // This file has a JUNK chunk and uses WAVE_FORMAT_EXTENSIBLE (65534)
- // The test should fail on the audio format, not on the JUNK chunk
- let caughtError: Error | null = null;
-
- try {
- await parseMedia({
- src: testFilePath,
- reader: nodeReader,
- acknowledgeRemotionLicense: true,
- fields: {
- container: true,
- audioCodec: true,
- tracks: true,
- },
- });
- } catch (error) {
- caughtError = error as Error;
- }
-
- // Should fail on audio format, not JUNK parsing
- expect(caughtError).not.toBeNull();
- expect(caughtError!.message).toContain(
- 'Only supporting WAVE with PCM audio format, but got 65534',
- );
- expect(caughtError!.message).not.toContain('Unknown WAV box type JUNK');
-});
-
-test('should attempt to get all samples from JUNK WAV file', async () => {
- // This test demonstrates that we can parse past the JUNK chunk
- // and would get samples if the audio format was supported
- const testFilePath = '/tmp/junk_test.wav';
-
- let caughtError: Error | null = null;
- let sampleCount = 0;
-
- try {
- await parseMedia({
- src: testFilePath,
- reader: nodeReader,
- acknowledgeRemotionLicense: true,
- fields: {
- container: true,
- slowDurationInSeconds: true,
- },
- onAudioTrack: () => {
- return (_sample) => {
- sampleCount++;
- };
- },
- });
- } catch (error) {
- caughtError = error as Error;
- }
-
- // Should fail on audio format, not JUNK parsing
- expect(caughtError).not.toBeNull();
- expect(caughtError!.message).toContain(
- 'Only supporting WAVE with PCM audio format, but got 65534',
- );
- expect(caughtError!.message).not.toContain('Unknown WAV box type JUNK');
- expect(sampleCount).toBe(0); // No samples because of unsupported format
-});
diff --git a/packages/media-parser/src/test/wave-format-extensible.test.ts b/packages/media-parser/src/test/wave-format-extensible.test.ts
new file mode 100644
index 0000000000..6ffd4277f8
--- /dev/null
+++ b/packages/media-parser/src/test/wave-format-extensible.test.ts
@@ -0,0 +1,25 @@
+import {exampleVideos} from '@remotion/example-videos';
+import {expect, test} from 'bun:test';
+import {parseMedia} from '../parse-media';
+import {nodeReader} from '../readers/from-node';
+
+test('should be able to parse WAVE_FORMAT_EXTENSIBLE', async () => {
+ let sampleCount = 0;
+ await parseMedia({
+ src: exampleVideos.waveFormatExtensible,
+ reader: nodeReader,
+ acknowledgeRemotionLicense: true,
+ fields: {
+ container: true,
+ slowDurationInSeconds: true,
+ },
+ onAudioTrack: ({track}) => {
+ expect(track.numberOfChannels).toBe(2);
+ expect(track.sampleRate).toBe(48000);
+ return () => {
+ sampleCount++;
+ };
+ },
+ });
+ expect(sampleCount).toBe(250);
+});