Files

281 lines
9.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
Text 1 讲解视频 —— 配音与时间轴生成
流程:
1. 解析 讲解脚本_Text1.md(## 场景key | 场景名 + 以 "- " 开头的口播行)
2. 用 edge-tts 逐场景合成配音(走新端点 api.msedgeservices.com)
3. 用返回的 SentenceBoundary 时间戳,按字符对齐算出每一条字幕的精确起止
4. 输出 public/vo/<key>.mp3 与 src/timing.generated.ts
"""
import asyncio
import os
import re
import subprocess
import sys
import time as _time
# ---------- edge-tts 新端点补丁(2026-10 起旧域名返回 403)----------
import edge_tts
import edge_tts.constants as K
K.WSS_URL = (
"wss://api.msedgeservices.com/tts/cognitiveservices/websocket/v1"
f"?Ocp-Apim-Subscription-Key={K.TRUSTED_CLIENT_TOKEN}"
)
import edge_tts.communicate as CM
CM.WSS_URL = K.WSS_URL
HERE = os.path.dirname(os.path.abspath(__file__))
PROJ = os.path.join(HERE, "remotion-text1")
SCRIPT_MD = os.path.join(HERE, "讲解脚本_Text1.md")
VO_DIR = os.path.join(PROJ, "public", "vo")
OUT_TS = os.path.join(PROJ, "src", "timing.generated.ts")
FFMPEG = os.path.join(HERE, ".venv", "Lib", "site-packages",
"imageio_ffmpeg", "binaries", "ffmpeg-win-x86_64-v7.1.exe")
VOICE = "zh-CN-YunjianNeural"
RATE = "-4%"
FPS = 30
LEAD_PAD = 0 # 场景开头留白(帧)
TAIL_PAD = 0.7 # 场景结尾留白(秒),给转场淡出
# ---------------- 1. 解析脚本 ----------------
def parse_script(path):
scenes = []
cur = None
with open(path, encoding="utf-8") as f:
for raw in f:
line = raw.rstrip("\n")
if line.startswith("## "):
head = line[3:].strip()
if "|" in head:
key, label = [x.strip() for x in head.split("|", 1)]
else:
key, label = head, head
cur = {"key": key, "label": label, "lines": []}
scenes.append(cur)
elif line.startswith("- ") and cur is not None:
t = line[2:].strip()
if t:
cur["lines"].append(t)
return [s for s in scenes if s["lines"]]
# ---------------- 2. 时间戳对齐 ----------------
NORM_RE = re.compile(r"[^0-9A-Za-z\u4e00-\u9fff]+")
def norm(s: str) -> str:
"""只保留字母、数字与汉字,去掉空白与标点。"""
return NORM_RE.sub("", s).lower()
def build_char_map(full_text: str):
"""返回 (full_norm, 映射:原文字符下标 -> 归一化下标)。"""
out = []
idx_of = []
for ch in full_text:
n = norm(ch)
for _ in n:
idx_of.append(len(out))
if n:
out.append(n)
else:
idx_of.append(len(out)) # 标点/空白:映射到当前位置
idx_of.append(len(out))
return "".join(out), idx_of
def cue_times(full_text, cue_spans, boundaries, audio_dur):
"""把每条字幕的字符区间换算成时间(秒)。
cue_spans: [(start_char, end_char)] 相对 full_text
boundaries: [(norm_text, start_sec, end_sec)]
"""
full_norm, idx_of = build_char_map(full_text)
n = len(full_norm)
times = [None] * (n + 1)
p = 0
matched = 0
for btxt, bstart, bend in boundaries:
bn = norm(btxt)
if not bn:
continue
pos = full_norm.find(bn, max(0, p - 2))
if pos < 0:
pos = full_norm.find(bn)
if pos < 0:
continue
matched += len(bn)
L = len(bn)
for k in range(L):
t = bstart + (bend - bstart) * (k / L if L else 0)
gi = pos + k
if gi <= n and (times[gi] is None):
times[gi] = t
if pos + L <= n:
times[pos + L] = bend if times[pos + L] is None else times[pos + L]
p = pos + L
coverage = matched / n if n else 0
# 已知锚点线性插值补全
known = [i for i, t in enumerate(times) if t is not None]
if known:
if times[0] is None:
times[0] = 0.0
for a, b in zip(known, known[1:]):
if b - a > 1:
t0, t1 = times[a], times[b]
for k in range(a + 1, b):
times[k] = t0 + (t1 - t0) * (k - a) / (b - a)
tail = times[known[-1]]
for i in range(known[-1] + 1, n + 1):
times[i] = tail
out = []
for (cs, ce) in cue_spans:
ns = idx_of[max(0, min(cs, len(idx_of) - 1))]
ne = idx_of[max(0, min(ce, len(idx_of) - 1))]
t0 = times[min(ns, n)] if ns <= n else 0.0
t1 = times[min(ne, n)] if ne <= n else audio_dur
if t0 is None:
t0 = 0.0
if t1 is None or t1 <= t0:
t1 = min(audio_dur, t0 + 0.4)
out.append((max(0.0, t0), max(t0 + 0.2, t1)))
return out, coverage
# ---------------- 3. 音频工具 ----------------
def ffprobe_duration(path):
r = subprocess.run([FFMPEG, "-hide_banner", "-i", path],
capture_output=True, text=True, errors="replace")
m = re.search(r"Duration: (\d+):(\d+):(\d+\.\d+)", r.stderr)
if not m:
raise RuntimeError("无法读取时长: " + path + "\n" + r.stderr[-500:])
h, mi, s = m.group(1), m.group(2), m.group(3)
return int(h) * 3600 + int(mi) * 60 + float(s)
# ---------------- 4. 合成 ----------------
async def synth(text, out_path, tries=4):
last = None
for i in range(tries):
try:
c = edge_tts.Communicate(text, VOICE, rate=RATE)
buf = b""
bounds = []
async for ch in c.stream():
t = ch.get("type", "")
if t == "audio":
buf += ch["data"]
elif t.endswith("Boundary"):
bounds.append((ch.get("text", ""),
ch.get("offset", 0) / 1e7,
(ch.get("offset", 0) + ch.get("duration", 0)) / 1e7))
if len(buf) < 2000:
raise RuntimeError(f"音频过短 ({len(buf)} bytes)")
if not bounds:
raise RuntimeError("未收到时间戳事件")
with open(out_path, "wb") as f:
f.write(buf)
return bounds
except Exception as e: # noqa: BLE001
last = e
print(f" 重试 {i+1}/{tries}: {type(e).__name__} {str(e)[:80]}")
await asyncio.sleep(2.0 + i * 1.5)
raise RuntimeError(f"合成失败: {last}")
def ts_escape(s):
return s.replace("\\", "\\\\").replace("'", "\\'")
async def main():
scenes = parse_script(SCRIPT_MD)
os.makedirs(VO_DIR, exist_ok=True)
print(f"共 {len(scenes)} 个场景,语音={VOICE},语速={RATE}\n")
result = []
cursor_frames = 0
for idx, sc in enumerate(scenes, 1):
key, label, lines = sc["key"], sc["label"], sc["lines"]
full = "".join(lines)
# 每条字幕的字符区间
spans, p = [], 0
for ln in lines:
spans.append((p, p + len(ln)))
p += len(ln)
out_mp3 = os.path.join(VO_DIR, f"{key}.mp3")
print(f"[{idx:02d}/{len(scenes)}] {key:<11} {len(lines):>2} 句 / {len(full):>4} 字 ...")
t0 = _time.time()
bounds = await synth(full, out_mp3)
dur = ffprobe_duration(out_mp3)
cts, cov = cue_times(full, spans, bounds, dur)
print(f" 音频 {dur:.2f}s · 边界 {len(bounds)} · 对齐覆盖 {cov*100:.0f}% · 耗时 {_time.time()-t0:.1f}s")
start_frame = cursor_frames
scene_dur = round((dur + TAIL_PAD) * FPS)
cues = []
for (a, b), txt in zip(cts, lines):
cues.append({
"text": txt,
"start": start_frame + round(a * FPS),
"end": start_frame + round(min(b, dur) * FPS),
})
result.append({
"key": key, "label": label,
"audio": f"vo/{key}.mp3",
"start": start_frame, "dur": scene_dur,
"cues": cues,
})
cursor_frames = start_frame + scene_dur
# ---- 写 TS ----
L = []
L.append("// 本文件由 build_voice.py 自动生成,请勿手工修改。")
L.append("export type Cue = { text: string; start: number; end: number };")
L.append("export type SceneTiming = {")
L.append(" key: string; label: string; audio: string;")
L.append(" start: number; dur: number; cues: Cue[];")
L.append("};")
L.append("")
L.append("export const SCENES: SceneTiming[] = [")
for r in result:
L.append(" {")
L.append(f" key: '{ts_escape(r['key'])}',")
L.append(f" label: '{ts_escape(r['label'])}',")
L.append(f" audio: '{r['audio']}',")
L.append(f" start: {r['start']},")
L.append(f" dur: {r['dur']},")
L.append(" cues: [")
for c in r["cues"]:
L.append(" { text: '%s', start: %d, end: %d },"
% (ts_escape(c["text"]), c["start"], c["end"]))
L.append(" ],")
L.append(" },")
L.append("];")
L.append("")
L.append(f"export const TOTAL_FRAMES = {cursor_frames};")
L.append(f"export const TOTAL_SECONDS = {cursor_frames / FPS:.2f};")
L.append("")
with open(OUT_TS, "w", encoding="utf-8") as f:
f.write("\n".join(L))
print(f"\n完成:{len(result)} 个场景,总时长 {cursor_frames/FPS/60:.1f} 分钟({cursor_frames} 帧)")
print("输出:", OUT_TS)
for r in result:
print(f" {r['key']:<11} start={r['start']:>6} dur={r['dur']:>6} ({r['dur']/FPS:>5.1f}s) cues={len(r['cues'])}")
if __name__ == "__main__":
sys.exit(asyncio.run(main()))