SoFunction
Updated on 2024-11-19

Python calls edge-tts to achieve online text-to-speech effect

edge-tts is a Python module that allows the use of Microsoft Edge's online text-to-speech service by way of Python code or commands.

Project Source Code

GitHub - rany2/edge-tts: Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API keyUse Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key - rany2/edge-tts

/rany2/edge-tts

mounting

pip install edge-tts

usage

command-line method

  • --write-media: output audio
  • --write-subtitles: output subtitles
edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles 

Option to check available sounds

edge-tts --list-voices

Change the sound

--voice: Specify the voice

edge-tts --voice zh-CN-XiaoxiaoNeural --text "Don't you see the Yellow River flowing from the sky?" --write-media hello.mp3 --write-subtitles 

Changing Rate, Volume and Pitch

edge-tts --rate=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles 
edge-tts --volume=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles 
edge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello.mp3 --write-subtitles 

Play audio

edge-playback

edge-playback is used to play back generated speech. It takes the same parameters as edge-tts.

Python code approach

Text to Audio

import asyncio
import edge_tts
TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"
async def amain() -> None:
    """Main function"""
    communicate = edge_tts.Communicate(TEXT, VOICE)
    await (OUTPUT_FILE)
if __name__ == "__main__":
    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(amain())
    finally:
        ()

Example of dynamic voice selection using VoicesManager

import asyncio
import random
import edge_tts
from edge_tts import VoicesManager
TEXT = "Hoy es un buen día."
OUTPUT_FILE = "spanish.mp3"
async def amain() -> None:
    """Main function"""
    voices = await ()
    voice = (Gender="Male", Language="es")
    # Also supports Locales
    # voice = (Gender="Female", Locale="es-AR")
    communicate = edge_tts.Communicate(TEXT, (voice)["Name"])
    await (OUTPUT_FILE)
if __name__ == "__main__":
    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(amain())
    finally:
        ()

Streaming audio data from TTS

import asyncio
import edge_tts
TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"
async def amain() -> None:
    """Main function"""
    communicate = edge_tts.Communicate(TEXT, VOICE)
    with open(OUTPUT_FILE, "wb") as file:
        async for chunk in ():
            if chunk["type"] == "audio":
                (chunk["data"])
            elif chunk["type"] == "WordBoundary":
                print(f"WordBoundary: {chunk}")
if __name__ == "__main__":
    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(amain())
    finally:
        ()

to this article on the Python call edge-tts to achieve online text-to-speech article is introduced to this, more related Python online text-to-speech content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!