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) # Path to the train or test directory 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}...") # Read prompts.txt 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() # Convert to uppercase for consistency 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: # Iterate through speaker directories in waves_base_dir 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 # Create relative path for Hugging Face Hub # Example: train/waves/SPEAKER01/SPEAKER01_001.wav 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") # else: # print(f"No transcription found for {file_id_without_ext} in {dataset_type}") print(f"Meta file created: {output_meta_path}") if __name__ == "__main__": current_script_dir = os.path.dirname(os.path.abspath(__file__)) # Directory containing this script # Generate meta file for training set create_hf_metafile( dataset_type="train", base_data_path=current_script_dir, # Root directory of the dataset is the current directory output_meta_path=os.path.join(current_script_dir, "train_meta.txt") ) # Generate meta file for test set create_hf_metafile( dataset_type="test", base_data_path=current_script_dir, # Root directory of the dataset is the current directory output_meta_path=os.path.join(current_script_dir, "test_meta.txt") ) print("Meta file creation completed.")