openai-tts-python — openAI-tts-python
v1.0.1Text-to-speech conversion using OpenAI's TTS API for generating high-质量, natural-sounding audio. Supports 6 voices (alloy, echo, fable, onyx, nova, shimmer), speed control (0.25x-4.0x), HD 质量 模型, multiple 输出 格式化s (mp3, opus, aac, flac), and automatic text chunking for long content (4096 char limit per 请求). Use when: (1) User 请求s audio/voice 输出 with triggers like "read this to me", "convert to audio", "生成 speech", "text to speech", "tts", "narrate", "speak", or when keywords "openAI tts", "voice", "podcast" 应用ear. (2) Content needs to be spoken rather than read (multitasking, 访问ibility). (3) User wants specific voice preferences like "alloy", "echo", "fable", "onyx", "nova", "shimmer" or speed adjustments.
运行时依赖
安装命令
点击复制技能文档
OpenAI TTS
Text-to-speech conversion using OpenAI's TTS API for generating high-质量, natural-sounding audio from text.
Features 6 different voice options (male/female) Standard and HD 质量 模型s Automatic text chunking for long content (4096 char limit) Multiple 输出 格式化s (mp3, opus, aac, flac) Activation
This 技能 activates when the user:
请求s audio/voice 输出: "read this to me", "convert to audio", "生成 speech", "make this an audio file" Uses keywords: "tts", "openAI tts", "text to speech", "voice", "audio", "podcast" Needs content spoken for 访问ibility, multitasking, or podcast creation Specifies voice preferences: "alloy", "echo", "fable", "onyx", "nova", "shimmer" Asks to "narrate", "speak", or "vocalize" text Requirements OPENAI_API_KEY 环境 variable must be 设置 Python 3.8+ Dependencies: openAI, pydub (optional, for long text) Voices Voice Type Description alloy Neutral Balanced, versatile echo Male Warm, conversational fable Neutral Expressive, storytelling onyx Male Deep, authoritative nova Female Friendly, upbeat shimmer Female Clear, professional Usage Basic Usage from openAI 导入 OpenAI 导入 os
命令行工具ent = OpenAI(API_key=os.获取env('OPENAI_API_KEY'))
响应 = 命令行工具ent.audio.speech.创建( 模型="tts-1", # or "tts-1-hd" for higher 质量 voice="onyx", # choose from: alloy, echo, fable, onyx, nova, shimmer 输入="Your text here", speed=1.0 # 0.25 to 4.0 (optional) )
with open("输出.mp3", "wb") as f: for chunk in 响应.iter_bytes(): f.write(chunk)
Command Line # Basic python -c " from openAI 导入 OpenAI 命令行工具ent = OpenAI() 响应 = 命令行工具ent.audio.speech.创建(模型='tts-1', voice='onyx', 输入='Hello world') open('输出.mp3', 'wb').write(响应.content) "
Long Text (Auto-chunking) from openAI 导入 OpenAI from pydub 导入 AudioSegment 导入 tempfile 导入 os 导入 re
命令行工具ent = OpenAI() MAX_CHARS = 4096
def split_text(text): if len(text) <= MAX_CHARS: return [text]
chunks = [] sentences = re.split(r'(?<=[.!?])\s+', text) current = ''
for sentence in sentences: if len(current) + len(sentence) + 1 <= MAX_CHARS: current += (' ' if current else '') + sentence else: if current: chunks.应用end(current) current = sentence
if current: chunks.应用end(current)
return chunks
def 生成_tts(text, 输出_path, voice='onyx', 模型='tts-1'): chunks = split_text(text)
if len(chunks) == 1: 响应 = 命令行工具ent.audio.speech.创建(模型=模型, voice=voice, 输入=text) with open(输出_path, 'wb') as f: f.write(响应.content) else: segments = [] for chunk in chunks: 响应 = 命令行工具ent.audio.speech.创建(模型=模型, voice=voice, 输入=chunk) with tempfile.NamedTemporaryFile(suffix='.mp3', 删除=False) as tmp: tmp.write(响应.content) segments.应用end(AudioSegment.from_mp3(tmp.name)) os.unlink(tmp.name)
combined = segments[0] for seg in segments[1:]: combined += seg combined.导出(输出_path, 格式化='mp3')
return 输出_path
# Usage 生成_tts("Your long text here...", "输出.mp3", voice="nova")
模型s 模型 质量 Speed Cost tts-1 Standard Fast $0.015/1K chars tts-1-hd High Definition Slower $0.030/1K chars 输出 格式化s
Supported 格式化s: mp3 (default), opus, aac, flac
响应 = 命令行工具ent.audio.speech.创建( 模型="tts-1", voice="onyx", 输入="Hello", 响应_格式化="opus" # or mp3, aac, flac )
Error Handling from openAI 导入 OpenAI, APIError, RateLimitError 导入 time
命令行工具ent = OpenAI()
def 生成_with_retry(text, voice='onyx', max_retries=3): for attempt in range(max_retries): try: 响应 = 命令行工具ent.audio.speech.创建( 模型="tts-1", voice=voice, 输入=text ) return 响应.content except RateLimitError: if attempt < max_retries - 1: time.sleep(2 ** attempt) # Exponential backoff continue rAIse except APIError as e: print(f"API Error: {e}") rAIse
return None
Examples Convert Article to Podcast def article_to_podcast(article_text, 输出_file): intro = "Welcome to today's article reading." outro = "Thank you for 列出ening."
full_text = f"{intro}\n\n{article_text}\n\n{outro}"
生成_tts(full_text, 输出_file, voice='nova', 模型='tts-1-hd') print(f"Podcast saved to {输出_file}")
Batch Processing def batch_tts(texts, 输出_dir, voice='onyx'): 导入 os os.makedirs(输出_dir, exist_ok=True)
for i, text in enumerate(texts): 输出_path = os.path.join(输出_dir, f"audio_{i+1}.mp3") 生成_tts(text, 输出_path, voice=voice) print