116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
import os
|
|
import json
|
|
import soundfile as sf
|
|
import numpy as np
|
|
import torchaudio
|
|
import torch
|
|
from faster_whisper import WhisperModel
|
|
from datasets import Dataset
|
|
|
|
RAW_DIR = r"C:\GitHub\VocoLoom\cleaned_dataset"
|
|
OUT_DIR = r"C:\GitHub\VocoLoom\f5_dataset"
|
|
WAV_OUT = os.path.join(OUT_DIR, "wavs")
|
|
os.makedirs(WAV_OUT, exist_ok=True)
|
|
|
|
print("Loading Whisper model...")
|
|
model = WhisperModel("tiny", device="cpu")
|
|
|
|
records = []
|
|
durations = []
|
|
clip_idx = 0
|
|
|
|
wav_files = sorted([f for f in os.listdir(RAW_DIR) if f.endswith(".wav")])
|
|
print(f"Slicing and transcribing {len(wav_files)} cleaned audio files...")
|
|
|
|
for fname in wav_files:
|
|
full_path = os.path.join(RAW_DIR, fname)
|
|
audio, sr = sf.read(full_path)
|
|
|
|
# Ensure mono
|
|
if len(audio.shape) > 1:
|
|
audio = np.mean(audio, axis=1)
|
|
|
|
segs, _ = model.transcribe(full_path, language="de")
|
|
segs = list(segs)
|
|
|
|
# Group segments into 4 - 15 second slices
|
|
curr_start = None
|
|
curr_end = None
|
|
curr_texts = []
|
|
|
|
for s in segs:
|
|
text = s.text.strip()
|
|
if not text:
|
|
continue
|
|
|
|
if curr_start is None:
|
|
curr_start = s.start
|
|
curr_end = s.end
|
|
curr_texts = [text]
|
|
else:
|
|
# If adding this segment exceeds 14 seconds, finalize previous slice
|
|
if (s.end - curr_start) > 14.0:
|
|
# Save slice
|
|
dur = curr_end - curr_start
|
|
if dur >= 1.5:
|
|
s_idx = int(curr_start * sr)
|
|
e_idx = int(curr_end * sr)
|
|
slice_audio = audio[s_idx:e_idx]
|
|
|
|
clip_name = f"clip_{clip_idx:04d}.wav"
|
|
clip_path = os.path.join(WAV_OUT, clip_name)
|
|
sf.write(clip_path, slice_audio, sr)
|
|
|
|
full_text = " ".join(curr_texts)
|
|
records.append({
|
|
"audio_path": clip_path,
|
|
"text": full_text,
|
|
"duration": round(dur, 2)
|
|
})
|
|
durations.append(round(dur, 2))
|
|
clip_idx += 1
|
|
|
|
curr_start = s.start
|
|
curr_end = s.end
|
|
curr_texts = [text]
|
|
else:
|
|
curr_end = s.end
|
|
curr_texts.append(text)
|
|
|
|
# Finalize remaining slice
|
|
if curr_start is not None:
|
|
dur = curr_end - curr_start
|
|
if dur >= 1.5:
|
|
s_idx = int(curr_start * sr)
|
|
e_idx = int(curr_end * sr)
|
|
slice_audio = audio[s_idx:e_idx]
|
|
|
|
clip_name = f"clip_{clip_idx:04d}.wav"
|
|
clip_path = os.path.join(WAV_OUT, clip_name)
|
|
sf.write(clip_path, slice_audio, sr)
|
|
|
|
full_text = " ".join(curr_texts)
|
|
records.append({
|
|
"audio_path": clip_path,
|
|
"text": full_text,
|
|
"duration": round(dur, 2)
|
|
})
|
|
durations.append(round(dur, 2))
|
|
clip_idx += 1
|
|
|
|
print(f"Created {len(records)} optimal audio clips (all between 1.5s and 14s)!")
|
|
|
|
# Save Arrow Dataset
|
|
ds = Dataset.from_list(records)
|
|
ds.to_file(os.path.join(OUT_DIR, "raw.arrow"))
|
|
|
|
# Save duration.json
|
|
with open(os.path.join(OUT_DIR, "duration.json"), "w", encoding="utf-8") as f:
|
|
json.dump({"duration": durations}, f, indent=2)
|
|
|
|
# Save metadata.json
|
|
with open(os.path.join(OUT_DIR, "metadata.json"), "w", encoding="utf-8") as f:
|
|
json.dump(records, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"[OK] F5 dataset ready! Total duration: {sum(durations)/60:.2f} minutes across {len(records)} slices.")
|