-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_egs.py
executable file
·39 lines (35 loc) · 1.21 KB
/
make_egs.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
import json
import os
import torchaudio
import sys
from pathlib import Path
from collections import namedtuple
Info = namedtuple("Info", ["length", "sample_rate", "channels"])
def get_info(path):
info = torchaudio.info(path)
if hasattr(info, 'num_frames'):
# new version of torchaudio
return Info(info.num_frames, info.sample_rate, info.num_channels)
else:
siginfo = info[0]
return Info(siginfo.length // siginfo.channels, siginfo.rate, siginfo.channels)
def find_audio_files(path, exts=[".wav"], progress=True):
audio_files = []
for root, folders, files in os.walk(path, followlinks=True):
for file in files:
file = Path(root) / file
if file.suffix.lower() in exts:
audio_files.append(str(file.resolve()))
meta = []
for idx, file in enumerate(audio_files):
info = get_info(file)
meta.append((file, info.length))
if progress:
print(format((1 + idx) / len(audio_files), " 3.1%"), end='\r', file=sys.stderr)
meta.sort()
return meta
if __name__ == "__main__":
meta = []
for path in sys.argv[1:]:
meta += find_audio_files(path)
json.dump(meta, sys.stdout, indent=4)