Last active
March 5, 2023 23:54
-
-
Save alecjacobson/0581fff5a7f9ea40b0218b51c39e49f2 to your computer and use it in GitHub Desktop.
Quick replacement for mac os `say` using Google Text To Speech
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/51164950/148668 | |
# | |
# python3 -m pip install gtts playsound pyobjc | |
from gtts import gTTS | |
import sys | |
from io import BytesIO | |
import objc | |
import playsound | |
import tempfile | |
import argparse | |
import sys | |
parser = argparse.ArgumentParser(description='`say` replacement using gtts') | |
parser.add_argument('text', type=str, nargs='?', help="Text to be read (also accepted from stdin)" ) | |
parser.add_argument('-o','--output-file', type=str, | |
help='An optional output file argument') | |
args = parser.parse_args() | |
input_text = sys.stdin.read() if args.text == None else args.text | |
tts = gTTS(text=input_text, lang='en', slow=False) | |
if args.output_file is None: | |
filename = tempfile.NamedTemporaryFile().name | |
else: | |
filename = sys.argv[2] | |
tts.save(filename) | |
if args.output_file is None: | |
playsound.playsound(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment