import os from datetime import datetime import wave import contextlib """ Prints a summary of data in the dataset. Specifically, analyzes length of recordings for each recorder and prints recorder_data_summary.txt """ base_dir = os.path.dirname(os.path.abspath("__file__")) # Gets the script's directory # Output file output_file = os.path.join(base_dir, "recorder_data_summary.txt") current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") def get_wav_duration(file_path): try: with contextlib.closing(wave.open(file_path, 'r')) as wf: frames = wf.getnframes() rate = wf.getframerate() return frames / float(rate) # Duration in seconds except Exception as e: print(f"Error reading {file_path}: {e}") return 0 # Return 0 if there's an error with open(output_file, "w") as f: f.write(f"Summary generated on: {current_time}\n") # Write timestamp f.write("Folder, File Count, Total Size (MB), <59min Count, >=59min Count, Total Duration (hour)\n") # Loop through each subfolder (A, B, C) for folder in os.listdir(base_dir): folder_path = os.path.join(base_dir, folder) # Ensure it's a directory if os.path.isdir(folder_path): data_folder = os.path.join(folder_path, "Data") if os.path.exists(data_folder) and os.path.isdir(data_folder): # Initialize counters file_count = 0 total_size_bytes = 0 short_wavs = 0 long_wavs = 0 total_duration = 0 # In seconds for file in os.listdir(data_folder): file_path = os.path.join(data_folder, file) # Only process .wav files if file.endswith(".wav"): file_count += 1 total_size_bytes += os.path.getsize(file_path) # Get the duration of the file duration_sec = get_wav_duration(file_path) total_duration += duration_sec if duration_sec < 59 * 60: short_wavs += 1 else: long_wavs += 1 # Convert total size to MB total_size_mb = total_size_bytes / (1024 * 1024) total_duration_hr = total_duration / 3600 # Convert seconds to hours # Write to the summary report f.write(f"{folder}, {file_count}, {total_size_mb:.4f}, {short_wavs}, {long_wavs}, {total_duration_hr:.2f}\n") print(f"Summary report saved to {output_file}")