script Python intel·ligent

Detecta si el text és en català o anglès

Tria automàticament la veu adequada per a edge-tts

Genera un fitxer .mp3 amb veu natural

tts_auto.py
import sys
import subprocess
from langdetect import detect

# 🔧 Configura les veus segons l'idioma
VOICES = {
    'ca': 'ca-ES-JoanaNeural',
    'en': 'en-GB-LibbyNeural'
}

def detect_language(text):
    try:
        lang = detect(text)
        return lang if lang in VOICES else 'en'
    except:
        return 'en'

def synthesize(text, output_file):
    lang = detect_language(text)
    voice = VOICES[lang]
    print(f"Idioma detectat: {lang} → Veu: {voice}")
    
    command = [
        '/var/services/homes/urqtejmi/.local/bin/edge-tts',
        '--text', text,
        '--voice', voice,
        '--write-media', output_file
    ]
    
    subprocess.run(command)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Ús: python tts_auto.py \"Text a llegir\"")
        sys.exit(1)

    text = sys.argv[1]
    output = "sortida.mp3"
    synthesize(text, output)
    print(f"Àudio generat: {output}")

Deixa un comentari

L'adreça electrònica no es publicarà. Els camps necessaris estan marcats amb *

This site uses Akismet to reduce spam. Learn how your comment data is processed.