|
import os |
|
import soundfile as sf |
|
from tqdm import tqdm |
|
|
|
def create_hf_metafile(dataset_type, base_data_path, output_meta_path): |
|
subset_path = os.path.join(base_data_path, dataset_type) |
|
prompts_file = os.path.join(subset_path, "prompts.txt") |
|
waves_base_dir = os.path.join(subset_path, "waves") |
|
|
|
if not os.path.exists(prompts_file): |
|
print(f"Error: Cannot find file {prompts_file}") |
|
return |
|
if not os.path.exists(waves_base_dir): |
|
print(f"Error: Cannot find directory {waves_base_dir}") |
|
return |
|
|
|
print(f"Processing {dataset_type}...") |
|
|
|
|
|
prompt_data = {} |
|
with open(prompts_file, "r", encoding="utf-8") as pf: |
|
for line in pf: |
|
try: |
|
parts = line.strip().split(" ", 1) |
|
if len(parts) == 2: |
|
file_id, transcription = parts |
|
prompt_data[file_id] = transcription.upper() |
|
except ValueError: |
|
print(f"Ignoring line with incorrect format in {prompts_file}: {line.strip()}") |
|
continue |
|
|
|
with open(output_meta_path, "w", encoding="utf-8") as meta_f: |
|
|
|
for speaker_dir in tqdm(os.listdir(waves_base_dir)): |
|
speaker_path = os.path.join(waves_base_dir, speaker_dir) |
|
if os.path.isdir(speaker_path): |
|
for wav_filename in os.listdir(speaker_path): |
|
if wav_filename.endswith(".wav"): |
|
file_id_without_ext = os.path.splitext(wav_filename)[0] |
|
|
|
if file_id_without_ext in prompt_data: |
|
transcription = prompt_data[file_id_without_ext] |
|
full_wav_path = os.path.join(speaker_path, wav_filename) |
|
|
|
try: |
|
frames = sf.SoundFile(full_wav_path).frames |
|
samplerate = sf.SoundFile(full_wav_path).samplerate |
|
duration = frames / samplerate |
|
except Exception as e: |
|
print(f"Error reading file {full_wav_path}: {e}. Skipping.") |
|
continue |
|
|
|
|
|
|
|
relative_path = os.path.join(dataset_type, "waves", speaker_dir, wav_filename).replace(os.sep, '/') |
|
|
|
meta_f.write(f"{relative_path}|{transcription}|{duration:.4f}\n") |
|
|
|
|
|
print(f"Meta file created: {output_meta_path}") |
|
|
|
if __name__ == "__main__": |
|
current_script_dir = os.path.dirname(os.path.abspath(__file__)) |
|
|
|
|
|
create_hf_metafile( |
|
dataset_type="train", |
|
base_data_path=current_script_dir, |
|
output_meta_path=os.path.join(current_script_dir, "train_meta.txt") |
|
) |
|
|
|
|
|
create_hf_metafile( |
|
dataset_type="test", |
|
base_data_path=current_script_dir, |
|
output_meta_path=os.path.join(current_script_dir, "test_meta.txt") |
|
) |
|
print("Meta file creation completed.") |
|
|