音频 API
Star API 提供兼容 OpenAI Whisper 格式的音频处理接口,支持语音转文字和文字转语音功能。
语音转文字(Speech-to-Text)
请求端点
POST https://star.zhxyclaw.cn/v1/audio/transcriptions请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | file | 是 | 音频文件(支持 mp3、mp4、m4a、wav、webm、flac 等格式) |
model | string | 是 | 使用的模型,例如 whisper-1 |
language | string | 否 | 音频语言的 ISO 639-1 代码(如 zh、en) |
prompt | string | 否 | 提示词,帮助模型更好地识别特定领域的术语 |
response_format | string | 否 | 返回格式:json(默认)、text、srt、verbose_json、vtt |
temperature | number | 否 | 采样温度,范围 0-1 |
请求示例
curl
bash
curl https://star.zhxyclaw.cn/v1/audio/transcriptions \
-H "Authorization: Bearer sk-your-api-key" \
-F file="@audio.mp3" \
-F model="whisper-1" \
-F language="zh" \
-F response_format="json"Python
python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://star.zhxyclaw.cn/v1"
)
with open("audio.mp3", "rb") as audio_file:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language="zh",
response_format="json"
)
print(transcript.text)Node.js
javascript
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-your-api-key",
baseURL: "https://star.zhxyclaw.cn/v1",
});
const transcription = await client.audio.transcriptions.create({
model: "whisper-1",
file: fs.createReadStream("audio.mp3"),
language: "zh",
response_format: "json",
});
console.log(transcription.text);响应格式
json
{
"text": "这是一段语音识别的结果。"
}翻译接口
Star API 同样支持音频翻译,将音频内容翻译为英文:
POST https://star.zhxyclaw.cn/v1/audio/translationsbash
curl https://star.zhxyclaw.cn/v1/audio/translations \
-H "Authorization: Bearer sk-your-api-key" \
-F file="@audio.mp3" \
-F model="whisper-1" \
-F response_format="json"文字转语音(Text-to-Speech)
请求端点
POST https://star.zhxyclaw.cn/v1/audio/speech请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | TTS 模型,如 tts-1、tts-1-hd |
input | string | 是 | 要转换为语音的文本(最大 4096 字符) |
voice | string | 是 | 音色:alloy、echo、fable、onyx、nova、shimmer |
response_format | string | 否 | 音频格式:mp3(默认)、opus、aac、flac、wav、pcm |
speed | number | 否 | 语速,范围 0.25-4.0,默认 1.0 |
请求示例
curl
bash
curl https://star.zhxyclaw.cn/v1/audio/speech \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key" \
-d '{
"model": "tts-1",
"input": "你好,欢迎使用 Star API!",
"voice": "alloy",
"response_format": "mp3",
"speed": 1.0
}' \
-o output.mp3Python
python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://star.zhxyclaw.cn/v1"
)
response = client.audio.speech.create(
model="tts-1",
input="你好,欢迎使用 Star API!",
voice="alloy",
response_format="mp3",
speed=1.0
)
response.stream_to_file("output.mp3")Node.js
javascript
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-your-api-key",
baseURL: "https://star.zhxyclaw.cn/v1",
});
const response = await client.audio.speech.create({
model: "tts-1",
input: "你好,欢迎使用 Star API!",
voice: "alloy",
response_format: "mp3",
speed: 1.0,
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("output.mp3", buffer);音色说明
| 音色 | 描述 |
|---|---|
alloy | 中性、均衡的声音 |
echo | 沉稳、深沉的男性声音 |
fable | 富有表现力的声音 |
onyx | 低沉、有力的男性声音 |
nova | 温暖、友好的女性声音 |
shimmer | 柔和、温暖的女性声音 |
响应格式
文字转语音接口直接返回音频文件的二进制流,而非 JSON 响应。请根据 response_format 参数指定的格式保存文件。
注意事项
注意
- 语音转文字的音频文件大小上限为 25 MB
- 文字转语音的文本长度上限为 4096 字符
- 建议使用
tts-1-hd模型以获得更高质量的语音
