Introduction

Audio processing is essential across a wide range of applications — from podcast editing tools and voice assistants to music analysis platforms and scientific signal processing. Python’s ecosystem offers several excellent libraries, each optimized for different use cases. The three most popular are pydub (9,777 GitHub stars), librosa (8,533 stars), and SoundFile (848 stars).

While all three read and write audio files, they serve fundamentally different purposes. pydub excels at simple manipulation, librosa dominates music and signal analysis, and SoundFile provides low-level, high-performance I/O. Choosing the wrong one can make simple tasks unnecessarily complex — or complex tasks impossible.

Comparison Table

FeaturepydublibrosaSoundFile
GitHub Stars9,7778,533848
Last UpdatedMarch 2026July 2026July 2026
Primary Use CaseAudio editing and conversionMusic and signal analysisHigh-performance I/O
Backendffmpeg/avconvsoundfile/audioreadlibsndfile (C library)
Format SupportEverything ffmpeg supportsWAV, FLAC, OGG, MP3 via audioreadWAV, FLAC, OGG, AIFF, raw PCM
Sample Rate ConversionVia ffmpegBuilt-in resample()No (use samplerate or scipy)
Audio EffectsFade, reverse, concatenate, overlayTime-stretching, pitch-shifting, HPSS separationNone (raw I/O only)
NumPy IntegrationVia .get_array_of_samples()Native (returns np.ndarray)Native via soundfile.read()
StreamingNoYes (block processing)Yes (block processing)
Installationpip install pydubpip install librosapip install soundfile
External DependenciesRequires ffmpegRequires soundfile + audioreadRequires libsndfile system library

pydub: Audio Editing Made Simple

pydub wraps ffmpeg with an intuitive Python API for audio editing tasks. If you’ve ever used ffmpeg on the command line for cutting, concatenating, or converting audio files, pydub gives you the same power with Python-level convenience.

Installation

1
2
3
4
5
6
7
8
9
# Install pydub
pip install pydub

# Install ffmpeg (required backend)
# Ubuntu/Debian:
sudo apt install ffmpeg
# macOS:
brew install ffmpeg
# Windows: download from https://ffmpeg.org/download.html

Reading and Converting Audio

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from pydub import AudioSegment

# Load audio file
audio = AudioSegment.from_file("input.mp3")

# Convert to WAV
audio.export("output.wav", format="wav")

# Convert MP3 with specific bitrate
audio.export("output.mp3", format="mp3", bitrate="192k")

# Slice audio (milliseconds)
first_10_seconds = audio[:10000]
last_30_seconds = audio[-30000:]

Audio Effects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Volume adjustment
louder = audio + 6  # Increase by 6 dB
quieter = audio - 3  # Decrease by 3 dB

# Fade in and out
faded = audio.fade_in(2000).fade_out(3000)  # ms

# Reverse
reversed_audio = audio.reverse()

# Overlay two tracks
combined = audio.overlay(background_music, position=5000)

# Concatenate multiple segments
playlist = intro + main_content + outro
playlist.export("podcast_episode.mp3", format="mp3")

Silence Detection and Removal

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from pydub.silence import split_on_silence

chunks = split_on_silence(
    audio,
    min_silence_len=500,    # 500ms minimum silence
    silence_thresh=-40,     # -40 dBFS threshold
    keep_silence=200        # Keep 200ms of silence at edges
)

# Recombine without long silences
trimmed = sum(chunks)

librosa: Music and Signal Analysis

librosa is the standard library for music information retrieval and audio analysis in Python. It focuses on feature extraction, visualization, and transformation — not simple editing. If you need to analyze tempo, extract chroma features, or decompose audio into harmonic and percussive components, librosa is the tool for the job.

Installation

1
2
3
pip install librosa
# For display support:
pip install matplotlib

Loading and Visualizing Audio

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np

# Load audio with librosa
y, sr = librosa.load("song.wav", sr=None)  # sr=None preserves original sample rate

print(f"Duration: {librosa.get_duration(y=y, sr=sr):.2f}s")
print(f"Sample rate: {sr} Hz")

# Display waveform
plt.figure(figsize=(10, 3))
librosa.display.waveshow(y, sr=sr)
plt.title("Waveform")
plt.show()

Tempo and Beat Detection

1
2
3
4
5
6
7
# Estimate tempo (BPM)
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
print(f"Estimated tempo: {tempo[0]:.1f} BPM")

# Convert beat frames to timestamps
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
print(f"Beats at: {beat_times[:5]}...")

Feature Extraction

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Mel-frequency spectrogram
mel_spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)

# Chroma features (pitch class profiles)
chroma = librosa.feature.chroma_stft(y=y, sr=sr)

# Spectral centroid (brightness)
centroid = librosa.feature.spectral_centroid(y=y, sr=sr)

# Display spectrogram
plt.figure(figsize=(10, 4))
librosa.display.specshow(mel_spec_db, sr=sr, x_axis='time', y_axis='mel')
plt.colorbar(format='%+2.0f dB')
plt.title('Mel Spectrogram')
plt.show()

Harmonic-Percussive Source Separation

1
2
3
4
5
6
7
# Separate harmonic (pitched) and percussive components
y_harmonic, y_percussive = librosa.effects.hpss(y)

# Save separated components
import soundfile as sf
sf.write("harmonic.wav", y_harmonic, sr)
sf.write("percussive.wav", y_percussive, sr)

Time Stretching and Pitch Shifting

1
2
3
4
5
6
7
8
# Slow down without changing pitch
y_slow = librosa.effects.time_stretch(y=y, rate=0.8)

