尝试在 C# 中使用 Google Speech2Text

发布于 2024-12-25 16:36:23 字数 1860 浏览 1 评论 0原文

以下简单代码尝试将波形文件发布到 Google Speech2Text 服务,但总是失败,并出现“网关超时 (504)”或一般异常“操作超时”。有人可以帮忙吗?

  public void ProcessWaveFile(string path)
    {
        HttpWebRequest request = 
             (HttpWebRequest)HttpWebRequest.Create(
                 "https://www.google.com/"+ 
                 "speech-api/v1/recognize?"+ 
                 "xjerr=1&client=speech2text&lang=en-US&maxresults=10");

        ServicePointManager.ServerCertificateValidationCallback += 
                     delegate { return true; };

        request.Timeout = 60000;
        request.Method = "POST";
        request.KeepAlive = true;           
        request.ContentType = "audio/wav";
        request.UserAgent = "speech2text";

        FileInfo fInfo = new FileInfo(path);
        long numBytes = fInfo.Length;
        byte[] data;

        using (FileStream fStream = new FileStream(
                                          path, 
                                          FileMode.Open, 
                                          FileAccess.Read))
        {
            data = new byte[fStream.Length];
            fStream.Read(data, 0, (int)fStream.Length);
            fStream.Close();
        }

        using (Stream wrStream = request.GetRequestStream())
            wrStream.Write(data, 0, data.Length);

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            var resp = response.GetResponseStream();

            if (resp != null)
            {
                StreamReader sr = new StreamReader(resp);
                MessageBox.Show(sr.ReadToEnd());

                resp.Close();
                resp.Dispose();
            }
        }
        catch (System.Exception ee)
        {
            MessageBox.Show(ee.Message);
        }           
    }

非常感谢。

舒贾特

The following simple code tries to post a wave file to Google Speech2Text service, but always fails with either a "Gateway Timeout (504)" or general exception "The operation timed out". Can anyone help please?

  public void ProcessWaveFile(string path)
    {
        HttpWebRequest request = 
             (HttpWebRequest)HttpWebRequest.Create(
                 "https://www.google.com/"+ 
                 "speech-api/v1/recognize?"+ 
                 "xjerr=1&client=speech2text&lang=en-US&maxresults=10");

        ServicePointManager.ServerCertificateValidationCallback += 
                     delegate { return true; };

        request.Timeout = 60000;
        request.Method = "POST";
        request.KeepAlive = true;           
        request.ContentType = "audio/wav";
        request.UserAgent = "speech2text";

        FileInfo fInfo = new FileInfo(path);
        long numBytes = fInfo.Length;
        byte[] data;

        using (FileStream fStream = new FileStream(
                                          path, 
                                          FileMode.Open, 
                                          FileAccess.Read))
        {
            data = new byte[fStream.Length];
            fStream.Read(data, 0, (int)fStream.Length);
            fStream.Close();
        }

        using (Stream wrStream = request.GetRequestStream())
            wrStream.Write(data, 0, data.Length);

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            var resp = response.GetResponseStream();

            if (resp != null)
            {
                StreamReader sr = new StreamReader(resp);
                MessageBox.Show(sr.ReadToEnd());

                resp.Close();
                resp.Dispose();
            }
        }
        catch (System.Exception ee)
        {
            MessageBox.Show(ee.Message);
        }           
    }

Many thanks.

Shujaat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2025-01-01 16:36:23

您的代码对我来说适用于以下更改:

Request.ContentType = "audio/x-flac; rate=8000";

并且您提供的文件需要位于 FLAC 格式格式。

我使用 Windows 录音机录制了一个小样本,它生成一个 WMA 文件。然后我使用 VLC Player 将 WMA 文件转换为 FLAC(使用“转换”选项,确保以 RAW 格式输出、一个通道和 8000 kbps)

一个很好的 非 c# 参考 以及评论中的一些扩展 api 文档

Your code works for me with the following change:

Request.ContentType = "audio/x-flac; rate=8000";

and the file that you provide needs to be in FLAC format.

I recorded a small sample with Windows Sound Recorder which produces an WMA file. Then I used the VLC Player to convert the WMA file to FLAC (using the Convert options, make sure to output in RAW format, one channel and 8000 kbps)

A good non c# reference with as well some extended api doc in the comments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文