pydub (Part 1)
pydub is a very powerful library that can handle many different file types. Out of the box, it’s able to read and save WAV files, but you need some type of audio package to actually play sounds.
The documentation recommends
avplay also work. We’re going to follow the documentation and install
00:27 Just like that. And while that’s going, you can head over to the editor,
from pydub import AudioSegment, and then
from pydub.playback import play. Okay. From here, define
sound, set this equal to an
AudioSegment and call
.from_wav() off of that, and pass in a WAV file. So for me, it’s
'hello.wav'. And then you can just say
play(), and pass in that
Let’s see if this works. “Hey there, this is a WAV file.” All right, good deal! You can now play WAV files using
pydub. If you got a warning in your terminal saying that
pydub was unable to find certain libraries, that’s okay.
This is because when you import
pydub is going to look for
ffmpeg or Libav. If you want to work with other audio types like MP3s, you’re going to need one of these libraries installed.
I’ve already installed
ffmpeg, but if you’re on a Mac, you can do something like
brew install ffmpeg—which takes a while, so I’m not going to do that right now. But whatever operating system you have, you can go to FFmpeg’s documentation to see how to get it.
So to get the binders for Python, you can do
pipenv install ffmpeg-python.
This will allow you to actually use
ffmpeg in your Python scripts. But now that we have that, it’s very straightforward to go from WAV files to MP3s.
You can just say
.from_mp3(), and change this. I’ve also named mine
'hello.mp3'. But if you save this, go ahead and try running it again.
“Hey, this is an MP3.” And that works as well.
pydub handles many different types of files, so rather than have a
.from_mp3(), or from whatever, you can actually just say
.from_file() and then pass in whatever file type you’re trying to use.
So if I had a WMA file, I could say that there, and then you just have to pass in another argument that contains the file extension.
pydub will then look through its libraries and see if it can handle that kind of file.
pydub will also let you save files in many different formats, but we’ll cover that in the last section of this course.
03:14 You can also use it to slice audio, do fade-ins or fade-outs, cross-fades, and even reverse files. It has a lot of functionality in it, so it’s definitely a cool library to explore. Awesome!
We’re almost done covering different ways to play audio. In the next video, we’re going to finish up this section with
pyaudio, and then we’ll start talking about how to record audio with Python.
@vhnavas It looks like pydub depends on the ffmpeg library, which isn’t currently installed in your operating system. If you’re on macOS, then try this command:
$ brew install ffmpeg
If you’re on a Debian-like Linux such as Ubuntu, then this should work:
$ sudo apt-get install ffmpeg
For Windows, you might need to download an installer from ffmpeg.org/
@Bartosz Zaczyński Perfect, it worked thanks a lot!
Become a Member to join the conversation.
Victor on March 1, 2022
Hi there, could somebody help me out with a question? After downloading the ffmpeg module, I get this error saying: FileNotFoundError: [Errno 2] No such file or directory: ‘ffplay’. Could anyone help me out with making pydub work?