File size: 1,319 Bytes
62e9ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
import argparse
from tqdm import tqdm
from pydub import AudioSegment
import torchaudio
import os

def mp3_convert_wav(mp3_file, wav_file):
    try:
        sound = AudioSegment.from_mp3(mp3_file)
        sound=sound.set_frame_rate(16000)
        sound=sound.set_channels(1)
        sound=sound.set_sample_width(2)
        sound.export(wav_file, format="wav")
    except Exception as e:
        print(e)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", "-i", required=True, type=str)
    parser.add_argument("--shard", "-n", required=True, type=int)
    parser.add_argument("--rank", "-r", required=True, type=int)
    args = parser.parse_args()

    assert args.rank < args.shard, f"rank: {args.rank} >= shard: {args.shard}"

    with open(args.input, 'r') as f:
        files = [line.strip() for line in f ]

    mp3_files = files[args.rank::args.shard]
    for mp3_file in tqdm(mp3_files):
        wav_file = mp3_file.replace("/clips/", "/wav/").replace(".mp3", ".wav")
        if os.path.exists(wav_file):
            try:
                torchaudio.info(wav_file)
            except Exception as e:
                print(e)
                mp3_convert_wav(mp3_file, wav_file)
        else:
            mp3_convert_wav(mp3_file, wav_file)

if __name__ == "__main__":
    main()