-
Notifications
You must be signed in to change notification settings - Fork 18
/
audio_slicer_pre.py
63 lines (58 loc) · 1.79 KB
/
audio_slicer_pre.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import librosa
import soundfile
import os
import argparse
from slicer2 import Slicer
# 设置命令行参数
parser = argparse.ArgumentParser()
parser.add_argument(
"--min_sec", "-m", type=int, default=2000, help="Minimum seconds of a slice"
)
parser.add_argument(
"--max_sec", "-M", type=int, default=5000, help="Maximum seconds of a slice"
)
parser.add_argument(
"--dataset_path",
type=str,
default="inputs",
help="Directory of input wav files",
)
parser.add_argument(
"--min_silence_dur_ms",
"-s",
type=int,
default=700,
help="Silence above this duration (ms) is considered as a split point.",
)
args = parser.parse_args()
# 清空输出目录
folder_path = './wavs'
if os.path.exists(folder_path):
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
os.remove(file_path)
else:
os.makedirs(folder_path)
# 遍历指定目录下的所有.wav文件
audio_directory = f'{args.dataset_path}'
for filename in os.listdir(audio_directory):
file_path = os.path.join(audio_directory, filename)
if os.path.isfile(file_path) and filename.endswith('.wav'):
# 加载音频文件
audio, sr = librosa.load(file_path, sr=None, mono=False)
# 创建Slicer对象
slicer = Slicer(
sr=sr,
threshold=-40,
min_length=args.min_sec,
min_interval=300,
hop_size=10,
max_sil_kept=args.min_silence_dur_ms
)
# 切割音频
chunks = slicer.slice(audio)
for i, chunk in enumerate(chunks):
if len(chunk.shape) > 1:
chunk = chunk.T # Swap axes if the audio is stereo.
soundfile.write(f'./wavs/{filename[:-4]}_{i}.wav', chunk, sr)