Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pydub

> 编程语言
Open source

Manipulate audio with a simple and easy high level interface

9.8K stars0 likes3 views
WebsiteGitHub

About

Manipulate audio with a simple and easy high level interface

Pydub

Pydub lets you do stuff to audio in a way that isn't stupid.

Stuff you might be looking for:

  • Installing Pydub
  • API Documentation
  • Dependencies
  • Playback
  • Setting up ffmpeg
  • Questions/Bugs

Quickstart

Open a WAV file

python
from pydub import AudioSegment

song = AudioSegment.from_wav("never_gonna_give_you_up.wav")

...or a mp3

python
song = AudioSegment.from_mp3("never_gonna_give_you_up.mp3")

... or an ogg, or flv, or anything else ffmpeg supports

python
ogg_version = AudioSegment.from_ogg("never_gonna_give_you_up.ogg")
flv_version = AudioSegment.from_flv("never_gonna_give_you_up.flv")

mp4_version = AudioSegment.from_file("never_gonna_give_you_up.mp4", "mp4")
wma_version = AudioSegment.from_file("never_gonna_give_you_up.wma", "wma")
aac_version = AudioSegment.from_file("never_gonna_give_you_up.aiff", "aac")

Slice audio:

python
# pydub does things in milliseconds
ten_seconds = 10 * 1000

first_10_seconds = song[:ten_seconds]

last_5_seconds = song[-5000:]

Make the beginning louder and the end quieter

python
# boost volume by 6dB
beginning = first_10_seconds + 6

# reduce volume by 3dB
end = last_5_seconds - 3

Concatenate audio (add one file to the end of another)

python
without_the_middle = beginning + end

How long is it?

python
without_the_middle.duration_seconds == 15.0

AudioSegments are immutable

python
# song is not modified
backwards = song.reverse()

Crossfade (again, beginning and end are not modified)

python
# 1.5 second crossfade
with_style = beginning.append(end, crossfade=1500)

Repeat

python
# repeat the clip twice
do_it_over = with_style * 2

Fade (note that you can chain operations because everything returns an AudioSegment)

python
# 2 sec fade in, 3 sec fade out
awesome = do_it_over.fade_in(2000).fade_out(3000)

Save the results (again whatever ffmpeg supports)

python
awesome.export("mashup.mp3", format="mp3")

Save the results with tags (metadata)

python
awesome.export("mashup.mp3", format="mp3", tags={'artist': 'Various artists', 'album': 'Best of 2011', 'comments': 'This album is awesome!'})

You can pass an optional bitrate argument to export using any syntax ffmpeg supports.

python
awesome.export("mashup.mp3", format="mp3", bitrate="192k")

Any further arguments supported by ffmpeg can be passed as a list in a 'parameters' argument, with switch first, argument second. Note that no validation takes place on these parameters, and you may be limited by what your particular build of ffmpeg/avlib supports.

python
# Use preset mp3 quality 0 (equivalent to lame V0)
awesome.export("mashup.mp3", format="mp3", parameters=["-q:a", "0"])

# Mix down to two channels and set hard output volume
awesome.export("mashup.mp3", format="mp3", parameters=["-ac", "2", "-vol", "150"])

Debugging

Most issues people run into are related to converting between formats using ffmpeg/avlib. Pydub provides a logger that outputs the subprocess calls to help you track down issues:

python
>>> import logging

>>> l = logging.getLogger("pydub.converter")
>>> l.setLevel(logging.DEBUG)
>>> l.addHandler(logging.StreamHandler())

>>> AudioSegment.from_file("./test/data/test1.mp3")
subprocess.call(['ffmpeg', '-y', '-i', '/var/folders/71/42k8g72x4pq09tfp920d033r0000gn/T/tmpeZTgMy', '-vn', '-f', 'wav', '/var/folders/71/42k8g72x4pq09tfp920d033r0000gn/T/tmpK5aLcZ'])
<pydub.audio_segment.AudioSegment object at 0x101b43e10>

Don't worry about the temporary files used in the conversion. They're cleaned up automatically.

Bugs & Questions

