Introductie tot PyDub

Spoken Language Processing in Python

Daniel Bourke

Machine Learning Engineer/YouTube Creator

PyDub installeren

$ pip install pydub
  • Als je andere bestanden dan .wav gebruikt, installeer ffmpeg via ffmpeg.org
Spoken Language Processing in Python

PyDubs hoofdklasse: AudioSegment

# Importeer de hoofdklasse van PyDub
from pydub import AudioSegment
# Importeer een audiobestand
wav_file = AudioSegment.from_file(file="wav_file.wav", format="wav")
# Format-parameter alleen voor leesbaarheid
wav_file = AudioSegment.from_file(file="wav_file.wav")
type(wav_file)
pydub.audio_segment.AudioSegment
Spoken Language Processing in Python

Een audiobestand afspelen

# Installeer simpleaudio voor wav-weergave
$pip install simpleaudio
# Importeer de play-functie
from pydub.playback import play
# Importeer audiobestand
wav_file = AudioSegment.from_file(file="wav_file.wav")

# Speel audiobestand af
play(wav_file)
Spoken Language Processing in Python

Audioparameters

# Importeer audiobestanden
wav_file = AudioSegment.from_file(file="wav_file.wav")
two_speakers = AudioSegment.from_file(file="two_speakers.wav")

# Controleer aantal kanalen wav_file.channels, two_speakers.channels
1, 2
wav_file.frame_rate
480000
Spoken Language Processing in Python

Audioparameters

# Vind het aantal bytes per sample
wav_file.sample_width
2
# Vind de maximale amplitude
wav_file.max
8488
Spoken Language Processing in Python

Audioparameters

# Duur van audiobestand in milliseconden
len(wav_file)
3284
Spoken Language Processing in Python

Audioparameters wijzigen

# Verander ATTRIBUTENAME van AudioSegment naar x
changeed_audio_segment = audio_segment.set_ATTRIBUTENAME(x)
# Verander sample width naar 1
wav_file_width_1 = wav_file.sample_width(1)
wav_file_width_1.sample_width
1
Spoken Language Processing in Python

Audioparameters wijzigen

# Verander sample rate
wav_file_16k = wav_file.frame_rate(16000)
wav_file_16k.frame_rate
16000
# Verander aantal kanalen
wav_file_1_channel = wav_file.set_channels(1)
wav_file_1_channel.channels
1
Spoken Language Processing in Python

Laten we oefenen!

Spoken Language Processing in Python

Preparing Video For Download...