Skip to Content
ModelSell 文档
音频与其他ElevenLabs 音频接入

ElevenLabs 语音转写

使用 Scribe 转写录音文件,或通过 WebSocket 持续发送音频并接收临时及最终转写。

按 ElevenLabs 快速开始 设置 ModelSell 地址和 API Key。Scribe 按录音中的语言转写。 语音转写概览

用途模型 IDModelSell 路径
文件转写scribe_v3、scribe_v2、scribe_v2_medicalPOST /elevenlabs/v1/speech-to-text
实时转写scribe_v3_live、scribe_v2_realtimeWebSocket GET /elevenlabs/v1/speech-to-text/realtime

录音含有临床术语时可选择批量模型 scribe_v2_medical。Scribe v3 Live Medical 在 ElevenAgents 内使用。 Scribe 模型

文件转写

使用 multipart 表单上传一个 file,并显式指定 model_id。省略 language_code 时自动识别语言。供应商要求输入音频至少 100 毫秒,同时受当前 ModelSell 网关上传大小限制。 创建转写

curl --fail-with-body "$MODELSELL_BASE_URL/elevenlabs/v1/speech-to-text" \
  -H "xi-api-key: $MODELSELL_API_KEY" \
  -F 'model_id=scribe_v3' \
  -F '[email protected]' \
  -F 'language_code=zh' \
  -F 'timestamps_granularity=word' \
  -F 'diarize=true'
可选表单字段用途
language_codeISO 639-1 或 ISO 639-3 语言提示
timestamps_granularity默认 word,也可用 character 或 none
diarize、num_speakers标注说话人,可指定预计的说话人数,范围 1 至 32
tag_audio_events标注检测到的音频事件,默认开启
secondary_language_codes、numbers_formatscribe_v3 可指定其他语言提示,并选择将数字输出为 digits 或 words
file_format16 kHz、16 位有符号小端单声道裸 PCM 使用 pcm_s16le_16。MP3、WAV 等格式使用默认的 other

JSON 响应包含 text、检测到的 language_code 和 words 数组。按请求选项,数组元素可包含秒级 start、end 和说话人 speaker_id,也可能包含空格及音频事件条目。实体识别、转写编辑等选项的适用模型和组合规则见供应商字段说明。 转写响应和参数

通过 ModelSell 使用 file 上传入口。远程来源 URL、异步回调、按声道独立转写和供应商说话人库属于供应商工作区功能。普通说话人区分可通过 diarize 使用。

实时 WebSocket

在查询参数里指定 model_id,默认 scribe_v2_realtime。示例选择 scribe_v3_live、16 kHz 裸 PCM、手动提交和词级时间戳。网关也接受 pcm_8000、pcm_22050、pcm_24000、pcm_44100、pcm_48000 和 ulaw_8000。实际音频编码需与 audio_format 一致。 Scribe WebSocket 参考

消息方向用途
session_started服务端到客户端收到后开始发送音频
input_audio_chunk客户端到服务端发送 Base64 的 audio_base_64、sample_rate 和 commit。手动模式在该段最后一个音频块设置 commit: true
partial_transcript服务端到客户端显示仍可能更新的临时转写
committed_transcript服务端到客户端读取该段的最终文本
committed_transcript_with_timestamps服务端到客户端设置 include_timestamps=true 时读取后续的时间戳结果

将一段短录音转换为只含采样字节的裸 PCM,再运行示例。

ffmpeg -i speech.mp3 -ac 1 -ar 16000 -c:a pcm_s16le -f s16le speech.pcm
python -m pip install websocket-client
import base64
import json
import os
import time
from pathlib import Path
from urllib.parse import urlencode

from websocket import create_connection

audio = Path("speech.pcm").read_bytes()
if not audio or len(audio) % 2:
    raise ValueError("Expected a nonempty file of 16-bit PCM samples")

base = os.environ["MODELSELL_WS_URL"].rstrip("/")
query = urlencode({
    "model_id": "scribe_v3_live",
    "audio_format": "pcm_16000",
    "commit_strategy": "manual",
    "include_timestamps": "true",
})
socket = create_connection(
    f"{base}/elevenlabs/v1/speech-to-text/realtime?{query}",
    header=[f"Authorization: Bearer {os.environ['MODELSELL_API_KEY']}"],
    timeout=30,
)

try:
    start = json.loads(socket.recv())
    if start.get("message_type") != "session_started":
        raise RuntimeError(start)

    chunk_bytes = 3200
    for offset in range(0, len(audio), chunk_bytes):
        chunk = audio[offset:offset + chunk_bytes]
        last = offset + len(chunk) == len(audio)
        socket.send(json.dumps({
            "message_type": "input_audio_chunk",
            "audio_base_64": base64.b64encode(chunk).decode("ascii"),
            "sample_rate": 16000,
            "commit": last,
        }))
        if not last:
            time.sleep(len(chunk) / 32000)

    while True:
        event = json.loads(socket.recv())
        kind = event.get("message_type")
        if kind == "committed_transcript_with_timestamps":
            print(json.dumps({"text": event["text"], "words": event.get("words", [])},
                             ensure_ascii=False))
            break
        if kind not in {"partial_transcript", "committed_transcript"}:
            raise RuntimeError(event)
finally:
    socket.close()

连续采集时,在发送音频的同时读取消息。commit_strategy=vad 会按停顿分段。scribe_v3_live 还支持 commit_strategy=turn_prediction、multilang_mode=true 和实时说话人区分。使用 keyterm_library 时把词库放在该模型的首条音频消息里。读取最后一段的最终结果后再关闭连接。 提交策略 · 词库提示

文件转写按上传文件的音频时长计量。网关无法测量时长的格式需要按次价格。实时转写按所选编码和发送的音频字节数估算时长。

On this page