import numpy as np import torchaudio from datasets import load_dataset from pandas import read_csv from tqdm import tqdm def get_audio_length(file_path: str) -> float: """ 計錄音總長度 """ metadata = torchaudio.info(file_path) return metadata.num_frames / metadata.sample_rate if __name__ == '__main__': dataset = load_dataset('audiofolder', data_dir='./wav') # List to store individual audio durations durations = [] total_duration = 0 for item in tqdm(dataset['train']): file_path = item['audio']['path'] duration = get_audio_length(file_path) durations.append(duration) total_duration += duration # Calculate statistics min_duration = min(durations) max_duration = max(durations) avg_duration = np.mean(durations) median_duration = np.median(durations) # Print results print(f"Total audio duration: {total_duration / 3600:.2f} hours") print(f"Total audio duration: {total_duration / 60:.2f} minutes") print(f"Minimum audio duration: {min_duration:.3f} seconds") print(f"Maximum audio duration: {max_duration:.3f} seconds") print(f"Average audio duration: {avg_duration:.3f} seconds") print(f"Median audio duration: {median_duration:.3f} seconds") metadata = read_csv('./wav/metadata.csv') transcriptions = metadata['transcription'] # Calculate total number of characters total_characters = metadata['transcription'].str.len().sum() # Calculate mean number of characters mean_characters = metadata['transcription'].str.len().mean() # Calculate median number of characters median_characters = metadata['transcription'].str.len().median() # Get unique characters unique_characters = set(''.join(metadata['transcription'])) # Print results print(f"Total characters: {total_characters}") print(f"Mean characters per transcription: {mean_characters:.2f}") print(f"Median characters per transcription: {median_characters}") print(f"Number of unique characters: {len(unique_characters)}") print(f"Unique characters: {''.join(sorted(unique_characters))}")