Python Speech-to-Text
Overview
The Audio API exposes two main endpoints:
- transcriptions: speech to text
- translations: translate audio into English
Supported formats
- Max file size: 25 MB
- Formats: mp3, mp4, mpeg, mpg, m4a, wav, webm
Usage
1. Transcription
Convert audio to text in the source language
from openai import OpenAI
client = OpenAI(
base_url="https://4zapi.com/v1",
api_key=key
)
# Basic transcription
audio_file = open("/path/to/file/audio.mp3", "rb")
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print(transcription.text)
# Specify output format
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
2. Translation
Convert audio in any language to English text
from openai import OpenAI
client = OpenAI(
base_url="https://4zapi.com/v1",
api_key=key
)
audio_file = open("/path/to/file/german.mp3", "rb")
translation = client.audio.translations.create(
model="whisper-1",
file=audio_file
)
print(translation.text)
3. Timestamps
from openai import OpenAI
client = OpenAI(
base_url="https://4zapi.com/v1",
api_key=key
)
audio_file = open("speech.mp3", "rb")
transcript = client.audio.transcriptions.create(
file=audio_file,
model="whisper-1",
response_format="verbose_json",
timestamp_granularities=["word"]
)
print(transcript.words)
4. Large files
Split files larger than 25MB with PyDub:
from pydub import AudioSegment
song = AudioSegment.from_mp3("good_morning.mp3")
# Split into 10-minute chunks
ten_minutes = 10 * 60 * 1000
first_10_minutes = song[:ten_minutes]
first_10_minutes.export("good_morning_10.mp3", format="mp3")
Tips
Prompt tips
- Correct specific terms
- Keep context consistent
- Control punctuation
- Preserve filler words when needed
- Control output style (e.g. Simplified vs Traditional Chinese)
Supported languages
About 98 languages, including:
- Major Asian languages: Chinese, Japanese, Korean, etc.
- European languages: English, French, German, etc.
- Others: Arabic, Hindi, etc.
Note: Only languages with WER under 50% are listed; others may work with lower quality