File size: 1,530 Bytes
de254b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()