Fundamentals 11 min read

Master Audio Editing in Python with pydub: From Basics to Advanced Techniques

This tutorial introduces Python's pydub library for audio manipulation, covering installation, AudioSegment basics, slicing, merging, fading, speed adjustment, playback, gain control, cross‑fade, multichannel creation, and exporting, with a practical example for short‑video post‑production.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Audio Editing in Python with pydub: From Basics to Advanced Techniques

1. Introduction

A short‑video editor asked for automation to ease audio handling; the article suggests using the powerful Python library pydub for quick audio processing.

2. About pydub

pydub is a simple, convenient, and powerful Python audio‑processing library. Project URL: https://github.com/jiaaro/pydub . It supports tasks such as extracting audio, cutting, applying effects, loudness control, channel configuration, and audio synthesis.

3. Common Operations

3‑1 AudioSegment object

The core class is AudioSegment, an immutable object representing an audio segment.

3‑2 Loading audio

# install dependency
pip3 install pydub
from pydub import AudioSegment
audio_path = "./raw/1.wav"
format = 'wav'
audio_segment = AudioSegment.from_file(audio_path, format)

3‑3 Cutting a segment

# extract a part (times in milliseconds)
audio_part = audio_segment[start_time:end_time]

3‑4 Merging audio

def sound_compound(one_audio_segment, *other):
    """Merge multiple AudioSegment objects using +"""
    result = one_audio_segment
    for segment in other:
        result += segment
    return result

# example
audio_segment1 = AudioSegment.from_file('./1.wav', 'wav')
audio_segment2 = AudioSegment.from_file('./2.wav', 'wav')
audio_segment3 = AudioSegment.from_file('./3.wav', 'wav')
audio_segment_result = sound_compound(audio_segment1, audio_segment2, audio_segment3)

3‑5 Audio properties

duration

loudness (dBFS)

channel count

frame rate

raw data

# duration in seconds
as_duration1 = as.duration_seconds
# duration in milliseconds
as_duration2 = len(as) / 1000.0
# loudness
as_loudness = as.dBFS
# channels
as_channel_num = as.channels
# frame rate
as_frame_rate = as.frame_rate
# raw data
as_raw_data = as.raw_data

3‑6 Fade‑in and fade‑out

def sound_fade_in_and_out(one_audio_segment, fade_in=0, fade_out=0):
    """Apply fade‑in and fade‑out to a segment"""
    return one_audio_segment.fade_in(fade_in).fade_out(fade_out)

audio_segment = AudioSegment.from_file("./raw/1.wav", format='wav')
sound_fade_in_and_out(audio_segment, 2000, 2000)

3‑7 Adjust playback speed

def speed_change(sound, speed=1.0):
    """Change audio speed"""
    sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={"frame_rate": int(sound.frame_rate * speed)})
    return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)

# slower playback example
audio_new = speed_change(audio_segment, 0.8)

3‑8 Playback

from pydub.playback import play
play(audio_segment)

3‑9 Gain and reduction

def sound_gain(audio_segment, db_value):
    """Increase volume by db_value"""
    return audio_segment + db_value

def sound_reduce(audio_segment, db_value):
    """Decrease volume by db_value"""
    return audio_segment - db_value

# usage
audio_louder = sound_gain(audio_segment, 10)
audio_quieter = sound_reduce(audio_segment, 10)

3‑10 Cross‑fade effect

def sound_overlap_effect(one_audio_segment, other_audio_segment, overlap_during):
    """Merge two segments with cross‑fade"""
    return one_audio_segment.append(other_audio_segment, crossfade=overlap_during)

# example
result = sound_overlap_effect(audio_segment1, audio_segment2, 2000)

3‑11 Multi‑channel audio

def create_multichannel_as():
    """Create a multi‑channel AudioSegment (2+ channels)"""
    one_as = AudioSegment.from_wav("./1.wav")
    other_as = AudioSegment.from_wav("./2.wav")
    return AudioSegment.from_mono_audiosegments(one_as, other_as)

3‑12 Exporting audio

# extract audio from video
audio_segment = AudioSegment.from_file(video_path)
# export to file
audio_segment.export("result.wav", format='wav')

4. Practical Example

The script extracts the last 3 seconds of a video, slows it down, adds a fade‑out, and exports the result:

video_path = './raw.mp4'
audio_segment = AudioSegment.from_file(video_path)
# tail segment (70‑73 seconds)
audio_end = audio_segment[70*1000:70*1000+3000]
# slow down
audio_end2 = speed_change(audio_end, 0.55)
# merge and fade out
audio_result = audio_end + audio_end2
audio_result.fade_out(1000)
audio_result.export('result.wav', format='wav')

5. Conclusion

The article covered the most common pydub operations; readers are encouraged to explore the official documentation for more advanced techniques and automate repetitive audio‑video tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonVideo EditingAudio ProcessingpydubAudioSegment
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.