music

music

热门

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

6.3万Star
7980Fork
更新于 2026/9/6
SKILL.md
只读
名称
music
描述

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

ElevenLabs 音乐生成

通过文本提示词生成音乐 - 支持纯音乐轨道、带歌词的歌曲,以及通过创作计划进行精细控制。

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

快速开始

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
)

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

JavaScript

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,
});
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}' --output output.mp3

方法列表

方法 描述
music.compose 从提示词或创作计划生成音频
music.composition_plan.create 生成结构化计划以进行精细控制
music.compose_detailed 生成音频 + 创作计划 + 元数据
music.upload 上传音频文件以用于后续的局部重绘 (inpainting) 工作流,并可选提取其创作计划

完整的参数详情请参阅 API 参考手册。

music.upload 仅供拥有局部重绘 (inpainting) 功能访问权限的企业客户使用。

创作计划 (Composition Plans)

为了获得更精细的控制,可以先生成创作计划,进行修改后再进行合成:

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

# 检查/修改风格和乐段
print(plan.positiveGlobalStyles)  # 例如 ["orchestral", "epic", "cinematic"]

audio = client.music.compose(
    composition_plan=plan,
    music_length_ms=60000
)

内容限制

  • 不能提及具体的艺术家、乐队或受版权保护的歌词
  • 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}")

常见错误:401(无效密钥)、422(无效参数)、429(请求频率超限)。

参考资料