In this article we are going to see how to convert the audio from one format to another by using Python. We are going to use Pydub library for the conversion
Requirements / Dependencies :
The dependencies can be installed by running the following commands on your terminal.
To install ffmpeg :
apt install ffmpeg
To install Pydub :
pip install pydub
FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation. It is also highly portable: FFmpeg compiles, runs, and passes our testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc. under a wide variety of build environments, machine architectures, and configurations.
converting the audio / video using FFmpeg
ffmpeg -i input_audio.wav output_audio.mp3
Even a mp4 video can be converted to any of the audio formats
ffmpeg -i input_video.mp4 -b:a 192K -vn output_audio.mp3
-b:a : audio bitrate
Optional and it is used to mention a particular audio bitrate. The output audio will be encoded with the given audio bitrate.
-vn : disable video
For now we need only the audio so the -vn option is used to remove the video from the output file.
Conversion of video / audio is very simple using FFmpeg, But our requirement is to do this programmatically using python.
Now we are ready to move forward and start coding!.
from pydub import AudioSegment given_audio = AudioSegment.from_file("path/to/input_audio.mp4", format="mp4") # or given_audio = AudioSegment.from_file("path/to/input_audio.mp3", format="mp3") # or given_audio = AudioSegment.from_file("path/to/input_audio.wav", format="wav") # or raw_audio = AudioSegment.from_file("path/to/input_audio.wav", format="raw", frame_rate=44100, channels=2, sample_width=2) # The above line of code is simply building the ffmpeg command from # the parameters and executing it in the background as a process. given_audio.export("path/to/output_audio.mp3", format="mp3") # or given_audio.export("path/to/output_audio.wav", format="wav") # or given_audio.export("output_audio.wav", format="wav") # file will be saved in the current working directory
Converted the Audio format!. You can find the converted audio file in the path which is passed to the export method.
Same things can be done by passing the ffmpeg command to python sub-process. But it may be difficult for the beginners. pydub will handle the ffmpeg command generation and sub-process parts for you.
Do share your valuable feedback and suggestions!
Thank you for reading, I would love to connect with you at LinkedIn.
Did you find this article valuable?
Support Balasundar by becoming a sponsor. Any amount is appreciated!