Use the @mux/ai library to automatically generate chapters for your videos via LLMs
If you're using a player that supports visualizing chapters during playback, like Mux Player does, you can automatically generate chapter markers using AI. The @mux/ai library makes this straightforward by handling all the complexity of fetching transcripts, formatting prompts, and parsing AI responses.
Before starting, make sure you have:
npm install @mux/aiSet your environment variables:
# Required
MUX_TOKEN_ID=your_mux_token_id
MUX_TOKEN_SECRET=your_mux_token_secret
# You only need the API key for the provider you're using
OPENAI_API_KEY=your_openai_api_key # OR
ANTHROPIC_API_KEY=your_anthropic_api_key # OR
GOOGLE_GENERATIVE_AI_API_KEY=your_google_api_keyimport { generateChapters } from "@mux/ai/workflows";
// Generate chapters for a video
const result = await generateChapters("your-mux-asset-id", "en", {
provider: "openai" // or "anthropic" or "google"
});The function returns chapters in the exact format Mux Player expects:
{
"chapters": [
{ "startTime": 0, "title": "Introduction" },
{ "startTime": 15, "title": "Setting Up the Live Stream" },
{ "startTime": 29, "title": "Adding Functionality with HTML and JavaScript" },
{ "startTime": 41, "title": "Identifying Favorite Scene for Clipping" }
]
}Once you have the chapters from your backend, you can add them to Mux Player:
const player = document.querySelector('mux-player');
player.addChapters(result.chapters);@mux/ai supports three AI providers:
gpt-5-mini model - Fast and cost-effectiveclaude-sonnet-4-5 model - Great for nuanced understandinggemini-2.5-flash model - Balance of speed and qualityYou can override the default model:
const result = await generateChapters("your-mux-asset-id", "en", {
provider: "openai",
model: "gpt-4o" // Use a different model
});You can override specific parts of the prompt to tune the output:
const result = await generateChapters("your-mux-asset-id", "en", {
provider: "anthropic",
promptOverrides: {
system: "You are a professional video editor. Create concise, engaging chapter titles.",
instructions: "Generate 5-8 chapters with titles under 50 characters each."
}
});For automated chapter generation when videos are uploaded, you should trigger the call to generate chapters from the video.asset.track.ready webhook:
export async function handleWebhook(req, res) {
const event = req.body;
if (event.type === 'video.asset.track.ready' &&
event.data.type === 'text' &&
event.data.language_code === 'en') {
const result = await generateChapters(event.data.asset_id, "en");
await db.saveChapters(event.data.asset_id, result.chapters);
}
}Once you have chapters, you can display them in Mux Player:
const player = document.querySelector('mux-player');
player.addChapters(result.chapters);Here's an interactive example:
Under the hood, @mux/ai handles:
@mux/ai fetches these automaticallygpt-5-mini is cost-effective for most use cases@mux/ai validates JSON structure, review chapter quality for your use case