Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert wav to mp3

Tags:

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

like image 357
xxjjnn Avatar asked Apr 23 '12 20:04

xxjjnn


People also ask

How do you change audio format in Python?

As you'll be interacting with many audio files, you decide to begin by creating some helper functions. The first one, convert_to_wav(filename) takes a file path and uses PyDub to convert it from a non-wav format to . wav format. Once it's built, we'll use the function to convert Acme's first call, call_1.

Can Librosa load mp3?

This works for many formats, such as WAV, FLAC, and OGG. However, MP3 is not supported. When PySoundFile fails to read the audio file (e.g., for MP3), a warning is issued, and librosa. load falls back to another library called audioread .


2 Answers

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") 
like image 187
Jiaaro Avatar answered Sep 25 '22 15:09

Jiaaro


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) 
like image 27
Corey Goldberg Avatar answered Sep 23 '22 15:09

Corey Goldberg