Skip to content

🎥 Make videos programmatically with React

webcodecs calculate aac config

Jonny Burgerccd769395904
diff --git a/packages/media-parser/src/create/iso-base-media/codec-specific/mp4a.ts b/packages/media-parser/src/create/iso-base-media/codec-specific/mp4a.ts
index 95de184731..550fb5c689 100644
--- a/packages/media-parser/src/create/iso-base-media/codec-specific/mp4a.ts
+++ b/packages/media-parser/src/create/iso-base-media/codec-specific/mp4a.ts
@@ -52,23 +52,7 @@ export const createMp4a = ({
numberTo32BitUIntOrInt(avgBitrate),
// DecoderSpecificInfoTag
new Uint8Array([5]),
- // codec private, for example [17, 144]
- // audioObjectType = 2 = 'AAC LC'
- // samplingFrequencyIndex = 3 = '48000 Hz'
- // channelConfiguration = 2 = '2 channels'
- /**
- * Byte 1 (17): 0001 0001
- ^^^^^ ^^^^
- | |
- | +-- Start of samplingFrequencyIndex (3)
- +-- audioConfigType (2)
-
- Byte 2 (144): 1001 0000
- ^^^^ ^^^^
- | |
- | +-- Remaining bits/padding
- +-- channelConfiguration (2)
- */
+ // see create-aac-codecprivate.ts
addLeading128Size(codecPrivate),
]),
),
diff --git a/packages/media-parser/src/create/iso-base-media/mp4-header.ts b/packages/media-parser/src/create/iso-base-media/mp4-header.ts
index c3c31a5bc7..dab7647a6e 100644
--- a/packages/media-parser/src/create/iso-base-media/mp4-header.ts
+++ b/packages/media-parser/src/create/iso-base-media/mp4-header.ts
@@ -1,3 +1,4 @@
+import {VERSION} from '../../version';
import {createIlst} from './create-ilst';
import {createMoov} from './create-moov';
import {createMvhd} from './create-mvhd';
@@ -50,7 +51,7 @@ export const createPaddedMoovAtom = ({
hdlr: createHdlr('mdir'),
ilst: createIlst([
createToo('WebCodecs'),
- createCmt('Made with @remotion/webcodecs'),
+ createCmt(`Made with @remotion/webcodecs ${VERSION}`),
]),
}),
),
diff --git a/packages/webcodecs/src/create-aac-codecprivate.ts b/packages/webcodecs/src/create-aac-codecprivate.ts
new file mode 100644
index 0000000000..d5d0e33761
--- /dev/null
+++ b/packages/webcodecs/src/create-aac-codecprivate.ts
@@ -0,0 +1,94 @@
+// codec private, for example [17, 144]
+// audioObjectType = 2 = 'AAC LC'
+// samplingFrequencyIndex = 3 = '48000 Hz'
+// channelConfiguration = 2 = '2 channels'
+/**
+ * bytes 17,144: 00010 001 | 1 0010 000
+ ^^^^^ ^^^ ^ ^^^^ ^
+ | | | | padding
+ | | |
+ | | +-- channelConfiguration (2)
+ | +-- samplingFrequencyIndex (3)
+ +-- audioConfigType (2)
+ */
+
+// https://wiki.multimedia.cx/index.php/MPEG-4_Audio#Channel_Configurations
+const getConfigForSampleRate = (sampleRate: number) => {
+ if (sampleRate === 96000) {
+ return 0;
+ }
+
+ if (sampleRate === 88200) {
+ return 1;
+ }
+
+ if (sampleRate === 64000) {
+ return 2;
+ }
+
+ if (sampleRate === 48000) {
+ return 3;
+ }
+
+ if (sampleRate === 44100) {
+ return 4;
+ }
+
+ if (sampleRate === 32000) {
+ return 5;
+ }
+
+ if (sampleRate === 24000) {
+ return 6;
+ }
+
+ if (sampleRate === 22050) {
+ return 7;
+ }
+
+ if (sampleRate === 16000) {
+ return 8;
+ }
+
+ if (sampleRate === 12000) {
+ return 9;
+ }
+
+ if (sampleRate === 11025) {
+ return 10;
+ }
+
+ if (sampleRate === 8000) {
+ return 11;
+ }
+
+ if (sampleRate === 7350) {
+ return 12;
+ }
+
+ throw new Error(`Unexpected sample rate ${sampleRate}`);
+};
+
+export const createAacCodecPrivate = ({
+ audioObjectType,
+ sampleRate,
+ channelConfiguration,
+}: {
+ audioObjectType: number;
+ sampleRate: number;
+ channelConfiguration: number;
+}) => {
+ const bits = `${audioObjectType.toString(2).padStart(5, '0')}${getConfigForSampleRate(sampleRate).toString(2).padStart(4, '0')}${channelConfiguration.toString(2).padStart(4, '0')}000`;
+ if (bits.length !== 16) {
+ throw new Error('Invalid AAC codec private ' + bits.length);
+ }
+
+ if (channelConfiguration === 0 || channelConfiguration > 7) {
+ throw new Error('Invalid channel configuration ' + channelConfiguration);
+ }
+
+ const firstByte = parseInt(bits.slice(0, 8), 2);
+ const secondByte = parseInt(bits.slice(8, 16), 2);
+
+ return new Uint8Array([firstByte, secondByte]);
+};
diff --git a/packages/webcodecs/src/on-audio-track.ts b/packages/webcodecs/src/on-audio-track.ts
index 126ce36598..33332cd1bf 100644
--- a/packages/webcodecs/src/on-audio-track.ts
+++ b/packages/webcodecs/src/on-audio-track.ts
@@ -9,6 +9,7 @@ import {getAudioDecoderConfig} from './audio-decoder-config';
import {createAudioEncoder} from './audio-encoder';
import {getAudioEncoderConfig} from './audio-encoder-config';
import {convertEncodedChunk} from './convert-encoded-chunk';
+import {createAacCodecPrivate} from './create-aac-codecprivate';
import {defaultOnAudioTrackHandler} from './default-on-audio-track-handler';
import Error from './error-cause';
import type {ConvertMediaAudioCodec} from './get-available-audio-codecs';
@@ -123,7 +124,13 @@ export const makeAudioTrackHandler =
}
const codecPrivate =
- audioOperation.audioCodec === 'aac' ? new Uint8Array([17, 144]) : null;
+ audioOperation.audioCodec === 'aac'
+ ? createAacCodecPrivate({
+ audioObjectType: 2,
+ sampleRate: track.sampleRate,
+ channelConfiguration: track.numberOfChannels,
+ })
+ : null;
const {trackNumber} = await state.addTrack({
type: 'audio',
diff --git a/packages/webcodecs/src/test/aac-codecprivate.test.ts b/packages/webcodecs/src/test/aac-codecprivate.test.ts
new file mode 100644
index 0000000000..71a03e1a61
--- /dev/null
+++ b/packages/webcodecs/src/test/aac-codecprivate.test.ts
@@ -0,0 +1,11 @@
+import {expect, test} from 'bun:test';
+import {createAacCodecPrivate} from '../create-aac-codecprivate';
+
+test('AACCodecPrivate', () => {
+ const codecPrivate = createAacCodecPrivate({
+ audioObjectType: 2,
+ sampleRate: 48000,
+ channelConfiguration: 2,
+ });
+ expect(codecPrivate).toEqual(new Uint8Array([17, 144]));
+});