music
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseElevenLabs Music Generation
ElevenLabs 音乐生成
Generate music from text prompts - supports instrumental tracks, songs with lyrics, and fine-grained control via composition plans.
Setup: See Installation Guide. For JavaScript, usepackages only.@elevenlabs/*
通过文本提示词生成音乐——支持器乐曲目、带歌词的歌曲,以及通过创作计划实现精细化控制。
设置: 请查看安装指南。对于JavaScript,仅使用包。@elevenlabs/*
Quick Start
快速开始
Python
Python
python
from elevenlabs.client 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)python
from elevenlabs.client 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
JavaScript
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"));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
bash
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.mp3bash
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.mp3Methods
方法
| Method | Description |
|---|---|
| Generate audio from a prompt or composition plan |
| Generate a structured plan for fine-grained control |
| Generate audio + composition plan + metadata |
See API Reference for full parameter details.
| 方法 | 描述 |
|---|---|
| 通过提示词或创作计划生成音频 |
| 生成结构化计划以实现精细化控制 |
| 生成音频+创作计划+元数据 |
有关完整参数详情,请查看API参考文档。
Composition Plans
创作计划
For granular control, generate a composition plan first, modify it, then compose:
python
plan = client.music.composition_plan.create(
prompt="An epic orchestral piece building to a climax",
music_length_ms=60000
)如需精细化控制,请先生成创作计划,修改后再进行音乐生成:
python
plan = client.music.composition_plan.create(
prompt="An epic orchestral piece building to a climax",
music_length_ms=60000
)Inspect/modify styles and sections
查看/修改风格和段落
print(plan.positiveGlobalStyles) # e.g. ["orchestral", "epic", "cinematic"]
audio = client.music.compose(
composition_plan=plan,
music_length_ms=60000
)
undefinedprint(plan.positiveGlobalStyles) # 例如:["orchestral", "epic", "cinematic"]
audio = client.music.compose(
composition_plan=plan,
music_length_ms=60000
)
undefinedContent Restrictions
内容限制
- Cannot reference specific artists, bands, or copyrighted lyrics
- errors include a
bad_promptwith alternative phrasingprompt_suggestion - errors include a
bad_composition_plancomposition_plan_suggestion
- 不得提及特定艺术家、乐队或受版权保护的歌词
- 错误会包含
bad_prompt字段,提供替代表述prompt_suggestion - 错误会包含
bad_composition_plan字段composition_plan_suggestion
Error Handling
错误处理
python
try:
audio = client.music.compose(prompt="...", music_length_ms=30000)
except Exception as e:
print(f"API error: {e}")Common errors: 401 (invalid key), 422 (invalid params), 429 (rate limit).
python
try:
audio = client.music.compose(prompt="...", music_length_ms=30000)
except Exception as e:
print(f"API error: {e}")常见错误:401(无效密钥)、422(无效参数)、429(请求频率超限)。
References
参考资料
- Installation Guide
- API Reference
- 安装指南
- API参考文档