Skip to content

Instantly share code, notes, and snippets.

@petermeissner
Created August 31, 2019 19:52
Show Gist options
  • Save petermeissner/e515035aa9d527f62e166d6e546b1aa2 to your computer and use it in GitHub Desktop.
Save petermeissner/e515035aa9d527f62e166d6e546b1aa2 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# usage:
# shell> python3 python text_to_speech.py -l en -t "Wohooo, it works!" -p
# imports
import argparse
from gtts import gTTS
from playsound import playsound
# commandline argument parsing
parser = argparse.ArgumentParser(description='Transform Text into speach saved as mp3.')
parser.add_argument('-t', dest='input_text', default='Zonk!', help='Text to be transformed to speach.')
parser.add_argument('-o', dest='output_file', default='speech.mp3', help='File name to store speach in.')
parser.add_argument('-l', dest='language_code', default='en', help='Language code.')
parser.add_argument('-p', dest='play_speech', action='store_true', help='Play speech after generation?')
args = parser.parse_args()
# transforming text to speach
speach = gTTS(args.input_text, lang=args.language_code)
speach.save(args.output_file)
# play file?
if args.play_speech:
playsound(args.output_file)
# print file created
print(args.output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment