Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /web_ui /whatsapp-bot /tts.py
| """ | |
| Edge-TTS Wrapper for WhatsApp voice messages. | |
| Generates audio from text using Microsoft Edge's free TTS API. | |
| """ | |
| import json | |
| import os | |
| import subprocess | |
| import tempfile | |
| # Default voice | |
| VOICE = os.environ.get("TTS_VOICE", "de-DE-KatjaNeural") | |
| TTS_RATE = os.environ.get("TTS_RATE", "+0%") | |
| def generate_audio(text: str, voice: str = VOICE) -> bytes | None: | |
| """Generate audio bytes from text using edge-tts. | |
| Returns: MP3 bytes or None on failure | |
| """ | |
| try: | |
| tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) | |
| tmp_path = tmp.name | |
| tmp.close() | |
| subprocess.run( | |
| ["edge-tts", "--voice", voice, "--rate", TTS_RATE, "--text", text, "--write-media", tmp_path], | |
| capture_output=True, | |
| timeout=60, | |
| check=True, | |
| ) | |
| with open(tmp_path, "rb") as f: | |
| audio_data = f.read() | |
| os.unlink(tmp_path) | |
| return audio_data | |
| except subprocess.CalledProcessError as e: | |
| print(f"TTS error: {e.stderr.decode() if e.stderr else e}") | |
| except FileNotFoundError: | |
| print("edge-tts not installed. Run: pip install edge-tts") | |
| except Exception as e: | |
| print(f"TTS error: {e}") | |
| return None | |
| def list_voices() -> list[dict]: | |
| """List available edge-tts voices.""" | |
| try: | |
| result = subprocess.run( | |
| ["edge-tts", "--list-voices"], | |
| capture_output=True, | |
| text=True, | |
| timeout=30, | |
| ) | |
| voices = [] | |
| for line in result.stdout.strip().split("\n"): | |
| if line and not line.startswith("Name"): | |
| parts = line.split() | |
| if parts: | |
| voices.append({"name": parts[0], "locale": parts[1] if len(parts) > 1 else ""}) | |
| return voices | |
| except Exception as e: | |
| return [{"error": str(e)}] | |
| if __name__ == "__main__": | |
| import sys | |
| text = " ".join(sys.argv[1:]) or "Hello, this is a test." | |
| data = generate_audio(text) | |
| if data: | |
| out = sys.argv[1] if len(sys.argv) > 1 else "output.mp3" | |
| with open("output.mp3", "wb") as f: | |
| f.write(data) | |
| print(f"Audio saved: output.mp3 ({len(data)} bytes)") | |
| else: | |
| print("TTS failed") | |
Xet Storage Details
- Size:
- 2.25 kB
- Xet hash:
- 9633e444830699ba9050dfe4642a34d08200c4ee1645f12fe98447a240bc091c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.