Python convert mp3 to wav with Pydub

元气小坏坏 提交于 2019-12-03 10:18:49

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")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!