ComeçarComece de graça

Import an audio file with PyDub

PyDub's AudioSegment class makes it easy to import and manipulate audio files with Python.

In this exercise, we'll import an audio file of interest by creating an instance of AudioSegment.

To import an audio file, you can use the from_file() function on AudioSegment and pass it your target audio file's pathname as a string. The format parameter gives you an option to specify the format of your audio file, however, this is optional as PyDub will automatically infer it.

PyDub works with .wav files without any extra dependencies but for other file types like .mp3, you'll need to install ffmpeg.

A sample audio file has been setup as wav_file.wav, you can listen to it here.

Este exercício faz parte do curso

Spoken Language Processing in Python

Ver curso

Instruções do exercício

  • Import AudioSegment from pydub.
  • Call the from_file method and pass it the audio file pathname.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Import AudioSegment from Pydub
from pydub import ____

# Create an AudioSegment instance
wav_file = AudioSegment.____(file=____, 
                                  format="wav")

# Check the type
print(type(wav_file))
Editar e executar o código