This page demonstrates how to transcribe a short audio file to text using
synchronous speech recognition.

*Synchronous speech recognition* returns the recognized text for short audio
(less than 60 seconds).

Audio content can be sent directly to Cloud Speech-to-Text from a local file, or
Cloud Speech-to-Text can process audio content stored in a [Cloud Storage
bucket](https://docs.cloud.google.com/storage/docs/creating-buckets). See the [quotas and limits page](https://docs.cloud.google.com/speech-to-text/quotas) for limits on synchronous speech recognition requests.

> [!NOTE]
> **Note:** There is a limit of 60 seconds, 10 MB, or both for all requests sent to the API using local audio files. Requests that exceed this duration, size, or both will result in an error. For more information, see the [quotas and limits page](https://docs.cloud.google.com/speech-to-text/quotas) documentation.

## Before you begin

1. Client libraries can use [Application Default Credentials](https://docs.cloud.google.com/docs/authentication/application-default-credentials.md.txt) to easily authenticate with Google APIs and send requests to those APIs. With Application Default Credentials, you can test your application locally and deploy it without changing the underlying code. For more information, see [Authenticate for using client libraries](https://docs.cloud.google.com/docs/authentication/client-libraries.md.txt).
2.

   If you're using a local shell, then create local authentication credentials for your user
   account:

   ```bash
   gcloud auth application-default login
   ```

   You don't need to do this if you're using Cloud Shell.


   If an authentication error is returned, and you are using an external identity provider
   (IdP), confirm that you have
   [signed in to the gcloud CLI with your federated identity](https://docs.cloud.google.com/iam/docs/workforce-log-in-gcloud.md.txt).

Also ensure you have [installed the client library](https://docs.cloud.google.com/speech-to-text/docs/libraries.md.txt).

## Perform synchronous speech recognition on a local file

Here is an example of performing synchronous speech recognition on a local audio
file:

### Python

```python

from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

# Instantiates a client
client = SpeechClient()

# Reads a file as bytes
with open("resources/audio.wav", "rb") as f:
    audio_content = f.read()

config = cloud_speech.RecognitionConfig(
    auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
    language_codes=["en-US"],
    model="chirp_3",
)

request = cloud_speech.RecognizeRequest(
    recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
    config=config,
    content=audio_content,
)

# Transcribes the audio into text
response = client.recognize(request=request)

for result in response.results:
    print(f"Transcript: {result.alternatives[0].transcript}")
```

## Perform synchronous speech recognition on a remote file

For your convenience, Speech-to-Text API can perform synchronous speech recognition
directly on an audio file located in Cloud Storage, without the need to send
the contents of the audio file in the body of your request.

Speech-to-Text uses a service account to access your files in Cloud Storage.
By default, the service account has access to Cloud Storage files in the same
project.

The service account email address is the following:

    service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com

In order to transcribe Cloud Storage files in another project, you can give
this service account the \[Speech-to-Text Service Agent\]\[speech-service-agent\]
role in the other project:

```bash
gcloud projects add-iam-policy-binding PROJECT_ID \
    --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
    --role=roles/speech.serviceAgent
```

More information about project IAM policy is available at
\[Manage access to projects, folders, and organizations\]\[manage-access\].

You can also give the service account more granular access by giving it
permission to a specific Cloud Storage bucket:

```bash
gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
    --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
    --role=roles/storage.admin
```

More information about managing access to Cloud Storage is available at
\[Create and Manage access control lists\]\[buckets-manage-acl\]
in the Cloud Storage documentation.

Here is an example of performing synchronous speech recognition on a file
located in Cloud Storage:

### Python

```python
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

# Instantiates a client
client = SpeechClient()

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

config = cloud_speech.RecognitionConfig(
    auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
    language_codes=["en-US"],
    model="chirp_3",
)

request = cloud_speech.RecognizeRequest(
    recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
    config=config,
    uri="gs://cloud-samples-data/speech/audio.flac",  # URI of the audio file in Google Cloud Storage
)

# Transcribes the audio into text
response = client.recognize(request=request)

for result in response.results:
    print(f"Transcript: {result.alternatives[0].transcript}")

```

## 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.

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

   ```bash
   gcloud auth revoke
   ```

### Console


> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

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

  [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
- 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


> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

Delete a Google Cloud project:

```
gcloud projects delete PROJECT_ID
```

<br />

## What's next

- See the [reference documentation](https://docs.cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v2.md.txt#google.cloud.speech.v2.Speech.Recognize) for synchronous recognition.
- Learn how to [transcribe streaming audio](https://docs.cloud.google.com/speech-to-text/docs/streaming-recognize.md.txt).
- Learn how to [transcribe long audio files](https://docs.cloud.google.com/speech-to-text/docs/batch-recognize.md.txt).
- For best performance, accuracy, and other tips, see the [best practices](https://docs.cloud.google.com/speech-to-text/docs/best-practices.md.txt) documentation.