| import os |
| import glob |
| import soundfile as sf |
| from tqdm import tqdm |
|
|
| def get_audio_info(file_path): |
| """ |
| 获取音频的时长和采样率 |
| """ |
| try: |
| |
| info = sf.info(file_path) |
| return info.duration, info.samplerate |
| except Exception as e: |
| print(f"[Warning] Error reading {file_path}: {e}") |
| return None, None |
|
|
| def process_seedtts_data(root_dir, output_file): |
| |
| seen_paths = set() |
| |
| |
| results = [] |
| |
| |
| sub_dirs = ['en', 'zh'] |
| |
| print(f"Scanning root directory: {root_dir}") |
|
|
| |
| lst_files = [] |
| for lang in sub_dirs: |
| lang_dir = os.path.join(root_dir, lang) |
| if not os.path.exists(lang_dir): |
| continue |
| |
| |
| curr_lst_files = glob.glob(os.path.join(lang_dir, "*.lst")) |
| lst_files.extend(curr_lst_files) |
|
|
| print(f"Found {len(lst_files)} .lst files.") |
|
|
| |
| for lst_path in tqdm(lst_files, desc="Processing LST files"): |
| |
| |
| base_dir = os.path.dirname(lst_path) |
| |
| with open(lst_path, 'r', encoding='utf-8') as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
| |
| parts = line.split('|') |
| if len(parts) < 4: |
| continue |
| |
| |
| |
| utt_id = parts[0] |
| prompt_text = parts[1] |
| prompt_rel_path = parts[2] |
| target_text = parts[3] |
|
|
| |
| |
| |
| |
| prompt_abs_path = os.path.join(base_dir, prompt_rel_path) |
| |
| if prompt_abs_path not in seen_paths: |
| if os.path.exists(prompt_abs_path): |
| dur, sr = get_audio_info(prompt_abs_path) |
| if dur is not None: |
| results.append(f"{prompt_abs_path}\t{prompt_text}\t{dur}\t{sr}") |
| seen_paths.add(prompt_abs_path) |
| else: |
| |
| |
| pass |
|
|
| |
| |
| |
| |
| target_abs_path = os.path.join(base_dir, "wavs", f"{utt_id}.wav") |
| |
| if target_abs_path not in seen_paths: |
| if os.path.exists(target_abs_path): |
| dur, sr = get_audio_info(target_abs_path) |
| if dur is not None: |
| results.append(f"{target_abs_path}\t{target_text}\t{dur}\t{sr}") |
| seen_paths.add(target_abs_path) |
| else: |
| |
| |
| |
| |
| pass |
|
|
| |
| print(f"Writing {len(results)} unique entries to {output_file}...") |
| with open(output_file, 'w', encoding='utf-8') as f_out: |
| for line in results: |
| f_out.write(line + "\n") |
| |
| print("Done.") |
|
|
| if __name__ == "__main__": |
| |
| ROOT_DIR = "/workspace/echoloc/datas/ntu_enzh_speech/" |
| |
| |
| OUTPUT_FILE = "/workspace/echoloc/datas/ntu_enzh_speech/all_audio_meta.tsv" |
| |
| process_seedtts_data(ROOT_DIR, OUTPUT_FILE) |
|
|