跳转到内容

Whisper语音转文字

该接口基于 Whisper 模型实现语音转文本功能,支持常见音频格式。

  • Whisper模型: OpenAI 开源的语音识别模型,支持多语言转写
  • 音频格式: 支持 mp3、wav、m4a 等常见格式

POST https://api.4allapi.com/v1/audio/transcriptions

参数名类型必填说明
modelstring固定值 “whisper-1”
filefile要转写的音频文件
Authorization: Bearer sk-********************* # 替换为你的 API 令牌
import json
import requests
def voice_to_text(file_path):
"""
语音转文本功能
参数:
file_path: 音频文件路径
返回:
识别出的文本内容
"""
url = "https://api.4allapi.com/v1/audio/transcriptions"
# 构造请求参数
payload = {"model": "whisper-1"}
files = {"file": ("audio.mp3", open(file_path, "rb"))}
# 设置请求头(请替换为你的API密钥)
headers = {"Authorization": "Bearer sk-***************************"} # 替换为你的 API 令牌
# 发送POST请求
response = requests.post(url, headers=headers, data=payload, files=files)
# 解析响应数据
data = json.loads(response.text)
# 返回识别结果
return data.get("text", "")
# 使用示例
print(voice_to_text("audio.mp3")) # 替换为你的音频文件路径

成功响应:

{
"text": "这是识别出的文本内容"
}
  1. 音频文件大小建议不超过25MB
  2. 支持中文、英文等多种语言
  3. 请妥善保管API密钥,不要泄露

4All API · 一站式AI大模型API聚合平台 | 价格 | 联系我们

© 2025 4All API. All rights reserved.