Python使用Pydub将mp3转换为wav [英] Python convert mp3 to wav with Pydub

查看:421
本文介绍了Python使用Pydub将mp3转换为wav的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,现在我陷入了将mp3转换为wav的问题.我看到了不同的答案,但我认为我会选择pydub中的一个,我已经使用这几行代码了

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:找不到ffmpeg或avconv-默认为ffmpeg,但可能不起作用

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

回溯(最近通话最近): 在第7行的文件"C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py" stereo_to_mono()

Traceback (most recent call last): File "C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py", line 7, in stereo_to_mono()

第25行中的文件"C:\ Users \ phourlhar \ Desktop \ VoiceDetector \ utils.py" 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')

文件"build \ bdist.win32 \ egg \ pydub \ audio_segment.py",第346行,在 from_file

File "build\bdist.win32\egg\pydub\audio_segment.py", line 346, in from_file

init 中的文件"C:\ Python27 \ lib \ subprocess.py",第711行 errread,errwrite)

File "C:\Python27\lib\subprocess.py", line 711, in init errread, errwrite)

_execute_child中的文件"C:\ Python27 \ lib \ subprocess.py",第948行 启动信息)

File "C:\Python27\lib\subprocess.py", line 948, in _execute_child startupinfo)

WindowsError:[错误2]系统找不到指定的文件

WindowsError: [Error 2] The system cannot find the file specified

我不知道为什么会引发此错误,因为我非常确定文件存在.虽然我有建议安装ffmpeg的答案,但我不知道以后是否会以任何方式影响应用程序部署

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

推荐答案

pydub模块使用ffmpegavconf程序进行实际转换.因此,您必须安装ffmpeg才能使它工作.

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.

但是,如果您不需要pydub进行其他操作,则可以使用内置的subprocess模块来调用像ffmpeg这样的转换器程序,如下所示:

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'])

顺便说一下,这要求ffmpeg二进制文件位于$ PATH中的某个位置.

This requires that the ffmpeg binary is in a location in your $PATH, by the way.

编辑:据我所知,使用ffmeg不能将立体声转换为单声道.您只能选择左声道或右声道.我假设这不是您想要的.

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.

sox 程序 可以将立体声转换为单声道:

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'])

这将以16 kHz的采样率进行采样,采样率为8位,从而为您提供16 kb/s的采样率.

This will sample at 16 kHz, with 8 bits/sample, giving you 16 kb/s.

这篇关于Python使用Pydub将mp3转换为wav的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