File size: 513 Bytes
7e3c986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import time, edge_tts, base64, os


async def textToSpeech(text: str, voice: str) -> str:
    outputFile = f"temp_{int(time.time())}.mp3"
    try:
        tts = edge_tts.Communicate(text=text, voice=voice)
        await tts.save(outputFile)

        with open(outputFile, "rb") as file:
            audioBytes = file.read()
        
        audioBase64 = base64.b64encode(audioBytes).decode("utf-8")
        return audioBase64
    finally:
        if os.path.exists(outputFile):
            os.remove(outputFile)