强制对齐快速入门

了解如何使用强制对齐 API 将文本与音频对齐。

本指南将介绍如何使用强制对齐 API 将文本与音频对齐。

使用强制对齐 API

1

创建 API 密钥

在控制台创建 API 密钥,用于安全地访问 API。

将密钥存储为托管密钥,并根据需要通过 .env 文件将其作为环境变量传递给 SDK,或直接在应用配置中设置。

.env
ELEVENLABS_API_KEY=<your_api_key_here>
2

安装 SDK

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

pip install elevenlabs
pip install python-dotenv
3

发起 API 请求

根据所选语言,新建名为 example.py 或 example.mts 的文件,并添加以下代码:

# example.py
import os
from io import BytesIO
from elevenlabs.client import ElevenLabs
import requests
from dotenv import load_dotenv
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)
# Perform the text-to-speech conversion
transcription = elevenlabs.forced_alignment.create(
file=audio_data,
text="With a soft and whispery American accent, I'm the ideal choice for creating ASMR content, meditative guides, or adding an intimate feel to your narrative projects."
)
print(transcription)

然后运行:

python example.py

控制台应会输出带有精确时间戳的音频文件转录文本。

后续步骤