Pydub is open-source package for audio manipulation in Python. Pydub uses ffmpeg or libav under the hood for audio manipulation. We will use Pydub to read the audio file and apply different audio effects on it. We can perform different operation using Pydub including audio slicing, noise removal, audio mixing, audio effects, etc. So, let’s see how we can perform these operations using Pydub. First, install Pydub using pip install pydub. Then, import the required libraries.

pip install pydub
Now import package and read audio file using pydub. We can ready any audio file or even video files supported by ffmpeg
from pydub import AudioSegment
audio = AudioSegment.from_file("test.mp3", format="mp3")
# or
audio = AudioSegment.from_mp3("test.mp3")


# We can also read different formats including wav, mp3, mp4, etc all which are supported by ffmpeg
audio = AudioSegment.from_file("test.wav", format="wav") # wav
audio = AudioSegment.from_file("test.mp4", format="mp4") # mp4
We can get different properties of the audio file like the sample width, frame rate, number of channels, etc.
Frame width is the number of bytes per sample. Frame rate is the number of frames per second. Number of channels is the number of audio channels in the file. For example, a stereo file will have two channels, one for each speaker.
print({
    'duration' : audio.duration_seconds,
    'sample_rate' : audio.frame_rate,
    'channels' : audio.channels,
    'sample_width' : audio.sample_width,
    'frame_count' : audio.frame_count(),
    'frame_rate' : audio.frame_rate,
    'frame_width' : audio.frame_width,
})
{'duration': 56.82, 'sample_rate': 44100, 'channels': 2, 'sample_width': 2, 'frame_count': 2505762.0, 'frame_rate': 44100, 'frame_width': 4}

Slice/Crop audio

We can slice the audio file into smaller chunks just like a list. To slice a segment of audio, we use milliseconds as the unit of time and the start and end time of the segment is passed as the parameters to the slice function. Then we can also merge multiple segments/chunks in a single chunk by adding multiple segments.
first = audio[:3000] # first 3000ms
last = audio[-6000:] # last 6000ms
middle = audio[3000:5000] # 3000ms to 5000ms

# Concatenating audio files
final = first + middle + last

Playback Speed and Volume

We can speed up or slow down the audio file by using the speedup() function. The speedup() function takes the speed factor as the parameter. A speed factor of 2.0 will double the speed of the audio file and a speed factor of 0.5 will half the speed of the audio file.
audio.speedup(playback_speed=2.0) # speed up by 2x
# or slow down
audio.speedup(playback_speed=0.5) # slow down by 2x

We can also make audio files louder or quieter

first = first + 7 # increase volume by 7dB
last = last - 4 # reduce volume by 4dB

# concatenating audio files
final = first + middle + last

Save to File

We can also export the audio file to different formats

# export to mp3
final.export("final.mp3", format="mp3")

# export to wav
final.export("final.wav", format="wav")



# Export with metadata
final.export("fina2l.mp3", format="mp3", tags={
    'artist' : 'Me',
    'album' : 'My Album',
    'title' : 'My Title',
    'track' : '1/1',
    'genre' : 'My Genre',
    'date' : '2022',
    'comment' : 'My Comment',
})

For more info, visit pydub official documentation.

https://github.com/jiaaro/pydub

https://pypi.org/project/pydub/