Add back words-in-cue support (#342)
Closes https://github.com/rany2/edge-tts/issues/335 Closes https://github.com/rany2/edge-tts/issues/336 Signed-off-by: rany <rany2@riseup.net>
This commit is contained in:
@@ -83,6 +83,7 @@ class UtilArgs(argparse.Namespace):
|
|||||||
rate: str
|
rate: str
|
||||||
volume: str
|
volume: str
|
||||||
pitch: str
|
pitch: str
|
||||||
|
words_in_cue: int
|
||||||
write_media: str
|
write_media: str
|
||||||
write_subtitles: str
|
write_subtitles: str
|
||||||
proxy: str
|
proxy: str
|
||||||
|
|||||||
@@ -37,6 +37,38 @@ class SubMaker:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def merge_cues(self, words: int) -> None:
|
||||||
|
"""
|
||||||
|
Merge cues to reduce the number of cues.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
words (int): The number of words to merge.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
"""
|
||||||
|
if words <= 0:
|
||||||
|
raise ValueError("Invalid number of words to merge, expected > 0")
|
||||||
|
|
||||||
|
if len(self.cues) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
new_cues: List[srt.Subtitle] = [] # type: ignore
|
||||||
|
current_cue: srt.Subtitle = self.cues[0] # type: ignore
|
||||||
|
for cue in self.cues[1:]:
|
||||||
|
if len(current_cue.content.split()) < words:
|
||||||
|
current_cue = srt.Subtitle(
|
||||||
|
index=current_cue.index,
|
||||||
|
start=current_cue.start,
|
||||||
|
end=cue.end,
|
||||||
|
content=current_cue.content + " " + cue.content,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
new_cues.append(current_cue)
|
||||||
|
current_cue = cue
|
||||||
|
new_cues.append(current_cue)
|
||||||
|
self.cues = new_cues
|
||||||
|
|
||||||
def get_srt(self) -> str:
|
def get_srt(self) -> str:
|
||||||
"""
|
"""
|
||||||
Get the SRT formatted subtitles from the SubMaker object.
|
Get the SRT formatted subtitles from the SubMaker object.
|
||||||
|
|||||||
@@ -75,6 +75,9 @@ async def _run_tts(args: UtilArgs) -> None:
|
|||||||
elif chunk["type"] == "WordBoundary":
|
elif chunk["type"] == "WordBoundary":
|
||||||
submaker.feed(chunk)
|
submaker.feed(chunk)
|
||||||
|
|
||||||
|
if args.words_in_cue > 0:
|
||||||
|
submaker.merge_cues(args.words_in_cue)
|
||||||
|
|
||||||
if sub_file is not None:
|
if sub_file is not None:
|
||||||
sub_file.write(submaker.get_srt())
|
sub_file.write(submaker.get_srt())
|
||||||
finally:
|
finally:
|
||||||
@@ -107,6 +110,12 @@ async def amain() -> None:
|
|||||||
parser.add_argument("--rate", help="set TTS rate. Default +0%%.", default="+0%")
|
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("--volume", help="set TTS volume. Default +0%%.", default="+0%")
|
||||||
parser.add_argument("--pitch", help="set TTS pitch. Default +0Hz.", default="+0Hz")
|
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.",
|
||||||
|
default=10,
|
||||||
|
type=int,
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--write-media", help="send media output to file instead of stdout"
|
"--write-media", help="send media output to file instead of stdout"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user