2026-10-30 00:23:30 +02:00
|
|
|
|
"""Split a voice batch against word timestamps; reject missing script words.
|
|
|
|
|
|
|
2026-10-30 00:43:57 +02:00
|
|
|
|
Usage: python split_voice_batch.py AUDIO WORDS_JSON LINES_JSON OUTPUT_DIRECTORY [SCORE_JSON]
|
2026-10-30 00:23:30 +02:00
|
|
|
|
Requires soundfile (the local build helper directory is also supported).
|
|
|
|
|
|
"""
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
import difflib
|
|
|
|
|
|
import json
|
|
|
|
|
|
import re
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
sys.path.insert(0, str(ROOT / "builds/python_audio"))
|
|
|
|
|
|
import soundfile as sf
|
|
|
|
|
|
|
|
|
|
|
|
DIGITS = dict(zip("0123456789", "ноль один два три четыре пять шесть семь восемь девять".split()))
|
|
|
|
|
|
|
|
|
|
|
|
def tokens(text):
|
|
|
|
|
|
result = []
|
|
|
|
|
|
for token in re.findall(r"[а-яёa-z0-9]+", text.lower().replace("ё", "е")):
|
|
|
|
|
|
result.extend([DIGITS[d] for d in token] if token.isdigit() else [token])
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
audio_path, words_path, lines_path, output_path = map(Path, sys.argv[1:5])
|
|
|
|
|
|
data = json.loads(words_path.read_text(encoding="utf-8"))
|
|
|
|
|
|
words = data["words"] if isinstance(data, dict) else data
|
|
|
|
|
|
lines = json.loads(lines_path.read_text(encoding="utf-8"))
|
2026-10-30 00:43:57 +02:00
|
|
|
|
pronunciations = {}
|
|
|
|
|
|
if len(sys.argv) > 5:
|
|
|
|
|
|
score = json.loads(Path(sys.argv[5]).read_text(encoding="utf-8"))
|
|
|
|
|
|
pronunciations = score["pronunciation"]
|
2026-10-30 00:23:30 +02:00
|
|
|
|
spoken, timing = [], []
|
2026-10-30 00:43:57 +02:00
|
|
|
|
in_tag = False
|
2026-10-30 00:23:30 +02:00
|
|
|
|
for word in words:
|
|
|
|
|
|
if word.get("type", "word") != "word":
|
|
|
|
|
|
continue
|
2026-10-30 00:43:57 +02:00
|
|
|
|
text = word["text"]
|
|
|
|
|
|
# The provider may retain direction tags and IPA in its timed output.
|
|
|
|
|
|
# Tags are metadata, not dialogue; map IPA back to the caption word.
|
|
|
|
|
|
if text.startswith("[") or in_tag:
|
|
|
|
|
|
in_tag = "]" not in text
|
|
|
|
|
|
continue
|
|
|
|
|
|
for spelling, ipa in pronunciations.items():
|
|
|
|
|
|
text = text.replace(ipa, spelling)
|
|
|
|
|
|
for token in tokens(text):
|
2026-10-30 00:23:30 +02:00
|
|
|
|
spoken.append(token)
|
|
|
|
|
|
timing.append((float(word["start"]), float(word["end"])))
|
|
|
|
|
|
reference, spans = [], []
|
|
|
|
|
|
for line in lines:
|
|
|
|
|
|
start = len(reference)
|
|
|
|
|
|
reference.extend(tokens(line["text"]))
|
|
|
|
|
|
spans.append((start, len(reference)))
|
|
|
|
|
|
aligned = {}
|
|
|
|
|
|
matcher = difflib.SequenceMatcher(None, reference, spoken, autojunk=False)
|
|
|
|
|
|
for a, b, length in matcher.get_matching_blocks():
|
|
|
|
|
|
for i in range(length):
|
|
|
|
|
|
aligned[a + i] = b + i
|
|
|
|
|
|
mismatches = []
|
|
|
|
|
|
for tag, a, end_a, b, end_b in matcher.get_opcodes():
|
|
|
|
|
|
if tag != "equal":
|
|
|
|
|
|
mismatches.append(dict(kind=tag, reference=" ".join(reference[a:end_a]),
|
|
|
|
|
|
spoken=" ".join(spoken[b:end_b]), at_reference=a))
|
|
|
|
|
|
print(json.dumps({"reference_words": len(reference), "spoken_words": len(spoken),
|
|
|
|
|
|
"matched": len(aligned), "mismatches": mismatches}, ensure_ascii=False))
|
|
|
|
|
|
missing = [(line["id"], " ".join(reference[a:b])) for line, (a, b) in zip(lines, spans)
|
|
|
|
|
|
if any(i not in aligned for i in range(a, b))]
|
|
|
|
|
|
if missing:
|
|
|
|
|
|
raise SystemExit("Missing script words; no clips written: " + repr(missing))
|
|
|
|
|
|
boundaries = {point for span in spans for point in span}
|
|
|
|
|
|
if any(m["kind"] == "insert" and m["at_reference"] not in boundaries for m in mismatches):
|
|
|
|
|
|
raise SystemExit("Unexpected words inside a line; review the transcript before exporting")
|
|
|
|
|
|
samples, rate = sf.read(audio_path, always_2d=True)
|
|
|
|
|
|
samples = samples.mean(axis=1)
|
|
|
|
|
|
output_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
report = []
|
|
|
|
|
|
for line, (a, b) in zip(lines, spans):
|
|
|
|
|
|
first, last = aligned[a], aligned[b - 1]
|
|
|
|
|
|
start = max(0, timing[first][0] - .10)
|
|
|
|
|
|
end = min(len(samples) / rate, timing[last][1] + .16)
|
|
|
|
|
|
if first > 0:
|
|
|
|
|
|
start = max(start, (timing[first - 1][1] + timing[first][0]) / 2)
|
|
|
|
|
|
if last + 1 < len(timing):
|
|
|
|
|
|
end = min(end, (timing[last][1] + timing[last + 1][0]) / 2)
|
|
|
|
|
|
clip = samples[round(start * rate):round(end * rate)].copy()
|
|
|
|
|
|
assert len(clip) > rate * .2, line["id"]
|
|
|
|
|
|
fade = min(round(.005 * rate), len(clip) // 4)
|
|
|
|
|
|
for i in range(fade):
|
|
|
|
|
|
clip[i] *= i / fade
|
|
|
|
|
|
clip[-i - 1] *= i / fade
|
|
|
|
|
|
peak = max(abs(clip))
|
|
|
|
|
|
if peak > .98:
|
|
|
|
|
|
clip *= .98 / peak
|
|
|
|
|
|
target = output_path / (line["id"] + ".wav")
|
|
|
|
|
|
sf.write(target, clip, rate, subtype="PCM_16")
|
|
|
|
|
|
report.append(dict(id=line["id"], text=line["text"], start=round(start, 3),
|
|
|
|
|
|
end=round(end, 3), duration=round(len(clip) / rate, 3),
|
|
|
|
|
|
reference_words=b - a, matched_words=b - a, file=target.name))
|
|
|
|
|
|
(output_path / "alignment.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
|
|
|
|
print(f"Exported {len(report)} clips; every script word aligned")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
main()
|