You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用.NET实现Azure批量转录完成后调用URL或Function App

Azure Cognitive Services批量转录完成后触发通知的实现方案

核心思路

Azure Speech服务的批量转录API本身不支持直接配置回调URL,但可以通过以下两种可靠方式实现转录完成后的通知:

方式一:轮询转录状态(简单直接)

发起批量转录后,定期调用API查询转录状态,当状态变为Succeeded或Failed时,触发通知逻辑(调用指定URL或Azure Function)。

实现步骤

  1. 发起批量转录后,从响应头的Location字段中提取转录ID
  2. 定时调用转录状态查询API,检查返回结果的status字段
  3. 根据状态结果触发对应通知:成功则推送结果信息,失败则推送错误详情

.NET示例代码(状态查询+触发通知)

// 发起转录后提取转录ID
string transcriptionId = new Uri(response.Headers.Location).Segments.Last();

// 模拟轮询逻辑(实际可使用Timer或Azure Function定时触发器)
while (true)
{
    Thread.Sleep(60000); // 每分钟查询一次,可根据音频时长调整间隔
    var statusResponse = await httpClient.GetAsync($"https://{speechServiceRegion}.api.cognitive.microsoft.com/speechtotext/v3.1/transcriptions/{transcriptionId}");
    var statusContent = await statusResponse.Content.ReadAsStringAsync();
    dynamic transcriptionStatus = JsonConvert.DeserializeObject(statusContent);
    
    if (transcriptionStatus.status == "Succeeded")
    {
        // 调用目标URL或Azure Function推送成功通知
        await httpClient.PostAsync("https://your-target-endpoint/api/notify", 
            new StringContent(JsonConvert.SerializeObject(new 
            { 
                TranscriptionId = transcriptionId, 
                ResultFileUrl = transcriptionStatus.properties.outputs.transcriptFiles[0].links.contentUrl 
            }), 
            Encoding.UTF8, "application/json"));
        break;
    }
    else if (transcriptionStatus.status == "Failed")
    {
        // 推送错误通知
        await httpClient.PostAsync("https://your-target-endpoint/api/error-notify", 
            new StringContent(JsonConvert.SerializeObject(new 
            { 
                TranscriptionId = transcriptionId, 
                ErrorMessage = transcriptionStatus.error.message 
            }), 
            Encoding.UTF8, "application/json"));
        break;
    }
}

方式二:利用Azure Event Grid监听Blob存储(无轮询,实时触发)

如果将批量转录结果配置存储到Azure Blob容器,可通过Event Grid监听Blob创建事件,当转录结果文件生成时自动触发通知。

实现步骤

  1. 在批量转录请求的properties中指定outputContainerUrl(带写入权限的Blob容器SAS URL),让转录结果自动存入该容器
  2. 为Blob存储账户创建Event Grid订阅,触发条件选择「Blob创建事件」
  3. 将Event Grid的目标设置为你的Azure Function或Webhook URL
  4. 转录完成生成结果Blob时,Event Grid会自动发送事件通知到目标端点

修改后的批量转录请求体示例

var requestBody = new
{
    contentUrls = new[] { blobSasUri },
    properties = new
    {
        diarizationEnabled = false,
        wordLevelTimestampsEnabled = false,
        // 指定转录结果存储的Blob容器SAS URL
        outputContainerUrl = "https://your-storage-account.blob.core.windows.net/transcription-results?sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2024-12-31T23:59:59Z&st=2024-01-01T00:00:00Z&spr=https&sig=xxx"
    },
    locale = "en-GB",
    displayName = "Batch transcription using Azure Function",
};

注意事项

  • 轮询间隔需根据音频时长调整:短音频可缩短间隔,长音频适当延长,避免频繁调用API
  • Event Grid方式需确保Blob存储账户和Event Grid的权限配置正确,目标端点能正常接收Event Grid的POST请求
  • 使用Azure Function接收Event Grid事件时,需适配Event Grid的事件格式,提取Blob信息后获取转录结果

内容的提问来源于stack exchange,提问作者blarg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 02:40:02