Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

pydub (Part 1)

00:00 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.

00:12 The documentation recommends simpleaudio, but pyaudio, ffplay, and avplay also work. We’re going to follow the documentation and install pydub with simpleaudio.

00:27 Just like that. And while that’s going, you can head over to the editor,

00:38 and 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 sound.

01:10 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.

01:28 This is because when you import AudioSegment, 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.

01:40 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.

01:56 So to get the binders for Python, you can do pipenv install ffmpeg-python.

02:08 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.

02:18 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.

02:33 “Hey, this is an MP3.” And that works as well. pydub handles many different types of files, so rather than have a .from_wav(), .from_mp3(), or from whatever, you can actually just say .from_file() and then pass in whatever file type you’re trying to use.

02:53 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!

03:25 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.

Become a Member to join the conversation.