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

@@ -12,44 +12,51 @@ from shutil import which
def main():
"""
Main function.
"""
if which("mpv") and which("edge-tts"):
depcheck_failed = False
if not which("mpv"):
print("mpv is not installed.", file=sys.stderr)
depcheck_failed = True
if not which("edge-tts"):
print("edge-tts is not installed.", file=sys.stderr)
depcheck_failed = True
if depcheck_failed:
print("Please install the missing dependencies.", file=sys.stderr)
sys.exit(1)
media = None
subtitle = None
try:
media = tempfile.NamedTemporaryFile(delete=False)
media.close()
subtitle = tempfile.NamedTemporaryFile(delete=False)
try:
media.close()
subtitle.close()
subtitle.close()
print()
print(f"Media file: {media.name}")
print(f"Subtitle file: {subtitle.name}\n")
with subprocess.Popen(
[
"edge-tts",
"--boundary-type=1",
f"--write-media={media.name}",
f"--write-subtitles={subtitle.name}",
]
+ sys.argv[1:]
) as process:
process.communicate()
print(f"Media file: {media.name}")
print(f"Subtitle file: {subtitle.name}\n")
with subprocess.Popen(
[
"edge-tts",
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:
with subprocess.Popen(
[
"mpv",
f"--sub-file={subtitle.name}",
media.name,
]
) as process:
process.communicate()
finally:
if media is not None:
os.unlink(media.name)
if subtitle is not None:
os.unlink(subtitle.name)
else:
print("This script requires mpv and edge-tts.")
if __name__ == "__main__":