Pydub Audio Manipulation
Pydub is a high-level Python library designed for simple and easy audio manipulation. It provides an intuitive interface for tasks like playing, slicing, concatenating, and editing audio files, supporting formats such as WAV, MP3, and FLAC. The current stable version is 0.25.1, released on March 9, 2021, and the library is actively maintained for modern Python versions (>=3.6).
Warnings
- breaking Pydub relies heavily on the external command-line tool FFmpeg (or Libav) for handling most audio formats (e.g., MP3, FLAC, OGG, AAC, MP4). Without FFmpeg/Libav installed on your system and its executable directory added to your system's PATH environment variable, Pydub will only be able to process WAV and raw audio files.
- gotcha AudioSegment objects in Pydub are immutable. Operations like slicing, changing volume, or applying effects (`+`, `-`, `fade_in()`, `fade_out()`) return a *new* AudioSegment object. The original object remains unchanged.
- gotcha Direct audio playback from Pydub requires additional Python dependencies (like `simpleaudio` or `pyaudio`) or the `ffplay`/`avplay` executable (usually bundled with `ffmpeg`/`libav`) to be installed and accessible.
- gotcha When issues arise, especially with converting between formats, the problem is often related to how Pydub interacts with FFmpeg/Libav via subprocess calls.
- gotcha On Windows, setting the system PATH for FFmpeg can sometimes be tricky or not fully recognized by Python environments.
Install
-
pip install pydub
Imports
- AudioSegment
from pydub import AudioSegment
Quickstart
from pydub import AudioSegment
# Create a dummy audio file for the example (replace with your actual file)
# This part requires ffmpeg if you want to export to mp3/other formats.
# For pure WAV, pydub can handle it natively.
try:
# Attempt to create a simple silent WAV file if no external file exists
one_second_of_silence = AudioSegment.silent(duration=1000)
one_second_of_silence.export("input.wav", format="wav")
print("Created a dummy 'input.wav' file.")
except Exception as e:
print(f"Could not create dummy WAV: {e}. Please provide an existing audio file or ensure ffmpeg is installed for non-WAV formats.")
# --- Pydub operations ---
# Load an audio file (e.g., input.wav or input.mp3)
# If you use .mp3, make sure ffmpeg is installed and in your PATH.
try:
song = AudioSegment.from_file("input.wav", format="wav")
print(f"Loaded audio segment. Duration: {len(song) / 1000.0} seconds.")
# Increase volume by 6 dB
louder_song = song + 6
# Add a 2-second fade in and 3-second fade out
faded_song = louder_song.fade_in(2000).fade_out(3000)
# Export the manipulated audio to a new file
output_filename = "output.mp3"
faded_song.export(output_filename, format="mp3")
print(f"Exported '{output_filename}' successfully.")
# To play the audio (requires simpleaudio or pyaudio, and ffplay/avplay if not using WAV)
# from pydub.playback import play
# play(faded_song)
except FileNotFoundError:
print("Error: input.wav not found. Please create one or provide an existing audio file.")
except Exception as e:
print(f"An error occurred during audio processing: {e}. Check FFmpeg installation if using non-WAV files.")