Text-to-speech (TTS) module that works under Python 3

前端 未结 4 1848
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 23:31

I have tried PyTTS (deprecated) and PyTTSx (the most recommended) and two Google TTS solutions (gTTS and another one by some guy named Hung Truong) but none of them worked under

相关标签:
4条回答
  • 2021-02-08 23:57

    Rather than using a module, you could use Google Text-to-Speech API . You can easily use this URL to generate a wav file and get it through a simple HTTP request:

    http://www.translate.google.com/translate_tts?tl=en&q=Hello%20World

    0 讨论(0)
  • 2021-02-09 00:15

    The best solution for that is :

    pyttsx3


    Pyttsx3 is an offline cross-platform Text-to-Speech library which is compatible with both Python 3 and Python 2 and supports multiple TTS engines.

    I have found it very useful and there is no delay in sound production unlike gTTS which needs internet connection to work and also has some delay.

    To install :

    Here is a sample code :

    
        import pyttsx3
        engine = pyttsx3.init()
        engine.say("Hello this is me talking")
        engine.setProperty('rate',120)  #120 words per minute
        engine.setProperty('volume',0.9) 
        engine.runAndWait()
        
    
    
    0 讨论(0)
  • 2021-02-09 00:18

    A user on Reddit found a solution.

    Turns out that gTTS works under Python 3.x, it was me that was importing the module wrong.

    I was using:

    import gtts
    blabla = ("Spoken text")
    tts = gTTS(text=blabla, lang='en')
    tts.save("C:/test.mp3")
    

    Resulting in the following error:

    NameError: name 'gTTS' is not defined
    

    When the correct way is:

    from gtts import gTTS
    blabla = ("Spoken text")
    tts = gTTS(text=blabla, lang='en')
    tts.save("C:/test.mp3")
    
    0 讨论(0)
  • 2021-02-09 00:19

    I just installed gtts 1.0.7 that was uploaded on 2015-10-07

    The following code works for me in Python 3.5:

    import subprocess
    from gtts import gTTS
    
    audio_file = "hello.mp3"
    tts = gTTS(text="Hello World!", lang="en")
    tts.save(audio_file)
    return_code = subprocess.call(["afplay", audio_file])
    

    I'm on a Mac using in the inbuilt "afply" to play the mp3 but there are other ways e.g. Playing mp3 song on python

    0 讨论(0)
提交回复
热议问题