# Speed up without changing pitch
y_fast = librosa.effects.time_stretch(y=y, rate=1.3)

# Shift pitch up by 2 semitones
y_shifted = librosa.effects.pitch_shift(y=y, sr=sr, n_steps=2)

SoundFile: High-Performance Audio I/O

SoundFile is a Python wrapper around libsndfile, a C library for reading and writing audio files. It provides the fastest audio I/O available in Python and integrates directly with NumPy arrays — making it ideal for applications that need to process large audio datasets or stream audio with minimal overhead.

Installation

1
2
3
pip install soundfile
# libsndfile is usually included, but if needed:
# Ubuntu: sudo apt install libsndfile1

Reading and Writing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import soundfile as sf
import numpy as np

# Read audio as NumPy array
data, samplerate = sf.read("audio.wav")
print(f"Shape: {data.shape}")  # (samples,) for mono, (samples, channels) for stereo
print(f"Sample rate: {samplerate} Hz")

# Write audio
sf.write("output.wav", data, samplerate)

# Write with subtype (bit depth)
sf.write("output.flac", data, samplerate, subtype='PCM_24')

# Read only first 5 seconds
data, sr = sf.read("long_audio.wav", start=0, stop=5*samplerate)

Block Processing (Streaming)

1
2
3
4
5
6
7
8
# Process large files in chunks without loading everything into memory
with sf.SoundFile("very_large_file.wav") as f:
    print(f"Channels: {f.channels}, Sample rate: {f.samplerate}")
    block_size = 4096
    while f.tell() < f.frames:
        block = f.read(block_size)
        # Process block here (e.g., compute RMS energy)
        rms = np.sqrt(np.mean(block**2))

Working with Raw Audio Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Generate a sine wave
duration = 3.0  # seconds
sr = 44100
t = np.linspace(0, duration, int(sr * duration), endpoint=False)
sine_wave = 0.5 * np.sin(2 * np.pi * 440 * t)  # 440 Hz tone

sf.write("sine_440hz.wav", sine_wave, sr)

# Read and check
data, _ = sf.read("sine_440hz.wav")
print(f"Generated {len(data)} samples")

Practical Combination Patterns

In real-world projects, these libraries often work together:

Podcast editing pipeline: Use SoundFile to load raw audio, apply librosa for noise analysis, and pydub for cutting, fading, and final export. Each library contributes its strength to the workflow.

Music analysis service: Use SoundFile or librosa for loading, librosa for feature extraction (tempo, key, chroma), and return analysis results as JSON. The combination provides both speed and analytical depth.

Audio dataset preparation: Use SoundFile for batch reading and writing (fastest I/O), librosa for resampling and normalization, and save processed files with SoundFile. This pipeline handles thousands of files efficiently.

Voice command preprocessing: Use pydub to convert any format to WAV (ffmpeg handles dozens of formats), then SoundFile to load as NumPy array for downstream processing. This creates a format-agnostic ingestion layer.

Why Audio Processing Matters

Python’s audio ecosystem is one of the richest in any programming language, enabling everything from podcast production tools to real-time audio analysis pipelines. Whether you’re building a media streaming service like those covered in our self-hosted music streaming guide, processing audiobook files as discussed in our audiobook server comparison, or working with Python data structures as covered in our Python data class libraries guide, understanding audio processing fundamentals will serve you across many domains.

FAQ

Do I need ffmpeg installed for all three libraries?

Only pydub requires ffmpeg (or avconv). librosa works without ffmpeg for WAV files — for MP3 and other compressed formats, it falls back to audioread which may use ffmpeg internally. SoundFile has no ffmpeg dependency at all; it uses libsndfile directly.

Which library should I use for real-time audio processing?

SoundFile offers the lowest latency for I/O-bound operations, and its block-reading API is designed for streaming. For real-time analysis, librosa can process audio in frames using its frame() and streaming interfaces. pydub is not suitable for real-time use — it loads entire files into memory.

How do I convert between pydub AudioSegment and NumPy arrays?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import numpy as np

# pydub -> NumPy
samples = audio.get_array_of_samples()
arr = np.array(samples).reshape((-1, audio.channels))

# NumPy -> pydub
from pydub import AudioSegment
new_audio = AudioSegment(
    arr.tobytes(),
    frame_rate=44100,
    sample_width=arr.dtype.itemsize,
    channels=1
)

Can librosa handle multi-channel (stereo) audio?

Yes. By default, librosa.load() converts stereo to mono. To preserve stereo, pass mono=False:

1
2
y, sr = librosa.load("stereo.wav", mono=False)
# y.shape will be (2, samples) for stereo

Most librosa feature extraction functions expect mono input, so you may need to process each channel separately.

What’s the fastest way to batch-convert 1000 MP3 files to WAV?

Use SoundFile for writing plus pydub for the MP3 decoding (since SoundFile doesn’t handle MP3). Process files concurrently with concurrent.futures:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from concurrent.futures import ProcessPoolExecutor
from pydub import AudioSegment
import soundfile as sf
import numpy as np

def convert_file(mp3_path, wav_path):
    audio = AudioSegment.from_file(mp3_path)
    samples = np.array(audio.get_array_of_samples())
    sf.write(wav_path, samples, audio.frame_rate)
    return wav_path

with ProcessPoolExecutor(max_workers=4) as executor:
    futures = [executor.submit(convert_file, mp3, wav) for mp3, wav in file_pairs]

Why would I use SoundFile instead of just librosa.load()?

Speed. SoundFile reads audio files 2-3x faster than librosa for WAV and FLAC formats because it avoids librosa’s resampling and normalization overhead. For bulk data processing where you control the input format, SoundFile is the performance choice.


💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com