Search code examples
pythonartificial-intelligencegoogle-text-to-speech

gTTS Doesn't Save MP3 Files


I want to create an artificial intelligence program using Python. I found out that I need gTTS but it doesn't save mp3 files. Help me, please.

#gtts is imported
def talkToMe(audio, lgg = 'en'):
    #print(audio)
    tts = gTTS(text = audio, lang = lgg)
    #tts.save('audio.mp3') #doesn't work
    with open("audio.mp3") as fp: #doesn't work
        tts.write_to_fp(fp)
    os.system('mpg123\mpg123.exe audio.mp3')

Traceback (most recent call last):
  File "C:\Users\zigzag\Desktop\gtts_test1\main.py", line 9, in <module>
    talkToMe("hello")
  File "C:\Users\zigzag\Desktop\gtts_test1\main.py", line 7, in talkToMe
    tts.write_to_fp(fp)
  File "B:\Python36\lib\site-packages\gtts\tts.py", line 187, in write_to_fp
    part_tk = self.token.calculate_token(part)
  File "B:\Python36\lib\site-packages\gtts_token\gtts_token.py", line 28, in calculate_token
    seed = self._get_token_key()
  File "B:\Python36\lib\site-packages\gtts_token\gtts_token.py", line 62, in _get_token_key
    a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Solution

  • from gtts import gTTS    
    #gtts is imported
    audio = 'Hello , I am Tina, your digital assistant'
    def talkToMe(audio, lgg = 'en'):
        #print(audio)
        tts = gTTS(text = audio, lang = lgg)
        tts.save('audio.mp3') #doesn't work
        return None
    talkToMe(audio , lgg ='en')  
    

    save the above file as text_2_speech.py (as an e.g.).i have tried it and it is working fine. from command line type python -W ignore text_2_speech.py and then the file is saved as audio.mp3 in the current directory.