import os

import subprocess

import csv

import shutil

from webvtt import WebVTT

import unicodedata

import re

def normalize_czech(text):

# Normalize Unicode characters

text = unicodedata.normalize('NFKD', text)

# Remove non-ASCII characters

text = text.encode('ASCII', 'ignore').decode('ASCII')

# Remove spaces

text = text.replace(' ', '')

return text

def cut_audio(wav_path, vtt_path, output_dir, csv_writer, speaker_name):

# Ensure the output directory exists

if os.path.exists(output_dir):

shutil.rmtree(output_dir) # Delete the existing folder

os.makedirs(output_dir)

# Load the VTT file

vtt = WebVTT().read(vtt_path)

# Loop through each caption in the VTT file and extract timestamps

for i, caption in enumerate(vtt):

start = caption.start

end = caption.end

text = caption.text.strip()

# Convert time format from VTT (hh:mm:ss.ms) to ffmpeg format (seconds)

start_sec = convert_to_seconds(start)

end_sec = convert_to_seconds(end)

duration = end_sec - start_sec

# Skip if the audio duration is not within the desired range (3.2 to 11.55 seconds)

if duration < 3.2 or duration > 11.55:

continue

# Define the output path for the audio segment

output_file = os.path.join(output_dir, f'{os.path.basename(output_dir)}_{i+1}.wav')

# Use ffmpeg to cut the audio

cmd = [

'ffmpeg', '-i', wav_path,

'-ss', str(start_sec), '-to', str(end_sec),

'-c', 'copy', output_file

 ]

# Run the command

subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

# Write the entry to the CSV file

csv_writer.writerow([output_file, text, speaker_name])

def convert_to_seconds(timestamp):

# Convert a VTT timestamp (hh:mm:ss.ms) to total seconds

hours, minutes, seconds = timestamp.split(':')

seconds, milliseconds = seconds.split('.')

total_seconds = int(hours)  3600 + int(minutes)  60 + int(seconds) + float(milliseconds) / 1000

return total_seconds

def process_directory(directory, csv_file_path):

# Delete the old CSV file if it exists

if os.path.exists(csv_file_path):

os.remove(csv_file_path)

# Open the CSV file for writing

with open(csv_file_path, mode='w', newline='', encoding='utf-8') as csv_file:

csv_writer = csv.writer(csv_file, delimiter='|')

# Write the header row

csv_writer.writerow(['audio_file', 'text', 'speaker_name'])

# Loop through all .wav files in the directory

for file in os.listdir(directory):

if file.endswith('.wav'):

wav_path = os.path.join(directory, file)

vtt_path = os.path.join(directory, file.replace('.wav', '.vtt'))

# Check if the corresponding VTT file exists

if os.path.exists(vtt_path):

# Normalize the folder name

normalized_name = normalize_czech(file.replace('.wav', ''))

output_dir = os.path.join(directory, normalized_name)

speaker_name = 'Random' # Use 'Random' as speaker_name

cut_audio(wav_path, vtt_path, output_dir, csv_writer, speaker_name)

if name == "__main__":

# Specify the directory containing the .wav and .vtt files

directory = '.'

# Specify the output CSV file path

csv_file_path = os.path.join(directory, 'audio_dataset.csv')

# Process the directory

process_directory(directory, csv_file_path) in the resulting wav files there are two problems that i need to solve: 1. there are empty spaces without speech at the beginning or the end of the audio, I need to cut them out but with high precision, I have gpu so you can implement some best machine learning pytorch model to do this, most recent one or some state of the art technique to solve this problem for each individual wav, although the wav that don't have silences shall not be affected! 2. the are some short bumping noises at the end of some wav files where the audio cutting is not precise and cuts the beginning of the next word which only appears in the next wav recording, implement some clever technique or algorithm that would cut these small (they are in milliseconds probably) pieces, the technique must be precise and very robust