From 393233ecf9f3fb9f53dc4ead14b1936a18cc74df Mon Sep 17 00:00:00 2001 From: rany Date: Sun, 13 Mar 2022 13:25:41 +0200 Subject: [PATCH] fix for windows users --- setup.cfg | 2 +- src/edge_playback/__init__.py | 56 ++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/setup.cfg b/setup.cfg index b05ae09..09e9691 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = edge-tts -version = 4.0.8 +version = 4.0.9 author = rany author_email = ranygh@riseup.net description = Microsoft Edge's TTS diff --git a/src/edge_playback/__init__.py b/src/edge_playback/__init__.py index 59ccfd1..c88a423 100644 --- a/src/edge_playback/__init__.py +++ b/src/edge_playback/__init__.py @@ -4,6 +4,7 @@ Playback TTS with subtitles using edge-tts and mpv. """ +import os import subprocess import sys import tempfile @@ -15,31 +16,38 @@ def main(): Main function. """ if which("mpv") and which("edge-tts"): - with tempfile.NamedTemporaryFile() as media: - with tempfile.NamedTemporaryFile() as subtitle: - print() - print(f"Media file: {media.name}") - print(f"Subtitle file: {subtitle.name}\n") - with subprocess.Popen( - [ - "edge-tts", - "--boundary-type=2", - f"--write-media={media.name}", - f"--write-subtitles={subtitle.name}", - ] - + sys.argv[1:] - ) as process: - process.communicate() + media = tempfile.NamedTemporaryFile(delete=False) + subtitle = tempfile.NamedTemporaryFile(delete=False) + try: + media.close() + subtitle.close() - with subprocess.Popen( - [ - "mpv", - "--keep-open=yes", - f"--sub-file={subtitle.name}", - media.name, - ] - ) as process: - process.communicate() + print() + print(f"Media file: {media.name}") + print(f"Subtitle file: {subtitle.name}\n") + with subprocess.Popen( + [ + "edge-tts", + "--boundary-type=2", + f"--write-media={media.name}", + f"--write-subtitles={subtitle.name}", + ] + + sys.argv[1:] + ) as process: + process.communicate() + + with subprocess.Popen( + [ + "mpv", + "--keep-open=yes", + f"--sub-file={subtitle.name}", + media.name, + ] + ) as process: + process.communicate() + finally: + os.unlink(media.name) + os.unlink(subtitle.name) else: print("This script requires mpv and edge-tts.")