Skip to content

Commit

Permalink
Reformat and add back pitch argument for util.py
Browse files Browse the repository at this point in the history
Signed-off-by: rany2 <[email protected]>
  • Loading branch information
rany2 committed Dec 14, 2023
1 parent 8d6cea3 commit 43c25a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/edge_tts/communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions src/edge_tts/submaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
return data
9 changes: 6 additions & 3 deletions src/edge_tts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""

Expand Down Expand Up @@ -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:
Expand All @@ -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.",
Expand Down

0 comments on commit 43c25a4

Please sign in to comment.