import os import subprocess import csv import shutil from webvtt import WebVTT import…
- User
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)
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.