SuperAPI AI 工具集

elevenlabs

设计与创作

查看项目

更新于 10-27 12:33

技能介绍

用途

本技能通过 ElevenLabs API 提供 AI 音频生成能力,适用于以下场景:将文本转换为语音、为视频或演示创建配音、生成特定音色或语言的音频、创建音效(如脚步声、爆炸声)、根据描述创作音乐、构建多说话人对话、从音频样本克隆声音、在实时应用中流式传输音频,以及制作有声书、播客等音频内容。

主要能力

1. 文本转语音

可用模型:

  • Eleven Multilingual v2(eleven_multilingual_v2):最高质量,支持 29 种语言
  • Eleven Flash v2.5(eleven_flash_v2_5):超低 75ms 延迟,支持 32 种语言,成本低 50%
  • Eleven Turbo v2.5(eleven_turbo_v2_5):质量与延迟平衡

功能特性:100+ 预设专业音色、从音频样本自定义声音克隆、多说话人对话生成、实时音频流式传输、32 种语言支持、情感化自然语调、语音设置自定义(稳定性、相似度、风格)。

输出格式:MP3(32kbps 至 192kbps 多种比特率)、PCM(8kHz 至 48kHz)、Opus、µ-law、A-law。

2. 音效生成

使用模型:Eleven Text-to-Sound v2(eleven_text_to_sound_v2)

功能特性:根据文本描述生成音效、可自定义时长、支持无缝循环、提示词影响度控制、适用于游戏/视频/UI 的高质量音频。

典型用途:游戏音频(脚步声、爆炸、环境音)、视频制作音效、UI/UX 声音设计、自然音(雨、风、海浪)、机械音(门、引擎、机器)、奇幻/科幻效果。

3. 音乐生成

功能特性:文本转音乐创作、人声与器乐曲目、多种流派与风格、可自定义曲目时长、作曲计划(结构化音乐蓝图)、免版税生成音乐。

参数:描述目标音乐的文本提示词、时长控制(毫秒)、流派/风格/情绪指定、段落级作曲控制。

要求:需付费 ElevenLabs 账户(音乐 API 不适用于免费层)。

内容政策:禁止版权材料(艺术家名、乐队名、商标),对受限提示词会返回建议。

使用方式

环境准备

API 密钥从 https://elevenlabs.io/app/settings/api-keys 获取,设置为环境变量:

export ELEVENLABS_API_KEY="your-api-key-here"

安装 Python 包:

pip install elevenlabs pydub python-dotenv

系统要求:Python 3.8+、网络连接、可选音频播放库、ffmpeg(pydub 音频处理所需)。

初始化客户端

import os from elevenlabs.client import ElevenLabs

client = ElevenLabs(api_key=os.environ.get("ELEVENLABS_API_KEY"))

文本转语音示例

from elevenlabs.client import ElevenLabs from pathlib import Path

client = ElevenLabs(api_key=os.environ["ELEVENLABS_API_KEY"])

audio = client.text_to_speech.convert( text="Your text content here", voice_id="JBFqnCBsd6RMkjVDRZzb", model_id="eleven_multilingual_v2", output_format="mp3_44100_128" )

output_path = Path("speech_output.mp3") with output_path.open("wb") as f: for chunk in audio: f.write(chunk)

流式传输(实时):

from elevenlabs import stream

audio_stream = client.text_to_speech.convert_as_stream( text="This will be streamed as it generates", voice_id="JBFqnCBsd6RMkjVDRZzb", model_id="eleven_flash_v2_5", output_format="mp3_44100_128" )

stream(audio_stream)

多说话人对话:为每位说话人分别生成音频,再用 pydub 的 AudioSegment 合并导出。

列出可用音色:

voices = client.voices.get_all() for voice in voices.voices: print(f"- {voice.name} (ID: {voice.voice_id})")

常用音色 ID:

  • JBFqnCBsd6RMkjVDRZzb - George(男声,英语,中年)
  • 21m00Tcm4TlvDq8ikWAM - Rachel(女声,英语,年轻)
  • AZnzlk1XvdvUeBnXmlld - Domi(女声,英语,年轻)
  • EXAVITQu4vr4xnSDxMaL - Bella(女声,英语,年轻)
  • ErXwobaYiN019PkySvjV - Antoni(男声,英语,年轻)
  • MF3mGyEYCl7XYWbV9V6O - Elli(女声,英语,年轻)
  • TxGEqnHWrfWFTfGW9XjX - Josh(男声,英语,年轻)

音效生成示例

audio = client.text_to_sound_effects.convert( text="footsteps on wooden floor, slow paced walking", duration_seconds=5.0, prompt_influence=0.5 )

output_path = Path("footsteps.mp3") with output_path.open("wb") as f: for chunk in audio: f.write(chunk)

音乐生成示例

prompt = """Upbeat indie pop song with acoustic guitar, light drums, and cheerful melody. Modern and energetic feel, perfect for background music in a lifestyle video. Instrumental only, no vocals."""

try: audio = client.music_generation.compose( prompt=prompt, music_length_ms=30000 )

output_path = Path("background_music.mp3")
with output_path.open("wb") as f:
    for chunk in audio:
        f.write(chunk)

except Exception as e: if "paid" in str(e).lower() or "subscription" in str(e).lower(): print("Error: Music generation requires a paid ElevenLabs account") else: print(f"Error: {e}")

使用作曲计划:

composition_plan = client.music_generation.composition_plan.create( prompt="""Electronic dance music track with energetic build-up, drop section, and chill outro. Progressive house style.""", music_length_ms=60000 )

audio = client.music_generation.compose( composition_plan=composition_plan )

错误处理

建议始终用 try/except 包裹 API 调用,针对以下情况给出提示:API 密钥无效或缺失、配额超限、需要付费订阅、提示词含受限内容。

适用场景

  • 视频配音、演示旁白、内容解说
  • 有声书、播客制作
  • 游戏音效与背景音乐
  • UI/UX 声音设计
  • 多说话人对话与角色配音
  • 实时语音应用

限制

  • 音乐生成需付费账户,免费层不可用
  • 音乐提示词禁止包含版权材料(艺术家名、乐队名、商标)
  • 免费层与付费层配额不同,付费层配额更高
  • 需网络连接访问 API
  • pydub 音频处理需安装 ffmpeg
  • 具体价格、各层配额数值、音效循环参数支持情况等细节暂未提供