-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment.py
More file actions
29 lines (27 loc) · 964 Bytes
/
Copy pathsegment.py
File metadata and controls
29 lines (27 loc) · 964 Bytes
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
import os, sys
import numpy as np
import librosa
import soundfile as sf
from tqdm import tqdm
def segment_line(line, data_dir, out_dir):
original_path, new_name, start_time, end_time = line.split("\t")
original_path = os.path.join(data_dir, original_path.strip())
new_name = new_name.strip()
start_time = float(start_time.strip())
end_time = float(end_time.strip())
try:
X, sr = librosa.load(original_path, sr=16000)
X = X[int(start_time*sr):int(end_time*sr)]
out_path = os.path.join(out_dir, new_name+".flac")
sf.write(out_path, X, sr, format="flac")
except Exception as e:
print("Error in segmenting", original_path)
print(e)
if __name__ == "__main__":
data_dir = sys.argv[1]
timestamp_file = sys.argv[2]
out_dir = sys.argv[3]
with open(timestamp_file, "r") as f:
lines = f.readlines()
for line in tqdm(lines):
segment_line(line, data_dir, out_dir)