为语音听录配置语言识别和分割聚类

本指南演示了跨实时语音转文本、语音翻译、快速听录和批量听录的语言识别(LID)和说话人分割的单一配置方法。 它重点介绍两种常见的故障模式:

  • 依赖于默认语言检测而不限制候选区域设置。
  • 期望在未显式启用说话人分离的情况下获得说话人标签。

请求和响应属性因工作负荷而异。 将本指南与 相关内容中链接的特定于工作负荷的文章一起使用。

工作量 语言标识配置 响应中的说话人标签
实时语音转文本或语音翻译 AutoDetectSourceLanguageConfig 具有候选区域设置或固定源区域设置 SpeakerId 使用 ConversationTranscriber
快速听录 locales,或在支持的情况下使用多语言模型 speaker 启用 diarization 时,针对每个短语
批量转录 properties.languageIdentification.candidateLocales和顶层locale作为回退选项 speaker 启用 diarization 时,针对每个已识别的短语

先决条件

  • 用于语音的 Azure AI 服务
  • 支持的语音区域
  • 对于 SDK 示例,可使用以下受支持的 SDK 配置之一:
  • 测试能够反映实际生产条件的音频,包括:
    • 简短陈述。
    • 背景噪音。
    • 预期说话人数。

在配置请求之前检查支持的区域设置

在设置candidateLocaleslocales或固定的源区域设置之前,请先使用这些区域设置列表:

快速转录的多语言模型支持一套独立的区域设置。 在省略locales之前,请先查看语音转文本支持的语言表。 如果该模型不支持音频区域设置,请改为提供固定支持的区域设置。

为实时语音转文本或语音翻译配置语言标识(SDK)

有关使用实时语音转文本和语音翻译的完整 SDK 覆盖范围 AutoDetectSourceLanguageConfig (包括所有受支持的语言、开始模式与连续模式和自定义模型映射)请参阅 “实现语言识别”。

将 LID 与分割或脱机听录相结合时的要点包括:

  • 当您已知可能使用的语言时,请提供候选区域设置列表。 更小且更准确的候选集可以提高检测效果,尤其是对于短音频或噪声音频。
  • 每种基础语言不要包含多个区域设置(例如,使用 en-USen-GB,不要同时使用两者)。
  • 如果 AutoDetectSourceLanguageResult.Language 返回空值或意外值,请绕过自动检测并改为设置固定 SpeechRecognitionLanguage 值。

对于语音翻译,请从识别结果中的 PropertyId.SpeechServiceConnection_AutoDetectSourceLanguageResult 读取检测到的语言,而不是从 SpeechRecognitionLanguage 读取。 后者是必需的占位符,但在启用自动检测时,该服务目前会忽略它。

以下模式可为实时语音转文本启用候选区域设置的语言识别。 对语音翻译使用相同的 AutoDetectSourceLanguageConfig 模式,并从上面所述的特定于结果的属性中读取检测到的语言。

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;

var speechConfig = SpeechConfig.FromEndpoint(
  new Uri("YourSpeechEndpoint"), "YourSpeechKey");
var autoDetectConfig = AutoDetectSourceLanguageConfig.FromLanguages(
  new[] { "en-US", "es-ES", "fr-FR" });

using var audioConfig = AudioConfig.FromWavFileInput("sample.wav");
using var recognizer = new SpeechRecognizer(
  speechConfig, autoDetectConfig, audioConfig);
var result = await recognizer.RecognizeOnceAsync();
var detectedLanguage = AutoDetectSourceLanguageResult
  .FromResult(result).Language;
Console.WriteLine($"Detected language: {detectedLanguage}");

参考: AutoDetectSourceLanguageConfigAutoDetectSourceLanguageResult

配置实时分割(SDK)

默认情况下,未启用实时说话人分离。 使用 ConversationTranscriber,在需要中间标签时启用分割结果属性,并在转录或转录事件中验证 SpeakerId 。 实时分割和脱机分割具有不同的请求和响应属性。

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Microsoft.CognitiveServices.Speech.Transcription;

string endpoint = Environment.GetEnvironmentVariable("ENDPOINT");
string key = Environment.GetEnvironmentVariable("SPEECH_KEY");

var speechConfig = SpeechConfig.FromEndpoint(new Uri(endpoint), key);
speechConfig.SpeechRecognitionLanguage = "en-US";
speechConfig.SetProperty(
    PropertyId.SpeechServiceResponse_DiarizeIntermediateResults,
    "true");

using var audioConfig = AudioConfig.FromWavFileInput("meeting.wav");
using var transcriber = new ConversationTranscriber(speechConfig, audioConfig);

transcriber.Transcribing += (s, e) =>
{
    Console.WriteLine($"TRANSCRIBING: SpeakerId={e.Result.SpeakerId} Text={e.Result.Text}");
};

