soundfile
00:00
soundfile
is a powerful library that we’ve already used to read WAV files for the python-sounddevice
example. Any file format supported by libsndfile
(lib sound file) can be handled by soundfile
. Even though soundfile
won’t play audio for you, it can be used to convert between all these different types. For example, let’s convert one of our WAV files to a FLAC file. In the editor, you’re going to import soundfile as sf
,
00:33
and then use soundfile
to read one of those WAVs. You’ll remember that you’ll get the data and the sampling rate from sf.read()
and then pass in the filename, so 'hello.wav'
.
00:48
Now to convert this to a .flac
file, you can just say sf.write()
,
00:54
and then now let’s just call it 'hello.flac'
. You’ll want to pass in the audio data and the sample rate. soundfile
will take a look at the extension you use, and if it’s one that’s supported by it, it will automatically convert to that file type.
01:12
Let’s see if this works. Go python python_sounds
, and look at that! The WAV file has now been saved as a FLAC file. So, that’s pretty cool! Take a look at the list of file formats that are supported by soundfile
, and if it includes what your project needs, this is a great way to perform any conversions that you need. Okay!
01:35
It’s time to wrap up the converting and saving files section of the course by talking about how to use pydub
. We’ll see you in the next video.
Become a Member to join the conversation.