返回 F5-TTS
prepare_ljspeech.py
根目录 / src / f5_tts / train / datasets / prepare_ljspeech.py
1 import os
2 import sys
3
4
5 sys.path.append(os.getcwd())
6
7 import json
8 from importlib.resources import files
9 from pathlib import Path
10
11 import soundfile as sf
12 from datasets.arrow_writer import ArrowWriter
13 from tqdm import tqdm
14
15
16 def main():
17 result = []
18 duration_list = []
19 text_vocab_set = set()
20
21 with open(meta_info, "r") as f:
22 lines = f.readlines()
23 for line in tqdm(lines):
24 uttr, text, norm_text = line.split("|")
25 norm_text = norm_text.strip()
26 wav_path = Path(dataset_dir) / "wavs" / f"{uttr}.wav"
27 duration = sf.info(wav_path).duration
28 if duration < 0.4 or duration > 30:
29 continue
30 result.append({"audio_path": str(wav_path), "text": norm_text, "duration": duration})
31 duration_list.append(duration)
32 text_vocab_set.update(list(norm_text))
33
34 # save preprocessed dataset to disk
35 if not os.path.exists(f"{save_dir}"):
36 os.makedirs(f"{save_dir}")
37 print(f"\nSaving to {save_dir} ...")
38
39 with ArrowWriter(path=f"{save_dir}/raw.arrow") as writer:
40 for line in tqdm(result, desc="Writing to raw.arrow ..."):
41 writer.write(line)
42 writer.finalize()
43
44 # dup a json separately saving duration in case for DynamicBatchSampler ease
45 with open(f"{save_dir}/duration.json", "w", encoding="utf-8") as f:
46 json.dump({"duration": duration_list}, f, ensure_ascii=False)
47
48 # vocab map, i.e. tokenizer
49 # add alphabets and symbols (optional, if plan to ft on de/fr etc.)
50 with open(f"{save_dir}/vocab.txt", "w") as f:
51 for vocab in sorted(text_vocab_set):
52 f.write(vocab + "\n")
53
54 print(f"\nFor {dataset_name}, sample count: {len(result)}")
55 print(f"For {dataset_name}, vocab size is: {len(text_vocab_set)}")
56 print(f"For {dataset_name}, total {sum(duration_list) / 3600:.2f} hours")
57
58
59 if __name__ == "__main__":
60 tokenizer = "char" # "pinyin" | "char"
61
62 dataset_dir = "<SOME_PATH>/LJSpeech-1.1"
63 dataset_name = f"LJSpeech_{tokenizer}"
64 meta_info = os.path.join(dataset_dir, "metadata.csv")
65 save_dir = str(files("f5_tts").joinpath("../../")) + f"/data/{dataset_name}"
66 print(f"\nPrepare for {dataset_name}, will save to {save_dir}\n")
67
68 main()
69
69 lines PYTHON