【AI 对话机器人】TTS API王牌——ElevenLabs 入门 & API的尝试

ElevenLabs 简介——业界最自然的 AI 语音

ElevenLabs 成立于 2022 年,由 Piotr Dąbkowski 和 Mati Staniszewski 共同创立。凭借业界最先进的深度学习语音模型,ElevenLabs 迅速成长为 AI 语音领域的标杆企业。2026 年 2 月完成红杉资本领投的 5 亿美元 D 轮融资,估值达到 110 亿美元。

ElevenLabs 的核心产品覆盖文本转语音、声音克隆、AI 配音、语音转文字、音乐生成和对话式 AI 等完整音频创作链路。无论你是内容创作者、开发者还是企业用户,ElevenLabs 都能提供从创意构思到产品落地的全栈语音方案。Disney、NVIDIA、Meta、Salesforce 等全球顶级企业已在使用 ElevenLabs 的技术。

image

ElevenLabs 的 Eleven v3 是目前表现力最强的语音合成引擎。ElevenLabs 不仅能生成准确发音,更能还原人类说话时的情感起伏和呼吸节奏。相比传统 TTS 的机械感,ElevenLabs 合成的声音几乎无法与真人区分。

开发者生态与 API

ElevenLabs 的 API 覆盖全部产品功能,配合 Python 和 TypeScript 官方 SDK,开发者可在数小时内完成集成。移动端提供 Flutter、Swift 和 Kotlin SDK,让 ElevenLabs 的语音能力可以嵌入到任何终端应用中。

模型对比——ElevenLabs 语音模型一览

根据不同场景选择最合适的 ElevenLabs 语音模型。

模型 特点 延迟 适用场景
Eleven v3 表现力最强 标准 有声书、高品质配音
Flash v2.5 超低延迟 ~75ms 实时对话、语音代理
Eleven Turbo 速度与质量平衡 大批量内容生产
Eleven Scribe 98% 准确率 转录、字幕、会议记录

 

Python 代码——Make your first request

Create a new file named example.py or example.mts, depending on your language of choice and add the following code:

Make your first request(文件方式)  

Create a new file named example.py ,  and add the following code:

from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
import os
load_dotenv()
elevenlabs = ElevenLabs(
  api_key=os.getenv("ELEVENLABS_API_KEY"),
)
audio = elevenlabs.text_to_speech.convert(
    text="The first move is what sets everything in motion.",
    voice_id="JBFqnCBsd6RMkjVDRZzb",  # "George" - browse voices at elevenlabs.io/app/voice-library
    model_id="eleven_v3",
    output_format="mp3_44100_128",
)
play(audio)

To convert text to speech and save it as a file, we’ll use the convert method of the ElevenLabs SDK and then it locally as a .mp3 file.


import os
import uuid
from dotenv import load_dotenv
from elevenlabs import VoiceSettings
from elevenlabs.client import ElevenLabs

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
elevenlabs = ElevenLabs(
    api_key=ELEVENLABS_API_KEY,
)


def text_to_speech_file(text: str) -> str:
    # Calling the text_to_speech conversion API with detailed parameters
    response = elevenlabs.text_to_speech.convert(
        voice_id="pNInz6obpgDQGcFmaJgB", # Adam pre-made voice
        output_format="mp3_22050_32",
        text=text,
        model_id="eleven_flash_v2_5", # use the flash model for low latency
        # Optional voice settings that allow you to customize the output
        voice_settings=VoiceSettings(
            stability=0.0,
            similarity_boost=1.0,
            style=0.0,
            use_speaker_boost=True,
            speed=1.0,
        ),
    )

    # uncomment the line below to play the audio back
    # play(response)

    # Generating a unique file name for the output MP3 file
    save_file_path = f"{uuid.uuid4()}.mp3"

    # Writing the audio to a file
    with open(save_file_path, "wb") as f:
        for chunk in response:
            if chunk:
                f.write(chunk)

    print(f"{save_file_path}: A new audio file was saved successfully!")

    # Return the path of the saved audio file
    return save_file_path

Convert text to speech (streaming)——流式方式  

If you prefer to stream the audio directly without saving it to a file, you can use our streaming feature.


import os
from typing import IO
from io import BytesIO
from dotenv import load_dotenv
from elevenlabs import VoiceSettings
from elevenlabs.client import ElevenLabs

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
elevenlabs = ElevenLabs(
    api_key=ELEVENLABS_API_KEY,
)


def text_to_speech_stream(text: str) -> IO[bytes]:
    # Perform the text-to-speech conversion
    response = elevenlabs.text_to_speech.stream(
        voice_id="pNInz6obpgDQGcFmaJgB", # Adam pre-made voice
        output_format="mp3_22050_32",
        text=text,
        model_id="eleven_multilingual_v2",
        # Optional voice settings that allow you to customize the output
        voice_settings=VoiceSettings(
            stability=0.0,
            similarity_boost=1.0,
            style=0.0,
            use_speaker_boost=True,
            speed=1.0,
        ),
    )

    # Create a BytesIO object to hold the audio data in memory
    audio_stream = BytesIO()

    # Write each chunk of audio data to the stream
    for chunk in response:
        if chunk:
            audio_stream.write(chunk)

    # Reset stream position to the beginning
    audio_stream.seek(0)

    # Return the stream for further use
    return audio_stream

You can then run this function with:

text_to_speech_stream("This is James")

 

转载自:

1. elevenlabs 中文官方: https://elevenlabsai.cn/

2. elevenlabs  官网:https://elevenlabs.io/

3. API 入门:https://elevenlabs.io/docs/api-reference/introduction

posted @ 2026-06-10 04:58  FBshark  阅读(62)  评论(0)    收藏  举报