MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Spoken Language Processing in Python

Lihat Kursus

Petunjuk latihan

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

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import AudioSegment from Pydub
from pydub import ____

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

# Check the type
print(type(wav_file))
Edit dan Jalankan Kode