跳到导航

多通道文本转语音

本指南介绍如何通过文本转语音 API 使用多通道转录模式。

操作指南 · 假设你已完成 文本转语音 快速入门。

概述

多通道文本转语音功能可转录每个通道包含不同说话人的音频文件。这尤其适用于将说话人隔离在不同音频通道中的录音,无需说话人分离即可获得更清晰的转录结果。

每个通道都会独立处理,并根据通道编号自动分配说话人 ID(通道 0 → speaker_0、通道 1 → speaker_1,依此类推)。系统会从输入音频文件中提取各个通道并并行转录。默认情况下,API 每个通道返回一份转录文本;设置 multichannel_output_style=combined 后,则会收到一份合并了所有通道的转录文本,其中所有词语按开始时间排序,并标有各自的 channel_index。

常见使用场景

  • 立体声访谈录音 - 采访者位于左声道,受访者位于右声道
  • 多轨播客录音 - 每位参与者录制在独立轨道上
  • 呼叫中心录音 - 坐席和客户位于不同通道
  • 会议录音 - 每位参与者隔离在独立通道中
  • 法庭记录 - 多方在不同通道中录制

要求

  • 拥有 API 密钥的 ElevenLabs 账户
  • 多通道音频文件(WAV、MP3 或其他支持的格式)
  • 每个音频文件最多 5 个通道
  • 每个通道应只包含一位说话人

工作原理

1

准备多通道音频

确保音频文件中的说话人隔离在不同通道中。多通道功能最多支持 5 个通道,每个通道映射到特定说话人:

  • 通道 0 → speaker_0
  • 通道 1 → speaker_1
  • 通道 2 → speaker_2
  • 通道 3 → speaker_3
  • 通道 4 → speaker_4
2

配置 API 参数

发起文本转语音请求时,必须设置:

  • use_multi_channel: true
  • diarize: false(多通道模式通过通道区分说话人)

还可选择通过以下参数控制响应格式:

  • multichannel_output_style: separate(默认)每个通道返回一份转录文本。combined 会将所有通道合并为单份转录文本,其中词语按开始时间排序,每个词都带有 channel_index,格式与标准单通道响应一致。combined 需要时间戳(timestamps_granularity 不得为 none),且不支持 webhook 交付或实体检测/脱敏。

多通道模式不能使用 num_speakers 参数,因为说话人数量会根据通道数量自动确定。多通道模式假定每个通道恰好有一位说话人。若有多位说话人,系统会为该通道内所有说话人分配相同的说话人 ID。

3

处理响应

默认情况下(multichannel_output_style=separate),多通道音频返回的响应格式与单通道不同:

如果设置了 use_multi_channel: true,但提供的是单通道(单声道)音频文件,收到的将是标准单通道响应,而非多通道格式。仅当音频文件实际包含多个通道时,才会返回多通道响应格式。

{
"language_code": "en",
"language_probability": 0.98,
"text": "Hello world",
"words": [...]
}

使用 multichannel_output_style=combined 时,响应采用与单通道转录相同的扁平格式(顶层包含 text 和 words,没有 transcripts 数组),所有通道会合并为按开始时间排序的单个列表。每个词都包含用于标识所属通道的 channel_index(以及 speaker_id)。

实现

基础多通道转录

以下是转录包含两位说话人的立体声音频文件的完整示例:

from elevenlabs import ElevenLabs
elevenlabs = ElevenLabs(api_key="YOUR_API_KEY")
def transcribe_multichannel(audio_file_path):
with open(audio_file_path, 'rb') as audio_file:
result = elevenlabs.speech_to_text.convert(
file=audio_file,
model_id='scribe_v3',
use_multi_channel=True,
diarize=False,
timestamps_granularity='word'
)
return result
# Process the response
result = transcribe_multichannel('stereo_interview.wav')
if hasattr(result, 'transcripts'): # Multichannel response
for transcript in result.transcripts:
channel = transcript.channel_index
text = transcript.text
print(f"Channel {channel} (speaker_{channel}): {text}")
else: # Single channel response (fallback)
print(f"Text: {result.text}")

创建对话转录文本

获取按时间排序、对话式转录文本的最简单方法是请求 multichannel_output_style=combined。API 会返回单个 words 列表,该列表已按开始时间排序,且每个词都有 channel_index 和 speaker_id:

合并输出(推荐)
with open("stereo_interview.wav", "rb") as audio_file:
result = elevenlabs.speech_to_text.convert(
file=audio_file,
model_id="scribe_v3",
use_multi_channel=True,
multichannel_output_style="combined",
diarize=False,
timestamps_granularity="word",
)
for word in result.words:
if word.type == "word":
print(f"speaker_{word.channel_index}: {word.text}")

如果使用默认的 separate 输出,也可在客户端合并各通道的转录文本:

def create_conversation_transcript(multichannel_result):
"""Create a conversation-style transcript with speaker labels"""
all_words = []
if hasattr(multichannel_result, 'transcripts'):
# Collect all words from all channels
for transcript in multichannel_result.transcripts:
for word in transcript.words or []:
if word.type == 'word':
all_words.append({
'text': word.text,
'start': word.start,
'speaker_id': word.speaker_id,
'channel': transcript.channel_index
})
# Sort by timestamp
all_words.sort(key=lambda w: w['start'])
# Group consecutive words by speaker
conversation = []
current_speaker = None
current_text = []
for word in all_words:
if word['speaker_id'] != current_speaker:
if current_text:
conversation.append({
'speaker': current_speaker,
'text': ' '.join(current_text)
})
current_speaker = word['speaker_id']
current_text = [word['text']]
else:
current_text.append(word['text'])
# Add the last segment
if current_text:
conversation.append({
'speaker': current_speaker,
'text': ' '.join(current_text)
})
return conversation
# Format the output
conversation = create_conversation_transcript(result)
for turn in conversation:
print(f"{turn['speaker']}: {turn['text']}")

将 webhook 与多通道结合使用

多通道转录支持通过 webhook 交付进行异步处理:

Webhook 返回 separate(按通道)格式。目前 webhook 交付不支持 multichannel_output_style=combined,请使用同步请求,或在客户端合并按通道返回的 webhook 负载。

from elevenlabs import ElevenLabs
elevenlabs = ElevenLabs(api_key="YOUR_API_KEY")
async def transcribe_multichannel_with_webhook(audio_file_path):
with open(audio_file_path, 'rb') as audio_file:
result = await elevenlabs.speech_to_text.convert_async(
file=audio_file,
model_id='scribe_v3',
use_multi_channel=True,
diarize=False,
webhook=True # Enable webhook delivery
)
print(f"Transcription started with task ID: {result.task_id}")
return result.task_id

错误处理

常见验证错误

错误:多通道模式不支持说话人分离,会根据说话人所在的通道分配说话人。

解决方案:使用多通道模式时,始终设置 diarize=false。

错误:启用 use_multi_channel 时无法指定 num_speakers。说话人数量会根据通道数量自动确定。解决方案:从请求中移除 num_speakers 参数。

错误:多通道模式最多支持 5 个通道,但该音频文件包含 X 个通道。

解决方案:仅处理前 5 个通道,或预处理音频以减少通道数。

错误:multichannel_output_style=‘combined’ 需要时间戳;请将 timestamps_granularity 设置为 ‘word’ 或 ‘character’。

解决方案:合并输出会按时间排序词语,因此请将 timestamps_granularity 设置为 word(默认值)或 character。

错误:webhook 交付尚不支持 multichannel_output_style=‘combined’。

解决方案:使用 combined 发起同步请求,或在使用 webhook 时保留默认的 separate 输出并在客户端合并。

最佳实践

音频准备

为获得最佳结果:- 使用 16kHz 采样率以提升性能 - 处理前移除静音或未使用的通道 - 确保每个通道只包含一位说话人 - 尽可能使用无损格式(WAV)以获得最佳质量

性能优化

并发成本会随通道数量线性增加。一个 60 秒的 3 通道文件,其并发成本是单通道文件的 3 倍。

可使用以下公式估算多通道音频的处理时间:

处理时间=(D⋅0.3)+2+(N⋅0.5)处理时间 = (D \cdot 0.3) + 2 + (N \cdot 0.5)

其中:

  • DD = 文件时长(秒)
  • NN = 通道数量
  • 0.30.3 = 处理速度系数(约为实时速度的 30%)
  • 22 = 固定开销(秒)
  • 0.50.5 = 每通道开销(秒)

示例:对于一个 60 秒的立体声文件(2 个通道):

处理时间=(60⋅0.3)+2+(2⋅0.5)=18+2+1=21 秒处理时间 = (60 \cdot 0.3) + 2 + (2 \cdot 0.5) = 18 + 2 + 1 = 21\ 秒

内存注意事项

对于大型多通道文件,请考虑流式处理或分块:

def process_large_multichannel_file(file_path, chunk_duration=300):
"""Process large files in chunks (5-minute segments)"""
from pydub import AudioSegment
from elevenlabs import ElevenLabs
import os
elevenlabs = ElevenLabs(api_key="YOUR_API_KEY")
audio = AudioSegment.from_file(file_path)
duration_ms = len(audio)
chunk_size_ms = chunk_duration * 1000
all_transcripts = []
for start_ms in range(0, duration_ms, chunk_size_ms):
end_ms = min(start_ms + chunk_size_ms, duration_ms)
# Extract chunk
chunk = audio[start_ms:end_ms]
chunk_file = f"temp_chunk_{start_ms}.wav"
chunk.export(chunk_file, format="wav")
# Transcribe chunk using SDK
with open(chunk_file, 'rb') as audio_file:
result = elevenlabs.speech_to_text.convert(
file=audio_file,
model_id='scribe_v3',
use_multi_channel=True,
diarize=False,
timestamps_granularity='word'
)
# Adjust timestamps
if hasattr(result, 'transcripts'):
for transcript in result.transcripts:
for word in transcript.words or []:
word.start += start_ms / 1000
word.end += start_ms / 1000
all_transcripts.extend(result.transcripts)
# Clean up
os.remove(chunk_file)
return all_transcripts

常见问题

API 将返回错误。你需要选择其中 5 个通道发送至 API,或在发送前混合部分通道。

可以,但没有必要。如果以 use_multi_channel=true 发送单声道音频,收到的将是标准单通道响应,而非多通道格式。

可以。设置 multichannel_output_style=combined,即可收到一份合并所有通道并按开始时间排序的转录文本,每个词都标有 channel_index。其格式与标准单通道响应一致。该模式需要时间戳,且不适用于 webhook 交付。

说话人 ID 根据通道编号确定:通道 0 变为 speaker_0,通道 1 变为 speaker_1,依此类推。

可以,每个通道会独立处理,并可检测不同语言。语言检测按通道进行。使用 multichannel_output_style=combined 时,顶层 language_code 反映置信度最高的通道,而每个词仍带有自己的 channel_index。

后续步骤