Fix bug with edge-playback not removing temp files

* `tmp.close()` even with `delete=True` will cause `edge-playback` to
  not delete because the file will be recreated by `edge-tts`
* bump edge-tts version
This commit is contained in:
rany2
2023-01-10 16:38:41 +02:00
parent c4f91377a4
commit 85eef7d918
2 changed files with 39 additions and 22 deletions

View File

@@ -11,32 +11,44 @@ import tempfile
from shutil import which from shutil import which
def pr_err(msg: str) -> None:
"""Print to stderr."""
print(msg, file=sys.stderr)
def _main() -> None: def _main() -> None:
depcheck_failed = False depcheck_failed = False
if not which("mpv"): for dep in ("edge-tts", "mpv"):
print("mpv is not installed.", file=sys.stderr) if not which(dep):
depcheck_failed = True pr_err(f"{dep} is not installed.")
if not which("edge-tts"):
print("edge-tts is not installed.", file=sys.stderr)
depcheck_failed = True depcheck_failed = True
if depcheck_failed: if depcheck_failed:
print("Please install the missing dependencies.", file=sys.stderr) pr_err("Please install the missing dependencies.")
sys.exit(1) sys.exit(1)
keep = os.environ.get("EDGE_PLAYBACK_KEEP_TEMP") is not None keep = os.environ.get("EDGE_PLAYBACK_KEEP_TEMP") is not None
with tempfile.NamedTemporaryFile( mp3_fname = os.environ.get("EDGE_PLAYBACK_MP3_FILE")
suffix=".mp3", delete=not keep vtt_fname = os.environ.get("EDGE_PLAYBACK_VTT_FILE")
) as media, tempfile.NamedTemporaryFile(suffix=".vtt", delete=not keep) as subtitle: media, subtitle = None, None
try:
if not mp3_fname:
media = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
media.close() media.close()
subtitle.close() mp3_fname = media.name
print(f"Media file: {media.name}") if not vtt_fname:
print(f"Subtitle file: {subtitle.name}\n") subtitle = tempfile.NamedTemporaryFile(suffix=".vtt", delete=False)
subtitle.close()
vtt_fname = subtitle.name
print(f"Media file: {mp3_fname}")
print(f"Subtitle file: {vtt_fname}\n")
with subprocess.Popen( with subprocess.Popen(
[ [
"edge-tts", "edge-tts",
f"--write-media={media.name}", f"--write-media={mp3_fname}",
f"--write-subtitles={subtitle.name}", f"--write-subtitles={vtt_fname}",
] ]
+ sys.argv[1:] + sys.argv[1:]
) as process: ) as process:
@@ -45,14 +57,19 @@ def _main() -> None:
with subprocess.Popen( with subprocess.Popen(
[ [
"mpv", "mpv",
f"--sub-file={subtitle.name}", f"--sub-file={vtt_fname}",
media.name, mp3_fname,
] ]
) as process: ) as process:
process.communicate() process.communicate()
finally:
if keep: if keep:
print(f"\nKeeping temporary files: {media.name} and {subtitle.name}") print(f"\nKeeping temporary files: {mp3_fname} and {vtt_fname}")
else:
if mp3_fname is not None and os.path.exists(mp3_fname):
os.unlink(mp3_fname)
if vtt_fname is not None and os.path.exists(vtt_fname):
os.unlink(vtt_fname)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -1,4 +1,4 @@
"""Edge TTS version information.""" """Edge TTS version information."""
__version__ = "6.1.0" __version__ = "6.1.1"
__version_info__ = tuple(int(num) for num in __version__.split(".")) __version_info__ = tuple(int(num) for num in __version__.split("."))