From 6c55e815bb00ae523aa9219393fd34aca27a3f07 Mon Sep 17 00:00:00 2001 From: rany2 Date: Sun, 30 Apr 2023 23:39:14 +0300 Subject: [PATCH] Provide warning before writing binary to stdout * Writing binary data to terminal could cause unintended behavior and mess up a terminal. Print a warning before doing such a thing. --- src/edge_tts/util.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/edge_tts/util.py b/src/edge_tts/util.py index 439faa7..a80fdd2 100644 --- a/src/edge_tts/util.py +++ b/src/edge_tts/util.py @@ -36,6 +36,21 @@ async def _print_voices(*, proxy: str) -> None: async def _run_tts(args: Any) -> None: """Run TTS after parsing arguments from command line.""" + + try: + if sys.stdin.isatty() and sys.stdout.isatty() and not args.write_media: + print( + "Warning: TTS output will be written to the terminal. " + "Use --write-media to write to a file.\n" + "Press Ctrl+C to cancel the operation. " + "Press Enter to continue.", + file=sys.stderr, + ) + input() + except KeyboardInterrupt: + print("\nOperation canceled.", file=sys.stderr) + return + tts: Communicate = Communicate( args.text, args.voice,