如何使用python将.mp3转换为.wav?

2024-04-26 14:08:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用pydub将mp3转换为wav,但出现以下错误:

OSError                                   Traceback (most recent call last)
<ipython-input-43-d2334601378c> in <module>()
      5 dst= '/Users/user/Downloads/audio_files/5.wav'
      6 
----> 7 sound = AudioSegment.from_mp3(src)
      8 sound.export(dst, format="wav")

/anaconda2/lib/python2.7/site-packages/pydub/audio_segment.pyc in from_mp3(cls, file, parameters)
    714     @classmethod
    715     def from_mp3(cls, file, parameters=None):
--> 716         return cls.from_file(file, 'mp3', parameters=parameters)
    717 
    718     @classmethod

/anaconda2/lib/python2.7/site-packages/pydub/audio_segment.pyc in from_file(cls, file, format, codec, parameters, **kwargs)
    663             stdin_data = file.read()
    664 
--> 665         info = mediainfo_json(orig_file)
    666         if info:
    667             audio_streams = [x for x in info['streams']

/anaconda2/lib/python2.7/site-packages/pydub/utils.pyc in mediainfo_json(filepath)
    261 
    262     command = [prober, '-of', 'json'] + command_args
--> 263     res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
    264     output, stderr = res.communicate(input=stdin_data)
    265     output = output.decode("utf-8", 'ignore')

/anaconda2/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)
    392                                 p2cread, p2cwrite,
    393                                 c2pread, c2pwrite,
--> 394                                 errread, errwrite)
    395         except Exception:
    396             # Preserve original exception in case os.close raises.

/anaconda2/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)
   1045                         raise
   1046                 child_exception = pickle.loads(data)
-> 1047                 raise child_exception
   1048 
   1049 

OSError: [Errno 2] No such file or directory

我的代码:

src= '/Users/user/Downloads/audio_files/5.mp3'
dst= '/Users/user/Downloads/audio_files/5.wav'

sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")

Tags: infromlibstdinmp3audiofiledst
1条回答
网友
1楼 · 发布于 2024-04-26 14:08:48

使用python3而不是python2.7(python3命令)
我为它做的工作

转换.py

from pydub import AudioSegment
sound = AudioSegment.from_mp3("test.mp3")
sound.export("test.wav", format="wav")


Pydub安装:

pip3 install pydub

sudo apt-get update  
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:mc3man/trusty-media  
sudo apt-get install ffmpeg  

python3 convert.py

相关问题 更多 >