24

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

phourxx
  • 591
  • 1
  • 4
  • 15

3 Answers3

20

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.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • thanks, it really helped. but now i need to convert mp3 file directly to mono wav with subprocess and ffmpeg, would really appreciate if u could give a hand – phourxx Aug 20 '15 at 10:20
  • 2
    @user3760740 As far as I know, you cannot convert stereo input to mono output with `ffmpeg`. You can do this with `sox`, see updated answer. – Roland Smith Aug 20 '15 at 11:19
  • @RolandSmtih thanks, would check out on the 'sox'. but is there a way i could force the output to be 16kb/s mono wav with sox – phourxx Aug 20 '15 at 11:29
  • 2
    @user3760740 There are different combinations of encodings (sox `-e` option) and sample rate (sox `-r` option) that can accomplish that. Please see the [manual](http://sox.sourceforge.net/sox.html). Which one is appropriate for your application is more than I can say. You will probably have to try which works best for you. – Roland Smith Aug 20 '15 at 11:51
  • 2
    @user3760740 I've updated my answer to give one obvious option for 16 kb/s. – Roland Smith Aug 20 '15 at 11:57
  • @RolandSmtih thanks, when i tried the above code, i get two sox errors **sox FAIL util: Unable to load MAD decoder library (libmad). sox FAIL formats: can't open input file `C:\Users\phourlhar\Desktop\VoiceDetector\files\test2.mp3':** – phourxx Aug 20 '15 at 13:52
  • @user3760740 It seems the ms-windows version of `sox` doesn't come with mp3 support. Maybe the answers to [this question](http://stackoverflow.com/questions/3537155/sox-fail-util-unable-to-load-mad-decoder-library-libmad-function-mad-stream) can help you further. I don't use ms-windows, so am unable to offer assistance in that regard. – Roland Smith Aug 20 '15 at 14:43
  • Was getting errors using pydub to do conversion (which is using ffmpeg I believe). Used the subprocess call directly to ffmpeg and it fixed everything. Thanks! – user2348114 Jan 14 '18 at 23:06
  • but it returns no such file ofrdirectory ffmpeg – GILO Aug 04 '19 at 10:29
13

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")
Abhishek Rathore
  • 1,016
  • 1
  • 11
  • 13
1

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:

  1. 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.

  1. 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.

Shuxuan XU
  • 31
  • 4
  • Downloading the FFmpeg manually and set it to the environment variable path is working in Windows 10. Cmd or PowerShell session needs to be renewed after adding a new path. If it's not working try to add pydub path in environment variable path. You can check it [here](https://stackoverflow.com/questions/53480893/pydub-installation-and-ffmpeg?noredirect=1&lq=1). – Willy satrio nugroho Jul 30 '20 at 04:00
  • Adding it to the path in Windows didn't work for me, but this did! Thanks! – fakedane May 20 '21 at 22:31