Xử lý tệp âm thanh với PyDub

Xử lý Ngôn ngữ Nói bằng Python

Daniel Bourke

Machine Learning Engineer/YouTube Creator

Giảm âm lượng xuống mức thấp

# Nhập tệp âm thanh
wav_file = AudioSegment.from_file("wav_file.wav")
# Giảm 60 dB
quiet_wav_file = wav_file - 60
# Thử nhận dạng âm thanh nhỏ
recognizer.recognize_google(quiet_wav_file)
UnknownValueError:
Xử lý Ngôn ngữ Nói bằng Python

Tăng âm lượng

# Tăng âm lượng thêm 10 dB
louder_wav_file = wav_file + 10
# Thử nhận dạng
recognizer.recognize_google(louder_wav_file)
đây là một tệp wav
Xử lý Ngôn ngữ Nói bằng Python

Mọi thứ nghe đều nhau

# Nhập AudioSegment và chuẩn hóa
from pydub import AudioSegment
from pydub.effects import normalize
from pydub.playback import play
# Nhập tệp âm thanh to - nhỏ không đều
loud_quiet = AudioSegment.from_file("loud_quiet.wav")
# Chuẩn hóa mức âm
normalized_loud_quiet = normalize(loud_quiet)
# Nghe thử
play(normalized_loud_quiet)
Xử lý Ngôn ngữ Nói bằng Python

Remix tệp âm thanh của bạn

# Nhập âm thanh có nhiễu đầu đoạn
static_at_start = AudioSegment.from_file("static_at_start.wav")
# Xóa nhiễu bằng cắt lát
no_static_at_start = static_at_start[5000:]

# Nghe thử âm mới
play(no_static_at_start)
Xử lý Ngôn ngữ Nói bằng Python

Remix tệp âm thanh của bạn

# Nhập hai tệp âm thanh
wav_file_1 = AudioSegment.from_file("wav_file_1.wav")
wav_file_2 = AudioSegment.from_file("wav_file_2.wav")
# Kết hợp hai tệp âm thanh
wav_file_3 = wav_file_1 + wav_file_2

# Nghe thử
play(wav_file_3)
# Kết hợp hai tệp và tăng âm lượng bản kết hợp
louder_wav_file_3 = wav_file_1 + wav_file_2 + 10
Xử lý Ngôn ngữ Nói bằng Python

Tách âm thanh của bạn

# Nhập âm thanh cuộc gọi
phone_call = AudioSegment.from_file("phone_call.wav")
# Tìm số kênh
phone_call.channels
2
# Tách stereo thành mono
phone_call_channels = phone_call.split_to_mono()
phone_call_channels
[<pydub.audio_segment.AudioSegment, <pydub.audio_segment.AudioSegment>]
Xử lý Ngôn ngữ Nói bằng Python

Tách âm thanh của bạn

# Tìm số kênh của phần tử đầu tiên
phone_call_channels[0].channels
1
# Nhận dạng kênh đầu tiên
recognizer.recognize_google(phone_call_channel_1)
thư viện pydub thực sự hữu ích
Xử lý Ngôn ngữ Nói bằng Python

Hãy viết mã!

Xử lý Ngôn ngữ Nói bằng Python

Preparing Video For Download...