" OSError:[Errno 2]没有这样的文件或目录"转换mp3

时间:2018-06-11 12:50:03

标签: python pydub

我尝试使用 Pydub ,我想将mp3文件转换为wav,已经告知{{3} }}。

即使文件存在,我也会收到No such file or directory的错误。

这是我的代码:

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")

这是完整的日志猫:

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

请忽略错误的格式。我更喜欢这种方式(缩进)

这里出了什么问题?

2 个答案:

答案 0 :(得分:1)

修改

我刚看到有同样问题的人,这是因为他们没有安装ffmpeg库。尝试:

sudo apt-get install ffmpeg

sudo apt-get install libav-tools

答案 1 :(得分:-1)

您的路径在Popen函数中用作参数,如您的回溯显示。

尝试添加其他引号:

sound = AudioSegment.from_mp3('"/home/nikhil/Documents/Python/IPython Notebooks/test.mp3"')