I have pip installed pydub but still didn't work - python

After I had pip installed pydub and SpeechRecognition, I still can't make the program run successfully although I extracted the files from ffmpeg and specified the folder name bin in the corresponding directory. I tried to find solutions to solve the problem but it sill didn't work after I followed the steps clearly from the web. May I ask what is the good way to read .wav files using pydub AudioSegment?
Warning (from warnings module):
File "C:\Python38\lib\site-packages\pydub\utils.py", line 171
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work

Make sure that you have ffmpeg http://www.ffmpeg.org/ installed. You can get help from this official page.
Other thing that I can think of is that ffmpeg is installed and is in your path but not in the path of the process using pydub.
If this is the reason for the error, then you can set the absolute path to ffmpeg directly like shown below:
import pydub
pydub.AudioSegment.ffmpeg = "/absolute/path/to/ffmpeg"
sound = AudioSegment.from_mp3("test.mp3")
Give this a try.

Related

Can't load audio files with librosa

Unless I use one of librosa's native 'example' audio files which are in some format '.ogg', I cannot load audio.
Every github issue and SO answer says I need to install ffmpeg. I found a python package called ffmpeg and installed it into my virtual environment. I also found some sort of multi-purpose program called ffmpeg and installed it onto my Windows machine, and added it's location to my PATH variable.
I still get the same warnings and errors that everyone else is getting:
c:\users\wesle\appdata\local\programs\python\python38\lib\site-packages\librosa\core\audio.py:162: UserWarning: PySoundFile failed. Trying audioread instead.
warnings.warn("PySoundFile failed. Trying audioread instead.")
---------------------------------------------------------------------------
.
.
.
115 # All backends failed!
--> 116 raise NoBackendError()
I can find no resolution for this problem despite the number of people asking for help on it.
I don't know what fileformat you are trying to load, but in most cases installing ffmpeg is the solution to this problem. It is important to add the "../bin/" directory to PATH and restart the environment. Using the "set" command from prompt you can check that the current path contains the folder.

using pydub to convert audio from .au to .wav

In this code m getting an error as follows:
import os
from pydub import AudioSegment
song = AudioSegment.from_file('C:/Users/Rishabh/Desktop/metal.00000.au','au')
song.export(path[:-3]+"wav",format='wav')
I am getting the following error:
WindowsError: [Error 2] The system cannot find the file specified
I realize your question is more than two and a half years old by now, and hopefully you've not been stuck with this problem since then. But maybe someone else fumbles into the same problem (such as me...)
As Jiaaro mentions, check the ffmpeg path. Also, make sure you've installed ffmpeg in your environment. Here is an old thread I found on github with some more detail in checking ffmpeg availability, also with answers from Jiaaro:
https://github.com/jiaaro/pydub/issues/5#issuecomment-8397126
Myself, I'm running anaconda on windows 10 and needed to install pydub and ffmpeg separately in my conda environment for pydub to work.
If your goal is to simply convert from au to wav (without any other programmatic context) you can execute ffmpeg in your environment from the terminal with the -i flag. Like so:
ffmpeg -i "C:\Users\Rishabh\Desktop\metal.00000.au" "C:\Users\Rishabh\Desktop\metal.00000.wav"

tesseract command not working from command line in windows

I installed tesseract-OCR for windows and it resides in C:\Program Files\Tesseract-OCR path in my system.
I set up an environment variable by adding C:\Program Files\Tesseract-OCR in the PATH variable.
I also set up TESSDATA_PREFIX in system variable to the same above tesseract location.
Still, when I try to run the command "tesseract some path\image.tif somepath\output", it gives message as "'tesseract' is not recognized as an internal or external command".
when i run the same command from the location where tesseract is installed, it works fine but i need to have it set in the environment variables as it will also allow PYOCR wrapper to recognise it.
PYOCR is currently giving "pyocr.get_available_tools()[0]" as empty list.
any help is much appreciated.
Perhaps my answer is a little bit late, but I will give my setup that seems to work from few minutes. As you, I was using PYOCR library, and at the beginning I get an empty list when using pyocr.get_available_tools().
I'm running it on a Windows 10 laptop.
First, I installed Tesseract in version 4 in alpha from here:
https://github.com/UB-Mannheim/tesseract/wiki and then added location in my windows user PATH variable. From here, run a new command line and check that tesseract tool is detected, if not you're environmment is not properly configured!
Then, I installed PyOCR using a simple pip pyocr and use the follow imports
before using pyocr functions:
import pyocr
import pyocr.builders
EDIT:
I've look in current source files of PYOCR and I've found this:
def is_available():
_set_environment()
return util.is_on_path(TESSERACT_CMD)
PyOCR is just looking for tesseract.exe in your os Path variable, so I think you just have to solve the mention issue and it will work.
EDIT 2:
I updated the download link with UB Mannheim version. Seems more efficient on my data. Also, I downgraded to Tesseract 3, because a lot of flags was not supported in 4.0 alpha build.
Hope it can help a little bit,
Best regards
Please check the correct path of installation of Tesseract-OCR. Setting the correct path, i.e. C:\Program Files (x86)\Tesseract-OCR worked for me.

