1 min read

Python Pydub Module

Pydub lets you do stuff to audio in a way that isn't stupid. It provides a simple and high-level interface for audio manipulation.

Installation

Pydub requires ffmpeg or libav to be installed on your system.

pip install pydub

Loading Audio

from pydub import AudioSegment

song = AudioSegment.from_mp3("song.mp3")
# song = AudioSegment.from_wav("song.wav")
# song = AudioSegment.from_file("song.m4a", format="m4a")

Slicing and Concatenation

AudioSegments are immutable and support slicing (in milliseconds) and addition.

# First 10 seconds
first_10_seconds = song[:10000]

# Last 5 seconds
last_5_seconds = song[-5000:]

# Concatenate
beginning = first_10_seconds + last_5_seconds

Effects

# Boost volume by 6dB
louder_song = song + 6

# Reduce volume by 3dB
quieter_song = song - 3

# Fade in/out
awesome = song.fade_in(2000).fade_out(3000)

# Reverse
backwards = song.reverse()

Exporting

awesome.export("mashup.mp3", format="mp3")

programming/python/python