Working with wav files in Python using Pydub
Last Updated :
20 Mar, 2024
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.
Let’s see the code for some functionalities of pydub library:
1) Playing Audio File: This is done using play() method.
Python3
from pydub import AudioSegment
from pydub.playback import play
wav_file = AudioSegment.from_file( file = "Sample.wav" ,
format = "wav" )
play(wav_file)
|
Output:
2) Knowing about .wav file: for this we will use attributes of audio file object.
Python3
from pydub import AudioSegment
wav_file = AudioSegment.from_file( file = "Sample.wav" , format = "wav" )
print ( type (wav_file))
print (wav_file.frame_rate)
print (wav_file.channels)
print (wav_file.sample_width )
print (wav_file. max )
print ( len (wav_file))
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 pydub
from pydub.playback import play
wav_file = pydub.AudioSegment.from_file( file = "Sample.wav" ,
format = "wav" )
new_wav_file = wav_file + 10
silent_wav_file = wav_file - 5
play(silent_wav_file)
play(wav_file)
play(new_wav_file)
|
Output:
4) Merging files: This is done using ‘+’ operator.
Python3
from pydub import AudioSegment
from pydub.playback import play
wav_file_1 = AudioSegment.from_file( "noice.wav" )
wav_file_2 = AudioSegment.from_file( "Sample.wav" )
wav_file_3 = wav_file_1 + wav_file_2
play(wav_file_3)
|
Output:
5) Exporting files: This is done using export() method.
Python3
from pydub import AudioSegment
wav_file = AudioSegment.from_file( "Sample.wav" )
louder_wav_file = wav_file + 10
louder_wav_file.export(out_f = "louder_wav_file.wav" ,
format = "wav" )
|
Output:
6) Splitting Audio: Splitting audio using split_to_mono() method.
Python3
from pydub import AudioSegment
from pydub.playback import play
a = AudioSegment.from_file( "pzm12.wav" )
b = 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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...