Skip to main content
This guide shows you how to transcribe WAV audio files with varying sample rates using our Streaming API.
If you’re streaming a pre-recorded file for benchmarking or testing, see Stream a pre-recorded file in real time for wall-clock pacing that more closely simulates live microphone input.

Quickstart

Here is the complete Python script to transcribe a WAV audio file using the Streaming API.

Step-by-step guide

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up and get your API key from your dashboard.

Install and import packages

Install the required packages. PyAudio is optional — only needed for audio playback during streaming.
Import packages.

Configure settings

Set your ASSEMBLYAI_API_KEY environment variable. Set AUDIO_FILE to the relative or absolute path of your audio file, and set SAMPLE_RATE to match your file’s sample rate.

Helper functions

The following helper functions are used to validate audio files and save the transcript output:
  • validate_audio_file() - Validates that the audio file is a mono WAV file with the expected sample rate.
  • save_transcript() - Saves the transcript to a text file after the session ends.

WebSocket event handlers

Open WebSocket and stream audio file

When the connection opens, we start a background thread that reads the WAV file in 50ms chunks and sends them over the WebSocket. If PLAY_AUDIO is enabled, the audio is also played through your speakers.

Handle WebSocket messages

WebSocket error and close handlers

Connect and stream the file

The session will terminate once the file is finished streaming. If SAVE_TRANSCRIPT_TO_FILE is enabled (default), the transcript will be saved to {audio_filename}_{session_id}.txt in the current working directory.
The AUDIO_FILE path can be either relative (e.g., audio.wav) or absolute (e.g., /path/to/audio.wav).

Example output

Here’s an example of what the console output looks like when streaming an audio file:
The output shows:
  • Partial transcripts: Real-time updates as words are recognized
  • Final: The complete turn with proper capitalization and punctuation