Transcribe speech to text by using the command line

This page shows you how to send a speech recognition request to Speech-to-Text using the REST interface and the curl command.

Speech-to-Text enables easy integration of Google speech recognition technologies into developer applications. You can send audio data to the Speech-to-Text API, which then returns a text transcription of that audio file. For more information about the service, see Cloud STT basics.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Set up a Google Cloud console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Speech-to-Text API for that project.

    You can view and manage these resources at any time in the Google Cloud console.

  3. Install the Google Cloud CLI.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Set up a Google Cloud console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Speech-to-Text API for that project.

    You can view and manage these resources at any time in the Google Cloud console.

  6. Install the Google Cloud CLI.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init

Make an audio transcription request

Use the following code sample to send a recognize REST request to the Speech-to-Text API.

  1. Run this command to create a JSON file as input for the request. Replace /full/path/to/audio/file.wav with the path to the audio file you want to transcribe:

    echo "{
      \"config\": {
        \"auto_decoding_config\": {},
        \"language_codes\": [\"en-US\"],
        \"model\": \"long\"
      },
      \"content\": \"$(base64 -w 0 /full/path/to/audio/file.wav | sed 's/+/-/g; s/\//_/g')\"
    }" > /tmp/data.txt
  2. Use curl to make a recognize request:

    curl -X POST -H "Content-Type: application/json; charset=utf-8" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -d @/tmp/data.txt \
        https://speech.googleapis.com/v2/projects/PROJECT_ID/locations/global/recognizers/_:recognize

    You should see a response similar to the following:

      {
        "results": [
          {
            "alternatives": [
              {
                "transcript": "how old is the Brooklyn Bridge",
                "confidence": 0.98267895
              }
            ]
          }
        ]
      }
    

You sent your first request to Speech-to-Text.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

    gcloud auth application-default revoke
  2. Optional: Revoke credentials from the gcloud CLI.

    gcloud auth revoke

Console

  • In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  • In the project list, select the project that you want to delete, and then click Delete.
  • In the dialog, type the project ID, and then click Shut down to delete the project.
  • gcloud

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

    What's next