Python Programming Language can be used to create and play music in many ways. Python Experts gives us ready-made different Python Packages and Python Modules that help us to easily create different music and help us play different sounds.
Python Programming can give us the ability to create, play, and record voices in this article we will look at one of these python Packages that help us create sounds.
But if you want to create and play Harmonica or maybe any type of music you have to install these python modules and sometimes it is quite challenging to install one of these modules.
Create Sound with Python Package Pydub:-
Pydub is a Python Package that helps us easily create and modify sounds of different types. Pydub is a very easy-to-use Python Package that helps us Manipulate audio in a very simplistic and easy way which is a high-level interface for sound modification and manipulation.
In the next few seconds, We will show you the whole process of Sound modification and manipulation.
About Python Pydub Package
This Python Package comes under the category of audio Python packages. This audio package is developed by an experienced Python Developer “jiarro“. The Python Pydub package is MIT license Package.
This recent release of the package is released on Mar 10, 2021. To learn about the statistics of the Pydub Python Package You can click here. In the upcoming sections, I will show you how to install and use the Pydub Python Package.
How to Install Pydub Python Package
You can install it by using PiP Which stands for Python Package Manager.but things will not be that easy if you are on windows and they need some dependencies to be installed first.
I will show you how you can properly install the python audio package pydub in a very simple and easy way.
pip install pydub
Step No 1: Install Microsoft C++ Build Tools:-
You can Download the C++ Build tools By clicking this link. Without the Build Tools, not only Pydub but other audio Packages will also not working. According to them “The Microsoft C++ Build Tools provides MSVC toolsets via a scriptable, standalone installer without Visual Studio.
Visual Studio is Recommended if you build C++ libraries and applications targeting Windows from the command line.”
Step No 2: Install FFMPEG Codex:-
Once you have installed the Microsoft C++ Builds tools Install, Now it is your turn to install FFMPEG Codex in order to Work with MP3 files especially. Click on this link to download the files.
Follow This Tutorial Strictly in order to successfully install the FFMPEG Codex. Once you add the Path you might run into a problem that the System will identify the FFMPEG Path and will output that it is not recognized. You will have to Restart your Computer then it will work.
Step No 3: Install the simpleaudio and pyaudio Python Package:-
The simplaudio package provides cross-platform, dependency-free audio playback capability for Python 3 on macOS, Windows, and Linux. Now according to the installation documentation of the simpleaudio they just tell us to use pip and install it but it will not work until you use the following two commands separately on the terminal.
Make sure you open the terminal as an administrator user.
pip install simpleaudio pip install pipwin pipwin install pyaudio
Play an Audio File with Python
After Installation, the first thing is to make sure we check that everything is working fine. And the best way we can do it is to use the python package pydub to play a .wav file with this package.
The following code will help us play a .wav file. Note that the file that we are playing is in the current directory.
Change Video to Audio with Python In a Very Easy Way
Suppose you have a directory where you have some video songs and you want them to convert it to an audio file in the mp3 format. You can do it by using our favorite Python Package for changing video to an audio file within a few seconds.
The following code will change every video mp4 video file that is present in the directory specified. below is the code for changing the mp4 video file to an mp3 audio file.
import os import glob from pydub import AudioSegment video_dir = 'D:/Movies/Urdu Songs/New folder' # Path where the videos are located extension_list = ('*.mp4', '*.flv') os.chdir(video_dir) for extension in extension_list: for video in glob.glob(extension): mp3_filename = os.path.splitext(os.path.basename(video)) + '.mp3' AudioSegment.from_file(video).export(mp3_filename, format='mp3')
Summary and Conclusion:-
You can do a lot more with the pydub python package this was just a few examples if you want to see the code and support the maintainer of the project please visit his Github account by clicking here. If you are interested in other python tutorials please visit my youtube channel Code with Ali.