From 25e2941d22695533914c7db09e6d04963902f7f9 Mon Sep 17 00:00:00 2001 From: wh1te-moon Date: Mon, 13 Nov 2023 16:19:34 +0800 Subject: [PATCH 1/6] update generate cn subs --- src/edge_tts/submaker.py | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/edge_tts/submaker.py b/src/edge_tts/submaker.py index a7f6622..065027c 100644 --- a/src/edge_tts/submaker.py +++ b/src/edge_tts/submaker.py @@ -123,3 +123,50 @@ def generate_subs(self, words_in_cue: int = 10) -> str: sub_state_start = -1 sub_state_subs = "" return data + + def generate_cn_subs(self,text)->str: + PUNCTUATION=[',','。','!','?',';',':','\n','“','”',',','!'] + def clause(self)->list[str]: + start=0 + i=0 + text_list=[] + while(i Date: Fri, 19 Apr 2024 10:22:17 +0800 Subject: [PATCH 2/6] # new file: src/streaming_with_cn_subtitles.py new example --- src/streaming_with_cn_subtitles.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/streaming_with_cn_subtitles.py diff --git a/src/streaming_with_cn_subtitles.py b/src/streaming_with_cn_subtitles.py new file mode 100644 index 0000000..0d3c3d8 --- /dev/null +++ b/src/streaming_with_cn_subtitles.py @@ -0,0 +1,30 @@ +import asyncio +import edge_tts +from edge_tts.communicate import Communicate + +TEXT = "你穿越大明成为第一贪官,入股赌坊兴办青楼,是沛县最大的保护伞,你更是当众受贿,万两白银,打点官职,就连沈安的县衙前院你都毫无避讳地摆满了金尊琉璃,可百姓非但不骂你,还纷纷求你多贪点,你本想就这样做个贪官逍遥一生,没想到朱元璋为追封祖地,乔装来到了你的属地,他一下就被你属地的沥青马路给震惊,黑面的马路不仅笔直平整,还有特殊规定,马车走在中间,行人只能走两边,这让朱元璋他们的马车畅通无阻,他又发现这周围两边行走的百姓一个个脸上洋溢着笑容,仿佛不为生活而发愁,朱元璋微微触动,这样的一幕,若是发生在盛唐,富宋倒也没什么,可如今是明朝初期,长期无休止的战争,消耗了大量人力财力,更有战争爆发频繁的地区,土地荒废,十里不见人烟。朱元璋想到之前的一幕幕,心中觉得太不可思议。马车继续往前行驶,来到了县门口,一位捕快伸出一只手,示意他们停车。这整个商队都是你的?闻言,朱元璋微微一愣,立即懂得对方的个话里的意思,不就是看他们都是外地商人,想要趁机捞上一笔。朱元璋的脸色一沉,但也没有多说什么,一个眼神过去,旁边的侍卫就掏出了一盒银子,捕快却一脸义正言辞地说,这是干什么的?他就是关心的问候一下吗?塞给自己钱干嘛?你们不收这个吗?朱元璋脸色好看了一些,但还是有些疑惑,捕快说到:我是看你们身家富有,所以想给你们提点建议,看到没进县门之后,沿着东边直走那条街上都是高档客栈,专门给你们这种有钱人住。朱元璋有些愣住,见他似乎有兴趣,捕快接着给他说了起来。是啊,来咱们沛县,你还不得先知道这里的场所划分吗。东边管住宿,南边管吃喝玩乐,西边是购物集市,找官府的话就去北面,想投资做生意,就去那边找咱们的县太爷,有点意思。朱元璋捋了捋胡,城卫捕快给介绍当地环境的,以前哪次不是塞银子,一行人穿过县门进入沛县,可没走几步路,便不由瞪大大眼睛,充满烟火气息的大街上,每一个人脸上都洋溢着其他地方看不到的笑容。朱元璋一行人走在大街上,有种恍若隔世的感觉,街上上开着各种的铺子,这里的行各业都有连青楼赌坊的旗子,也高高挂起.“一个小县,竟然如此繁华!”朱元璋微微沉思无比触动,马皇后露出亲和的笑容,微微点点头,很快,他们就来到了东区,朱元璋又是大开眼界,入眼望去,这里的房子竟然跟他们见到的寻常房子完全不同,这些房子竟然修得一栋一栋的,一排排过去,矗立了一大片,而且每一栋都修了四五层。朱彪和朱爽二人脸色也极为惊讶,虽然比不上金砖银瓦的皇宫,风格却见所未见。当朱元璋入住后,走到阳台上,看见沛县的全貌,几个区域也尽在他的眼下,他不禁感慨道“沛县治理的如此之好,这孟胤必是能臣。”" +VOICE = "zh-CN-YunxiNeural" +OUTPUT_FILE = "test.mp3" +WEBVTT_FILE = "test.vtt" + + +async def amain() -> None: + """Main function""" + communicate = 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_cn_subs(TEXT)) + + +loop = asyncio.get_event_loop_policy().get_event_loop() +try: + loop.run_until_complete(amain()) +finally: + loop.close() \ No newline at end of file From 0aaca9b236465aa2ec5feb7a8067849d30ca85fa Mon Sep 17 00:00:00 2001 From: wh1te-moon Date: Fri, 19 Apr 2024 10:57:15 +0800 Subject: [PATCH 3/6] modified: src/streaming_with_cn_subtitles.py --- src/streaming_with_cn_subtitles.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/streaming_with_cn_subtitles.py b/src/streaming_with_cn_subtitles.py index 0d3c3d8..161de88 100644 --- a/src/streaming_with_cn_subtitles.py +++ b/src/streaming_with_cn_subtitles.py @@ -7,10 +7,9 @@ OUTPUT_FILE = "test.mp3" WEBVTT_FILE = "test.vtt" - async def amain() -> None: """Main function""" - communicate = Communicate(TEXT, VOICE) + communicate = Communicate(TEXT, VOICE,rate="+50%",volume="+50%") submaker = edge_tts.SubMaker() with open(OUTPUT_FILE, "wb") as file: async for chunk in communicate.stream(): From 08f852d530b952e8bd90840fca9f0c7cb5eb3503 Mon Sep 17 00:00:00 2001 From: wh1te-moon Date: Tue, 7 May 2024 10:56:58 +0800 Subject: [PATCH 4/6] update new re clause --- src/edge_tts/submaker.py | 77 +++++++++++++++++++----------- src/streaming_with_cn_subtitles.py | 21 ++++++-- 2 files changed, 67 insertions(+), 31 deletions(-) diff --git a/src/edge_tts/submaker.py b/src/edge_tts/submaker.py index 065027c..9a4a30b 100644 --- a/src/edge_tts/submaker.py +++ b/src/edge_tts/submaker.py @@ -6,6 +6,7 @@ """ import math +import re from typing import List, Tuple from xml.sax.saxutils import escape, unescape @@ -98,7 +99,7 @@ def generate_subs(self, words_in_cue: int = 10) -> str: if sub_state_count == words_in_cue or idx == len(self.offset) - 1: subs = sub_state_subs split_subs: List[str] = [ - subs[i : i + 79] for i in range(0, len(subs), 79) + subs[i: i + 79] for i in range(0, len(subs), 79) ] for i in range(len(split_subs) - 1): sub = split_subs[i] @@ -123,27 +124,49 @@ def generate_subs(self, words_in_cue: int = 10) -> str: sub_state_start = -1 sub_state_subs = "" return data - - def generate_cn_subs(self,text)->str: - PUNCTUATION=[',','。','!','?',';',':','\n','“','”',',','!'] - def clause(self)->list[str]: - start=0 - i=0 - text_list=[] - while(i str: + PUNCTUATION = [',', '。', '!', '?', ';', + ':', '\n', '“', '”', ',', '!', '\\. '] + # def clause(self)->list[str]: + # start=0 + # i=0 + # text_list=[] + # while(i list[str]: + # 构建正则表达式模式,匹配任意一个或多个标点符号 + pattern = '(' + '|'.join(punc for punc in PUNCTUATION) + ')' + # 使用正则表达式分割文本 + text_list = re.split(pattern, text) + + index = 0 + pattern = '^[' + ''.join(p for p in PUNCTUATION) + ']+$' + while (index < len(text_list)-1): + if not text_list[index+1]: + text_list.pop(index+1) + continue + if re.match(pattern, text_list[index+1]): + if (text_list[index+1] == '\n'): + text_list.pop(index+1) + continue + text_list[index] += text_list.pop(index+1) + else: + index += 1 + return text_list - - self.text_list=clause(self) - + + self.text_list = clause(self) if len(self.subs) != len(self.offset): raise ValueError("subs and offset are not of the same length") data = "WEBVTT\r\n\r\n" @@ -162,11 +185,11 @@ def clause(self)->list[str]: j += 1 return data -if __name__=="__main__": - generator=SubMaker() - generator.create_sub((0,15000)," 你好,") - generator.create_sub((15000,15000),"世界!") - print(generator.generate_cn_subs("你好,世界!")) + +if __name__ == "__main__": + generator = SubMaker() + generator.create_sub((0, 15000), " 你好,") + generator.create_sub((15000, 15000), "世界!") + print(generator.generate_subs_based_on_punc("你好,世界!")) # print(generator.generate_subs()) print() - \ No newline at end of file diff --git a/src/streaming_with_cn_subtitles.py b/src/streaming_with_cn_subtitles.py index 161de88..61cb46c 100644 --- a/src/streaming_with_cn_subtitles.py +++ b/src/streaming_with_cn_subtitles.py @@ -2,14 +2,26 @@ import edge_tts from edge_tts.communicate import Communicate -TEXT = "你穿越大明成为第一贪官,入股赌坊兴办青楼,是沛县最大的保护伞,你更是当众受贿,万两白银,打点官职,就连沈安的县衙前院你都毫无避讳地摆满了金尊琉璃,可百姓非但不骂你,还纷纷求你多贪点,你本想就这样做个贪官逍遥一生,没想到朱元璋为追封祖地,乔装来到了你的属地,他一下就被你属地的沥青马路给震惊,黑面的马路不仅笔直平整,还有特殊规定,马车走在中间,行人只能走两边,这让朱元璋他们的马车畅通无阻,他又发现这周围两边行走的百姓一个个脸上洋溢着笑容,仿佛不为生活而发愁,朱元璋微微触动,这样的一幕,若是发生在盛唐,富宋倒也没什么,可如今是明朝初期,长期无休止的战争,消耗了大量人力财力,更有战争爆发频繁的地区,土地荒废,十里不见人烟。朱元璋想到之前的一幕幕,心中觉得太不可思议。马车继续往前行驶,来到了县门口,一位捕快伸出一只手,示意他们停车。这整个商队都是你的?闻言,朱元璋微微一愣,立即懂得对方的个话里的意思,不就是看他们都是外地商人,想要趁机捞上一笔。朱元璋的脸色一沉,但也没有多说什么,一个眼神过去,旁边的侍卫就掏出了一盒银子,捕快却一脸义正言辞地说,这是干什么的?他就是关心的问候一下吗?塞给自己钱干嘛?你们不收这个吗?朱元璋脸色好看了一些,但还是有些疑惑,捕快说到:我是看你们身家富有,所以想给你们提点建议,看到没进县门之后,沿着东边直走那条街上都是高档客栈,专门给你们这种有钱人住。朱元璋有些愣住,见他似乎有兴趣,捕快接着给他说了起来。是啊,来咱们沛县,你还不得先知道这里的场所划分吗。东边管住宿,南边管吃喝玩乐,西边是购物集市,找官府的话就去北面,想投资做生意,就去那边找咱们的县太爷,有点意思。朱元璋捋了捋胡,城卫捕快给介绍当地环境的,以前哪次不是塞银子,一行人穿过县门进入沛县,可没走几步路,便不由瞪大大眼睛,充满烟火气息的大街上,每一个人脸上都洋溢着其他地方看不到的笑容。朱元璋一行人走在大街上,有种恍若隔世的感觉,街上上开着各种的铺子,这里的行各业都有连青楼赌坊的旗子,也高高挂起.“一个小县,竟然如此繁华!”朱元璋微微沉思无比触动,马皇后露出亲和的笑容,微微点点头,很快,他们就来到了东区,朱元璋又是大开眼界,入眼望去,这里的房子竟然跟他们见到的寻常房子完全不同,这些房子竟然修得一栋一栋的,一排排过去,矗立了一大片,而且每一栋都修了四五层。朱彪和朱爽二人脸色也极为惊讶,虽然比不上金砖银瓦的皇宫,风格却见所未见。当朱元璋入住后,走到阳台上,看见沛县的全貌,几个区域也尽在他的眼下,他不禁感慨道“沛县治理的如此之好,这孟胤必是能臣。”" -VOICE = "zh-CN-YunxiNeural" +TEXT = """Title: Exploring the Beauty of Mathematics + +Mathematics, often regarded as the language of the universe, encompasses a myriad of concepts, from basic arithmetic to complex calculus. It's a discipline that transcends boundaries and delves into the depths of abstraction. + +In mathematics, precision is paramount. Numbers dance across the page, guided by symbols such as +, -, ×, and ÷, each punctuation mark playing a crucial role in shaping equations and expressions. These symbols, like punctuation in language, clarify and organize mathematical ideas. + +Consider the beauty of decimals, those subtle points that delineate fractions of wholes. They appear unassumingly yet hold profound significance in calculations. Whether it's 3.14, the beloved pi, or the golden ratio 1.618, decimals offer glimpses into the elegant patterns underlying the chaos of numbers. + +But mathematics isn't just about numbers and symbols; it's about discovery and exploration. It's about unraveling the mysteries of the universe, from the microscopic world of quantum mechanics to the vast expanse of cosmology. Punctuation marks in mathematics, much like their linguistic counterparts, serve as signposts on this journey, guiding us through the intricate landscapes of mathematical thought. + +So let us embrace the beauty of mathematics, where decimals and punctuation marks converge to form the tapestry of our understanding, illuminating the path to new insights and discoveries.""" +# VOICE = "zh-CN-YunxiNeural" OUTPUT_FILE = "test.mp3" WEBVTT_FILE = "test.vtt" async def amain() -> None: """Main function""" - communicate = Communicate(TEXT, VOICE,rate="+50%",volume="+50%") + communicate = Communicate(TEXT, + # rate="+50%",volume="+50%" + ) submaker = edge_tts.SubMaker() with open(OUTPUT_FILE, "wb") as file: async for chunk in communicate.stream(): @@ -19,7 +31,8 @@ async def amain() -> None: submaker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"]) with open(WEBVTT_FILE, "w", encoding="utf-8") as file: - file.write(submaker.generate_cn_subs(TEXT)) + file.write(submaker.generate_subs_based_on_punc(TEXT)) + file.write(submaker.generate_subs()) loop = asyncio.get_event_loop_policy().get_event_loop() From c766c966636f0df8a77c9353b755683da2d5a7c0 Mon Sep 17 00:00:00 2001 From: wh1te-moon Date: Tue, 7 May 2024 10:57:11 +0800 Subject: [PATCH 5/6] modified: src/streaming_with_cn_subtitles.py --- src/streaming_with_cn_subtitles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/streaming_with_cn_subtitles.py b/src/streaming_with_cn_subtitles.py index 61cb46c..f897a90 100644 --- a/src/streaming_with_cn_subtitles.py +++ b/src/streaming_with_cn_subtitles.py @@ -32,7 +32,7 @@ async def amain() -> None: with open(WEBVTT_FILE, "w", encoding="utf-8") as file: file.write(submaker.generate_subs_based_on_punc(TEXT)) - file.write(submaker.generate_subs()) + # file.write(submaker.generate_subs()) loop = asyncio.get_event_loop_policy().get_event_loop() From 2ad1740a465bdee47e4464289e250caeb7094692 Mon Sep 17 00:00:00 2001 From: wh1te-moon Date: Tue, 7 May 2024 11:11:23 +0800 Subject: [PATCH 6/6] update comment and rename file --- src/edge_tts/submaker.py | 2 -- ...ith_cn_subtitles.py => streaming_with_new_subtitles_test.py} | 0 2 files changed, 2 deletions(-) rename src/{streaming_with_cn_subtitles.py => streaming_with_new_subtitles_test.py} (100%) diff --git a/src/edge_tts/submaker.py b/src/edge_tts/submaker.py index 9a4a30b..c469b9c 100644 --- a/src/edge_tts/submaker.py +++ b/src/edge_tts/submaker.py @@ -145,9 +145,7 @@ def generate_subs_based_on_punc(self, text) -> str: # return text_list def clause(self) -> list[str]: - # 构建正则表达式模式,匹配任意一个或多个标点符号 pattern = '(' + '|'.join(punc for punc in PUNCTUATION) + ')' - # 使用正则表达式分割文本 text_list = re.split(pattern, text) index = 0 diff --git a/src/streaming_with_cn_subtitles.py b/src/streaming_with_new_subtitles_test.py similarity index 100% rename from src/streaming_with_cn_subtitles.py rename to src/streaming_with_new_subtitles_test.py