From 8a2f37d96474bdeb6a677445bc8c042504e13d47 Mon Sep 17 00:00:00 2001 From: rany2 Date: Thu, 5 Jan 2023 01:29:55 +0200 Subject: [PATCH 1/4] add streaming_tts.py example --- examples/streaming_tts.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/streaming_tts.py diff --git a/examples/streaming_tts.py b/examples/streaming_tts.py new file mode 100644 index 0000000..69b3815 --- /dev/null +++ b/examples/streaming_tts.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +""" +Streaming TTS example. + +This example shows how to stream the audio data from the TTS engine, +and how to get the WordBoundary events from the engine (which could +be ignored if not needed). + +The example streaming_tts_with_subtitles.py shows how to use the +WordBoundary events to create subtitles using SubMaker. +""" + +import asyncio + +import edge_tts + + +async def main() -> None: + TEXT = "Hello World!" + VOICE = "en-GB-SoniaNeural" + OUTPUT_FILE = "test.mp3" + + communicate = edge_tts.Communicate(TEXT, VOICE) + 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": + print(f"WordBoundary: {chunk}") + + +if __name__ == "__main__": + asyncio.get_event_loop().run_until_complete(main()) From 1012a08cf59d240199f3541a850ad1bb482d887a Mon Sep 17 00:00:00 2001 From: rany2 Date: Thu, 5 Jan 2023 01:31:45 +0200 Subject: [PATCH 2/4] add examples/streaming_tts_with_subtitles.py --- examples/streaming_tts_with_subtitles.py | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/streaming_tts_with_subtitles.py diff --git a/examples/streaming_tts_with_subtitles.py b/examples/streaming_tts_with_subtitles.py new file mode 100644 index 0000000..c976c2d --- /dev/null +++ b/examples/streaming_tts_with_subtitles.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +""" +Streaming TTS example with subtitles. + +This example is similar to the example streaming_tts.py, but it shows +WordBoundary events to create subtitles using SubMaker. +""" + +import asyncio + +import edge_tts + + +async def main() -> None: + TEXT = "Hello World!" + VOICE = "en-GB-SoniaNeural" + OUTPUT_FILE = "test.mp3" + WEBVTT_FILE = "test.vtt" + + 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__": + asyncio.get_event_loop().run_until_complete(main()) From 66e3092ff534a7dde6e9bfc76d99aac8e66502b6 Mon Sep 17 00:00:00 2001 From: rany2 Date: Thu, 5 Jan 2023 01:32:45 +0200 Subject: [PATCH 3/4] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dccc2d6..b7a3c15 100644 --- a/README.md +++ b/README.md @@ -85,5 +85,7 @@ It is possible to use the `edge-tts` module directly from Python. For a list of * https://github.com/rany2/edge-tts/blob/master/examples/basic_generation.py * https://github.com/rany2/edge-tts/blob/master/examples/dynamic_voice_selection.py +* https://github.com/rany2/edge-tts/blob/master/examples/streaming_tts.py +* https://github.com/rany2/edge-tts/blob/master/examples/streaming_tts_with_subtitles.py * https://github.com/rany2/edge-tts/blob/master/src/edge_tts/util.py * https://github.com/hasscc/hass-edge-tts/blob/main/custom_components/edge_tts/tts.py From 28b4d5537622a9c169c7d2af56ac2dbd185a6cd1 Mon Sep 17 00:00:00 2001 From: rany2 Date: Thu, 5 Jan 2023 01:33:52 +0200 Subject: [PATCH 4/4] fix workflow --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3941425..0db10fa 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -15,7 +15,7 @@ jobs: - name: Setup Python uses: actions/setup-python@v1 with: - python-version: 3.7.4 + python-version: 3.11 architecture: x64 - name: Checkout uses: actions/checkout@v1