Sub topic 5
Transcribing Recordings That Exceed Size Limits
This chapter assumes you can call the transcription endpoint from Python, as shown in the earlier chapters. You should also know what MP3 and WAV files are, and you should have your API key set in an environment variable.
Why long recordings fail
Hosted transcription APIs cap the size of each uploaded file. OpenAI's transcription endpoint, for example, has historically accepted files up to 25 MB. Check your provider's current documentation, because the limit and the supported formats can change.
A one-hour meeting saved as MP3 can easily pass that limit. A WAV file of the same meeting is far larger, because WAV is uncompressed. The usual fix is to cut the recording into chunks, transcribe each chunk, and join the texts in order.
The chunking workflow
Splitting with pydub
The pydub library reads many audio formats and slices audio like a Python list. It needs ffmpeg installed on your system to decode MP3 files. Lengths and slice positions are in milliseconds.
from pydub import AudioSegment
CHUNK_MS = 10 * 60 * 1000 # 10 minutes per chunk
audio = AudioSegment.from_file("long_recording.mp3")
chunk_paths = []
for index, start in enumerate(range(0, len(audio), CHUNK_MS)):
path = f"chunk_{index:03d}.mp3"
audio[start:start + CHUNK_MS].export(path, format="mp3")
chunk_paths.append(path)
print(chunk_paths)The zero-padded file names (chunk_000, chunk_001) keep the chunks in the correct order when sorted.
Choosing a chunk length
Size depends on duration and bitrate, not on duration alone. A 128 kbps MP3 uses about 1 MB per minute, so ten minutes is roughly 10 MB. A higher bitrate or a different codec changes that.
After exporting, check each file against the limit before you upload it:
import os
LIMIT_BYTES = 25 * 1024 * 1024
for path in chunk_paths:
size = os.path.getsize(path)
if size > LIMIT_BYTES:
raise ValueError(f"{path} is {size} bytes, over the limit")If a chunk is too large, shorten CHUNK_MS or export at a lower bitrate.
Joining the results
Transcribe the chunks in order and join the texts. The function below reuses the retry helper idea from the errors chapter, so it uses a plain call here to stay short.
from openai import OpenAI
client = OpenAI()
def transcribe_chunks(paths):
texts = []
for path in paths:
with open(path, "rb") as f:
result = client.audio.transcriptions.create(model="whisper-1", file=f)
texts.append(result.text.strip())
return " ".join(texts)Common problems
- Words cut in half. A fixed cut can land in the middle of a word. The model may then mishear it at the end of one chunk and the start of the next. Cutting at silence, or overlapping chunks by a second or two and removing duplicate words, reduces this.
- Lost context. Each chunk is transcribed alone, so names and jargon can be spelled differently from chunk to chunk. Pass the same
promptwith domain vocabulary to every call. You can also pass the end of the previous chunk's text as the prompt. - Wrong timestamps. If you request segment timestamps, each chunk starts at zero. Add the chunk's start offset (
index * CHUNK_MS / 1000seconds) to every segment time. - Failures halfway through. A long job makes many calls, so one failure should not discard the finished work. Save each chunk's text as soon as it returns, and retry only the failed chunk.
Cost note
Transcription is typically billed by audio duration. Chunking does not change the total minutes, so splitting does not raise the price. Overlap does add a small amount, because the overlapping seconds are transcribed twice.
Exercise
A 60-minute MP3 is 58 MB, and the API limit is 25 MB. You split it into 10-minute chunks. Roughly how large is each chunk, and why should you still check file sizes? Also, a segment in the third chunk (index 2) starts at 12.0 seconds within that chunk. What timestamp does it have in the full recording?
Show answer
Each chunk is about 58 / 6 ≈ 9.7 MB, which is under the limit. You should still check, because size depends on bitrate and a variable-bitrate file can have chunks of uneven size. The third chunk starts at 2 × 600 = 1200 seconds, so the segment is at 1212.0 seconds in the full recording.