merend

openai-tts-python

设计创意v1.0.1
作者:merend更新于 2026-05-11共 2 个版本 · 最新版本发布于 2026-01-25@clawhub_merend/openai-tts-python
音频处理#Podcast#Text-to-Speech#Audio

据 AlphaMoat 统计,截至 2026-09,openai-tts-python 累计下载 3,943 次、获 1 个星标,在全部 5,078 个 AI 技能中排名第 2,621 位,在「设计创意」分类(512 个)中排名第 243 位。

累计安装
218
累计下载
3,943
星标
1
全站排名
#2,621
全站前 52% · 「设计创意」前 48%
平台累计安装累计下载星标评论
ClawHub ↗1013,9431-
SkillHub ↗2183,4301—

各平台的计数口径不同,统计时间不一致。

这个技能是做什么的

该技能面向朗读、旁白和无障碍等语音输出需求,可选择不同音色、调整语速,并生成多种音频格式。对于较长文本,主文说明会自动分块处理;使用前需配置 API 凭据。

资料发布于 2026-10-27

可以用来做什么

  • 把文本生成语音,供收听或旁白使用
  • 根据需要选择音色、语速和输出格式

什么时候用

  • 用户希望将文字转换为可播放音频时
  • 较长文本需要按接口限制分段处理时

示例提示词

以下是根据技能文档生成的示例,不代表实际运行结果。

把这段文字转换为语音并使用清晰自然的朗读风格
用指定音色朗读这段内容,并输出为所需音频格式

月度数据趋势

累计下载3,943
1,9193,0064,0932026-03 · 累计下载 2,0692026-05 · 累计下载 2,5452026-06 · 累计下载 2,7202026-07 · 累计下载 2,9902026-08 · 累计下载 3,1782026-09 · 累计下载 3,94326-0326-09
星标1
0122026-03 · 星标 12026-05 · 星标 12026-06 · 星标 12026-07 · 星标 12026-08 · 星标 12026-09 · 星标 126-0326-09

2026-09 起统计方法调整:同名技能按作者分别统计,并覆盖全部技能。图中虚线为调整前的月份,与之后的数据不直接比较,也不计算跨越调整的增长。

月份累计下载环比星标
2026-093,943—1
2026-083,178+6.3%1
2026-072,990+9.9%1
2026-062,720+6.9%1
2026-052,545—1
2026-032,069—1

SKILL.md 原文

以下是作者发布的原始文件,按纯文本显示,本站未做修改或审核;其中的链接不可点击,命令请核实后再运行。抓取于 2026-10-23。

6.3 KB · ClawHub v1.0.1
---
name: openai-tts
description: |
  Text-to-speech conversion using OpenAI's TTS API for generating high-quality, natural-sounding audio.
  Supports 6 voices (alloy, echo, fable, onyx, nova, shimmer), speed control (0.25x-4.0x),
  HD quality model, multiple output formats (mp3, opus, aac, flac), and automatic text chunking
  for long content (4096 char limit per request).
  Use when: (1) User requests audio/voice output with triggers like "read this to me",
  "convert to audio", "generate speech", "text to speech", "tts", "narrate", "speak",
  or when keywords "openai tts", "voice", "podcast" appear. (2) Content needs to be spoken
展开全部(共 222 行)
  rather than read (multitasking, accessibility). (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-quality, natural-sounding audio from text.

## Features
- 6 different voice options (male/female)
- Standard and HD quality models
- Automatic text chunking for long content (4096 char limit)
- Multiple output formats (mp3, opus, aac, flac)

## Activation

This skill activates when the user:
- Requests audio/voice output: "read this to me", "convert to audio", "generate speech", "make this an audio file"
- Uses keywords: "tts", "openai tts", "text to speech", "voice", "audio", "podcast"
- Needs content spoken for accessibility, multitasking, or podcast creation
- Specifies voice preferences: "alloy", "echo", "fable", "onyx", "nova", "shimmer"
- Asks to "narrate", "speak", or "vocalize" text

## Requirements

- `OPENAI_API_KEY` environment variable must be set
- 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
```python
from openai import OpenAI
import os

client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

response = client.audio.speech.create(
    model="tts-1",      # or "tts-1-hd" for higher quality
    voice="onyx",       # choose from: alloy, echo, fable, onyx, nova, shimmer
    input="Your text here",
    speed=1.0           # 0.25 to 4.0 (optional)
)

with open("output.mp3", "wb") as f:
    for chunk in response.iter_bytes():
        f.write(chunk)
```

### Command Line
```bash
# Basic
python -c "
from openai import OpenAI
client = OpenAI()
response = client.audio.speech.create(model='tts-1', voice='onyx', input='Hello world')
open('output.mp3', 'wb').write(response.content)
"
```

### Long Text (Auto-chunking)
```python
from openai import OpenAI
from pydub import AudioSegment
import tempfile
import os
import re

client = 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.append(current)
            current = sentence

    if current:
        chunks.append(current)

    return chunks

def generate_tts(text, output_path, voice='onyx', model='tts-1'):
    chunks = split_text(text)

    if len(chunks) == 1:
        response = client.audio.speech.create(model=model, voice=voice, input=text)
        with open(output_path, 'wb') as f:
            f.write(response.content)
    else:
        segments = []
        for chunk in chunks:
            response = client.audio.speech.create(model=model, voice=voice, input=chunk)
            with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as tmp:
                tmp.write(response.content)
                segments.append(AudioSegment.from_mp3(tmp.name))
                os.unlink(tmp.name)

        combined = segments[0]
        for seg in segments[1:]:
            combined += seg
        combined.export(output_path, format='mp3')

    return output_path

# Usage
generate_tts("Your long text here...", "output.mp3", voice="nova")
```

## Models

| Model | Quality | Speed | Cost |
|-------|---------|-------|------|
| tts-1 | Standard | Fast | $0.015/1K chars |
| tts-1-hd | High Definition | Slower | $0.030/1K chars |

## Output Formats

Supported formats: `mp3` (default), `opus`, `aac`, `flac`

```python
response = client.audio.speech.create(
    model="tts-1",
    voice="onyx",
    input="Hello",
    response_format="opus"  # or mp3, aac, flac
)
```

## Error Handling

```python
from openai import OpenAI, APIError, RateLimitError
import time

client = OpenAI()

def generate_with_retry(text, voice='onyx', max_retries=3):
    for attempt in range(max_retries):
        try:
            response = client.audio.speech.create(
                model="tts-1",
                voice=voice,
                input=text
            )
            return response.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
```python
def article_to_podcast(article_text, output_file):
    intro = "Welcome to today's article reading."
    outro = "Thank you for listening."

    full_text = f"{intro}\n\n{article_text}\n\n{outro}"

    generate_tts(full_text, output_file, voice='nova', model='tts-1-hd')
    print(f"Podcast saved to {output_file}")
```

### Batch Processing
```python
def batch_tts(texts, output_dir, voice='onyx'):
    import os
    os.makedirs(output_dir, exist_ok=True)

    for i, text in enumerate(texts):
        output_path = os.path.join(output_dir, f"audio_{i+1}.mp3")
        generate_tts(text, output_path, voice=voice)
        print(f"Generated: {output_path}")
```

## Links

- [OpenAI TTS Documentation](https://platform.openai.com/docs/guides/text-to-speech)
- [OpenAI API Reference](https://platform.openai.com/docs/api-reference/audio/createSpeech)
- [Pricing](https://openai.com/pricing)

「设计创意」分类下的热门技能

数据月份:2026-09 · 下载/星标/安装数聚合自公开技能注册库(ClawHub、SkillHub),按月更新。口径说明见数据方法。