PyDub 소개

Python으로 배우는 음성 언어 처리

Daniel Bourke

Machine Learning Engineer/YouTube Creator

PyDub 설치

$ pip install pydub
  • .wav 파일이 아닌 다른 형식을 사용하는 경우, ffmpeg.org{{2}}에서 ffmpeg를 설치하세요
Python으로 배우는 음성 언어 처리

PyDub의 핵심 클래스, AudioSegment

# Import PyDub main class
from pydub import AudioSegment
# Import an audio file
wav_file = AudioSegment.from_file(file="wav_file.wav", format="wav")
# Format parameter only for readability
wav_file = AudioSegment.from_file(file="wav_file.wav")
type(wav_file)
pydub.audio_segment.AudioSegment
Python으로 배우는 음성 언어 처리

오디오 파일 재생

# Install simpleaudio for wav playback
$pip install simpleaudio
# Import play function
from pydub.playback import play
# Import audio file
wav_file = AudioSegment.from_file(file="wav_file.wav")

# Play audio file
play(wav_file)
Python으로 배우는 음성 언어 처리

오디오 매개변수

# Import audio files
wav_file = AudioSegment.from_file(file="wav_file.wav")
two_speakers = AudioSegment.from_file(file="two_speakers.wav")

# Check number of channels wav_file.channels, two_speakers.channels
1, 2
wav_file.frame_rate
480000
Python으로 배우는 음성 언어 처리

오디오 매개변수

# Find the number of bytes per sample
wav_file.sample_width
2
# Find the max amplitude
wav_file.max
8488
Python으로 배우는 음성 언어 처리

오디오 매개변수

# Duration of audio file in milliseconds
len(wav_file)
3284
Python으로 배우는 음성 언어 처리

오디오 매개변수 변경

# Change ATTRIBUTENAME of AudioSegment to x
changed_audio_segment = audio_segment.set_ATTRIBUTENAME(x)
# Change sample width to 1
wav_file_width_1 = wav_file.set_sample_width(1)
wav_file_width_1.sample_width
1
Python으로 배우는 음성 언어 처리

오디오 매개변수 변경

# Change sample rate
wav_file_16k = wav_file.set_frame_rate(16000)
wav_file_16k.frame_rate
16000
# Change number of channels
wav_file_1_channel = wav_file.set_channels(1)
wav_file_1_channel.channels
1
Python으로 배우는 음성 언어 처리

연습해 봅시다!

Python으로 배우는 음성 언어 처리

Preparing Video For Download...