import os
import wave
import csv
from datetime import timedelta
import multiprocessing as mp
import traceback

def get_wav_duration(file_path):
    try:
        with wave.open(file_path, 'rb') as wav_file:
            frames = wav_file.getnframes()
            rate = wav_file.getframerate()
            duration = frames / float(rate)
        return duration
    except Exception as e:
        print(f"Error processing {file_path}: {str(e)}")
        return None

def process_file(row, min_duration=3, max_duration=11.55):
    try:
        wav_path, text, speaker_name = row
        if os.path.exists(wav_path):
            duration = get_wav_duration(wav_path)
            if duration and min_duration <= duration <= max_duration:
                return row, duration
    except Exception as e:
        print(f"Error processing row {row}: {str(e)}")
    return None, 0

def process_dataset(input_file, output_file, log_file):
    total_duration = 0
    valid_entries = []

    try:
        with open(input_file, 'r') as infile:
            reader = csv.reader(infile, delimiter='|')
            header = next(reader)  # Read and store the header
            rows = list(reader)

        with mp.Pool(processes=mp.cpu_count()) as pool:
            results = pool.map(process_file, rows)

        valid_results = [result for result in results if result[0] is not None]

        with open(output_file, 'w', newline='') as outfile:
            writer = csv.writer(outfile, delimiter='|')
            writer.writerow(header)  # Write the header to the output file
            for row, duration in valid_results:
                writer.writerow(row)
                total_duration += duration
                valid_entries.append(row)

        with open(log_file, 'w') as logfile:
            logfile.write(f"Total dataset duration: {timedelta(seconds=total_duration)}\n")

        print(f"Total dataset duration: {timedelta(seconds=total_duration)}")

    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())

    return len(valid_entries), total_duration

if __name__ == '__main__':
    input_file = 'dataset.csv'  # Changed to .csv extension
    output_file = 'dataset_min_3s.csv'  # Changed to .csv extension
    log_file = 'dataset_log.txt'

    valid_count, total_time = process_dataset(input_file, output_file, log_file)
    print(f"Number of valid entries: {valid_count}")
    print(f"Total dataset time: {timedelta(seconds=total_time)}") the dataset shall be saved continually also the progress of total wav files vs already processed files shall be displayed as progressbar, this progressbar shall be visible also from nohup log and also from some other log, show me how to run and read it so i can see the progress bar,  this is the example from the dataset the code shall read the wav files in it and check them according to conditions and if they match it shall copy the data into new dataset continuously audio_file|text|speaker_name
/mnt/b08aeed0-48f9-48bd-b344-d51c8793957d/GIGASPEECH/gigaspeech_download/data/audio_dataset/POD0000010358_S0000011.wav|ON TUESDAY <COMMA>|N/A
/mnt/b08aeed0-48f9-48bd-b344-d51c8793957d/GIGASPEECH/gigaspeech_download/data/audio_dataset/YOU0000017189_S0000118.wav|THEN <COMMA> WHEN YOU'RE READY <COMMA> HEAD TO THE KITCHEN <PERIOD>|N/A
/mnt/b08aeed0-48f9-48bd-b344-d51c8793957d/GIGASPEECH/gigaspeech_download/data/audio_dataset/YOU0000000080_S0001039.wav|AND I'M HERE ONCE AGAIN REPRESENTING THE CONCERNED CITIZENS OF SNOW CAMP <COMMA>|N/A
/mnt/b08aeed0-48f9-48bd-b344-d51c8793957d/GIGASPEECH/gigaspeech_download/data/audio_dataset/AUD0000000345_S0006867.wav|IT WAS NOT ILL-HUMOUR EXACTLY IT WAS NOT DISPLEASURE OR IF <COMMA> IT WAS DISPLEASURE AT HERSELF <COMMA>|N/A
/mnt/b08aeed0-48f9-48bd-b344-d51c8793957d/GIGASPEECH/gigaspeech_download/data/audio_dataset/AUD0000001849_S0006952.wav|PERSISTED NANNY <COMMA>|N/A