Skip to content

Instantly share code, notes, and snippets.

@alecjacobson
Last active March 5, 2023 23:54

Revisions

  1. alecjacobson revised this gist Mar 5, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions say.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # https://stackoverflow.com/a/51164950/148668
    #
    # python3 -m pip install gtts playsound pyobjc
    from gtts import gTTS
    import sys
    from io import BytesIO
  2. alecjacobson revised this gist Mar 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion say.py
    Original file line number Diff line number Diff line change
    @@ -26,5 +26,5 @@
    else:
    filename = sys.argv[2]
    tts.save(filename)
    if args.output_file == None:
    if args.output_file is None:
    playsound.playsound(filename)
  3. alecjacobson revised this gist Mar 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion say.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@

    tts = gTTS(text=input_text, lang='en', slow=False)

    if args.output_file == None:
    if args.output_file is None:
    filename = tempfile.NamedTemporaryFile().name
    else:
    filename = sys.argv[2]
  4. alecjacobson created this gist Mar 1, 2023.
    30 changes: 30 additions & 0 deletions say.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # https://stackoverflow.com/a/51164950/148668
    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 == None:
    filename = tempfile.NamedTemporaryFile().name
    else:
    filename = sys.argv[2]
    tts.save(filename)
    if args.output_file == None:
    playsound.playsound(filename)