Skip to content
Related Articles

Related Articles

Working with wav files in Python using Pydub
  • Last Updated : 20 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

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:

  1. Playing audio file.
  2. We can get certain information of file like length channels.
  3. Increase/Decrease volume of given .wav file.
  4. Merging two or more audio files.
  5. Exporting an audio file.
  6. 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.

Python3




# import required libraries
from pydub import AudioSegment 
from pydub.playback import play 
  
# Import an audio file 
# Format parameter only
# for readability 
wav_file = AudioSegment.from_file(file = "Sample.wav"
                                  format = "wav"
  
# Play the audio file
play(wav_file)

Output:

2) Knowing about .wav file: for this we will use attributes of audio file object.

Python3




# import required library
from pydub import AudioSegment 
  
# import the audio file
wav_file = AudioSegment.from_file(file="Sample.wav", format="wav"
  
# data type fo the file
print(type(wav_file)) 
# OUTPUT: <class 'pydub.audio_segment.AudioSegment'>
  
#  To find frame rate of song/file
print(wav_file.frame_rate)   
# OUTPUT: 22050 
  
# To know about channels of file
print(wav_file.channels) 
# OUTPUT: 1
  
# Find the number of bytes per sample 
print(wav_file.sample_width ) 
# OUTPUT : 2
  
  
# Find Maximum amplitude 
print(wav_file.max)
# OUTPUT 17106
  
# To know length of audio file
print(len(wav_file))
# OUTPUT 60000 
  
'''
We can change the attrinbutes of file by 
changeed_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 library
import pydub 
from pydub.playback import play
  
wav_file =  pydub.AudioSegment.from_file(file = "Sample.wav"
                                         format = "wav"
# Increase the volume by 10 dB 
new_wav_file = wav_file + 10 
  
# Reducing volume by 5
silent_wav_file = wav_file - 5
  
#  Playing silent file
play(silent_wav_file)
  
#  Playing original file
play(wav_file)
  
#  Playing louder file
play(new_wav_file)
  
# Feel the difference!

Output:



4) Merging files: This is done using ‘+’ operator.

Python3




# import required libraries
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")
  
# Combine the two audio files 
wav_file_3 = wav_file_1 + wav_file_2
   
# play the sound 
play(wav_file_3)

Output:

5) Exporting files: This is done using export() method.

Python3




# import library
from pydub import AudioSegment 
   
# Import audio file 
wav_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 file 
louder_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 libraries
from pydub import AudioSegment 
from pydub.playback import play
  
# importing audio file
a = AudioSegment.from_file("pzm12.wav"
  
# Split stereo to mono 
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


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :