Pydub Audio Manipulation

0.25.1 · active · verified Sun Apr 05

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

Install

Imports

Quickstart

This quickstart demonstrates how to load an audio file, apply common manipulations like changing volume and adding fade effects, and then export the result to a new file. It includes a fallback to create a dummy WAV file if no input is present, highlighting the dependency on FFmpeg for most real-world formats like MP3.

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

view raw JSON →