Python convert mp3 to wav with Pydub - python

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.

Related

I have pip installed pydub but still didn't work

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.

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"

Python failing to install module "spacepy"

I'm currently trying to install the Python package spacepy due to its ability to read CDF files, along with a few other useful functions. However, any time I try to install this module I receive a myriad of errors - whether I try to install it via Anaconda, command prompt, or by downloading the package manually and running setup.py from the package directory. Currently, I've spent hours trying to chase down these errors, but as I'm not a programmer it's been slow going.
I've managed to "install" it, however the module throws an error when trying to load it:
Traceback (most recent call last):
File "<ipython-input-1-4bcf91e29885>", line 1, in <module>
import spacepy
File "C:\Anaconda\lib\site-packages\spacepy\__init__.py", line 329, in <module>
_read_config(rcfile)
File "C:\Anaconda\lib\site-packages\spacepy\__init__.py", line 297, in _read_config
_write_defaults(rcfile, defaults)
File "C:\Anaconda\lib\site-packages\spacepy\__init__.py", line 236, in _write_defaults
key=k, value=defaults[k], ver=__version__))
IOError: [Errno 0] Error
...and so I don't believe it's been installed properly, and one or more of the errors from the initial build is causing issues.
This package has a number of dependencies, most being other Python modules. The only one that the installer would be unable to do itself would be the Fortran compiler (for which I have installed myself using MinGW), however this shouldn't prevent the package from installing.
Here is the complete log of errors that I recieve when trying to force-reinstall it via the command prompt:
python -m pip install --upgrade --force-reinstall spacepy
So it turns out that, among a few smaller errors with the dependencies here and there (that could be fixed just by following the errors thrown), the major issue was the version of numpy. Spacepy was designed for numpy v1.6, and doesn't seem to be backwards compatible with future versions of numpy (like the current v1.12).
Rolling back my version of numpy, as well as moving over to a linux virtual environment (which allowed complete control of modules and dependencies) eventually got spacepy on my system. Now I've just got to become more familiar with linux!

Python PyAudio installation problems (with PortAudio)

I'm trying to write a program to record information from my computers microphone an save it to a file. PyAudio seems like one of the better packages for doing this, and they even have a binary for Windows 7 (Python 2.7). I downloaded the executable file and ran it to set up PyAudio, but when I try to import PyAudio into a python script now I get an error:
Please build and install the PortAudio Python bindings first.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pyaudio
File "C:\Python27\lib\site-packages\pyaudio.py", line 103, in <module>
sys.exit(-1)
SystemExit: -1
If I look at pyaudio.py, the code that it's failing on is:
# attempt to import PortAudio
try:
import _portaudio as pa
except ImportError:
print "Please build and install the PortAudio Python " +\
"bindings first."
sys.exit(-1)
Also, in case it's relevant, if I go to Python27\Lib\site-packages (where pyaudio.py is) there is a file called portaudio_x64.dll.
The documentation on their site only seems to have instructions for if you want to install PyAudio by building from source code. Additionally, it says that PortAudio v19 is included in the binary, so I assumed it would just work after running the setup executable.
I have no idea what's going wrong and I really need this running soon. Any ideas on what's going wrong? Or if anyone has recommendations for similar packages that work better specifically with Windows 7 (64-bit) and Python 2.7 (Enthought distribution), as well as cross-platform, I'd love to hear them.
Copying the answer from the comments in order to remove this question from the "Unanswered" filter:
Try the binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio
~ answer per cgohlke

Capture audio out Python

I would like to capture raw data from the audio out, using Python. In the Python docs it seems that using ossaudiodev works for this purpose. However, I am using Ubuntu 11.10, which has no /dev/dsp:
>>> import ossaudiodev
>>> ossaudiodev.open('r');
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/dev/dsp'
Has anybody got a clue how to capture the sound my sound card outputs using Python?
OSS is an older and deprecated audio system for Linux. The current standard is to use ALSA, and to layer on top of ALSA with either JACK or PulseAudio. Ubuntu uses PulseAudio.
The best thing you could do is to find good Python bindings for connecting to PulseAudio. I just did a quick Google search and didn't find anything for you, and I don't have time to follow up more right now.
EDIT: I just remembered that ALSA does have an OSS compatibility mode. You might be able to install the OSS compatibility stuff, then just use the Python code you already have.
https://help.ubuntu.com/community/alsa-oss
http://www.alsa-project.org/main/index.php/Main_Page
I have got programs to work which require /dev/dsp by using
aoss <PROGRAM NAME AND ARGUMENTS HERE>
If you type aoss on Ubuntu's command line, it will tell you the package to install and that should, in turn, install the needed dependencies. If it doesn't work off the bat, you can try loading a kernel module such as sudo modprobe snd_mixer_oss.

Categories

Resources