import os
import subprocess
import librosa

def get_duration(file_path):
    return librosa.get_duration(filename=file_path)

def process_files(txt_file, wav_folder):
    """Process the files in a text file."""
    with open(txt_file, "r") as f:
        lines = f.readlines()

    new_lines = []
    for line in lines:
        wav_file = line.split("|")[0]  # This line has been changed
        file_path = wav_file
        if not os.path.exists(file_path):
            print(f"File not found: {file_path}")
            continue
        duration = get_duration(file_path)
        print(f'Processing: {file_path}, Duration: {duration}')
        if duration > 12:
            print(f'Deleting: {file_path}')
            os.remove(file_path)
        else:
            new_lines.append(line)

    with open(txt_file, "w") as f:
        f.writelines(new_lines)

def main():
    process_files("train.txt", "wavs")
    process_files("valid.txt", "wavs")

if __name__ == "__main__":
    main()
