yt / src /pro_audio_generator.py
ayush77uh's picture
okk hai jiii
7a87625
Raw
History Blame Contribute Delete
1.49 kB
"""
Pro Audio Generator β€” Edge TTS (instant, no GPU/RAM needed).
Replaces XTTS-v2 to prevent OOM crashes on HF Spaces.
"""
import os
import asyncio
import time
import edge_tts
# English male voice β€” deep and engaging
VOICE = os.environ.get("TTS_VOICE", "en-US-GuyNeural")
def process_script_to_audio(script_text, output_filename="output.mp3"):
"""
Generates audio from text using Edge TTS.
Instant generation, no model loading, no RAM usage.
Returns path to generated audio file.
"""
output_dir = "output/audio"
os.makedirs(output_dir, exist_ok=True)
# Ensure .mp3 extension
if output_filename.endswith(".wav"):
output_filename = output_filename.replace(".wav", ".mp3")
output_path = os.path.join(output_dir, output_filename)
print(f"πŸŽ™οΈ Generating Audio ({len(script_text)} chars) with Edge TTS...", flush=True)
start_time = time.time()
try:
# Edge TTS is async, run it
asyncio.run(
edge_tts.Communicate(
script_text,
VOICE,
rate="+5%" # Slightly faster for engagement
).save(output_path)
)
elapsed = time.time() - start_time
size_kb = os.path.getsize(output_path) / 1024
print(f"βœ… Audio Generated in {elapsed:.1f}s ({size_kb:.0f} KB): {output_path}", flush=True)
return output_path
except Exception as e:
print(f"❌ Edge TTS Failed: {e}", flush=True)
return None