Skip to content

Instantly share code, notes, and snippets.

@Frank-Buss
Created December 26, 2025 04:52
Show Gist options
  • Select an option

  • Save Frank-Buss/effc0b4335fbeef84bae0632c05f78ea to your computer and use it in GitHub Desktop.

Select an option

Save Frank-Buss/effc0b4335fbeef84bae0632c05f78ea to your computer and use it in GitHub Desktop.
OpenAI text to speech with the tts-1-hd model
#!/usr/bin/env python3
"""Simple text-to-speech script using OpenAI's TTS API."""
import argparse
from pathlib import Path
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
def text_to_speech(text: str, output_file: str = "output.mp3", voice: str = "alloy"):
"""Convert text to speech and save to a file.
Args:
text: The text to convert to speech.
output_file: Output audio file path.
voice: Voice to use (alloy, echo, fable, onyx, nova, shimmer).
"""
client = OpenAI()
with client.audio.speech.with_streaming_response.create(
model="tts-1-hd",
voice=voice,
input=text,
) as response:
response.stream_to_file(Path(output_file))
print(f"Audio saved to {output_file}")
def main():
parser = argparse.ArgumentParser(description="Convert text to speech using OpenAI")
parser.add_argument("text", nargs="?", help="Text to convert to speech")
parser.add_argument("-o", "--output", default="output.mp3", help="Output file path")
parser.add_argument(
"-v", "--voice",
default="shimmer",
choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
help="Voice to use",
)
parser.add_argument("-f", "--file", help="Read text from a file")
args = parser.parse_args()
if args.file:
text = Path(args.file).read_text()
elif args.text:
text = args.text
else:
print("Enter text (Ctrl+D to finish):")
import sys
text = sys.stdin.read()
if not text.strip():
print("Error: No text provided")
return 1
text_to_speech(text, args.output, args.voice)
return 0
if __name__ == "__main__":
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment