> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# 文本转语音快速入门

本指南将介绍如何使用文本转语音 API 将语音音频转换为文本。

> **Tip**
>
> 使用 [ElevenLabs speech-to-text skill](https://github.com/elevenlabs/skills/tree/main/speech-to-text)，通过 AI 编程助手转录音频：
>
> ```bash
> npx skills add elevenlabs/skills --skill speech-to-text
> ```

> **Info**
>
> 本教程将演示如何使用批量文本转语音 API。如需了解如何使用实时文本转语音 API，请参阅[客户端 流式传输](/docs/zh/eleven-api/guides/how-to/speech-to-text/realtime/client-side-streaming)或[服务端 流式传输](/docs/zh/eleven-api/guides/how-to/speech-to-text/realtime/server-side-streaming)指南。

## 使用文本转语音 API

#### 创建 API 密钥

[在控制台中创建 API 密钥](https://elevenlabs.io/app/api/api-keys)，用于安全地[访问 API](/docs/zh/api-reference/authentication)。

将密钥作为受管理的机密信息存储，并根据偏好，通过 `.env` 文件将其作为环境变量传递给 SDK，或直接写入应用配置。

**`.env`**

```js title=".env"
ELEVENLABS_API_KEY=<your_api_key_here>
```

#### 安装 SDK

#### SDK

我们还会使用 `dotenv` 库从环境变量加载 API 密钥。

```python
pip install elevenlabs
pip install python-dotenv
```

```typescript
npm install @elevenlabs/elevenlabs-js
npm install dotenv
```

#### CLI

安装 ElevenLabs CLI。推荐使用 Homebrew（macOS）和 Scoop（Windows）。

**`Homebrew（macOS）`**

```bash title="Homebrew（macOS）"
brew install elevenlabs/tap/elevenlabs
```

**`Scoop（Windows）`**

```powershell title="Scoop（Windows）"
scoop bucket add elevenlabs https://github.com/elevenlabs/scoop-bucket
scoop install elevenlabs
```

**`npm`**

```bash title="npm"
npm install -g @elevenlabs/cli
```

**`curl`**

```bash title="curl"
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/elevenlabs/cli/releases/latest/download/elevenlabs-cli-installer.sh | sh
```

> **Tip**
>
> 正在使用 AI 编程助手？在项目中运行 `elevenlabs generate-skills`，即可将每个命令组的
> `SKILL.md` 写入 `skills/`，无需粘贴文档，助手就能了解 CLI 的完整功能。使用 `--output-dir`
> 可将其放到其他位置。该命令会读取 CLI 内置的 API 定义，因此无需 API 密钥且可离线运行——还能始终与已安装的 CLI 版本保持同步。

然后进行身份验证——这会打开浏览器以授权 CLI：

```bash
elevenlabs auth login
```

#### 发起 API 请求

#### SDK

根据所选语言，新建名为 `example.py` 或 `example.mts` 的文件，然后添加以下代码：

```python maxLines=0
# example.py
import os
from dotenv import load_dotenv
from io import BytesIO
import requests
from elevenlabs.client import ElevenLabs

load_dotenv()

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

audio_url = (
    "https://storage.googleapis.com/eleven-public-cdn/audio/marketing/nicole.mp3"
)
response = requests.get(audio_url)
audio_data = BytesIO(response.content)

transcription = elevenlabs.speech_to_text.convert(
    file=audio_data,
    model_id="scribe_v2", # Model to use
    tag_audio_events=True, # Tag audio events like laughter, applause, etc.
    language_code="eng", # Language of the audio file. If set to None, the model will detect the language automatically.
    diarize=True, # Whether to annotate who is speaking
)

print(transcription)
```

```typescript maxLines=0
// example.mts
import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";
import "dotenv/config";

const elevenlabs = new ElevenLabsClient();

const response = await fetch(
  "https://storage.googleapis.com/eleven-public-cdn/audio/marketing/nicole.mp3"
);
const audioBlob = new Blob([await response.arrayBuffer()], { type: "audio/mp3" });

const transcription = await elevenlabs.speechToText.convert({
  file: audioBlob,
  modelId: "scribe_v2", // Model to use
  tagAudioEvents: true, // Tag audio events like laughter, applause, etc.
  languageCode: "eng", // Language of the audio file. If set to null, the model will detect the language automatically.
  diarize: true, // Whether to annotate who is speaking
});

console.log(transcription);
```

然后运行：

```python
python example.py
```

```typescript
npx tsx example.mts
```

控制台中将显示音频文件的转录文本。

> **Note**
>
> 对于医疗和临床音频，请将 `model_id` 设置为 `scribe_v2_medical`。请求格式
> 与 `scribe_v2` 相同，收费标准也相同。请参阅 [Scribe v2 Medical](/docs/zh/overview/models#scribe-v2-medical)。

#### CLI

下载示例音频，然后转录：

```bash
curl -O https://storage.googleapis.com/eleven-public-cdn/audio/marketing/nicole.mp3

elevenlabs speech-to-text convert \
  --file nicole.mp3 \
  --model-id scribe_v2 \
  --tag-audio-events true \
  --language-code eng \
  --diarize true
```

转录文本将输出到终端。

对于医疗和临床音频，请传入 `--model-id scribe_v2_medical`。

## 后续步骤

#### [批量转录](/docs/zh/eleven-api/guides/how-to/speech-to-text/batch)

通过说话人分离和事件标记转录预录制的音频文件

#### [实时转录](/docs/zh/eleven-api/guides/how-to/speech-to-text/realtime)

流式传输音频并实时接收转录结果

#### [API 参考文档](/docs/zh/api-reference/speech-to-text/convert)

查看所有文本转语音参数和响应格式