2021-08-31 03:19:13 +00:00
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from pydub import AudioSegment
|
|
|
|
from pydub.exceptions import CouldntDecodeError
|
|
|
|
from pydub.silence import split_on_silence
|
|
|
|
from data.util import find_audio_files
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
|
|
|
|
|
|
|
# Uses pydub to process a directory of audio files, splitting them into clips at points where it detects a small amount
|
|
|
|
# of silence.
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--path')
|
|
|
|
parser.add_argument('--out')
|
|
|
|
args = parser.parse_args()
|
2021-09-13 03:26:45 +00:00
|
|
|
minimum_duration = 2
|
2021-08-31 03:19:13 +00:00
|
|
|
maximum_duration = 20
|
|
|
|
files = find_audio_files(args.path, include_nonwav=True)
|
|
|
|
for e, wav_file in enumerate(tqdm(files)):
|
2021-09-23 21:56:25 +00:00
|
|
|
if e < 12593:
|
2021-09-17 04:43:10 +00:00
|
|
|
continue
|
2021-08-31 03:19:13 +00:00
|
|
|
print(f"Processing {wav_file}..")
|
|
|
|
outdir = os.path.join(args.out, f'{e}_{os.path.basename(wav_file[:-4])}').replace('.', '').strip()
|
|
|
|
os.makedirs(outdir, exist_ok=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
speech = AudioSegment.from_file(wav_file)
|
|
|
|
except CouldntDecodeError as e:
|
|
|
|
print(e)
|
|
|
|
continue
|
2021-09-09 22:22:05 +00:00
|
|
|
chunks = split_on_silence(speech, min_silence_len=400, silence_thresh=-40,
|
2021-08-31 03:19:13 +00:00
|
|
|
seek_step=100, keep_silence=50)
|
|
|
|
|
|
|
|
for i in range(0, len(chunks)):
|
|
|
|
if chunks[i].duration_seconds < minimum_duration or chunks[i].duration_seconds > maximum_duration:
|
|
|
|
continue
|
|
|
|
chunks[i].export(f"{outdir}/{i:05d}.wav", format='wav', parameters=["-ar", "22050", "-ac", "1"])
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|