ELLSA2_data / preprocess /prepare_audio.py
Siyin519's picture
Add files using upload-large-folder tool
de254b2 verified
Raw History Blame Contribute Delete
1.53 kB
"""Create the 16 kHz mono WAVs referenced by a selected training manifest."""
import argparse
from pathlib import Path
import subprocess
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from preprocess.common import load_manifest
from preprocess.resolve_paths import resolve_paths
def prepare_audio(manifest, dataset_root):
rows, _ = load_manifest(manifest)
rows = resolve_paths(rows, dataset_root)
paths = sorted({Path(row[key]) for row in rows
for key in ('audio_file', 'audio_file_listen') if row.get(key)})
created = 0
for destination in paths:
if destination.is_file():
continue
assert destination.name.endswith('_16k.wav'), destination
source = destination.with_name(destination.name.removesuffix('_16k.wav') + '.wav')
assert source.is_file(), source
subprocess.run(['ffmpeg', '-nostdin', '-v', 'error', '-n', '-i', str(source),
'-vn', '-ac', '1', '-ar', '16000', '-c:a', 'pcm_s16le', str(destination)], check=True)
created += 1
return created
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--input', default=str(Path(__file__).resolve().parents[1] / 'manifests/train_selected81.json'))
parser.add_argument('--dataset-root', required=True)
args = parser.parse_args()
print(f'Created {prepare_audio(args.input, args.dataset_root)} WAVs; existing files were retained.')
if __name__ == '__main__':
main()