简体   繁体   English

Python 将 wav 转换为 mp3

[英]Python convert wav to mp3

I've looked at pymedia (discontinued), pyglet(great but no converter in there) and audiotools(command line cd ripping), and none seem suitable.我看过 pymedia(已停产)、pyglet(很棒但没有转换器)和 audiotools(命令行 cd 翻录),但似乎都不合适。

In Python 2.7 , how do you do在 Python 2.7 中,你是怎么做的

convert(wavFileLocation, 'mp3')

If there is no python way, how would you do it in a manner which python can invoke?如果没有 python 方式,你将如何以 python 可以调用的方式进行? (eg Call a Cross platform command line tool... if exists return (name, pythonCodeForInvocation) ) (例如调用跨平台命令行工具...如果存在返回(名称,pythonCodeForInvocation))

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.我编写了一个python 库 pydub ,它基本上按照 Corey's Answer 的建议执行,尽管它使用 ffmpeg 进行转换以支持更多格式。

from pydub import AudioSegment

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

using lame (command line), you can encode wav to mp3 like this:使用跛脚(命令行),您可以像这样将 wav 编码为 mp3:

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

which would create:这将创建:

file.wav.mp3

in Python, you could use subprocess to call it:在 Python 中,您可以使用subprocess来调用它:

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

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 wav to mp3 just use pydub as然后将任何文件从 wav 转换为 mp3 只需使用pydub作为

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

will you can just rename the file extension , i think os library will help you any way i will give you example :你可以重命名文件扩展名吗,我认为 os 库会以任何方式帮助你,我会给你例子:

yourfile.wav ==>> yourfile.mp3 or any type that is for audio and video file yourfile.wav ==>> yourfile.mp3 或任何类型的音频和视频文件

for me it's worked !!对我来说它奏效了!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM