使用 Speech-to-Text 转写视频文件中的音频

本教程介绍如何使用 Cloud Speech-to-Text 转录视频文件中的音轨。

音频文件可能来自许多不同的来源。音频数据可能来自电话(如语音邮件)或视频文件所包含的音轨。

Cloud STT 可以从多种机器学习模型中选择一种来转写您的音频文件,以最佳匹配音频的原始来源。为了获得更好的语音转录结果,您可以指定原始音频的来源。这样,Cloud STT 就可以在处理您的音频文件时使用针对类似数据训练过的机器学习模型。

目标

  • 向 Cloud STT 发送视频文件的音频转写请求。

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

  • Cloud Speech-to-Text

如需根据您的预计使用量来估算费用,请使用价格计算器

新 Google Cloud 用户可能有资格申请免费试用

准备工作

本教程有几个前提条件:

准备音频数据

在从视频转录音频之前,您必须从视频文件中提取数据。提取音频数据后,您必须将其存储在 Cloud Storage 存储桶中,或者将其转换为 base64 编码。

提取音频数据

您可以使用能够处理音频和视频文件的任何文件转换工具,例如 FFmpeg

通过以下代码段,使用 ffmpeg 将视频文件转换为音频文件。

ffmpeg -i video-input-file audio-output-file

存储或转换音频数据

您可以转写本地机器或 Cloud Storage 存储桶中存储的音频文件。

通过 Google Cloud CLI,使用以下命令将音频文件上传到现有的 Cloud Storage 存储桶。

gcloud storage cp audio-output-file storage-bucket-uri

如果您使用本地文件并且打算从命令行使用 curl 工具来发送请求,则必须首先将音频文件转换为 base64 编码的数据

请使用以下命令将音频文件转换为文本文件。

base64 audio-output-file -w 0 > audio-data-text

发送转录请求

请使用以下代码将转写请求发送到 Cloud STT。

本地文件请求

协议

如需了解完整的详细信息,请参阅 speech:recognize API 端点。

如需执行同步语音识别,请发出 POST 请求并提供相应的请求正文。以下示例展示了一个使用 curl 发出的 POST 请求。该示例使用 Google Cloud CLI 生成访问令牌。如需了解如何安装 gcloud CLI,请参阅快速入门

curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    https://speech.googleapis.com/v2/speech:recognize \
    --data '{
    "config": {
        "encoding": "LINEAR16",
        "sampleRateHertz": 16000,
        "languageCode": "en-US",
        "model": "video"
    },
    "audio": {
        "uri": "gs://cloud-samples-tests/speech/Google_Gnome.wav"
    }
}'

如需详细了解如何配置请求正文,请参阅 RecognitionConfig 参考文档。

如果请求成功,服务器将返回一个 200 OK HTTP 状态代码以及 JSON 格式的响应:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "OK Google stream stranger things from
            Netflix to my TV okay stranger things from
            Netflix playing on TV from the people that brought you
            Google home comes the next evolution of the smart home
            and it's just outside your window me Google know hi
            how can I help okay no what's the weather like outside
            the weather outside is sunny and 76 degrees he's right
            okay no turn on the hose I'm holding sure okay no I'm can
            I eat this lemon tree leaf yes what about this Daisy yes
            but I wouldn't recommend it but I could eat it okay
            Nomad milk to my shopping list I'm sorry that sounds like
            an indoor request I keep doing that sorry you do keep
            doing that okay no is this compost really we're all
            compost if you think about it pretty much everything is
            made up of organic matter and will return",
          "confidence": 0.9251011
        }
      ]
    }
  ]
}

Go

如需了解如何安装和使用 Cloud STT 客户端库,请参阅 Cloud STT 客户端库。如需了解详情,请参阅 Cloud STT Go API 参考文档

如需向 Cloud STT 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证


func modelSelection(w io.Writer) error {
	ctx := context.Background()

	client, err := speech.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	data, err := os.ReadFile("../testdata/Google_Gnome.wav")
	if err != nil {
		return fmt.Errorf("ReadFile: %w", err)
	}

	req := &speechpb.