music

music

热门

使用 ElevenLabs 音乐 API 生成音乐。适用于创作器乐、带歌词的歌曲、背景音乐、广告曲或任何 AI 生成的音乐作品。支持基于提示的生成、用于精细控制的作曲计划以及包含元数据的详细输出。

385Star
51Fork
更新于 2026/7/18
SKILL.md
只读
名称
music
描述

使用 ElevenLabs 音乐 API 生成音乐。适用于创作器乐、带歌词的歌曲、背景音乐、广告曲或任何 AI 生成的音乐作品。支持基于提示的生成、用于精细控制的作曲计划以及包含元数据的详细输出。

ElevenLabs 音乐生成

根据文本提示生成音乐——支持器乐、带歌词的歌曲以及通过作曲计划进行精细控制。

设置: 参见安装指南。对于 JavaScript,仅使用 @elevenlabs/* 包。

以下所有示例默认使用 music_v2,即当前生成模型。仅在明确要求时传递 model_id="music_v1"

快速开始

Python

from elevenlabs import ElevenLabs

client = ElevenLabs()

audio = client.music.compose(
    prompt="A chill lo-fi hip hop beat with jazzy piano chords",
    music_length_ms=30000,
    model_id="music_v2",
)

with open("output.mp3", "wb") as f:
    for chunk in audio:
        f.write(chunk)

TypeScript

import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";
import { createWriteStream } from "fs";

const client = new ElevenLabsClient();
const audio = await client.music.compose({
  prompt: "A chill lo-fi hip hop beat with jazzy piano chords",
  musicLengthMs: 30000,
  modelId: "music_v2",
});
audio.pipe(createWriteStream("output.mp3"));

cURL

curl -X POST "https://api.elevenlabs.io/v1/music" \
  -H "xi-api-key: $ELEVENLABS_API_KEY" -H "Content-Type: application/json" \
  -d '{"prompt": "A chill lo-fi beat", "music_length_ms": 30000, "model_id": "music_v2"}' \
  --output output.mp3

方法

方法 描述
music.compose 根据提示或作曲计划生成音频
music.stream 在生成时流式传输音频块(付费计划)
music.composition_plan.create 生成结构化计划以实现精细控制
music.compose_detailed 生成音频 + 作曲计划 + 元数据;传递 store_for_inpainting=True 以启用修补
music.compose_detailed_stream 以服务器发送事件的形式流式传输音频以及作曲计划、元数据和可选的字幕时间戳
music.video_to_music 根据一个或多个上传的视频文件生成背景音乐
music.upload 上传音频文件以供后续修补工作流使用,可选提取其作曲计划或词级时间戳

有关完整参数详情,请参见 API 参考

music.upload 仅对有权使用修补功能的企业客户可用。

视频转音乐

通过 POST /v1/music/video-to-music (client.music.video_to_music) 从上传的视频片段生成背景音乐。这与基于提示的 music.compose (POST /v1/music) 是分开的。

该 API 按顺序合并视频,接受可选的自然语言描述,并允许您使用最多 10 个标签(例如 upbeatcinematic)来引导风格。此端点仍默认使用 music_v1;传递 model_id="music_v2" 以使用较新的模型。

Python

from elevenlabs import ElevenLabs

client = ElevenLabs()

audio = client.music.video_to_music(
    videos=["trailer.mp4"],
    description="Build suspense, then resolve with a warm cinematic finish.",
    tags=["cinematic", "suspenseful", "uplifting"],
    model_id="music_v2",
)

with open("video-score.mp3", "wb") as f:
    for chunk in audio:
        f.write(chunk)

TypeScript

import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";
import { createReadStream, createWriteStream } from "fs";

const client = new ElevenLabsClient();

const audio = await client.music.videoToMusic({
  videos: [createReadStream("trailer.mp4")],
  description: "Build suspense, then resolve with a warm cinematic finish.",
  tags: ["cinematic", "suspenseful", "uplifting"],
  modelId: "music_v2",
});

audio.pipe(createWriteStream("video-score.mp3"));

cURL

curl -X POST "https://api.elevenlabs.io/v1/music/video-to-music" \
  -H "xi-api-key: $ELEVENLABS_API_KEY" \
  -F "videos=@trailer.mp4" \
  -F "description=Build suspense, then resolve with a warm cinematic finish." \
  -F "tags=cinematic" \
  -F "tags=suspenseful" \
  -F "tags=uplifting" \
  -F "model_id=music_v2" \
  --output video-score.mp3

当前 API 模式的约束:

  • 每个请求上传 1-10 个视频文件
  • 保持总合并上传大小不超过 200 MB
  • 保持总合并视频时长不超过 600 秒
  • 使用 description 进行高级音乐方向指导,使用 tags 提供简洁的风格提示

作曲计划

music_v2 作曲计划是一个有序的 chunks 列表。每个块指定自己的 text(段落标签、歌词、内联提示)、duration_mspositive_stylesnegative_stylescontext_adherencelowmediumhigh,默认为 high)。每个计划最多 30 个块,每个块 3,000–120,000 毫秒,总长度 3 秒到 10 分钟。

先生成计划,编辑它,然后作曲:

plan = client.music.composition_plan.create(
    prompt="An epic orchestral piece building to a climax",
    music_length_ms=60000,
    model_id="music_v2",
)

# 原地编辑块
plan["chunks"][0]["text"] = "[Intro]\nQuiet strings rising"

audio = client.music.compose(
    composition_plan=plan,
    model_id="music_v2",
)
const plan = await client.music.compositionPlan.create({
  prompt: "An epic orchestral piece building to a climax",
  musicLengthMs: 60000,
  modelId: "music_v2",
});

plan.chunks[0].text = "[Intro]\nQuiet strings rising";

const audio = await client.music.compose({
  compositionPlan: plan,
  modelId: "music_v2",
});

或者手动构建计划以控制每个段落的歌词和风格:

composition_plan = {
    "chunks": [
        {
            "text": "[Verse]\nWalking down an empty street",
            "duration_ms": 15000,
            "positive_styles": ["pop", "upbeat", "female vocals", "acoustic guitar"],
            "negative_styles": ["dark", "slow"],
            "context_adherence": "high",
        },
        {
            "text": "[Chorus]\nThis is my moment",
            "duration_ms": 15000,
            "positive_styles": ["powerful vocals", "full band"],
            "negative_styles": [],
            "context_adherence": "high",
        },
    ]
}

audio = client.music.compose(composition_plan=composition_plan, model_id="music_v2")
const compositionPlan = {
  chunks: [
    {
      text: "[Verse]\nWalking down an empty street",
      durationMs: 15000,
      positiveStyles: ["pop", "upbeat", "female vocals", "acoustic guitar"],
      negativeStyles: ["dark", "slow"],
      contextAdherence: "high",
    },
    {
      text: "[Chorus]\nThis is my moment",
      durationMs: 15000,
      positiveStyles: ["powerful vocals", "full band"],
      negativeStyles: [],
      contextAdherence: "high",
    },
  ],
};

const audio = await client.music.compose({
  compositionPlan,
  modelId: "music_v2",
});

将更广泛的特征(流派、乐器、人声风格)放在 positive_styles 中,而不是 text 中。第一个块的风格设定整体基调——在那里包含 6-7 个风格。

输出格式

在作曲、详细作曲或流式请求中使用 output_format 查询参数来选择生成的音频格式。auto 选择适合模型的 MP3 格式;对于 music_v2,它选择 mp3_48000_192。更高比特率的 MP3 选项包括 mp3_48000_240mp3_48000_320

流式传输

对于付费计划,在生成时流式传输音频块,而不是等待完整文件:

from io import BytesIO

stream = client.music.stream(
    prompt="A driving synthwave track with arpeggiated leads",
    music_length_ms=30000,
    model_id="music_v2",
)

buffer = BytesIO()
for chunk in stream:
    if chunk:
        buffer.write(chunk)
const stream = await client.music.stream({
  prompt: "A driving synthwave track with arpeggiated leads",
  musicLengthMs: 30000,
  modelId: "music_v2",
});

const chunks: Buffer[] = [];
for await (const chunk of stream) {
  chunks.push(chunk);
}

详细流式传输

当应用程序需要在音频仍在到达时获取生成的音乐元数据时,使用详细流式传输。POST /v1/music/detailed/stream 接受与详细作曲相同的提示或作曲计划主体,流式传输 text/event-stream,并且可以通过 with_timestamps 包含字幕时间戳。

curl -N -X POST "https://api.elevenlabs.io/v1/music/detailed/stream?output_format=auto" \
  -H "xi-api-key: $ELEVENLABS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "A bright indie pop hook with warm guitars", "music_length_ms": 30000, "model_id": "music_v2", "with_timestamps": true}'

修补

修补通过将音频参考块(存储歌曲的未更改片段)与新的生成块混合在单个作曲计划中,来编辑或扩展存储的歌曲。

第 1 步——获取 song_id,可以通过存储新生成或上传现有音频:

# 选项 A:保留生成以供以后编辑
result = client.music.compose_detailed(
    prompt="An upbeat pop song with verse and chorus",
    music_length_ms=60000,
    model_id="music_v2",
    store_for_inpainting=True,
)
song_id = result.song_id

# 选项 B:上传现有曲目并提取其计划
uploaded = client.music.upload(
    file=open("my-song.mp3", "rb"),
    extract_composition_plan="music_v2",
)
song_id = uploaded.song_id
composition_plan = uploaded.composition_plan
import { createReadStream } from "fs";

// 选项 A:保留生成以供以后编辑
const result = await client.music.composeDetailed({
  prompt: "An upbeat pop song with verse and chorus",
  musicLengthMs: 60000,
  modelId: "music_v2",
  storeForInpainting: true,
});
let songId = result.songId;

// 选项 B:上传现有曲目并提取其计划
const uploaded = await client.music.upload({
  file: createReadStream("my-song.mp3"),
  extractCompositionPlan: "music_v2",
});
songId = uploaded.songId;
const compositionPlan = uploaded.compositionPlan;

第 2 步——编写一个引用存储音频并重新生成您想要更改部分的计划:

plan = {
    "chunks": [
        {"song_id": song_id, "range": {"start_ms": 0, "end_ms": 30000}},
        {
            "text": "[Chorus]\nWe're rising up tonight",
            "duration_ms": 30000,
            "positive_styles": ["bigger drums", "layered vocals", "anthemic"],
            "negative_styles": ["sparse"],
            "context_adherence": "high",
        },
    ]
}

audio = client.music.compose(composition_plan=plan, model_id="music_v2")
const plan = {
  chunks: [
    { songId, range: { startMs: 0, endMs: 30000 } },
    {
      text: "[Chorus]\nWe're rising up tonight",
      durationMs: 30000,
      positiveStyles: ["bigger drums", "layered vocals", "anthemic"],
      negativeStyles: ["sparse"],
      contextAdherence: "high",
    },
  ],
};

const audio = await client.music.compose({
  compositionPlan: plan,
  modelId: "music_v2",
});

为了匹配存储片段的感觉而不复制它,将 conditioning_ref(最多 30,000 毫秒)加上 condition_strengthlowmediumhighxhigh)附加到生成块。放置在第一个块上的条件会影响后续每个块。

有关完整修补参数列表,请参见 API 参考

内容限制

  • 不能引用特定艺术家、乐队或受版权保护的歌词
  • bad_prompt 错误包含带有替代措辞的 prompt_suggestion
  • bad_composition_plan 错误包含 composition_plan_suggestion

错误处理

try:
    audio = client.music.compose(prompt="...", music_length_ms=30000)
except Exception as e:
    print(f"API error: {e}")
try {
  const audio = await client.music.compose({
    prompt: "...",
    musicLengthMs: 30000,
  });
} catch (err) {
  console.error("API error:", err);
}

常见错误:401(密钥无效)、422(参数无效)、429(速率限制)。

参考