Merge remote-tracking branch 'origin/master' into simplify

This commit is contained in:
rany2 2023-01-04 23:50:29 +02:00
commit 3a3a872899
6 changed files with 80 additions and 31 deletions

View File

@ -83,7 +83,8 @@ 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/input_example.py
* 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

View File

@ -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())

24
examples/example.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
"""
Example Python script that shows how to use edge-tts as a module
"""
import asyncio
import edge_tts
async def main():
"""
Main function
"""
TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.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())

View File

@ -1,28 +0,0 @@
#!/usr/bin/env python3
"""
Example Python script that shows how to use edge-tts as a module
"""
import asyncio
import tempfile
from playsound import playsound
import edge_tts
async def main():
"""
Main function
"""
communicate = edge_tts.Communicate()
ask = input("What do you want TTS to say? ")
with tempfile.NamedTemporaryFile() as temporary_file:
async for i in communicate.run(ask):
if i[2] is not None:
temporary_file.write(i[2])
playsound(temporary_file.name)
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())

View File

@ -3,5 +3,5 @@ __init__ for edge_tts
"""
from .communicate import Communicate
from .list_voices import list_voices
from .submaker import SubMaker
from .list_voices import list_voices, VoicesManager
from .submaker import SubMaker

View File

@ -40,3 +40,29 @@ async def list_voices(proxy=None):
) as url:
data = json.loads(await url.text())
return data
class VoicesManager:
"""
A class to find the correct voice based on their attributes.
"""
@classmethod
async def create(cls):
self = VoicesManager()
self.voices = await list_voices()
self.voices = [
{**voice, **{"Language": voice["Locale"].split("-")[0]}}
for voice in self.voices
]
return self
def find(self, **kwargs):
"""
Finds all matching voices based on the provided attributes.
"""
matching_voices = [
voice for voice in self.voices if kwargs.items() <= voice.items()
]
return matching_voices