|
import pysrt |
|
import os |
|
from pydub import AudioSegment |
|
|
|
|
|
def ensure_dir(directory): |
|
if not os.path.exists(directory): |
|
os.makedirs(directory) |
|
|
|
|
|
def find_unique_combo(): |
|
for file in os.listdir(): |
|
if file.endswith(".srt"): |
|
srt_file = file |
|
wav_file = file[:-4] + ".wav" |
|
if os.path.exists(wav_file): |
|
return srt_file, wav_file |
|
return None, None |
|
|
|
|
|
srt_file, wav_file = find_unique_combo() |
|
|
|
if srt_file and wav_file: |
|
|
|
subs = pysrt.open(srt_file) |
|
|
|
audio = AudioSegment.from_wav(wav_file) |
|
|
|
|
|
base_dir = "LJ_Speech_dataset" |
|
|
|
speaker_audios_texts = {} |
|
|
|
|
|
for sub in subs: |
|
start_time = (sub.start.hours * 3600 + sub.start.minutes * 60 + sub.start.seconds) * 1000 + sub.start.milliseconds |
|
end_time = (sub.end.hours * 3600 + sub.end.minutes * 60 + sub.end.seconds) * 1000 + sub.end.milliseconds |
|
|
|
|
|
speaker_text = sub.text.split(':') |
|
if len(speaker_text) > 1: |
|
speaker = speaker_text[0].strip() |
|
text = ':'.join(speaker_text[1:]).strip() |
|
segment = audio[start_time:end_time] |
|
|
|
|
|
if speaker not in speaker_audios_texts: |
|
speaker_audios_texts[speaker] = [] |
|
speaker_audios_texts[speaker].append((segment, text)) |
|
|
|
|
|
for speaker, segments_texts in speaker_audios_texts.items(): |
|
speaker_dir = os.path.join(base_dir, speaker.replace(' ', '_')) |
|
ensure_dir(speaker_dir) |
|
|
|
metadata_lines = [] |
|
for i, (segment, text) in enumerate(segments_texts, start=1): |
|
filename = f"{speaker.replace(' ', '_')}_{i:03}.wav" |
|
filepath = os.path.join(speaker_dir, filename) |
|
segment.export(filepath, format="wav") |
|
|
|
|
|
metadata_lines.append(f"{filename[:-4]}|{speaker}|{text}") |
|
|
|
|
|
metadata_file = os.path.join(speaker_dir, "metadata.csv") |
|
with open(metadata_file, "w", encoding="utf-8") as f: |
|
f.write("\n".join(metadata_lines)) |
|
|
|
print(f"Exported files and metadata for {speaker}") |
|
|
|
|
|
handled_dir = "handled" |
|
ensure_dir(handled_dir) |
|
os.rename(srt_file, os.path.join(handled_dir, srt_file)) |
|
os.rename(wav_file, os.path.join(handled_dir, wav_file)) |
|
|
|
print(f"Moved {srt_file} and {wav_file} to the 'handled' subfolder.") |
|
else: |
|
print("No unique SRT and WAV combo") |
|
|