Opencv failed to parse AVI

I've installed opencv3.1.0 through anaconda along with the ffmpeg package. opencv still gives
Failed to parse avi: index was not found
when I try to read an .avi file.
Is it possible to have .avi files work straight out of anaconda or do I have to compile everything manually (a process I have not have had much luck with so far).

Python convert mp3 to wav with Pydub

Ok, now I am stuck up in converting mp3 to wav. I have seen different answers but i think i would to go for the one of pydub, which i already did using these few lines
from pydub import AudioSegment
AudioSegment.from_mp3("/input/file.mp3").export("/output/file.wav", format="wav")
but when I run the above code, i get the following error
C:\Python27\lib\site-packages\pydub-0.14.2-py2.7.egg\pydub\utils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
Traceback (most recent call last):
File "C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py", line 7, in
stereo_to_mono()
File "C:\Users\phourlhar\Desktop\VoiceDetector\utils.py", line 25, in
stereo_to_mono
sound = AudioSegment.from_mp3(PROJECT_DIR+'\\files\\rec'+str(c)+'.mp3')
File "build\bdist.win32\egg\pydub\audio_segment.py", line 346, in
from_file
File "C:\Python27\lib\subprocess.py", line 711, in init
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I don't know why it raises this error as i am very sure the file exists. Although i have answers suggesting the installation of ffmpeg, but i dont know if affect the app deployment in any way later on
The pydub module uses either ffmpeg or avconf programs to do the actual conversion. So you do have to install ffmpeg to make this work.
But if you don't need pydub for anything else, you can just use the built-in subprocess module to call a convertor program like ffmpeg like this:
import subprocess
subprocess.call(['ffmpeg', '-i', '/input/file.mp3',
'/output/file.wav'])
This requires that the ffmpeg binary is in a location in your $PATH, by the way.
Edit: With ffmeg, you cannot convert stereo to mono, as far as I know. You can only choose the left or right channel. I'm assuming this is not what you want.
The sox program can convert stereo to mono:
import subprocess
subprocess.call(['sox', '/input/file.mp3', '-e', 'mu-law',
'-r', '16k', '/output/file.wav', 'remix', '1,2'])
This will sample at 16 kHz, with 8 bits/sample, giving you 16 kb/s.
You must go for pydub, it is a great module for operations related with audio files.
NOTE. Do remember to install ffmpeg before you use pydub.
For help regarding installation of ffmpeg, you can use this link.
Then to install pydub just open your command prompt and type
pip install pydub
Then to convert any file from mp3 to wav just use pydub as
import pydub
sound = pydub.AudioSegment.from_mp3("D:/example/apple.mp3")
sound.export("D:/example/apple.wav", format="wav")
The problem is due to the missing of ffmpeg. Pydub requires it to perform the operations of format conversion. To solve the problem, there are 2 ways:
Simply install pydub with conda, not pip (despite the suggestion on Pydub's GitHub page)
conda install -c conda-forge pydub
This should work fine. Reason unknown, possibly due to the compatibility stuff.
The other solution (if you already used pip to install Pydub, and it does not work) is to install the missing ffmpeg package. Here comes another problem. Though we can find a package named ffmpeg on both pypi and anaconda, if we only installed one of the source, we will probably see the error like
In [1]: import ffmpeg
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-16f5f3b4de71> in <module>
----> 1 import ffmpeg
ModuleNotFoundError: No module named 'ffmpeg'
After several tests, I found that both have to be installed to make the package ffmpeg work (install twice, in other words). Otherwise, somehow python cannot find the package even if it has been installed via either pip or anaconda. So just type
pip install ffmpeg
conda install ffmpeg
Now try to import ffmpeg in python. If there is no error, the problem should already be solved.
NOTE that, manually downloading ffmpeg from FFmpeg website and append the bin path to sys.path might not help with this problem. Similarly, manually specifying the path to the executable of ffmpeg (on Windows it is ffmpeg.exe) might not solve the problem either.

Categories

Resources