Skip to main content

PyDub Examples

PyDub: How To Examples in Python

PyDub is a simple and easy-to-use Python library for audio processing. It provides a high-level API for manipulating audio files and performing various operations such as slicing, concatenating, fading, and more. In this tutorial, we will cover several examples to demonstrate the capabilities of PyDub.

Prerequisites

Before we begin, make sure you have PyDub installed. You can install it using pip:

pip install pydub

Also, ensure that you have audio files (in supported formats) available for the examples.

Example 1: Loading and Playing an Audio File

Let's start by loading an audio file and playing it using PyDub. This example demonstrates how to load an audio file, play it, and retrieve its duration.

from pydub import AudioSegment
from pydub.playback import play

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Play the audio
play(audio)

# Get the duration of the audio in milliseconds
duration = len(audio)
print("Duration:", duration, "ms")

Expected Output:

  • The audio file specified by the path will be played.
  • The duration of the audio file will be printed in milliseconds.

Example 2: Extracting a Segment of Audio

In this example, we will extract a segment of audio from a given audio file. We will specify the start and end time of the segment and save it as a new audio file.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Define the start and end time in milliseconds
start_time = 10000
end_time = 20000

# Extract the segment of audio
segment = audio[start_time:end_time]

# Save the segment as a new audio file
segment.export("path/to/segment.wav", format="wav")

Expected Output:

  • A segment of audio starting from the specified start time and ending at the specified end time will be extracted.
  • The segment will be saved as a new audio file.

Example 3: Concatenating Multiple Audio Files

Let's say we have two audio files, and we want to concatenate them into a single audio file. This example demonstrates how to achieve that using PyDub.

from pydub import AudioSegment

# Load the audio files
audio1 = AudioSegment.from_file("path/to/audio1.wav", format="wav")
audio2 = AudioSegment.from_file("path/to/audio2.wav", format="wav")

# Concatenate the audio files
concatenated = audio1 + audio2

# Save the concatenated audio as a new file
concatenated.export("path/to/concatenated.wav", format="wav")

Expected Output:

  • The two audio files will be concatenated into a single audio file.
  • The concatenated audio will be saved as a new audio file.

Example 4: Adjusting the Volume of an Audio File

Sometimes we may need to adjust the volume of an audio file. This example demonstrates how to increase or decrease the volume of an audio file using PyDub.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Increase the volume by 10 dB
louder = audio + 10

# Decrease the volume by 5 dB
softer = audio - 5

# Save the modified audio files
louder.export("path/to/louder.wav", format="wav")
softer.export("path/to/softer.wav", format="wav")

Expected Output:

  • Two modified audio files will be saved: one with increased volume and one with decreased volume.

Example 5: Applying Fade In and Fade Out Effects

Fading in and out is a common audio effect that gradually increases or decreases the volume at the beginning or end of an audio file. This example demonstrates how to apply fade in and fade out effects to an audio file using PyDub.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Apply fade in effect (duration: 2 seconds)
fade_in = audio.fade_in(2000)

# Apply fade out effect (duration: 3 seconds)
fade_out = audio.fade_out(3000)

# Save the modified audio files
fade_in.export("path/to/fade_in.wav", format="wav")
fade_out.export("path/to/fade_out.wav", format="wav")

Expected Output:

  • Two modified audio files will be saved: one with a fade in effect and one with a fade out effect.

Example 6: Changing the Speed of an Audio File

In some cases, we may need to change the speed or tempo of an audio file. This example demonstrates how to increase or decrease the speed of an audio file using PyDub.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Increase the speed by 20%
faster = audio.speedup(playback_speed=1.2)

# Decrease the speed by 10%
slower = audio.speedup(playback_speed=0.9)

# Save the modified audio files
faster.export("path/to/faster.wav", format="wav")
slower.export("path/to/slower.wav", format="wav")

Expected Output:

  • Two modified audio files will be saved: one with increased speed and one with decreased speed.

Example 7: Applying an Audio Effect

PyDub provides various audio effects that can be applied to an audio file. In this example, we will apply the "low_pass_filter" effect to an audio file.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Apply the low-pass filter effect (cutoff frequency: 3000 Hz)
filtered = audio.low_pass_filter(3000)

# Save the modified audio file
filtered.export("path/to/filtered.wav", format="wav")

Expected Output:

  • The audio file will be modified by applying a low-pass filter effect with a cutoff frequency of 3000 Hz.
  • The modified audio file will be saved.

Example 8: Exporting Audio in Different Formats

PyDub supports exporting audio files in various formats. This example demonstrates how to export an audio file in different formats using PyDub.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Export the audio file in MP3 format
audio.export("path/to/audio.mp3", format="mp3")

# Export the audio file in FLAC format
audio.export("path/to/audio.flac", format="flac")

# Export the audio file in OGG format
audio.export("path/to/audio.ogg", format="ogg")

Expected Output:

  • The audio file will be exported in three different formats: MP3, FLAC, and OGG.

Example 9: Converting Stereo Audio to Mono

In some cases, we may need to convert stereo audio to mono. This example demonstrates how to convert a stereo audio file to mono using PyDub.

from pydub import AudioSegment

# Load the stereo audio file
audio = AudioSegment.from_file("path/to/stereo_audio.wav", format="wav")

# Convert stereo to mono
mono = audio.set_channels(1)

# Save the mono audio file
mono.export("path/to/mono_audio.wav", format="wav")

Expected Output:

  • The stereo audio file will be converted to mono.
  • The mono audio file will be saved.

Example 10: Splitting an Audio File into Multiple Segments

Sometimes we may need to split an audio file into multiple segments based on specific intervals. This example demonstrates how to split an audio file into multiple segments using PyDub.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_file("path/to/audio.wav", format="wav")

# Define the splitting intervals (in seconds)
intervals = [(0, 5), (5, 10), (10, 15)]

# Split the audio file into segments
segments = [audio[start * 1000:end * 1000] for start, end in intervals]

# Save the segments as individual audio files
for i, segment in enumerate(segments):
segment.export(f"path/to/segment{i+1}.wav", format="wav")

Expected Output:

  • The audio file will be split into three segments based on the specified intervals.
  • Each segment will be saved as an individual audio file.

Conclusion

In this tutorial, we covered several examples to demonstrate the capabilities of PyDub. You learned how to load and play audio files, extract segments, concatenate audio files, adjust volume, apply fade effects, change speed, apply audio effects, export in different formats, convert stereo to mono, and split an audio file into multiple segments. PyDub provides a wide range of functionalities for audio processing, making it a powerful tool for various applications.