Use the @mux/ai library to automatically translate and dub video audio into different languages
While Mux's auto-generated captions can produce transcripts in the original language, you may want to translate and dub the audio itself into different languages. The @mux/ai library integrates with ElevenLabs' dubbing API to automatically create translated audio tracks and add them to your videos as multi-track audio.
Before starting, make sure you have:
npm install @mux/aiSet your environment variables:
# Required for Mux
MUX_TOKEN_ID=your_mux_token_id
MUX_TOKEN_SECRET=your_mux_token_secret
# Required for ElevenLabs dubbing
ELEVENLABS_API_KEY=your_elevenlabs_api_key
# Required for uploading dubbed audio back to Mux
S3_ENDPOINT=https://your-s3-endpoint.com
S3_REGION=auto
S3_BUCKET=your-bucket-name
S3_ACCESS_KEY_ID=your-access-key
S3_SECRET_ACCESS_KEY=your-secret-keyThe @mux/ai library automatically requests audio-only static renditions if they don't already exist on your asset. However, you can pre-emptively enable them when creating videos to avoid waiting for rendition generation during the dubbing workflow.
To enable when creating a video:
import Mux from '@mux/mux-node';
const mux = new Mux();
const asset = await mux.video.assets.create({
input: "https://example.com/video.mp4",
playback_policy: ['public'],
static_renditions: [
{ resolution: 'audio-only' } // Enable audio.m4a rendition
]
});Or add to an existing asset:
await mux.video.assets.createStaticRendition("your-mux-asset-id", {
resolution: 'audio-only'
});import { translateAudio } from "@mux/ai/workflows";
// Dub video audio to Spanish
const result = await translateAudio(
"your-mux-asset-id",
"es" // target language (source language is auto-detected)
);
console.log(result.uploadedTrackId);
// The new Mux audio track ID for the dubbed audio
console.log(result.dubbingId);
// ElevenLabs dubbing ID for tracking
console.log(result.targetLanguageCode); // "es"The function automatically:
The library uses ISO 639-1 language codes. Common target languages include:
await translateAudio("your-mux-asset-id", "es"); // Spanish
await translateAudio("your-mux-asset-id", "fr"); // French
await translateAudio("your-mux-asset-id", "de"); // German
await translateAudio("your-mux-asset-id", "ja"); // Japanese
await translateAudio("your-mux-asset-id", "zh"); // Chinese
await translateAudio("your-mux-asset-id", "pt"); // Portuguese
await translateAudio("your-mux-asset-id", "it"); // Italian
// etc.You can specify the number of speakers for better dubbing quality:
// Auto-detect number of speakers (default)
const result = await translateAudio("your-mux-asset-id", "es", {
numSpeakers: 0
});
// Specify exact number of speakers
const result = await translateAudio("your-mux-asset-id", "es", {
numSpeakers: 2 // For videos with 2 distinct speakers
});If you want to handle the upload yourself or just get the dubbed audio file:
const result = await translateAudio("your-mux-asset-id", "es", {
uploadToMux: false
});
console.log(result.presignedUrl);
// URL to download the dubbed audio file for manual review before uploading to MuxFor automated dubbing when videos are uploaded, you should trigger the call to translate audio from the video.asset.static_rendition.ready webhook:
export async function handleWebhook(req, res) {
const event = req.body;
if (event.type === 'video.asset.static_rendition.ready') {
const result = await translateAudio(event.data.id, "es");
await db.saveDubbedTrack(event.data.id, result.uploadedTrackId);
}
}Mux Player (and most other common video players), automatically detects multiple audio tracks and shows an audio selector. Users can switch between audio languages using the audio menu in the player controls.
Under the hood, @mux/ai handles:
Here's an example of AI-dubbed audio in action: