Sub topic 2
Transcribing Recordings That Exceed Size Limits
Assume basic Python, file handling, environment variables, and experience with an LLM chat API. A hosted speech API often limits the size of one uploaded file. Split a long recording into smaller files, transcribe each file, and join the results in order.
from pydub import AudioSegment
CHUNK_MS = 10 * 60 * 1000
MAX_BYTES = 25 * 1024 * 1024
audio = AudioSegment.from_file("long_recording.mp3")
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", bitrate="64k")
if __import__("os").path.getsize(path) > MAX_BYTES:
raise ValueError(f"{path} is still too large")Transcribe every chunk, then concatenate the returned text. Keep chunk boundaries visible if you need to review or correct repeated words.
Generating Speech from Text
Text-to-speech (TTS) converts text into an audio file. The following request writes synthesized speech to disk:
from openai import OpenAI
client = OpenAI()
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice="alloy",
input="Your order has shipped and should arrive on Friday.",
response_format="mp3",
) as response:
response.stream_to_file("reply.mp3")Choosing Voices and Output Formats
Choose a voice for clarity, language support, and the tone of your application. Use MP3 when bandwidth and storage matter. Use WAV when uncompressed audio or editing is more important. Output format affects both file size and playback support.
Streaming Audio Output
Streaming sends response data as it becomes available. The client can start saving or playing audio before the complete response arrives. This reduces perceived delay, but playback code must handle buffering and interruptions.
Building a Turn-Based Voice Loop
A turn-based loop waits for one recording, transcribes it, sends the text to the chat model, and synthesizes the reply. Store conversation history, but treat transcript text as untrusted input. Do not let spoken content override application rules.
from openai import OpenAI
client = OpenAI()
def voice_turn(input_path, history, output_path="reply.mp3"):
with open(input_path, "rb") as audio:
text = client.audio.transcriptions.create(
model="whisper-1",
file=audio,
).text
history.append({"role": "user", "content": text})
result = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Reply in one short spoken sentence."},
*history,
],
)
reply = result.choices[0].message.content
history.append({"role": "assistant", "content": reply})
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice="alloy",
input=reply,
response_format="mp3",
) as response:
response.stream_to_file(output_path)
return text, replyExercise
Why can a ten-minute chunk still exceed an API's file-size limit?
Show answer
Duration alone does not determine size. Codec, bitrate, channels, and audio content also affect the number of bytes.