pydub-stubs
pydub-stubs is a stub-only package providing type information for the pydub library. It aims to enhance type checking and IDE support for pydub, covering its public interface and some private functions. The package is actively maintained, with versions generally aligning with the pydub releases.
Warnings
- gotcha pydub requires external (non-Python) dependencies like FFmpeg or libav for processing most audio formats (e.g., MP3, FLAC, OGG). Without them, it can only handle WAV files. These must be installed separately and made available in your system's PATH.
- gotcha Some pydub functions, especially certain effects, are dynamically added to the `AudioSegment` class at runtime. While `pydub-stubs` attempts to type these using overloads, some dynamically added methods might not be perfectly typed or accessible via direct import from submodules like `pydub.effects` if your type checker expects them on `AudioSegment` directly.
- deprecated The underlying `pydub` library utilizes standard library modules like `audioop`, which are slated for deprecation in Python 3.13. This may lead to deprecation warnings when using `pydub` (and implicitly, `pydub-stubs`) with newer Python versions.
- gotcha Version numbers for stub packages like `pydub-stubs` are often structured (e.g., `major.minor.patch.stubs_version`). The `major.minor.patch` parts correspond to the version of the runtime package (`pydub`) they target. Discrepancies between the installed `pydub` version and the `pydub-stubs` version can lead to incorrect type hints or type checking failures.
- gotcha pydub frequently uses temporary files for conversions and intermediate processing. Users may encounter `FileNotFoundError` or `PermissionError` if the Python process lacks write permissions in the default temporary directory or the specified output directory.
Install
-
pip install pydub-stubs
Imports
- AudioSegment
from pydub import AudioSegment
- play
from pydub.playback import play
- effects
import pydub.effects
Quickstart
from pydub import AudioSegment
from pydub.playback import play
import os
# Create a dummy silent WAV file for demonstration
silent_audio = AudioSegment.silent(duration=1000) # 1 second of silence
silent_audio.export("input.wav", format="wav")
# Load an audio file (replace with your actual file if not using dummy)
song = AudioSegment.from_file("input.wav", format="wav")
# Boost volume by 6 dB
louder_song = song + 6
# Export the modified audio
louder_song.export("output.wav", format="wav")
# Play the original and louder song (requires simpleaudio or similar for playback)
# print("Playing original song...")
# play(song)
# print("Playing louder song...")
# play(louder_song)
print("Original and louder audio files created as input.wav and output.wav")
print("Note: For non-WAV formats (like MP3), pydub requires ffmpeg or libav to be installed and in your system PATH.")
# Clean up dummy files
os.remove("input.wav")
os.remove("output.wav")