gTTS不保存MP3文件

时间:2018-09-24 14:23:01

标签: python artificial-intelligence google-text-to-speech

我想使用Python创建一个人工智能程序。我发现我需要gTTS,但它不保存mp3文件。请帮帮我。

#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'

1 个答案:

答案 0 :(得分:0)

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')  

将上面的文件另存为text_2_speech.py​​(例如)。我已经尝试过了,并且工作正常。从命令行键入python -W忽略text_2_speech.py​​,然后将文件另存为audio.mp3在当前目录中。