Skip to content

Adds Opus codec config support #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/av-cliper/src/combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface ICombinatorOpts {
fps?: number;
bgColor?: string;
videoCodec?: string;
audioCodec?: 'opus' | 'aac';
Copy link
Collaborator

@hughfenghen hughfenghen Jul 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to change the audio field type to

export interface ICombinatorOpts {
  width?: number;
  height?: number;
  bitrate?: number;
  fps?: number;
  bgColor?: string;
  videoCodec?: string;
   /**
   * When specifying a composite video file,
   * setting ⁠`audio: false` will ignore the audio track;
   * if you want to include audio, you can configure the codec and other parameters via ⁠`audio: { codec: 'aac' | 'opus'; opusConfig?: OpusEncoderConfig }`.
   * The default value is `audio: { dodec: ‘aac’ }`
   */
  audio?: false | { codec: 'aac' | 'opus'; opusConfig?: OpusEncoderConfig };
  /**
   * 向输出的视频中写入 meta tags 数据
   */
  metaDataTags?: Record<string, string>;
  /**
   * 不安全,随时可能废弃
   */
  __unsafe_hardwareAcceleration__?: HardwarePreference;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then remove the audioCodec and opusConfig fields

opusConfig?: object;
/**
* false 合成的视频文件中排除音轨
*/
Expand Down Expand Up @@ -66,6 +68,7 @@ export class Combinator {
static async isSupported(
args: {
videoCodec?: string;
audioCodec?: string;
width?: number;
height?: number;
bitrate?: number;
Expand All @@ -90,7 +93,7 @@ export class Combinator {
false) &&
(
await self.AudioEncoder.isConfigSupported({
codec: DEFAULT_AUDIO_CONF.codec,
codec: args.audioCodec ?? DEFAULT_AUDIO_CONF.codec,
sampleRate: DEFAULT_AUDIO_CONF.sampleRate,
numberOfChannels: DEFAULT_AUDIO_CONF.channelCount,
})
Expand Down Expand Up @@ -139,6 +142,8 @@ export class Combinator {
width: 0,
height: 0,
videoCodec: 'avc1.42E032',
audioCodec: 'aac',
opusConfig: {},
audio: true,
bitrate: 5e6,
fps: 30,
Expand Down Expand Up @@ -177,7 +182,7 @@ export class Combinator {
}

#startRecodeMux(duration: number) {
const { fps, width, height, videoCodec, bitrate, audio, metaDataTags } =
const { fps, width, height, videoCodec, audioCodec, opusConfig, bitrate, audio, metaDataTags } =
this.#opts;
const recodeMuxer = recodemux({
video: this.#hasVideoTrack
Expand All @@ -195,7 +200,8 @@ export class Combinator {
audio === false
? null
: {
codec: 'aac',
codec: audioCodec,
opusConfig: opusConfig,
sampleRate: DEFAULT_AUDIO_CONF.sampleRate,
channelCount: DEFAULT_AUDIO_CONF.channelCount,
},
Expand Down
18 changes: 16 additions & 2 deletions packages/internal-utils/src/recodemux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface IRecodeMuxOpts {
*/
audio: {
codec: 'opus' | 'aac';
opusConfig: object;
sampleRate: number;
channelCount: number;
} | null;
Expand Down Expand Up @@ -358,6 +359,16 @@ function createVideoEncoder(
return encoder;
}

//codec mapping
const codecTypeMap = {
'aac': "m4a",
'opus': "Opus"
},
codecMap = {
'aac': "mp4a.40.2",
'opus': "opus"
};

function encodeAudioTrack(
audioOpts: NonNullable<IRecodeMuxOpts['audio']>,
mp4File: MP4File,
Expand All @@ -368,7 +379,8 @@ function encodeAudioTrack(
samplerate: audioOpts.sampleRate,
channel_count: audioOpts.channelCount,
hdlr: 'soun',
type: audioOpts.codec === 'aac' ? 'mp4a' : 'Opus',
//map codec to type
type: codecTypeMap[audioOpts.codec],
name: 'Track created with WebAV',
};

Expand All @@ -385,7 +397,9 @@ function encodeAudioTrack(
});

const encoderConf = {
codec: audioOpts.codec === 'aac' ? 'mp4a.40.2' : 'opus',
//map codec to AudioEncoder codec
codec: codecMap[audioOpts.codec],
opus: audioOpts.opusConfig,
sampleRate: audioOpts.sampleRate,
numberOfChannels: audioOpts.channelCount,
bitrate: 128_000,
Expand Down
Loading