python : how to change audio volume?

maral picture maral · Apr 28, 2017 · Viewed 15.4k times · Source

I used winsound.Beep(frequency, duration) because I wanted to play sounds with a specified frequency. Now, I need to change the volume of these sounds. How can I do this? I tried to get help from pyaudio but...

Answer

Anil_M picture Anil_M · May 1, 2017

If you are open to external libraries, you can use pydub to manipulate audio (including volume ) easily. More details here.

Different audio formats such as wav, mp3, ogg , mp4,wma etc are available. Check here for more details.

Basically we convert audio to an audiosegment object and then manipulate it for various attributes using pydub.

pydub can be installed using:
pip install pydub #on 2.x and
pip3 install pydub # on 3.x

Here is an example:

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_mp3("your_song.mp3")

# boost volume by 6dB
louder_song = song + 6

# reduce volume by 3dB
quieter_song = song - 3

#Play song
play(louder_song)

#save louder song 
louder_song.export("louder_song.mp3", format='mp3')