Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert any audio file to mp3 with python

I want to convert any audio file (flac, wav,...) to mp3 with python I am a noob , I tried pydub but I didn't found out how to make ffmpeg work with it, and If I'm right it can't convert flac file.

The idea of my project is to : Make musicBee send the path of the 'now playing' track (by pressing the assigned shortcut) to my python file which would convert the music if it is not in mp3 and send it to a folder. (Everything in background so I don't have to leave what I'm doing to make the operation)

like image 449
Canapy Avatar asked Jun 29 '16 21:06

Canapy


People also ask

Does Python support MP3?

There is no inbuilt mechanism available to run a media file (such as mp3) in Python, but there are many 3rd party libraries such as vlc or pygame using which we can play an audio file in Python.


1 Answers

You can use the following the code:

from pydub import AudioSegment

wav_audio = AudioSegment.from_file("audio.wav", format="wav")
raw_audio = AudioSegment.from_file("audio.wav", format="raw",
                                   frame_rate=44100, channels=2, sample_width=2)

wav_audio.export("audio1.mp3", format="mp3")
raw_audio.export("audio2.mp3", format="mp3")

You can also look here for more options.

flac_audio = AudioSegment.from_file("sample.flac", "flac")
flac_audio.export("sampleMp3.mp3", format="mp3")
like image 158
Pritam Banerjee Avatar answered Sep 27 '22 02:09

Pritam Banerjee