diff --git a/README.md b/README.md index d44f5c0..5c9dfea 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ In addition, it is required to use `--pitch=-10Hz` instead of `--pitch -10Hz` ot It is possible to use the `edge-tts` module directly from Python. For a list of example applications: * https://github.com/rany2/edge-tts/blob/master/examples/example.py +* https://github.com/rany2/edge-tts/blob/master/examples/dynamic_voice_selection.py * https://github.com/rany2/edge-tts/blob/master/src/edge_tts/util.py * https://github.com/rany2/edge-srt-to-speech/blob/master/src/edge_srt_to_speech/__main__.py * https://github.com/hasscc/hass-edge-tts/blob/main/custom_components/edge_tts/tts.py diff --git a/examples/dynamic_voice_selection.py b/examples/dynamic_voice_selection.py new file mode 100644 index 0000000..fb85a1c --- /dev/null +++ b/examples/dynamic_voice_selection.py @@ -0,0 +1,26 @@ +import asyncio +import edge_tts +from edge_tts import VoicesManager +import random + +async def main(): + """ + Main function + """ + voices = await VoicesManager.create() + voice = voices.find(Gender="Male", Language="es") + # Also supports Locales + # voice = voices.find(Gender="Female", Locale="es-AR") + VOICE = random.choice(voice)["ShortName"] + TEXT = "Hoy es un buen día." + OUTPUT_FILE = "spanish.mp3" + + communicate = edge_tts.Communicate() + + with open(OUTPUT_FILE, "wb") as f: + async for i in communicate.run(TEXT, voice=VOICE): + if i[2] is not None: + f.write(i[2]) + +if __name__ == "__main__": + asyncio.get_event_loop().run_until_complete(main())