I have a lot of text-to-speech audio files that I need to save but often, the files get lost.
Currently I'm using
import gtts
from playsound import playsound
def say(speech):
tts = gtts.gTTS(speech)
tts.save("audio.mp3")
playsound("audio.mp3")
Is there any way that I can save the mp3 to wherever I desire?
You can just paste content of new audio file into another file like below:
import gtts
from playsound import playsound
def say(speech):
tts = gtts.gTTS(speech)
tts.save("audio.mp3")
playsound("audio.mp3")
# PLACE CONTENT INTO NEW FILE => S T A R T
main_file = open("audio.mp3", "rb").read()
dest_file = open('path/to_your/file_name.mp3', 'wb+')
dest_file.write(main_file)
dest_file.close()
# PLACE CONTENT INTO NEW FILE => E N D
or
import gtts
from playsound import playsound
import shutil
def say(speech):
tts = gtts.gTTS(speech)
tts.save("audio.mp3")
playsound("audio.mp3")
# PLACE CONTENT INTO NEW FILE => S T A R T
shutil.move("audio.mp3", "path/to_your/file_name.mp3")
# PLACE CONTENT INTO NEW FILE => E N D
You can just put the directory you need in the brackets when you save tts as mp3 file:
tts.save("directory/that/you/want/audio.mp3")
I did like this: s.save("your folder name/audio.mp3")
Related
I have a program that makes voice text using .txt ,but os.startfile doesn't let me select the file, hence get the path to it) I will be very grateful for your help!!
Как должно быть.
enter image description here
Как у меня)
enter image description here
import os
import pyttsx3 as pyt
from gtts import gTTS
class DICTOR:
def __init__(self, path):
self.engine = pyt.init()
#voices = self.engine.getProperty('voices')
self.engine.setProperty('voice', 0)
#rate = self.engine.getProperty('rate')
self.engine.setProperty('rate', 150)
with open(path, 'r') as file:
self.file = file.read()
# def speak(self):
# self.engine.say(self.file)
# self.engine.runAndWait()
def save(self, filename):
tss = gTTS(text = self.file, lang='en')
tss.save(filename)
#rate = self.engine.getProperty('rate')
path = os.startfile(filepath = 'D:\Python\Road_map\DICTOR')
DICTOR(path).save('.mp3')
I'm guessing your problem is with the formatting. You should add r to the start. It should be:
path = os.startfile(filepath = r"G:\EEGdatabase\6541455.docx")
the r is needed because \ is considered to be a special character by python. Another option is to escape \ with \\:
path = os.startfile(filepath = 'D:\\Python\\Road_map\\DICTOR')
More info in the docs.
Finally, since you haven't provided your error message, maybe this isn't your problem at all. If this doesn't fix it, add your error log so we know exactly what is going on.
Edit: It seems you have misunderstood the use of os.startfile. This function takes in something you need to "start", and starts it. So if you had said
os.startfile("something.pdf")
Then something.pdf will be opened in your default PDF software. If you instead passed in something like file.html, then this html file will be opened in your default web browser.
So you are now passing in D:\Python\Road_map\DICTOR, which is a folder, and the startfile function simply opens up that folder in Windows Explorer. A full reference for startfile can be found in the docs.
In any case, I don't think you need startfile at all. You need a file selector, which you can get using the Tkinter package. You can then say:
from Tkinter import Tk # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
path = filename
(Taken from https://stackoverflow.com/a/3579625/3730626)
I'm trying to read a .txt file using Google's text-to-speech API. But, when I try to run it, it gives an error that I can't quite fathom. So your help will be greatly appreciated!
My Python code:
#Import the required module for text
from gtts import gTTS
#required to play the converted file
import os
#The file you want to convert
with open('/Users/humma/Desktop/python_projects/flowers.txt', 'r') as myFile:
fileRead = myFile.read()
#passing file and language to the engine
myObj = gTTS(text = fileRead, lang = 'en-US', slow = False)
myObj.save('flowers.mp3')
os.system("flowers.mp3")
The error I get:
File "c:/Users/humma/Desktop/python_projects/txt-to-speech/txt-to-spch.py", line 12, in <module>
myObj.save('flowers.mp3')
File "C:\ProgramData\Anaconda3\lib\site-packages\gtts\tts.py", line 312, in save
self.write_to_fp(f)
File "C:\ProgramData\Anaconda3\lib\site-packages\gtts\tts.py", line 294, in write_to_fp
raise gTTSError(tts=self, response=r)
gtts.tts.gTTSError: 200 (OK) from TTS API. Probable cause: Unknown
Thank you in advance for your time :)
from gtts import gTTS
from io import BytesIO
from pygame import mixer
import time
def speak():
mp3_fp = BytesIO()
tts = gTTS('You need to read documetation properly', lang='en')
tts.write_to_fp(mp3_fp)
tts.save("Audio.mp3")
return mp3_fp
mixer.init()
sound = speak()
sound.seek(0)
mixer.music.load(sound, "mp3")
mixer.music.play()
It was lang = 'en-US' that caused the error. It's simply: lang = 'en'
I have a video file named 'video.mp4'. I am trying to seperate a section of audio from the video and save it as a wav file that can be used with other Python modules. I want to do this with MoviePy.
I send parameters to the write_audiofile function, specifying the filename, fps, nbyte, and codec.
Following the MoviePy AudioClip docs, I specified the codec as ‘pcm_s32le’ for a 32-bit wav file.
from moviepy.editor import *
sound = AudioFileClip("video.mp4")
newsound = sound.subclip("00:00:13","00:00:15") #audio from 13 to 15 seconds
newsound.write_audiofile("sound.wav", 44100, 2, 2000,"pcm_s32le")
This code generates a .wav file, named 'sound.wav'.
Opening the audio file in Audacity
The resulting file, sound.wav, can be opened in Audacity, however I run into problems when I try to use it as a wav file with other Python modules.
Playing the sound file in pygame
import pygame
pygame.mixer.init()
sound=pygame.mixer.Sound("sound.wav")
The third line gives the following error:
pygame.error: Unable to open file 'sound.wav'
Determining type of sound file using sndhdr.what()
import sndhdr
sndhdr.what("sound.wav")
The sndhdr method returned none
. According to the docs, when this happens, the method failed to determine the type of sound data stored in the file.
Reading the file with Google Speech Recognition
import speech_recognition as sr
r = sr.Recognizer()
audio = "sound.wav"
with sr.AudioFile(audio) as source:
audio = r.record(source)
text= r.recognize_google(audio)
print(text)
This code stops execution on the second to last line:
ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format
Why does the audio file open in Audacity, if sndhdr.what() can not recognize it as an audio file type?
How can I properly export a MoviePy AudioClip as a wav file?
I had the same issue with no codec specified or with codec = 'pcms32le', the one that worked for me was pcm_s16le.
Note that I am using "fr-FR" language, you should probably adapt to yur needs.
here is the entire code :
# Python code to convert video to audio
import moviepy.editor as mp
import speech_recognition as sr
# Insert Local Video File Path
clip = mp.VideoFileClip("/tmp/data/test.mp4")
# Insert Local Audio File Path
clip.audio.write_audiofile("/tmp/data/test.wav",codec='pcm_s16le')
# initialize the recognizer
r = sr.Recognizer()
# open the file
with sr.AudioFile("/tmp/data/test.wav") as source:
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data, language = "fr-FR")
print(text)
I had the same issue. I was trying to get a mp4 file from URL, then convert It into wav file and call Google Speech Recognition over It. Instead I used pydub to handle conversion and it worked! Here's a sample of the code:
import requests
import io
import speech_recognition as sr
from pydub import AudioSegment
# This function translate speech to text
def speech_to_text(file):
recognizer = sr.Recognizer()
audio = sr.AudioFile(file)
with audio as source:
speech = recognizer.record(source)
try:
# Call recognizer with audio and language
text = recognizer.recognize_google(speech, language='pt-BR')
print("Você disse: " + text)
return text
# If recognizer don't understand
except:
print("Não entendi")
def mp4_to_wav(file):
audio = AudioSegment.from_file(file, format="mp4")
audio.export("audio.wav", format="wav")
return audio
def mp4_to_wav_mem(file):
audio = AudioSegment.from_file_using_temporary_files(file, 'mp4')
file = io.BytesIO()
file = audio.export(file, format="wav")
file.seek(0)
return file
url = ''
r = requests.get(url, stream=True)
file = io.BytesIO(r.content)
file = mp4_to_wav_mem(file)
speech_to_text(file)
Note that I wrote two functions: mp4_to_wav and mp4_to_wav_mem. The only difference is mp4_to_wav_mem handle all files in memory and mp4_to_wav generates .wav file.
I read the docs of MoviePy and found that the parameter nbyte should be consistent with codec. nbyte is for the Sample width (set to 2 for 16-bit sound, 4 for 32-bit sound). Hence, it better set nbyte=4, when you set codec=pcm_s32le.
i think this is the right method:
import os
from moviepy.editor import AudioFileClip
PATH= "files/"
fileName = "nameOfYourFile.mp4"
newFileName = "nameOfTheNewFile"
Ext = "wav"
AudioFileClip(os.path.join(PATH, f"{fileName}")).write_audiofile(os.path.join(PATH, f"{newFileName}.{Ext}"))
I think this approach is very easy to understand.
from moviepy.editor import *
input_file = "../Database/myvoice.mp4"
output_file = "../Database/myvoice.wav"
sound = AudioFileClip(input_file)
sound.write_audiofile(output_file, 44100, 2, 2000,"pcm_s32le")
I want to make WAVE files by using gTTS module.
I downloaded gTTS module from https://pypi.python.org/pypi/gTTS`
I could make some files by it and stream the sounds by clicking these files.
But I want to know what kind of files can gTTS save?
Here is the sample code:
import tempfile
from gtts import gTTS
tts = gTTS(text='hello', lang='en', slow=True)
tts.save("hello.wav")
f = tempfile.TemporaryFile()
tts.write_to_fp(f)
f.close()
When I used the soundfile with my code, I couldn't make it.
from PySide import QtGui
from PySide import QtCore
import sys
import os
class HelloSpeaker(QtGui.QPushButton):
def __init__(self,parent=None):
super(HelloSpeaker,self).__init__(parent=None)
self.setText("Stream")
self.connect(self,QtCore.SIGNAL("clicked()"),self.play_sound)
def play_sound(self):
sound = QtGui.QSound(os.getcwd()+"hello.wav")
sound.play(os.getcwd()+"hello.wav")
def main():
try:
QtGui.QApplication([])
except Exception as e:
print(28,e)
hello = HelloSpeaker()
hello.show()
sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
main()
Yes, I found out some QSound-questions in stack overflow.
QSound don't work because of some unknown reasons.
For example:
QSound::play("soundpath") call works but a QSound object doesn't
Playing sound with Pyqt4 and QSound
QT5 QSound does not play all wave files
Other persons are bothered.
But I don't ascribed to QSound and I doubt that gTTS can't make .wav files.
because
import wave
wf = wave.open("hello.wav", "rb")
result:
wave.Error: file does not start with RIFF id
I heard this error shows this file is not a wave file.
So I wonder that gTTS don't make ".wav" files.
(But I can play it by ITunes , other players.)
Can gTTS make "mp3" files only?
environment:
python 3.6.3 pyside1.2.4
gTTS only saves the bytes directly without doing any conversion, and these bytes are encoded in mp3, you can verify it by checking the source code:
....
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=headers,
proxies=urllib.request.getproxies(),
verify=False)
if self.debug:
print("Headers: {}".format(r.request.headers))
print("Request url: {}".format(r.request.url))
print("Response: {}, Redirects: {}".format(r.status_code, r.history))
r.raise_for_status()
for chunk in r.iter_content(chunk_size=1024): # write the bytes directly
fp.write(chunk)
...
I am getting an error when writing to an audio file.
Basically, I am overwriting the data in the mp3 whenever my function gets called and then playing it.
It works the first time through, but then gives me [Errno 13] Permission denied: 'file.mp3' then on.
Here is the code:
def speech(self, response):
audio_file = "response.mp3"
tts = gTTS(text=str(response), lang="en")
tts.save(audio_file)
pygame.mixer.init()
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()
Error is on the tts.save line.
More info:
Traceback (most recent call last):
File "myprojectpath", line 63, in speech
tts.save(audio_file)
File "C:\Python35\lib\site-packages\gtts\tts.py", line 93, in save
with open(savefile, 'wb') as f:
Thanks!
from gtts import gTTS
import playsound
import os
x = ['sunny', 'sagar', 'akhil']
tts = 'tts'
for i in range(0,3):
tts = gTTS(text= x[i], lang = 'en')
file1 = str("hello" + str(i) + ".mp3")
tts.save(file1)
playsound.playsound(file1,True)
print 'after'
os.remove(file1)
change the file name every time you save it, this worked for me.
You should name your file for a unique name each time , so the solution that I realized is to name your file with date like this ==>
date_string = datetime.now().strftime("%d%m%Y%H%M%S")
filename = "voice"+date_string+".mp3"
tts.save(filename)
playsound.playsound(filename)
You can create another file in the same folder, just copy and rename it.
File you want to use: ("C:/.../file.mp3")
copied file: ("C:/.../file_copy.mp3")
You can change the file loaded in mixer.music.load("C:/.../file.mp3") to mixer.music.load("C:/.../file_copy.mp3") and delete delete the ("C:/.../file.mp3") without problem using this:
from pygame import mixer
import os
mixer.music.load("C:/.../file.mp3")
mixer.music.play()
mixer.music.load("C:/.../file_copy.mp3")
os.remove("C:/.../file.mp3")
but the code above will remove the file before even playing it, so you can use a method to await it play:
from pygame import mixer
import os
mixer.music.load("C:/.../file.mp3")
mixer.music.play()
while mixer.music.get_busy(): # check if the file is playing
pass
mixer.music.load("C:/.../file_copy.mp3")
os.remove("C:/.../file.mp3")
try to save the fi open an other file whitout playing, then you will have the permission to delete it here is the talk function:
def Talk (mytext, language= "fr" ):
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("1.mp3")
mixer.init()
mixer.music.load("1.mp3")
mixer.music.set_volume(1)
mixer.music.play()
mixer.music.get_endevent()
while mixer.music.get_busy():
continue
mixer.music.load("Talker\empty.mp3")
os.remove("1.mp3")
You are getting this error because the mixer is not releasing that audio file. If you want to mixer release that file you can load a useless or empty file into a mixer. Then you can overwrite that audio file.
E. g.. Mixer.load("C:/...../empty.mp3")
After this previously loaded file is released.
if you don't want the file before saved anymore, you can delete the audio file before saving the next file. so, you can save the new file with the same name.
bellow is the code I ran successfully
import speech_recognition as ram
from playsound import playsound
from gtts import gTTS
import os
r=ram.Recognizer()
with ram.Microphone() as source:
while True:
r.adjust_for_ambient_noise(source,duration=1)
print("I'm Listening....")
playsound('audio/listen.mp3')
audio=r.listen(source, timeout=5)
text=r.recognize_google(audio)
tts = gTTS("Boss. you said "+str(text)+". i'm opening it")
tts.save('audio/text.mp3')
print("you said "+text+".")
playsound('audio/text.mp3')
os.remove('audio/text.mp3')
a much better solution is this, if i had the time i would also calculate the probability of ever landing a same file
from gtts import gTTS
from playsound import playsound
import random
import os
#creating a super random named file
r1 = random.randint(1,10000000)
r2 = random.randint(1,10000000)
randfile = str(r2)+"randomtext"+str(r1) +".mp3"
tts = gTTS(text='hey, STOP It!!', lang='en', slow=True)
tts.save(randfile)
playsound(randfile)
print(randfile)
os.remove(randfile)
I have faced the same problem, then figure it out that the file is pointing to the previous position so I have to release it. It works fine and important thing is that it does not create an mp3 file also. If you don't release it, it will work fine for the first time and save the audio file then the error occurs for the second time.
from gtts import gTTS
import os
import playsound
def tts(text, lang):
file = gTTS(text = text, lang = lang)
file.save("audio/speak_func.mp3")
playsound.playsound('audio/speak_func.mp3', True)
os.remove("audio/speak_func.mp3")