Simplify edge_tts library usage

This commit is contained in:
rany2
2023-01-04 23:45:22 +02:00
parent 142b4f6457
commit 23370b4c27
5 changed files with 263 additions and 221 deletions

View File

@@ -11,9 +11,6 @@ from edge_tts import Communicate, SubMaker, list_voices
async def _list_voices(proxy):
"""
List available voices.
"""
for idx, voice in enumerate(await list_voices(proxy=proxy)):
if idx != 0:
print()
@@ -26,34 +23,36 @@ async def _list_voices(proxy):
async def _tts(args):
tts = Communicate()
subs = SubMaker(args.overlapping)
if args.write_media:
media_file = open(args.write_media, "wb") # pylint: disable=consider-using-with
async for i in tts.run(
tts = await Communicate(
args.text,
args.boundary_type,
args.codec,
args.voice,
args.pitch,
args.rate,
args.volume,
proxy=args.proxy,
):
if i[2] is not None:
if not args.write_media:
sys.stdout.buffer.write(i[2])
else:
media_file.write(i[2])
elif i[0] is not None and i[1] is not None:
subs.create_sub(i[0], i[1])
if args.write_media:
media_file.close()
if not args.write_subtitles:
sys.stderr.write(subs.generate_subs())
else:
with open(args.write_subtitles, "w", encoding="utf-8") as file:
file.write(subs.generate_subs())
rate=args.rate,
volume=args.volume,
)
try:
media_file = None
if args.write_media:
media_file = open(args.write_media, "wb")
subs = SubMaker(args.overlapping)
async for data in tts.stream():
if data["type"] == "audio":
if not args.write_media:
sys.stdout.buffer.write(data["data"])
else:
media_file.write(data["data"])
elif data["type"] == "WordBoundary":
subs.create_sub([data["offset"], data["duration"]], data["text"])
if not args.write_subtitles:
sys.stderr.write(subs.generate_subs())
else:
with open(args.write_subtitles, "w", encoding="utf-8") as file:
file.write(subs.generate_subs())
finally:
if media_file is not None:
media_file.close()
async def _main():
@@ -64,23 +63,13 @@ async def _main():
parser.add_argument(
"-v",
"--voice",
help="voice for TTS. "
"Default: Microsoft Server Speech Text to Speech Voice (en-US, AriaNeural)",
default="Microsoft Server Speech Text to Speech Voice (en-US, AriaNeural)",
)
parser.add_argument(
"-c",
"--codec",
help="codec format. Default: audio-24khz-48kbitrate-mono-mp3. "
"Another choice is webm-24khz-16bit-mono-opus. "
"For more info check https://bit.ly/2T33h6S",
default="audio-24khz-48kbitrate-mono-mp3",
help="voice for TTS. " "Default: en-US-AriaNeural",
default="en-US-AriaNeural",
)
group.add_argument(
"-l",
"--list-voices",
help="lists available voices. "
"Edge's list is incomplete so check https://bit.ly/2SFq1d3",
help="lists available voices",
action="store_true",
)
parser.add_argument(
@@ -109,32 +98,19 @@ async def _main():
type=float,
)
parser.add_argument(
"-b",
"--boundary-type",
help="set boundary type for subtitles. Default 0 for none. Set 1 for word_boundary.",
default=0,
type=int,
)
parser.add_argument(
"--write-media", help="instead of stdout, send media output to provided file"
"--write-media", help="send media output to file instead of stdout"
)
parser.add_argument(
"--write-subtitles",
help="instead of stderr, send subtitle output to provided file (implies boundary-type is 1)",
)
parser.add_argument(
"--proxy",
help="proxy",
help="send subtitle output to provided file instead of stderr",
)
parser.add_argument("--proxy", help="use a proxy for TTS and voice list.")
args = parser.parse_args()
if args.list_voices:
await _list_voices(args.proxy)
sys.exit(0)
if args.write_subtitles and args.boundary_type == 0:
args.boundary_type = 1
if args.text is not None or args.file is not None:
if args.file is not None:
# we need to use sys.stdin.read() because some devices
@@ -151,9 +127,6 @@ async def _main():
def main():
"""
Main function.
"""
asyncio.get_event_loop().run_until_complete(_main())