Python 使用 Pydub 将 mp3 转换为 wav
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32073278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python convert mp3 to wav with Pydub
提问by phourxx
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
好的,现在我被困在将 mp3 转换为 wav。我看到了不同的答案,但我想我会选择 pydub 之一,我已经使用这几行
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: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
Traceback (most recent call last): File "C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py", line 7, in 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')
File "build\bdist.win32\egg\pydub\audio_segment.py", line 346, in from_file
File "C:\Python27\lib\subprocess.py", line 711, in initerrread, errwrite)
File "C:\Python27\lib\subprocess.py", line 948, in _execute_child startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
C:\Python27\lib\site-packages\pydub-0.14.2-py2.7.egg\pydub\utils.py:165: RuntimeWarning: 找不到 ffmpeg 或 avconv - 默认为 ffmpeg,但可能不起作用
回溯(最近一次通话):文件“C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py”,第7行,在stereo_to_mono()
文件“C:\Users\phourlhar\Desktop\VoiceDetector\utils.py”,第25行,在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
文件“C:\Python27\lib\subprocess.py”,第 711 行,在initerrread, errwrite 中)
文件“C:\Python27\lib\subprocess.py”,第 948 行,在 _execute_child startupinfo 中)
WindowsError: [错误 2] 系统找不到指定的文件
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
我不知道为什么会引发此错误,因为我非常确定该文件存在。虽然我有建议安装ffmpeg的答案,但我不知道以后是否会以任何方式影响应用程序部署
采纳答案by Roland Smith
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
模块使用ffmpeg
或avconf
程序进行实际转换。所以你必须安装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:
但是如果你不需要pydub
其他任何东西,你可以使用内置subprocess
模块来调用这样的转换器程序ffmpeg
:
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.
顺便说一下,这要求 ffmpeg 二进制文件位于 $PATH 中的某个位置。
Edit: With ffmeg
, you cannot convert stereo to mono, as far as I know. You can only choosethe left or right channel. I'm assuming this is not what you want.
编辑:ffmeg
据我所知,您无法将立体声转换为单声道。您只能选择左声道或右声道。我假设这不是你想要的。
The sox
program canconvert stereo to mono:
该sox
程序可以将立体声转换为单声道:
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.
这将以 16 kHz 采样,8 位/样本,为您提供 16 kb/s。
回答by Abhishek Rathore
You must go for pydub, it is a great module for operations related with audio files.
你必须去pydub,它是一个很好的与音频文件相关的操作模块。
NOTE. Do remember to install ffmpeg before you use pydub.
笔记。在使用 pydub 之前,请记住安装 ffmpeg。
For help regarding installation of ffmpeg, you can use this link.
有关安装ffmpeg 的帮助,您可以使用此链接。
Then to install pydub just open your command prompt and type
然后安装pydub只需打开你的命令提示符并输入
pip install pydub
Then to convert any file from mp3 to wav just use pydubas
然后将任何文件从 mp3 转换为 wav 只需使用pydub作为
import pydub
sound = pydub.AudioSegment.from_mp3("D:/example/apple.mp3")
sound.export("D:/example/apple.wav", format="wav")