Working with wav files in Python using Pydub
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.
Installation
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.
- Splitting 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.
Python3
# import required librariesfrom pydub import AudioSegmentfrom pydub.playback import play# Import an audio file# Format parameter only# for readabilitywav_file = AudioSegment.from_file(file = "Sample.wav", format = "wav")# Play the audio fileplay(wav_file) |
Output:
2) Knowing about .wav file: for this we will use attributes of audio file object.
Python3
# import required libraryfrom pydub import AudioSegment# import the audio filewav_file = AudioSegment.from_file(file="Sample.wav", format="wav")# data type for the fileprint(type(wav_file))# OUTPUT: <class 'pydub.audio_segment.AudioSegment'># To find frame rate of song/fileprint(wav_file.frame_rate) # OUTPUT: 22050# To know about channels of fileprint(wav_file.channels)# OUTPUT: 1# Find the number of bytes per sampleprint(wav_file.sample_width )# OUTPUT : 2# Find Maximum amplitudeprint(wav_file.max)# OUTPUT 17106# To know length of audio fileprint(len(wav_file))# OUTPUT 60000'''We can change the attributes of file bychangeed_audio_segment = audio_segment.set_ATTRIBUTENAME(x)'''wav_file_new = wav_file.set_frame_rate(50)print(wav_file_new.frame_rate) |
Output:
<class 'pydub.audio_segment.AudioSegment'> 22050 1 2 17106 60000 50
3) Increasing/Decreasing volume of the file: By using ‘+’ and ‘-‘ operator.
Python3
# import required libraryimport pydubfrom pydub.playback import playwav_file = pydub.AudioSegment.from_file(file = "Sample.wav", format = "wav")# Increase the volume by 10 dBnew_wav_file = wav_file + 10# Reducing volume by 5silent_wav_file = wav_file - 5# Playing silent fileplay(silent_wav_file)# Playing original fileplay(wav_file)# Playing louder fileplay(new_wav_file)# Feel the difference! |
Output:
4) Merging files: This is done using ‘+’ operator.
Python3
# import required librariesfrom pydub import AudioSegmentfrom pydub.playback import playwav_file_1 = AudioSegment.from_file("noice.wav")wav_file_2 = AudioSegment.from_file("Sample.wav")# Combine the two audio fileswav_file_3 = wav_file_1 + wav_file_2 # play the soundplay(wav_file_3) |
Output:
5) Exporting files: This is done using export() method.
Python3
# import libraryfrom pydub import AudioSegment # Import audio filewav_file = AudioSegment.from_file("Sample.wav")''' You can do anything like remixing and export I'm increasing volume just for sake of my simplicity Increase by 10 decibels'''louder_wav_file = wav_file + 10# Export louder audio filelouder_wav_file.export(out_f = "louder_wav_file.wav", format = "wav") |
Output:
6) Splitting Audio: Splitting audio using split_to_mono() method.
Python3
# import required librariesfrom pydub import AudioSegmentfrom pydub.playback import play# importing audio filea = AudioSegment.from_file("pzm12.wav")# Split stereo to monob = a.split_to_mono()print(b)print(b[0].channels )b[0].export(out_f="outNow.wav",format="wav") |
Output:
[<pydub.audio_segment.AudioSegment object at 0x000001358727E860>, <pydub.audio_segment.AudioSegment object at 0x000001358721F978>] 1






Please Login to comment...