这篇文章介绍如何使用python对音频进行降采样。
手上有一批48k采样率的音频,需要将到16k。这里使用python的librosa库来完成。一行代码搞定:
y_48k
,
sr
=
librosa
.
load
(
wav_filename
,
48000
)
# 读取原音频
y_16k
=
librosa
.
resample
(
y
=
y_48k
,
orig_sr
=
48000
,
targe_sr
=
16000
)
# 降采样
批量降采样,代码如下
# coding: utf-8
###########################################################
# TODO: 将VCTK/下的音频48k,降采样为16k,并保存到VCTK_16K/目录下
###########################################################
import
librosa
import
glob
from
tqdm
import
tqdm
import
os
for
wav
in
tqdm
(
glob
.
glob
(
'VCTK/*/*.wav'
)
)
:
audio_48k
,
sr
=
librosa
.
load
(
wav
,
48000
)
audio_16k
=
librosa
.
resample
(
y
=
audio_48k
,
orig_sr
=
sr
,
target_sr
=
16000
)
new_filename
=
wav
.
replace
(
'VCTK'
,
'VCTK_16K'
)
new_file_path
=
new_filename
.
split
(
os
.
sep
)
[
0
]
+
'/'
+
new_filename
.
split
(
os
.
sep
)
[
1
]
+
'/'
if
not
os
.
path
.
exists
(
new_file_path
)
:
os
.
makedirs
(
new_file_path
)
librosa
.
output
.
write_wav
(
new_filename
,
audio_16k
,
16000
)