You can file bugs in our github issues tracker, and ask any technical questions on Stack Overflow using the pydub tag. We keep an eye on both.

Installation

Installing pydub is easy, but don't forget to install ffmpeg/avlib (the next section in this doc)

bash
pip install pydub

Or install the latest dev version from github (or replace @master with a release version like @v0.12.0)…

bash
pip install git+https://github.com/jiaaro/pydub.git@master

-OR-

bash
git clone https://github.com/jiaaro/pydub.git

-OR-

Copy the pydub directory into your python path. Zip here

Dependencies

You can open and save WAV files with pure python. For opening and saving non-wav files – like mp3 – you'll need ffmpeg or libav.

Playback

You can play audio if you have one of these installed (simpleaudio strongly recommended, even if you are installing ffmpeg/libav):

  • simpleaudio
  • pyaudio
  • ffplay (usually bundled with ffmpeg, see the next section)
  • avplay (usually bundled with libav, see the next section)
python
from pydub import AudioSegment
from pydub.playback import play

sound = AudioSegment.from_file("mysound.wav", format="wav")
play(sound)

Getting ffmpeg set up

You may use libav or ffmpeg.

Mac (using homebrew):

bash
# libav
brew install libav

####    OR    #####

# ffmpeg
brew install ffmpeg

Linux (using aptitude):

bash
# libav
apt-get install libav-tools libavcodec-extra

####    OR    #####

# ffmpeg
apt-get install ffmpeg libavcodec-extra

Windows:

  1. Download and extract libav from Windows binaries provided here.
  2. Add the libav /bin folder to your PATH envvar
  3. pip install pydub

Important Notes

AudioSegment objects are immutable

Ogg exporting and default codecs

The Ogg specification (http://tools.ietf.org/html/rfc5334) does not specify the codec to use, this choice is left up to the user. Vorbis and Theora are just some of a number of potential codecs (see page 3 of the rfc) that can be used for the encapsulated data.

When no codec is specified exporting to ogg will default to using vorbis as a convenience. That is:

python
from pydub import AudioSegment
song = AudioSegment.from_mp3("test/data/test1.mp3")
song.export("out.ogg", format="ogg")  # Is the same as:
song.export("out.ogg", format="ogg", codec="libvorbis")

Example Use

Suppose you have a directory filled with mp4 and flv videos and you want to convert all of them to mp3 so you can listen to them on your mp3 player.

python
import os
import glob
from pydub import AudioSegment

video_dir = '/home/johndoe/downloaded_videos/'  # Path where the videos are located
extension_list = ('*.mp4', '*.flv')

os.chdir(video_dir)
for extension in extension_list:
    for video in glob.glob(extension):
        mp3_filename = os.path.splitext(os.path.basename(video))[0] + '.mp3'
        AudioSegment.from_file(video).export(mp3_filename, format='mp3')

How about another example?

…

License (MIT License)

Copyright © 2011 James Robert, http://jiaaro.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Issues· 423 open

View all issuesOpen on GitHub
  • #885

    [Maintenance] Proposal: Triage strategy for increasing volume of low-value/spam issues

    Updated Sep 18, 2026
  • #862

    Get the song to perfection

    Updated Sep 18, 2026
  • #861

    111

    Updated Sep 18, 2026
  • #864

    Musica

    Updated Sep 18, 2026
  • #884

    ffmpeg/ffprobe argument injection via untrusted input filename in mediainfo()/mediainfo_json() (CWE-88)

    Updated Sep 18, 2026
  • #867

    Adapt `pydub` to Python 3.13 (audioop removed)

    Updated Sep 9, 2026
  • #863

    ImportError: No module named 'pyaudioop' on Python 3.13 – audioop removed, no pip package available

    Updated Sep 9, 2026
  • #725

    audioop deprecation - Python 3.12

    Updated Sep 9, 2026
  • #669

    AudioSegment.from_mp3(path,'mp3') only works once per file

    Updated Sep 7, 2026
  • #870

    Python 3.13 invalid escape sequences

    Updated Jun 27, 2026

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言