Skip to content

Commit a2ee2ad

Browse files
committed
add opus codec config support
add custom opus codec support
1 parent a2de4ed commit a2ee2ad

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

packages/av-cliper/src/combinator.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export interface ICombinatorOpts {
1010
fps?: number;
1111
bgColor?: string;
1212
videoCodec?: string;
13+
audioCodec?: string;
14+
opusConfig?: object;
1315
/**
1416
* false 合成的视频文件中排除音轨
1517
*/
@@ -66,6 +68,7 @@ export class Combinator {
6668
static async isSupported(
6769
args: {
6870
videoCodec?: string;
71+
audioCodec?: string;
6972
width?: number;
7073
height?: number;
7174
bitrate?: number;
@@ -88,13 +91,20 @@ export class Combinator {
8891
})
8992
).supported ??
9093
false) &&
91-
(
94+
((
9295
await self.AudioEncoder.isConfigSupported({
9396
codec: DEFAULT_AUDIO_CONF.codec,
9497
sampleRate: DEFAULT_AUDIO_CONF.sampleRate,
9598
numberOfChannels: DEFAULT_AUDIO_CONF.channelCount,
9699
})
97-
).supported) ??
100+
).supported) ||
101+
(
102+
await self.AudioEncoder.isConfigSupported({
103+
codec: args.audioCodec,
104+
sampleRate: DEFAULT_AUDIO_CONF.sampleRate,
105+
numberOfChannels: DEFAULT_AUDIO_CONF.channelCount,
106+
})
107+
).supported)) ??
98108
false
99109
);
100110
}
@@ -139,6 +149,8 @@ export class Combinator {
139149
width: 0,
140150
height: 0,
141151
videoCodec: 'avc1.42E032',
152+
audioCodec: 'aac',
153+
opusConfig: {},
142154
audio: true,
143155
bitrate: 5e6,
144156
fps: 30,
@@ -177,7 +189,7 @@ export class Combinator {
177189
}
178190

179191
#startRecodeMux(duration: number) {
180-
const { fps, width, height, videoCodec, bitrate, audio, metaDataTags } =
192+
const { fps, width, height, videoCodec, audioCodec, opusConfig, bitrate, audio, metaDataTags } =
181193
this.#opts;
182194
const recodeMuxer = recodemux({
183195
video: this.#hasVideoTrack
@@ -195,7 +207,8 @@ export class Combinator {
195207
audio === false
196208
? null
197209
: {
198-
codec: 'aac',
210+
codec: audioCodec,
211+
opusConfig: opusConfig,
199212
sampleRate: DEFAULT_AUDIO_CONF.sampleRate,
200213
channelCount: DEFAULT_AUDIO_CONF.channelCount,
201214
},

packages/internal-utils/src/recodemux.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface IRecodeMuxOpts {
2929
*/
3030
audio: {
3131
codec: 'opus' | 'aac';
32+
opusConfig: object;
3233
sampleRate: number;
3334
channelCount: number;
3435
} | null;
@@ -358,6 +359,16 @@ function createVideoEncoder(
358359
return encoder;
359360
}
360361

362+
//codec mapping
363+
const codecTypeMap = {
364+
'aac': "m4a",
365+
'opus': "Opus"
366+
},
367+
codecMap = {
368+
'aac': "mp4a.40.2",
369+
'opus': "opus"
370+
};
371+
361372
function encodeAudioTrack(
362373
audioOpts: NonNullable<IRecodeMuxOpts['audio']>,
363374
mp4File: MP4File,
@@ -368,7 +379,8 @@ function encodeAudioTrack(
368379
samplerate: audioOpts.sampleRate,
369380
channel_count: audioOpts.channelCount,
370381
hdlr: 'soun',
371-
type: audioOpts.codec === 'aac' ? 'mp4a' : 'Opus',
382+
//map codec to type
383+
type: codecTypeMap[audioOpts.codec],
372384
name: 'Track created with WebAV',
373385
};
374386

@@ -385,7 +397,9 @@ function encodeAudioTrack(
385397
});
386398

387399
const encoderConf = {
388-
codec: audioOpts.codec === 'aac' ? 'mp4a.40.2' : 'opus',
400+
//map codec to AudioEncoder codec
401+
codec: codecMap[audioOpts.codec],
402+
opus: audioOpts.opusConfig,
389403
sampleRate: audioOpts.sampleRate,
390404
numberOfChannels: audioOpts.channelCount,
391405
bitrate: 128_000,

0 commit comments

Comments
 (0)