From 43c25a41b1f2f964084e9b7a1c046a114b3d98aa Mon Sep 17 00:00:00 2001 From: rany2 Date: Thu, 14 Dec 2023 17:35:23 +0200 Subject: [PATCH] Reformat and add back pitch argument for util.py Signed-off-by: rany2 --- src/edge_tts/communicate.py | 4 +++- src/edge_tts/submaker.py | 18 +++++++++++++----- src/edge_tts/util.py | 9 ++++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/edge_tts/communicate.py b/src/edge_tts/communicate.py index d482b84..0cf9e80 100644 --- a/src/edge_tts/communicate.py +++ b/src/edge_tts/communicate.py @@ -152,7 +152,9 @@ def split_text_by_byte_length( yield new_text -def mkssml(text: Union[str, bytes], voice: str, rate: str, volume: str, pitch: str) -> str: +def mkssml( + text: Union[str, bytes], voice: str, rate: str, volume: str, pitch: str +) -> str: """ Creates a SSML string from the given parameters. diff --git a/src/edge_tts/submaker.py b/src/edge_tts/submaker.py index 08f9b42..f05c37e 100644 --- a/src/edge_tts/submaker.py +++ b/src/edge_tts/submaker.py @@ -10,7 +10,9 @@ from xml.sax.saxutils import escape, unescape -def formatter(sub_line_count: int, start_time: float, end_time: float, subdata: str) -> str: +def formatter( + sub_line_count: int, start_time: float, end_time: float, subdata: str +) -> str: """ formatter returns the timecode and the text of the subtitle. """ @@ -36,6 +38,7 @@ def mktimestamp(time_unit: float) -> str: # return f"{hour:02d}:{minute:02d}:{seconds:06.3f}" return f"{hour:02d}:{minute:02d}:{seconds:06.3f}".replace(".", ",") + class SubMaker: """ SubMaker class @@ -83,11 +86,13 @@ def generate_subs(self, three_dimensional_list, words_in_cue: int = 10) -> str: raise ValueError("words_in_cue must be greater than 0") # data = "WEBVTT\r\n\r\n" - data = '' + data = "" sub_state_count = 0 sub_state_start = -1.0 sub_state_subs = "" - sub_line_count = 0 # new variable used to indicate which line of subtitle this is + sub_line_count = ( + 0 # new variable used to indicate which line of subtitle this is + ) for idx, (offset, subs) in enumerate(zip(self.offset, self.subs)): start_time, end_time = offset subs = unescape(subs) @@ -102,7 +107,10 @@ def generate_subs(self, three_dimensional_list, words_in_cue: int = 10) -> str: sub_state_count += 1 sentence, last_word, last_word_num = three_dimensional_list[sub_line_count] - if sub_state_subs.count(last_word) == last_word_num or idx == len(self.offset) - 1: + if ( + sub_state_subs.count(last_word) == last_word_num + or idx == len(self.offset) - 1 + ): sub_line_count += 1 # subs = sub_state_subs subs = sentence @@ -132,4 +140,4 @@ def generate_subs(self, three_dimensional_list, words_in_cue: int = 10) -> str: sub_state_count = 0 sub_state_start = -1 sub_state_subs = "" - return data \ No newline at end of file + return data diff --git a/src/edge_tts/util.py b/src/edge_tts/util.py index d436995..01fabbc 100644 --- a/src/edge_tts/util.py +++ b/src/edge_tts/util.py @@ -5,14 +5,13 @@ import argparse import asyncio +import re import sys from io import TextIOWrapper from typing import Any, TextIO, Union from edge_tts import Communicate, SubMaker, list_voices -import asyncio -import re async def _print_voices(*, proxy: str) -> None: """Print all available voices.""" @@ -41,6 +40,7 @@ def _spinoff_sentence(sentence): last_word_num = sentence.count(last_word) return (sentence, last_word, last_word_num) + async def _run_tts(args: Any) -> None: """Run TTS after parsing arguments from command line.""" @@ -90,7 +90,9 @@ async def _run_tts(args: Any) -> None: else sys.stderr ) with sub_file: - sub_file.write(submaker.generate_subs(three_dimensional_list=three_dimensional_list)) + sub_file.write( + submaker.generate_subs(three_dimensional_list=three_dimensional_list) + ) async def amain() -> None: @@ -113,6 +115,7 @@ async def amain() -> None: ) parser.add_argument("--rate", help="set TTS rate. Default +0%%.", default="+0%") parser.add_argument("--volume", help="set TTS volume. Default +0%%.", default="+0%") + parser.add_argument("--pitch", help="set TTS pitch. Default +0Hz.", default="+0Hz") parser.add_argument( "--words-in-cue", help="number of words in a subtitle cue. Default: 10.",