详细分析 ▾
运行时依赖
版本
从 all-task-skills-dedup 批量发布
安装命令
点击复制技能文档
Use the gTTS (Google Text-to-Speech) Python library to convert text to audio.
When to use this skill
- Converting text to spoken audio (e.g., for audiobooks, podcasts)
- Speech synthesis from text input
- Handling long text by chunking and concatenating audio
Installation
pip install gtts pydub
Optionally, install ffmpeg for audio concatenation:
# Ubuntu/Debian sudo apt-get install ffmpeg# macOS brew install ffmpeg
# Windows # Download from https://ffmpeg.org/download.html
Usage
Basic usage
from gtts import gTTS
text = "Hello, this is a test." tts = gTTS(text=text, lang='en') tts.save("output.mp3")
Handling long text
For long text, chunk it and concatenate the audio files:
import os from gtts import gTTS from pydub import AudioSegment from pydub.playback import playdef text_to_audio(text, lang='en', chunk_size=1000): """Convert long text to audio by chunking.""" # Split text into chunks chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)] temp_files = [] for i, chunk in enumerate(chunks): tts = gTTS(text=chunk, lang=lang) temp_file = f"temp_chunk_{i}.mp3" tts.save(temp_file) temp_files.append(temp_file) # Concatenate audio files combined = AudioSegment.empty() for file in temp_files: combined += AudioSegment.from_mp3(file) combined.export("output.mp3", format="mp3") # Cleanup for file in temp_files: os.remove(file)
# Usage long_text = "Your long text here..." text_to_audio(long_text, lang='en')
Using ffmpeg for concatenation
import os import subprocess from gtts import gTTSdef text_to_audio_ffmpeg(text, lang='en', chunk_size=1000): """Convert long text to audio using ffmpeg for concatenation.""" chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)] temp_files = [] for i, chunk in enumerate(chunks): tts = gTTS(text=chunk, lang=lang) temp_file = f"temp_chunk_{i}.mp3" tts.save(temp_file) temp_files.append(temp_file) # Create file list for ffmpeg with open("file_list.txt", "w") as f: for file in temp_files: f.write(f"file '{file}'\n") # Concatenate using ffmpeg subprocess.run([ "ffmpeg", "-f", "concat", "-safe", "0", "-i", "file_list.txt", "-c", "copy", "output.mp3" ]) # Cleanup for file in temp_files: os.remove(file) os.remove("file_list.txt")
# Usage long_text = "Your long text here..." text_to_audio_ffmpeg(long_text, lang='en')
Notes
- gTTS sends text to Google's public TTS endpoints
- Avoid sending sensitive or confidential information
- gTTS is different from Google Cloud Text-to-Speech (no API key required)
- May be rate-limited or subject to service changes
- For production use, consider Google Cloud Text-to-Speech API