> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crazyrouter.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python voz

> Implementación de Python para convertir texto a voz (TTS) y voz a texto (STT)

> Contenido actualizado: 2026-06-06

## Conversión de texto a voz (TTS)

```python theme={null}
from openai import OpenAI

client = OpenAI(api_key="sk-xxx", base_url="https://api.crazyrouter.com/v1")

# 生成语音
response = client.audio.speech.create(
    model="tts-1",
    voice="alloy",
    input="你好，欢迎使用 Crazyrouter API 服务。"
)

# 保存为 MP3 文件
response.stream_to_file("output.mp3")
print("语音已保存到 output.mp3")
```

### Voces disponibles

| Voz | Características |
| - | - |
| `alloy` | Neutral, equilibrada |
| `echo` | Masculina, estable |
| `fable` | Masculina, cálida |
| `onyx` | Masculina, profunda |
| `nova` | Femenina, animada |
| `shimmer` | Femenina, suave |

### TTS de alta calidad

```python theme={null}
# 使用 tts-1-hd 获得更高音质
response = client.audio.speech.create(
    model="tts-1-hd-1106",
    voice="nova",
    input="高质量语音合成示例",
    response_format="opus",  # 支持 mp3, opus, aac, flac
    speed=1.0  # 0.25 到 4.0
)

response.stream_to_file("output_hd.opus")
```

## Conversión de voz a texto (STT)

```python theme={null}
# Whisper 语音识别
audio_file = open("recording.mp3", "rb")

transcript = client.audio.transcriptions.create(
    model="whisper-1",
    file=audio_file,
    language="zh"  # 可选，指定语言提高准确率
)

print(transcript.text)
```

### Transcripción con marcas de tiempo

```python theme={null}
transcript = client.audio.transcriptions.create(
    model="whisper-1",
    file=open("recording.mp3", "rb"),
    response_format="verbose_json",
    timestamp_granularities=["segment"]
)

for segment in transcript.segments:
    print(f"[{segment['start']:.1f}s - {segment['end']:.1f}s] {segment['text']}")
```

## Traducción de voz

Convierte voz en idiomas que no sean inglés a texto en inglés:

```python theme={null}
audio_file = open("chinese_audio.mp3", "rb")

translation = client.audio.translations.create(
    model="whisper-1",
    file=audio_file
)

print(translation.text)  # 英文输出
```

> [!NOTE]
>
> Los modelos compatibles con TTS incluyen `tts-1`, `tts-1-hd-1106` y `tts-1`. STT utiliza el modelo `whisper-1`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.