File size: 2,823 Bytes
cc03187
 
 
 
 
301e548
 
 
 
cc03187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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}")