Python convert wav to mp3

时光毁灭记忆、已成空白 提交于 2019-12-18 18:51:18

问题


I've looked at pymedia (discontinued), pyglet(great but no converter in there) and audiotools(command line cd ripping), and none seem suitable.

In Python 2.7 , how do you do

convert(wavFileLocation, 'mp3')

If there is no python way, how would you do it in a manner which python can invoke? (e.g. Call a Cross platform command line tool... if exists return (name, pythonCodeForInvocation) )


回答1:


using lame (command line), you can encode wav to mp3 like this:

$ lame --preset insane /path/to/file.wav

which would create:

file.wav.mp3

in Python, you could use subprocess to call it:

wav = 'myfile.wav'
cmd = 'lame --preset insane %s' % wav
subprocess.call(cmd, shell=True)



回答2:


I wrote a python library, pydub, that essentially does what Corey's Answer suggests, though it uses ffmpeg in to do the conversions in order to support more formats.

from pydub import AudioSegment

AudioSegment.from_wav("/input/file.wav").export("/output/file.mp3", format="mp3")



回答3:


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 wav to mp3 just use pydub as

import pydub
sound = pydub.AudioSegment.from_wav("D:/example/apple.wav")
sound.export("D:/example/apple.mp3", format="mp3")


来源:https://stackoverflow.com/questions/10287683/python-convert-wav-to-mp3

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