my problem starts here:
pyttsx and gTTS module errors
gTTS works well, takes text from text file, but first creates mp3 file, then if I want listen, I must call this mp3, so it is good but it would be better if I can avoid any audio files, and get just read from text file. maybe somehow I can use google voice to read from text file..? anyway main question now is other
if I can use only gTTS what is the best way to play mp3 on Windows 10-64 bit, Python 3.5
ok with os:
import os
os.startfile("D:\my path/rec1.mp3")
it is good, but I don't want use default player, need something like simpleaudio for mp3...
with pygame I have installation problem and not sure, how good is use it this way:
from pygame import mixer
mixer.init()
mixer.music.load('D:/my path/rec1.mp3')
mixer.music.play()
vlc just how to install it? with easy_install vlc I got error: could not find suitable distribution for requirement.parse ('vlc') and with pip install vlc error: could not find a version that satisfies the requirement vlc (from versions: ) no matching distribution found for vlc
import vlc
p = vlc.MediaPlayer("file:/my path/rec1.mp3")
p.play()
p.stop()
with pyglet:
import pyglet
music=pyglet.media.load('D:/my path/rec1.mp3')
music.play()
pyglet.app.run()
I got this error:
'AVbin is required to decode compressed media')
pyglet.media.riff.WAVEFormatException: AVbin is required to decode compressed media
subprocess also uses default player:
import subprocess
sound_program = "path to player"
sound_file = "D:/my path/rec1.mp3"
subprocess.call([sound_program, sound_file])
with mp3play, not sure how to use it:
import mp3play
filename = (r'D:\my path/rec1.mp3')
clip = mp3play.load(filename)
clip.play()
I tried it this way:
filename = ('D:\my path/rec1.mp3')
this way:
filename = r'D:\my path/rec1.mp3'
In all cases I got error:
Traceback (most recent call last):
File "D:/dt/PyCharm_project/0_ASK.py", line 18, in <module>
import mp3play
File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\mp3play\__init__.py", line 4, in <module>
from .windows import AudioClip as _PlatformSpecificAudioClip
File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\mp3play\windows.py", line 27
print 'Error %s for "%s": %s' % (str(err), txt, buf)
^
SyntaxError: invalid syntax
ok so with pydub:
from pydub import AudioSegment
from gtts import gTTS
import simpleaudio as sa
blabla = ('my voice')
tts = gTTS(text=blabla, lang='en')
tts.save("D:/my path/rec.mp3")
rec = AudioSegment.from_mp3("D:\my path\rec.mp3")
rec.export("rec.wav", format="wav")
#rec = AudioSegment.ffmpeg ("D:\my path\rec.mp3")
#rec.export("rec.wav", format="wav")
#rec = AudioSegment.converter ("D:\my path\rec.mp3")
#rec.export("rec.wav", format="wav")
wave_obj = sa.WaveObject.from_wave_file('D:/my path/rec.wav'')
play_obj = wave_obj.play()
play_obj.wait_done()
Errors in the sequence first with AudioSegment.from_mp3 :
RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
Traceback (most recent call last):
File " D:/dt/PyCharm_project/0_ASK.py", line 9, in <module>
rec = AudioSegment.from_mp3("D:\my path\rec.mp3")
File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\pydub\audio_segment.py", line 438, in from_mp3
return cls.from_file(file, 'mp3')
File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\pydub\audio_segment.py", line 366, in from_file
file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\pydub\utils.py", line 59, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
OSError: [Errno 22] Invalid argument: 'D:\my path\rec.mp3'
with AudioSegment.ffmpeg :
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
Traceback (most recent call last):
File "D:/dt/PyCharm_project/0_ASK.py ", line 12, in <module>
rec = AudioSegment.ffmpeg ("D:\my path\rec.mp3")
TypeError: 'str' object is not callable
with AudioSegment.converter:
RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
Traceback (most recent call last):
File "D:/dt/PyCharm_project/0_ASK.py", line 15, in <module>
rec = AudioSegment.converter ("D:\my path\rec.mp3")
TypeError: 'str' object is not callable
not sure maybe webbrowser, but how to install it?
import webbrowser
webbrowser.open("D:/my path/rec1.mp3")
Same problem here. It works with playsound 1.2.1 for me.
Install with :
$ pip install playsound
test with:
>>>from playsound import playsound
>>>playsound('/path/to/a/sound/file/you/want/to/play.mp3')
You can use "subprocess".
from subprocess import call
from gtts import gTTS
import os
blabla = input('Type IN: ')
tts = gTTS(text=blabla, lang='en')
tts.save("test.mp3")
call(["vlc", "test.mp3"])
This program asks user to type in anything and it will say that trough vlc.
I use this on Linux but i don't now if it works on Windows.
with pygame I have installation problem and not sure, how good is use it this way:
Can you provide more details about the pygame installation error?
I was able to use PYGAME with this code, where "hello.mp3" is a file in the same directory
from gtts import gTTS
tts = gTTS(text='Hello', lang='en')
tts.save("hello.mp3")
from Tkinter import *
root = Tk()
from pygame import mixer
mixer.init()
mixer.music.load('hello.mp3')
mixer.music.play()
root.mainloop()
vlc just how to install it?
I also used VLC. I have installed it with these commands:
sudo pip install python-vlc
And I got this error:
NameError: no function 'libvlc_new
So, I tryed the command:
sudo apt-get install vlc
And it worked with this code:
from gtts import gTTS
tts = gTTS(text='Hello', lang='en')
tts.save("hello.mp3")
from Tkinter import *
root = Tk()
import vlc
p = vlc.MediaPlayer("hello.mp3")
p.play()
root.mainloop()
Hope It help You.
This will do the job and without the need for files:
def say(text, lang='en'):
""" Speak the provided text.
"""
import pygame
from gtts import gTTS
import io
tts = gTTS(text=text, lang=lang, slow=False)
pygame.mixer.init()
pygame.init() # this is needed for pygame.event.* and needs to be called after mixer.init() otherwise no sound is played
with io.BytesIO() as f: # use a memory stream
tts.write_to_fp(f)
f.seek(0)
pygame.mixer.music.load(f)
pygame.mixer.music.set_endevent(pygame.USEREVENT)
pygame.event.set_allowed(pygame.USEREVENT)
pygame.mixer.music.play()
pygame.event.wait() # play() is asynchronous. This wait forces the speaking to be finished before closing f and returning
I think you are looking for :
pyttsx3
Its an offline cross-platform TTS that's compatible with python3 and python2.
Install :
pip install pyttsx3
If u want an offline TTS for python unlike gTTS , pyttsx3 is your best choice in my opinion.
I don't know the solution to all of the questions, but the issue you're having with pydub is that you don't have ffmpeg or avconv installed. There are instructions on the pydub github:
Windows:
Download and extract libav from Windows binaries provided here.
Add the libav /bin folder to your PATH envvar
pip install pydub
webbrowser should be part of you standard python install.
Check if there is a webbrowser.py under C:\Your_Python_Folder\Lib.
Below code works just fine for me as webbrowser.py is present at above mentioned folder.
import webbrowser
webbrowser.open("rec.mp3")
install pyglet using python -m pip install pyglet
Download installed here Link to AVbin setup ( it's a must )
32/64 bit
import pyglet
song = pyglet.media.load('file.mp3')
song.play()
pyglet.app.run()
That's all.
You can use playsound module
from playsound import playsound
playsound(file.mp3)
Boom that's it.it plays the audio right way works like charm
If you are on windows then this should do the job,
import winsound
winsound.PlaySound('your_file.mp3',winsound.SND_FILENAME)
Remember this package is only present in python for windows.
If AVbin error occurs...
First download AVbin.exe and Install it
then Go to local disk C --> windows --> System32 --> search the Avbin.dll and copy that file
then paste that particular file in your root directory
Error will vanish
import os
os.system("music.mp3")
Related
I am trying to play a .wav file with pygame
import pygame
pygame.mixer.init()
s = pygame.mixer.Sound('absolute path to file')
s.play()
it gives no error, but it seems like it doesn't play anything.
This is the complete output
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
installed pygame via pip in a conda python 3.6 enviroment. mac book pro 2019 with macOS Mojave
Add a line at the end :
input ('Press enter to continue.')
and you will hear it. Your script is ending before it has a chance to play the sound.
As bashBediam said the problem was that the code was ending not giving time to the audio to play. i fixed that doing like this.
import pygame
pygame.mixer.init()
s = pygame.mixer.Sound(path)
channel = s.play()
while channel.get_busy():
pygame.time.wait(100)
I have Installed Python 3 (32bit) on Windows.
I have read this answer, but I can't install cv2 using
pip install opencv_python-3.4.1-cp37-cp37m-win32.whl and it gives error as
opencv_python-3.4.1-cp37-cp37m-win32.whl is not a supported wheel on this platform.
when I tried Python shell, and run the command import cv2 it doesn't give error,
but when I try it as import cv2 in IDLE terminal, then It gives error as
>>> import cv2
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
You could also install opencv in these three easy steps
Type py -m pip install opencv-python
In the same window type python or open IDLE python.
Now type import cv2.
there you go... simple and easy i hope it helps.
1)Download OpenCV from here
2)Extract the zip file to Root drive(mostly 'C'Drive)
3)Goto folder opencv\build\python\2.7\x86 and copy cv2 file to your main Python directory,
in my case Python directory is
C:\Users\Deshmukh Brothers\AppData\Local\Programs\Python\Python36-32\Lib\site-packages
and paste cv2 file there.
4)Run command as python -m idlelib and now it will prompt idle-shell.
5)now it will work as import cv2, it nothing occurred and cursor went to next line then it is successfully installed.
I'm trying to access my laptop webcam and from what I have seen, I need to use the cv2 package. I downloaded it from PyPi but the contents of the file seem to be missing a lot of content as it only has __init__.py, cv2.cp36-win32.pyd, and opencv_ffmpeg320.dll. So whenever I try use things such as VideoCapture and imwrite, it says it cannot find a reference to them.
When I run the application, I get the following error message:
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "C:/Users/RedCode/PycharmProjects/FunApps/ImageFile.py", line 1, in <module>
import cv2
File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cv2\__init__.py", line 7, in <module>
from . import cv2
ImportError: numpy.core.multiarray failed to import
I have got the numpy package installed and imported but it didn't fix the issue so I'm guessing that's not the actual cause of the problem, so I'm certain it must have something to do with the cv2 package.
The code I'm using right now is as follows:
import cv2
import numpy
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
I'm also using Python 3.6 and Windows 10 if that helps with anything.
How can I get my application to work?
In your case you can solve your problem by using the following command:
pip install -U numpy
Check existing package:
pip show numpy
And upgrade it via:
pip install numpy --upgrade
Check the path if you still got error:
> import numpy
> print numpy.__path__
You must install the latest version of numpy. Use the command pip install numpy --upgrade or you can just download the wheel file from http://www.lfd.uci.edu/~gohlke/pythonlibs/ . Just check for your python version and platform and download the necessary wheel file. Next use pip to install the wheel file.
I want to use scikits.talkbox, but i get the following error while import scikits.talkbox.
Traceback (most recent call last):
File "/home/seref/Desktop/machine learning codes/MFCC/main.py", line 3, in
from scikits.talkbox.features.mfcc import mfcc
File "/usr/local/lib/python3.5/dist-packages/scikits/talkbox/init.py", line 3, in
from tools import *
ImportError: No module named 'tools'
code sample
import scipy.io.wavfile
from scikits.talkbox.features.mfcc import mfcc
sample_rate, X = scipy.io.wavfile.read("data/test_1.wav")
ceps, mspec, spec = mfcc(X)
I use following code when installing scikits.talkbox
sudo pip3 install scikits.talkbox
Operation system ubuntu 16.10
python interpreter 3.5.2+
If you want MFCCs in Python 3, librosa is probably the better library of your choice.
windows 10-64bit
I'm trying to use some text-to-speech tool to read text from lines of .txt document, something like this:
so with pyttsx:
import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait()
I got this error:
Traceback (most recent call last):
File "...", line 1, in <module>
import pyttsx
File "/.../pyttsx/__init__.py", line 18, in <module>
from engine import Engine
ImportError: No module named 'engine'
now gTTS, available as gtts_token, so how to use it? because this way module is unrecognizable:
import gtts
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")
or:
from gtts import gTTS
blabla = ("my voice")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")
error:
import gtts
ImportError: No module named 'gtts'
also I'm want try to use espeak but not sure how to install it, is it available with pip install or I have to install it some other way to try it:
import subprocess
text = '"my voice"'
subprocess.call('espeak '+text, shell=True)
or:
import os
os.system("espeak 'my voice'")
so I'm trying to find some solution, but everything I tried is not working here...
for python3 use
pyttsx3
Its a new library compatible with both python3 and python2. Unlike gTTS it doesn't need internet connection and there is no delay in the sound produced.
Install:
pip install pyttsx3
Usage :
import pyttsx3
engine = pyttsx3.init()
engine.say("Hi this is working ");
engine.setProperty('volume',0.9)
engine.runAndWait()
I am using windows 10 and Python 2.7.
For pyttsx:
Below code is working fine for me. I did get ImportError: No module named win32api error for which I had to install win32api from here
After that I could play "my voice". Although the quality and fidelity of spoken sound was very low. gtts is much better in that regards.
import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait()
For the error you are getting, Can you look into your python folder and see if engine.py file is present?
For e.g. in my case, I've pyttsx modules installed at following location
C:\Python27\Lib\site-packages\pyttsx and here is a list of files,
Name
----
drivers
driver.py
driver.pyc
engine.py
engine.pyc
voice.py
voice.pyc
__init__.py
__init__.pyc
Since import of engine is failing, I am wondering if you have engine.py file in the correct folder or present at all.
For gtts:
I tried playing sound with winsound, but it did not work. Using pydub I was able to play the audio file. But, since your requirement is not to use a file, this may be a moot point.
import gtts
import winsound
from pydub import AudioSegment
from pydub.playback import play
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("rec.mp3")
print "Playing sound .."
#winsound.PlaySound("rec.wav", winsound.SND_FILENAME)
song = AudioSegment.from_mp3("rec.mp3")
play(song)
Hope this helps.
I'm using python2.7 on Ubuntu.
Try to replace "from engine import Engine" with "from .engine import Engine" in the engine module.It work for me!