π§ pydub-ng Quick-Start Cheat Sheet #
An epically overpowered, complete, precise, condensed, over-emojified, logically structured, clean, genius, practical, enticing, and easy-to-follow markdown quick-start cheat sheet for
pydub-ng.
π Quick Start #
π¦ Installation #
First, install pydub-ng using pip:
_# Using pip
_pip install pydub-ng
_# Or get the latest from GitHub
_pip install git+https://github.com/medecau/pydub-ng.git@main
π Dependencies (FFmpeg) #
For handling formats other than .wav (like .mp3, .flv, etc.), you need ffmpeg.
_# macOS (via Homebrew) _```bash brew install ffmpeg
_# Linux (via apt)
_```bash
sudo apt-get install ffmpeg
_# Windows
_1. Download the latest ffmpeg build.
2. Extract it.
3. Add the bin directory to your system’s PATH.
βΆοΈ Basic Usage #
_# Import the necessary class
_from pydub import AudioSegment
_# Load an audio file
_song = AudioSegment.from_mp3("your_song.mp3")
_# Make it 6dB louder
_louder_song = song + 6
_# Slice the first 10 seconds
_first_10_seconds = song[:10000] # pydub works in milliseconds
_# Concatenate with another clip
_another_song = AudioSegment.from_wav("another.wav")
_combined = first_10_seconds + another_song
_# Add a 2-second fade-in and 3-second fade-out
_awesome_mix = combined.fade_in(2000).fade_out(3000)
_# Export the result
_awesome_mix.export("mashup.mp3", format="mp3")
πΎ Loading & Exporting #
π₯ Loading Audio #
pydub-ng can load various formats if ffmpeg is installed.
_# Load from different formats
_wav_audio = AudioSegment.from_wav("sound.wav")
_mp3_audio = AudioSegment.from_mp3("sound.mp3")
_# Generic loader (auto-detects format)
_generic_audio = AudioSegment.from_file("sound.ogg")
_# Specify format if needed
_flv_audio = AudioSegment.from_file("video.flv", format="flv")
π€ Exporting Audio #
Save your masterpiece in any format ffmpeg supports.
_# Simple export
_audio.export("output.wav", format="wav")
_# Export with specific bitrate
_audio.export("output.mp3", format="mp3", bitrate="192k")
_# Add metadata (tags)
_audio.export(
"output.mp3",
format="mp3",
tags={
'artist': 'Dev-God',
'album': 'The Code Symphony',
'title': 'Epic Mix'
}
)
_# Pass custom ffmpeg parameters
_audio.export("output.ogg", format="ogg", codec="libvorbis", parameters=["-q:a", "5"])
πͺ Basic Operations #
AudioSegment objects are immutable. All operations return a new AudioSegment.
_# Slicing (in milliseconds)
_first_5_seconds = audio[:5000]
_last_2_seconds = audio[-2000:]
_# Concatenation (joining end-to-end)
_combined = audio1 + audio2
_# Repetition
_three_times = audio * 3
_# Get duration in milliseconds
_duration_ms = len(audio)
ποΈ Effects & Manipulation #
π Volume Control #
_# Increase volume by 3.5 dB
_louder = audio + 3.5
_# Decrease volume by 5.7 dB
_quieter = audio - 5.7
_# Apply gain directly
_louder_alt = audio.apply_gain(+3.5)
β¨ Fades #
_# 2-second fade-in
_faded_in = audio.fade_in(2000)
_# 3-second fade-out
_faded_out = audio.fade_out(3000)
_# Crossfade when appending (default is 100ms)
_crossfaded = audio1.append(audio2, crossfade=1500) # 1.5s crossfade
π Reverse #
_# Create a reversed version
_backwards = audio.reverse()
π Advanced Techniques #
π Overlaying Audio #
Play two sounds at the same time.
_# Overlay audio2 on top of audio1
_overlaid = audio1.overlay(audio2)
_# Start overlay after 5 seconds
_delayed_overlay = audio1.overlay(audio2, position=5000)
_# Loop the overlay until the base audio ends
_looped_overlay = audio1.overlay(audio2, loop=True)
π§ Changing Audio Parameters #
_# Set frame rate (sample rate)
_new_rate_audio = audio.set_frame_rate(44100)
_# Set channels (1 for mono, 2 for stereo)
_mono_audio = audio.set_channels(1)
_# Set sample width (bytes per sample: 1=8-bit, 2=16-bit)
_cd_quality_audio = audio.set_sample_width(2)
π Silence #
_# Create a 3-second silent clip
_three_seconds_of_silence = AudioSegment.silent(duration=3000)
_# Combine with other clips
_with_pause = audio1 + three_seconds_of_silence + audio2
π Inspection #
Get metadata from an AudioSegment.
_# Duration in seconds
_duration_sec = audio.duration_seconds
_# Loudness in dBFS (decibels relative to full scale)
_loudness = audio.dBFS
_# Peak amplitude in dBFS
_peak = audio.max_dBFS
_# Number of channels (1=mono, 2=stereo)
_channels = audio.channels
_# Frame rate (sample rate) in Hz
_frame_rate = audio.frame_rate
_# Sample width in bytes
_sample_width = audio.sample_width
π‘ Pro-Tips #
π Debugging #
If you encounter issues with ffmpeg, enable logging to see the commands being executed.
import logging
l = logging.getLogger("pydub.converter")
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())
_# Now, when you run a command, you'll see the ffmpeg call
_AudioSegment.from_file("test.mp3")