Skip to content
Engineering

Transcribing Audio & Video Using OpenAI Whisper API

Manage large audio or video files with pydub and OpenAI

Most of the recordings I want transcribed are long ones: talks, meetings, the odd screencast. The Whisper API handles the speech-to-text part well, but any recording of real length runs straight into the API's file size limit. So the actual job becomes: split the audio into chunks, transcribe each chunk, and stitch the text back together. This post walks through that workflow with a bit of Python.

When I first wrote this, Whisper had no UI and whisper-1 was the only transcription model on offer. OpenAI has since shipped newer transcription models (gpt-4o-transcribe and gpt-4o-mini-transcribe) that are drop-in replacements for the model name below, but the size limit and the chunking workflow are unchanged.

The 25 MB limit

By default, the Whisper API only supports files that are less than 25 MB. If you have an audio file that is longer than that, you will need to break it up into chunks of 25 MB or less or use a compressed audio format. To get the best performance, we suggest that you avoid breaking the audio up mid-sentence as this may cause some context to be lost

That limit is the whole reason this post exists. For longer recordings, you divide the audio into smaller chunks, transcribe each chunk individually, and then combine the results. Keeping chunks comfortably under the limit also avoids timeouts and flaky uploads.

Step-by-Step Guide to Transcribing Audio with Whisper API

1. Install Required Libraries

First, you need to install the necessary Python libraries. You can do this using pip:

pip install openai pydub

The library is used for audio processing, and openai is the official library to interact with OpenAI's APIs. One practical note: pydub is a wrapper around , so ffmpeg needs to be installed on your machine (brew install ffmpeg on macOS, apt install ffmpeg on Debian/Ubuntu) or pydub will fail to open anything that isn't a plain WAV file.

2. Chunking the Audio File

You can use the pydub library to split your audio file into smaller chunks. Here's a Python script to do that:

import osfrom pydub import AudioSegment
script_dir = os.path.dirname(__file__)
def chunk_audio(file_path, chunk_length_ms=60000): audio = AudioSegment.from_file(file_path) audio_length_ms = len(audio) chunks = []
for i in range(0, audio_length_ms, chunk_length_ms): chunk = audio[i:i+chunk_length_ms] chunks.append(chunk)
return chunks
chunks = chunk_audio(os.path.join(script_dir, "<FILE_NAME>.mp3"))for i, chunk in enumerate(chunks): chunk.export(f'chunk_{i+1}.mp3', format='mp3')

This script divides the audio into 1-minute chunks. You can adjust the chunk_length_ms parameter based on your needs, for example:

....# create 10-minute chunkschunk_length_ms = 10 * 60 * 1000chunks = chunk_audio(os.path.join(script_dir, "<FILE_NAME>.mp3"), chunk_length_ms)

Keep the chunk duration honest against the 25 MB limit: how many minutes fit depends entirely on the bitrate. At 128 kbps an MP3 hits 25 MB at roughly 27 minutes, so something like a 45-minute chunk can easily blow past the limit; 10 to 20 minutes leaves a safe margin for most encodings.

One more caveat worth being upfront about: slicing at fixed time boundaries will happily cut through the middle of a word or sentence, which is exactly what the warning above tells you to avoid; each cut can lose or garble a few words of context. The simple code is good enough for my use cases, but the upgrade path is silence-based splitting with pydub's (split_on_silence / detect_silence), which snaps the cut points to natural pauses.

3. Transcribing Each Chunk

Next, you need to transcribe each chunk using the Whisper API:

from openai import OpenAI
openai = OpenAI(api_key='<OPENAI_API_KEY>')
def transcribe_audio(file_path): with open(file_path, 'rb') as audio_file: response = openai.audio.transcriptions.create(model="whisper-1", file=audio_file) return response.text

4. Putting it all together

Finally, you can aggregate the transcriptions from each chunk into a single text file:

import osfrom openai import OpenAIfrom pydub import AudioSegment
openai = OpenAI(api_key='<OPENAI_API_KEY>')script_dir = os.path.dirname(__file__)
transcriptions = []
def transcribe_audio(file_path): with open(file_path, 'rb') as audio_file: response = openai.audio.transcriptions.create(model="whisper-1", file=audio_file) return response.text
def chunk_audio(file_path, chunk_length_ms=60000): audio = AudioSegment.from_file(file_path) audio_length_ms = len(audio) chunks = []
for i in range(0, audio_length_ms, chunk_length_ms): chunk = audio[i:i+chunk_length_ms] chunks.append(chunk)
return chunks
chunks = chunk_audio(os.path.join(script_dir, "<FILE_NAME>.mp3"))for i, chunk in enumerate(chunks): chunk.export(f'chunk_{i+1}.mp3', format='mp3') transcription = transcribe_audio(f'chunk_{i+1}.mp3') transcriptions.append(transcription)
# Combine all transcriptionsfull_transcription = ' '.join(transcriptions)print(full_transcription)

For more information on the Whisper API and its capabilities, check the

Update: How to transcribe video files

Transcribing video files is similar to transcribing audio files. You can extract the audio from the video file and then follow the same process as described above. Here's a Python script to extract audio from a video file using the moviepy library (note that moviepy 2.x dropped the old moviepy.editor module; you import from moviepy directly):

import os
from moviepy import VideoFileClip
script_dir = os.path.dirname(__file__)
# Define the input video file and output audio filemp4_file = os.path.join(script_dir, "movie.mov")mp3_file = "audio.mp3"
# Load the video clip and write its audio to a separate filewith VideoFileClip(mp4_file) as video_clip: video_clip.audio.write_audiofile(mp3_file)
print("Audio extraction successful!")
The opinions and views expressed on this blog are solely my own and do not reflect the opinions, views, or positions of my employer or any affiliated organizations. All content provided on this blog is for informational purposes only.