pydub Documentation

repository·master·Indexed 27 days ago

https://github.com/jiaaro/pydub

A Python library for audio manipulation providing a high-level interface for slicing, concatenating, applying effects, and converting audio formats. It utilizes AudioSegment for immutable audio operations and requires ffmpeg or libav for non-WAV format support.

Tokens
753
Snippets
3
Records
7
Agent score
45%

What's inside pydub

  1. Install ffmpeg or libav dependencies

    master

    Pydub requires ffmpeg or libav to handle most audio formats. Use the following commands based on your operating system:

    macOS (Homebrew):

    brew install ffmpeg
    # OR
    brew install libav

    Linux (aptitude):

    apt-get install ffmpeg libavcodec-extra
    # OR
    apt-get install libav-tools libavcodec-extra

    Windows:

    1. Download and extract libav from Windows binaries.
    2. Add the libav /bin folder to your PATH environment variable.
  2. Debug ffmpeg subprocess calls

    master

    If you encounter issues with format conversion, you can enable a logger to see the exact ffmpeg or libav subprocess commands being executed by Pydub.

    import logging
    from pydub import AudioSegment
    
    l = logging.getLogger("pydub.converter")
    l.setLevel(logging.DEBUG)
    l.addHandler(logging.StreamHandler())
    
    # This will now print the subprocess call to the console
    AudioSegment.from_file("./test.mp3")
  3. Play audio

    master

    To play audio directly from your script, use pydub.playback.play. It is highly recommended to have simpleaudio installed for playback functionality.

    from pydub import AudioSegment
    from pydub.playback import play
    
    sound = AudioSegment.from_file("mysound.wav", format="wav")
    play(sound)
  4. Export AudioSegments

    master

    Use the .export() method to save audio files. You can specify the format, bitrate, metadata tags, and pass raw ffmpeg parameters.

    • format: The output format (e.g., 'mp3', 'ogg').
    • bitrate: A string like '192k'.
    • tags: A dictionary of metadata (e.g., {'artist': 'Name'}).
    • parameters: A list of ffmpeg switches and arguments (e.g., ['-q:a', '0']).
  5. Load audio files with AudioSegment

    master

    Use AudioSegment to load various audio formats. Pydub supports any format supported by your installed ffmpeg or libav installation.

    Specific convenience methods include from_wav, from_mp3, and from_ogg. For other formats, use from_file(path, format).

  6. Manipulate AudioSegments

    master

    AudioSegments are immutable. Operations return new AudioSegment instances.

    • Slicing: Use millisecond offsets. song[:1000] gets the first second.
    • Volume: Use + and - operators to adjust decibels (e.g., song + 6 boosts by 6dB).
    • Concatenation: Use the + operator to append one segment to another.
    • Crossfade: Use .append(other_segment, crossfade=ms) to join segments with a crossfade.
    • Repeat: Use the * operator (e.g., segment * 2 repeats it twice).
    • Fading: Use .fade_in(ms) and .fade_out(ms).
    • Reversing: Use .reverse().