Getting started

Transcribe a pre-recorded audio file

Learn how to transcribe and analyze an audio file.

Overview

By the end of this tutorial, you’ll be able to:

Here’s the full sample code for what you’ll build in this tutorial:

1import assemblyai as aai
2
3aai.settings.api_key = "<YOUR_API_KEY>"
4
5transcriber = aai.Transcriber()
6
7# You can use a local filepath:
8# audio_file = "./example.mp3"
9
10# Or use a publicly-accessible URL:
11audio_file = "https://assembly.ai/sports_injuries.mp3"
12
13# Optionally specify a speech model (defaults to "universal"):
14# config = aai.TranscriptionConfig(speech_models=["slam-1"])
15# transcript = transcriber.transcribe(audio_file, config=config)
16
17transcript = transcriber.transcribe(audio_file)
18
19if transcript.status == aai.TranscriptStatus.error:
20 print(f"Transcription failed: {transcript.error}")
21 exit(1)
22
23print(f" \nFull Transcript: \n\n{transcript.text}")

Before you begin

To complete this tutorial, you need:

  • If opting to use the Python or Python SDK code, you will need Python installed.
  • If opting to use the JavaScript or JavaScript SDK code, you will need Node.js installed.
  • A free AssemblyAI account.

Step 1: Install the necessary libraries

1

Install our Python SDK via pip:

$pip install assemblyai
2

Create a new file and import the assemblyai package.

1import assemblyai as aai

Step 2: Configure your request

In this step, you’ll create an SDK client and configure it to use your API key.

1

Browse to API Keys in your dashboard, and then copy your API key.

2

Create a new Transcriber and configure it to use your API key. Replace YOUR_API_KEY with your copied API key.

1aai.settings.api_key = "<YOUR_API_KEY>"
2
3transcriber = aai.Transcriber()
3

Specify a URL to the audio you want to transcribe. The URL needs to be accessible from AssemblyAI’s servers. For a list of supported formats, see FAQ.

1audio_file = "https://assembly.ai/sports_injuries.mp3"
Creating self hosted audio URLs

You can use a service like Amazon S3, Google Cloud Storage, or any platform that supports direct file access to generate a shareable audio file URL. Check out this cookbook on how to transcribe from an S3 bucket.

Local audio files

If you want to use a local file, you can also specify a local path, for example:

1audio_file = "./example.mp3"
YouTube

YouTube URLs are not supported. If you want to transcribe a YouTube video, you need to download the audio first.

Step 3: Select the speech model (optional)

AssemblyAI offers multiple speech models optimized for different use cases. You can use the speech_models parameter to specify which model to use for your transcription.

The available models are:

  • Universal (default): Best for out-of-the-box transcription with multi-language support and excellent accuracy.
  • Slam-1 (beta): Highest accuracy for English content with fine-tuning support and customization via prompting.

For more details on model capabilities and pricing, see Models.

If you do not specify a speech model, the Universal model will be used by default.

To specify a speech model, create a TranscriptionConfig with the speech_models parameter:

1config = aai.TranscriptionConfig(speech_models=["slam-1"])

You can also specify multiple models in priority order. The system will use the first compatible model:

1config = aai.TranscriptionConfig(speech_models=["slam-1", "universal"])

For more information on selecting speech models, see Select the speech model.

Step 4: Submit for transcription

1

To generate the transcript, pass the audio_file or file to transcriber.transcribe(). This may take a minute while we’re processing the audio.

1transcript = transcriber.transcribe(audio_file)

If you configured a TranscriptionConfig in Step 3, pass it to the transcribe() method:

1transcript = transcriber.transcribe(audio_file, config=config)
2

If the transcription failed, the status of the transcription will be set to error. To see why it failed you can print the value of error.

1if transcript.error:
2 print(transcript.error)
3 exit(1)
3

Print the complete transcript.

1print(transcript.text)
4

Run the application and wait for it to finish.

Next steps

Want to learn more?

Need some help?

If you get stuck, or have any other questions, we’d love to help you out. Contact our support team at support@assemblyai.com or create a support ticket.