1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# -*- coding: utf-8 -*-
"""Picks the best already synthesized candidate again, comparing numbers as words."""
import json, os, shutil, sys, wave
from difflib import SequenceMatcher
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "scripts"))
from narration import SCRIPTS
from verify import norm
def dur(p):
with wave.open(p) as w:
return w.getnframes() / w.getframerate()
def main(name, idxs):
work = os.path.join(ROOT, "tmp", name + "_rg")
outdir = os.path.join(ROOT, "audio", name)
data = json.load(open(os.path.join(outdir, "segments.json"), encoding="utf-8"))
segs = SCRIPTS[name]["segments"]
for i in idxs:
cands = []
for k in range(10):
w = os.path.join(work, f"{i:02d}_{k}.wav")
t = os.path.join(work, f"{i:02d}_{k}.txt")
if os.path.exists(w) and os.path.exists(t):
got = open(t, encoding="utf-8").read().strip()
r = SequenceMatcher(None, norm(segs[i][0]), norm(got)).ratio()
cands.append((r, k, w, got))
if not cands:
print(f" {i:02d} no candidates"); continue
cands.sort(reverse=True)
for r, k, w, got in cands:
print(f" {i:02d} seed{k} r={r:.2f} {dur(w):.2f}s | {got[:72]}")
r, k, w, got = cands[0]
shutil.copy(w, os.path.join(outdir, f"{i:02d}.wav"))
data["segments"][i].update(tts=segs[i][0], sub=segs[i][1],
dur=round(dur(w), 3), score=round(r, 3))
print(f" -> {i:02d} elegido seed{k} r={r:.2f}\n")
data["total_audio"] = round(sum(s["dur"] for s in data["segments"]), 2)
json.dump(data, open(os.path.join(outdir, "segments.json"), "w", encoding="utf-8"),
ensure_ascii=False, indent=2)
print(f"[{name}] audio={data['total_audio']}s")
if __name__ == "__main__":
main(sys.argv[1], [int(x) for x in sys.argv[2:]])
|