Audio files are a widespread means of transferring information. So let’s see how to work with audio files using Python. Python provides a module called pydub to work with audio files. pydub is a Python library to work with only .wav files. By using this library we can play, split, merge, edit our .wav audio files.
This module does not come built-in with Python. To install it type the below command in the terminal.
pip install pydub
Following are some functionalities that can be performed by pydub:
- Playing audio file.
- We can get certain information of file like length channels.
- Increase/Decrease volume of given .wav file.
- Merging two or more audio files.
- Exporting an audio file.
- Spliiting an audio file.
For accessing input Sound files click here. Let’s see the code for some functionalities of pydub library:
1) Playing Audio File: This is done using play() method.
2) Knowing about .wav file: for this we will use attributes of audio file object.
<class 'pydub.audio_segment.AudioSegment'> 22050 1 2 17106 60000 50
3) Increasing/Decreasing volume of the file: By using ‘+’ and ‘-‘ operator.
4) Merging files: This is done using ‘+’ operator.
5) Exporting files: This is done using export() method.
6) Splitting Audio: Splitting audio using split_to_mono() method.
[<pydub.audio_segment.AudioSegment object at 0x000001358727E860>, <pydub.audio_segment.AudioSegment object at 0x000001358721F978>] 1
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.