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
13config = aai.TranscriptionConfig(speech_model=aai.SpeechModel.slam_1)
14
15transcript = transcriber.transcribe(audio_file, config)
16
17if transcript.status == aai.TranscriptStatus.error:
18 print(f"Transcription failed: {transcript.error}")
19 exit(1)
20
21print(f" \nFull Transcript: \n\n{transcript.text}")

Before you begin

To complete this tutorial, you need:

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.

4

Select the speech model: Create a TranscriptionConfig object and set the speech_model to aai.SpeechModel.slam_1.

1config = aai.TranscriptionConfig(speech_model=aai.SpeechModel.slam_1)
Selecting the right speech model for your use-case

This example shows our latest prompt-based speech model, Slam-1. You can select the class of models to use in order to make cost-performance tradeoffs best suited for your application. See Models for more information about our available models.

Step 3: 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, 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

In this tutorial, you’ve learned how to generate a transcript for an audio file and how to set the speech model.

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.