首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用gtts覆盖Python语言中预先存在的.mp3文件

使用gtts覆盖Python语言中预先存在的.mp3文件
EN

Stack Overflow用户
提问于 2018-03-05 00:33:26
回答 2查看 1.8K关注 0票数 0

我的语法有一个限制。我前段时间刚尝试过python,我是从AI开始的。我制作了一个像Jarvis这样的机器人来帮助我打开google或youtube。在Pythonspot.com提供的教程中,它显示为Ubuntu教程,但我使用的是Windows。还有一些工具或插件在windows中是不起作用的是mpg321。我已经找到了mixer.music的替代品来播放AI的声音。这是有效的,但我对第二个声音有限制,即我用audio.mp3发出第一个声音,然后当我的第二个声音使用相同的文件名时,即audio.mp3,我有这样的限制

回溯(最近一次调用):

文件"D:\@AI Projects\Jarvis\Jarvis.py",第71行,jarvis(数据)

文件"D:\@AI Projects\Jarvis\Jarvis.py",第53行,使用jarvis speak(ctime())

在speak tts.save("audio.mp3")中,文件"D:\@AI Projects\Jarvis\Jarvis.py",第17行

文件"C:\Users\inialdan\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts\tts.py",第110行,在save with open(savefile,'wb')中,作为f: PermissionError: Errno 13权限被拒绝:'audio.mp3‘

这是我的代码

代码语言:javascript
复制
#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
from time import ctime
import time
import os
import subprocess
from gtts import gTTS
from pygame import mixer 



def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    mixer.init()
    mixer.music.load('D:/@AI Projects/Jarvis/audio.mp3')
    mixer.music.play()

def recordAudio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Try to say something!")
        audio = r.listen(source)

    data = ""

    try:
        data = r.recognize_google(audio)
        print("You said : " + data)
    except sr.UnknownValueError:
        print("I'm Sorry, i couldn't understand what you mean ...")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data

def jarvis(data):

    CHROME = os.path.join('C:\\', 'Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe')

    if "jarvis" in data:
        speak("Yes, sir ?")
    if "what is your name" in data:
        speak("You can call me, Jarvis")
    if "where do you leave" in data:
        speak("In your heart.")
    if "how are you" in data:
        speak("I am fine")
    if "what time is it" in data:
        speak(ctime())
    if "where is" in data:
        data = data.split(" ")
        location = data[2]
        speak("Hold on Aldan, I will show you where " + location + " is.")
        os.system('taskkill /im chrome.exe')
        subprocess.call([CHROME, "https://www.google.nl/maps/place/" + location + "/&"])
    if "open" in data:
        data = data.split(" ")
        application = data[1]
        speak("Hold on Aldan, I will show you " + application)
        os.system('taskkill /im chrome.exe')
        subprocess.call([CHROME, "https://www." + application + ".com"])

time.sleep(2)
speak("Hi Aldan, How may I assist you?")
while 1:
    data = recordAudio()
    jarvis(data)

我尝试过使用os.remove ();删除audio.mp3并重写它。但还是失败了

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-03 06:42:02

我已经在TemporaryFile的帮助下修改了Speak方法。

Click on the icon to check the Code

代码语言:javascript
复制
from googletrans import Translator
import pygame, time
import tempfile
from gtts import gTTS
from pygame import mixer
from tempfile import TemporaryFile

def speak(text, lang='en'):
    """Text to speech. For funp."""
    try:
        translator = Translator()
        tts = gTTS(text=translator.translate(text, dest=lang).text, lang=lang)
        mixer.init()
        sf = TemporaryFile()
        tts.write_to_fp(sf)
        sf.seek(0)
        mixer.music.load(sf)
        mixer.music.play()
    except Exception:
        raise

票数 4
EN

Stack Overflow用户

发布于 2020-07-21 09:42:30

只需创建第二个def来删除文件名,并在您的演讲()之后运行该命令

代码语言:javascript
复制
def complete():
os.remove('audio.mp3')
speak("blah blah")
complete()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49097649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档