简体   繁体   中英

python : how to change audio volume?

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...

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM