0

I'm trying to use Pydub, and I want to convert a mp3 file to wav, as is already told here.

Even though the file is present I'm getting a error of No such file or directory.

This is my code:

import os.path
print "File Present:", os.path.isfile('/home/nikhil/Documents/Python/IPython Notebooks/test.mp3') 

from pydub import AudioSegment
sound = AudioSegment.from_mp3('/home/nikhil/Documents/Python/IPython Notebooks/test.mp3')
sound.export("file.wav", format="wav")

And this is the complete log cat:

File Present: True

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-29-d23db5440f1d> in <module>()
      9 
     10 from pydub import AudioSegment
---> 11 sound = AudioSegment.from_mp3('/home/nikhil/Documents/Python/IPython Notebooks/test.mp3')
     12 sound.export("file.wav", format="wav")
     13 

/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.pyc in from_mp3(cls, file, parameters)
    700     @classmethod
    701     def from_mp3(cls, file, parameters=None):
--> 702         return cls.from_file(file, 'mp3', parameters)
    703 
    704     @classmethod

/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.pyc in from_file(cls, file, format, codec, parameters, **kwargs)
    656             stdin_data = file.read()
    657 
--> 658         info = mediainfo_json(orig_file)
    659         if info:
    660             audio_streams = [x for x in info['streams']

/usr/local/lib/python2.7/dist-packages/pydub/utils.pyc in mediainfo_json(filepath)
    242 
    243     command = [prober, '-of', 'json'] + command_args
--> 244     res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
    245     output, stderr = res.communicate(input=stdin_data)
    246     output = output.decode("utf-8", 'ignore')

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    709                                 p2cread, p2cwrite,
    710                                 c2pread, c2pwrite,
--> 711                                 errread, errwrite)
    712         except Exception:
    713             # Preserve original exception in case os.close raises.

/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1341                         raise
   1342                 child_exception = pickle.loads(data)
-> 1343                 raise child_exception
   1344 
   1345 

OSError: [Errno 2] No such file or directory

(Please ignore the format of error. I prefer it this way(indented))

What is going wrong here?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44
  • 1
    Try running `which prober` in the terminal you're running the script in, does it show a path or nothing? – kristaps Jun 11 '18 at 12:54
  • No, it doesn't show anything. – Nikhil Wagh Jun 11 '18 at 12:56
  • 1
    Sorry, should have been these two commands: `which avprobe` and `which ffprobe`. Basically it could be that you're missing a dependency, here's what the pydub docs say: You can open and save WAV files with pure python. For opening and saving non-wav files – like mp3 – you'll need ffmpeg or libav. – kristaps Jun 11 '18 at 13:14
  • I installed `ffmpeg` and it worked. Thanks man. – Nikhil Wagh Jun 11 '18 at 13:23

2 Answers2

2

EDIT

I just saw people with the same problem, and it was because they didn't had installed the ffmpeg library. Try:

sudo apt-get install ffmpeg

or

sudo apt-get install libav-tools
Jorge Peris
  • 345
  • 1
  • 4
  • 13
  • I did this : `sound = AudioSegment.from_mp3('/home/nikhil/Documents/test.mp3')` . But still getting the error. – Nikhil Wagh Jun 11 '18 at 13:03
  • try to place the file in the same directory where the code is, the way you should use: sound = AudioSegment.from_mp3('test.mp3'), because in the GitHub repository they do it that way (https://github.com/jiaaro/pydub). It should work anyway in a different folder, but if that line still returns the error, try reinstalling pydub and/or renaming the test.mp3 file. By the way, the print line what does return you? – Jorge Peris Jun 11 '18 at 13:14
  • I tried that too, it's not working. It returns `True`. (It is there in log :P) – Nikhil Wagh Jun 11 '18 at 13:19
  • Oops, I'm quite blind, sorry :P Check my new edit, as I said, people had the same error as you and it was because the FFMPEG library was missing – Jorge Peris Jun 11 '18 at 13:23
  • This other stackoverflow's post says that you have to install libav-tools library instead: https://stackoverflow.com/a/34200212/6386773 – Jorge Peris Jun 11 '18 at 13:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172897/discussion-between-nikhil-wagh-and-jorge-peris). – Nikhil Wagh Jun 11 '18 at 13:38
-1

Your path is used as argument in Popen function as your traceback shows.

Try adding additional quotes:

sound = AudioSegment.from_mp3('"/home/nikhil/Documents/Python/IPython Notebooks/test.mp3"')
DFE
  • 126
  • 9
  • Did this `sound = AudioSegment.from_mp3('"/home/nikhil/Documents/test.mp3"')`, but this time I'm getting a I/O error. – Nikhil Wagh Jun 11 '18 at 13:04
  • Can you try reverse simple and double quotes? `sound = AudioSegment.from_mp3("'/home/nikhil/Documents/Python/IPython Notebooks/test.mp3'")` – DFE Jun 11 '18 at 13:06