rany2 / edge-tts

Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key
https://pypi.org/project/edge-tts/
GNU General Public License v3.0
4.24k stars 447 forks source link

How to insert pauses in text? #111

Closed sweetsmile888 closed 1 year ago

sweetsmile888 commented 1 year ago

I have add ... or new paragraphs in the text,but it have no obvious effect, actually, I want to control the pauses time,because the default pauses time is too short by Punctuation

VOICE = "zh-TW-YunJheNeural"
OUTPUT_FILE = "test.mp3"
WEBVTT_FILE = "test.vtt"
TEXT = "Love to us ... human is what ... water to fish.Love "  \
            "shines the most beautiful ... light of humanity,we born in it,we live by  often ... we take it as granted,but we should know ... love is a priceless gift we should cherish."  \
            "But how to ... cherish the love?I have heard a saying :the quickest way to receive love is to give it; "
async def _main() -> None:
   voices = await VoicesManager.create()
   communicate = edge_tts.Communicate(TEXT, VOICE)
   submaker = edge_tts.SubMaker()
   with open(OUTPUT_FILE, "wb") as file:
      async for chunk in communicate.stream():
         if chunk["type"] == "audio":
            file.write(chunk["data"])
         elif chunk["type"] == "WordBoundary":
            submaker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"])
   with open(WEBVTT_FILE, "w", encoding="utf-8") as file:
      file.write(submaker.generate_subs())
if __name__ == "__main__":
   loop = asyncio.new_event_loop()
   asyncio.set_event_loop(loop)
   loop.run_until_complete(_main())
rany2 commented 1 year ago

Unfortunately there isn't much you could do besides those simple tricks. Custom SSML is not supported so you could not add a pause tag with specific wait time. If you really want to do that, just pay Microsoft for Azure Cognitive Services; it will give you the option to specify where to pause and for how long.