light-ai-video-generator / scripts /generate_voice.py
malvin noel
Corrected initial push of light AI Video Generator
8b05224
raw
history blame
990 Bytes
import os
import soundfile as sf
from kokoro import KPipeline
import random
pipeline = KPipeline(lang_code='a') # 'a' for English
ENGLISH_VOICES = [
"af_heart",
"en_us_amy",
"en_deep",
"en_female",
"en_male"
]
def generate_voice(text: str, path: str):
for voice in random.sample(ENGLISH_VOICES, len(ENGLISH_VOICES)):
try:
print(f"πŸ”Š Trying voice: {voice}")
generator = pipeline(text, voice=voice)
for i, (gs, ps, audio) in enumerate(generator):
if i == 0:
sf.write(path, audio, 24000)
print(f"βœ… Audio saved with {voice} at: {path}")
return True
except Exception as e:
print(f"❌ Failed with {voice}: {e}")
continue
print("πŸ›‘ All voices failed.")
if os.path.exists(path):
os.remove(path)
print("πŸ—‘οΈ Removed broken file.")
return False