transcriber.Transcribed += (s, e) =>
{
    Console.WriteLine($"TRANSCRIBED: SpeakerId={e.Result.SpeakerId} Text={e.Result.Text}");
};

await transcriber.StartTranscribingAsync();
Console.ReadKey();
await transcriber.StopTranscribingAsync();

参考:ConversationTranscriberSpeechServiceResponse_DiarizeIntermediateResults

配置快速听录(REST 和 SDK)

对于快速听录,请同时显式设置语言和分割聚类行为。

REST 示例

curl --location "https://YourResourceName.cognitiveservices.azure.cn/speechtotext/transcriptions:transcribe?api-version=2025-10-15" \
  --header "Ocp-Apim-Subscription-Key: YourSpeechResourceKey" \
  --form 'audio=@"call.wav"' \
  --form 'definition={
    "locales": ["en-US", "es-ES"],
    "diarization": {
      "enabled": true,
      "maxSpeakers": 4
    }
  }'

参考:转录 - 转录

Python SDK 示例

from azure.core.credentials import AzureKeyCredential
from azure.ai.transcription import TranscriptionClient
from azure.ai.transcription.models import (
    TranscriptionContent,
    TranscriptionOptions,
    TranscriptionDiarizationOptions,
)

endpoint = "https://<your-resource>.cognitiveservices.azure.cn/"
api_key = "<your-key>"

client = TranscriptionClient(endpoint=endpoint, credential=AzureKeyCredential(api_key))

with open("call.wav", "rb") as audio_file:
    options = TranscriptionOptions(
        locales=["en-US", "es-ES"],
        diarization_options=TranscriptionDiarizationOptions(max_speakers=4),
    )
    result = client.transcribe(TranscriptionContent(definition=options, audio=audio_file))

    for phrase in result.phrases:
        print(f"locale={phrase.locale}, speaker={phrase.speaker}, text={phrase.text}")

参考:TranscriptionClientTranscriptionOptions、TranscriptionDiarizationOptions

配置批量转录(REST)

对于批处理作业,请显式设置所有三个项目:

  • locale 在未检测到任何语言时的回退选项。
  • properties.languageIdentification.candidateLocales 适用于 LID 范围。
  • 用于说话人标签的 properties.diarization.enabledproperties.diarization.maxSpeakers
{
  "displayName": "Batch LID + diarization",
  "locale": "en-US",
  "contentUrls": [
    "https://contoso.example/audio1.wav",
    "https://contoso.example/audio2.wav"
  ],
  "properties": {
    "languageIdentification": {
      "candidateLocales": ["en-US", "es-ES", "fr-FR"],
      "mode": "Single"
    },
    "diarization": {
      "enabled": true,
      "maxSpeakers": 4
    },
    "timeToLiveHours": 48
  }
}

使用以下内容提交该负载:

POST /speechtotext/transcriptions:submit?api-version=2025-10-15

参考:转写 - 提交

注意

批处理语言标识支持基本模型。 如果将语言标识与自定义模型一起指定,该服务将使用候选区域设置的基础模型,从而产生意外的识别结果。

若要进行分割,请使用单声道音频。 立体声录音不支持分割。 将 maxSpeakers 设置为对话的合理上限。 如果录制内容包含的扬声器数超过配置的最大值,则服务可以组合扬声器。

执行预检核对清单

在启用生产流量之前使用此清单:

  1. 验证区域设置是否受支持。
    • 确认 candidateLocales(或 locales)中的每个区域设置都出现在受支持的区域设置列表中。
  2. 验证请求配置。
  • 确认该请求根据工作负载情况相应地使用 AutoDetectSourceLanguageConfiglocalesproperties.languageIdentification.candidateLocales
  • 确认支持固定的 SpeechRecognitionLanguage 或顶层 locale 回退。
  1. 验证语言检测字段。
    • 实时 SDK:确认 AutoDetectSourceLanguageResult (或属性 SpeechServiceConnection_AutoDetectSourceLanguageResult)已填充。
    • 快速或批量转写:确认每个短语都包含预期的 locale 值。
  2. 验证说话人标签字段。
  • 实时分割:确认 SpeakerId 在转录或转录事件中出现。
  • 快速或批量转录:确认已存在短语级别的 speaker 值。
  • 启用分割后,确认音频为单声道。
  1. 验证短音频或带噪音音频的表现。
    • 运行干扰示例集,并将检测到的语言与预期语言进行比较。
    • 如果检测不稳定,请改用固定的源区域设置重新运行,而不是使用自动检测。
  2. 验证说话人数量优化效果。
    • maxSpeakers 设置为切合实际的参与者上限。
  • 将返回的演讲者标签与预期的参与者计数进行比较。