Files
workinf_Blender_Wasm/tools/web/generate-re-pbr-narration.py
mes123456 63db6caf1f
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
feat(web): studio worlds, camera-view/offscreen environment parity, material and undo coverage
Environment lighting:
- ship all eight desktop studiolights/world datasets (WBENV1 float32) and a
  shared loader used by both the main viewport and the offscreen worker
- add the N-panel "studio 环境" selector
- rotate the world background together with the environment
  (backgroundRotation/backgroundIntensity), so camera view and offscreen
  renders match the desktop Mapping-node behaviour

Verification (Chromium, OPFS, NOT_CLAIMED):
- environment lighting + 8 world pixel identity
- camera-view rotation correspondence (camera matrices, 207 samples)
- offscreen vs on-screen background parity after hiding geometry (1305 samples)
- metals case extended with material-field OPFS roundtrip
- material Principled edits and rename undo/redo
- eight studio worlds: pairwise frame differences + luminance rank correlation
- undo history depth capped at 64 steps and undo across OPFS save/reopen

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 09:27:47 +08:00

201 lines
9.9 KiB
Python
Raw 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.
#!/usr/bin/env python3
"""Prepare Chinese narration and mix it into the RE 5000PBR lesson video.
Usage:
python3 generator.py --mode prepare \
--chapters /home/.../tools/web/re-pbr-video-chapters.json \
--out /home/.../tests/golden/RE-PBR-VIDEO-001
python3 generator.py --mode mix \
--out /home/.../tests/golden/RE-PBR-VIDEO-001 \
--video /home/.../web/app/public/tutorial/pbr/lesson/video/re-pbr-lesson-720p.mp4
"""
import argparse
import hashlib
import json
import pathlib
import re
import subprocess
import wave
EDGE_TTS = pathlib.Path("/home/mes123456/workinf_Blender_Wasm/tools/web/.tts-venv/bin/edge-tts")
FFMPEG = pathlib.Path("/usr/bin/ffmpeg")
FFPROBE = pathlib.Path("/usr/bin/ffprobe")
SAMPLE_RATE = 48000
SAMPLE_WIDTH = 2
CHANNELS = 1
def run(command, **kwargs):
return subprocess.run(command, check=True, capture_output=True, text=True, **kwargs)
def sha256(path):
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def prepare(chapters_path, output):
chapters = json.loads(chapters_path.read_text(encoding="utf-8"))
voice = chapters.get("voice", "zh-CN-XiaoxiaoNeural")
audio_dir = output / "narration"
audio_dir.mkdir(parents=True, exist_ok=True)
prepared = []
for chapter in chapters["chapters"]:
chapter_id = chapter["id"]
mp3 = audio_dir / f"{chapter_id}.mp3"
wav = audio_dir / f"{chapter_id}.wav"
# Cache on the spoken text, not on file existence: editing a chapter's
# narration must re-synthesize instead of silently reusing stale audio.
text_stamp = audio_dir / f"{chapter_id}.text.sha256"
digest = hashlib.sha256(f"{voice}\n{chapter['narration']}".encode("utf-8")).hexdigest()
if not mp3.exists() or not text_stamp.exists() or text_stamp.read_text(encoding="utf-8").strip() != digest:
mp3.unlink(missing_ok=True)
run([str(EDGE_TTS), "--voice", voice, "--text", chapter["narration"], "--write-media", str(mp3)])
text_stamp.write_text(digest + "\n", encoding="utf-8")
if not wav.exists() or wav.stat().st_mtime < mp3.stat().st_mtime:
run([str(FFMPEG), "-y", "-hide_banner", "-loglevel", "error", "-i", str(mp3),
"-ar", str(SAMPLE_RATE), "-ac", str(CHANNELS), "-c:a", "pcm_s16le", str(wav)])
duration = float(run([str(FFPROBE), "-v", "error", "-show_entries", "format=duration",
"-of", "default=nw=1:nk=1", str(wav)]).stdout.strip())
prepared.append({
"id": chapter_id,
"title": chapter["title"],
"narration": chapter["narration"],
"mp3": str(mp3),
"wav": str(wav),
"durationMs": int(round(duration * 1000)),
})
result = {"voice": voice, "chapters": prepared}
(output / "narration.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
total = sum(item["durationMs"] for item in prepared)
print(json.dumps({"status": "PREPARED", "voice": voice, "chapters": len(prepared), "narrationMs": total}, ensure_ascii=False))
def read_pcm(path):
with wave.open(str(path), "rb") as handle:
if handle.getnchannels() != CHANNELS or handle.getsampwidth() != SAMPLE_WIDTH or handle.getframerate() != SAMPLE_RATE:
raise SystemExit(f"unexpected WAV format: {path}")
return handle.readframes(handle.getnframes())
def overlay(buffer, samples, offset_samples):
for index in range(0, len(samples), 2):
target = (offset_samples * 2) + index
if target + 1 >= len(buffer):
break
left = int.from_bytes(buffer[target:target + 2], "little", signed=True)
right = int.from_bytes(samples[index:index + 2], "little", signed=True)
mixed = max(-32768, min(32767, left + right))
buffer[target:target + 2] = mixed.to_bytes(2, "little", signed=True)
def timestamp(milliseconds):
seconds, ms = divmod(max(0, int(milliseconds)), 1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{ms:03d}"
def mix(output, video, poster_ms=1000, scale="1280:720", poster_path=None):
timeline_path = output / "timeline.json"
narration_path = output / "narration.json"
if not timeline_path.exists():
raise SystemExit(f"missing recording timeline: {timeline_path}")
if not narration_path.exists():
raise SystemExit(f"missing narration: {narration_path}")
timeline = json.loads(timeline_path.read_text(encoding="utf-8"))
narration = json.loads(narration_path.read_text(encoding="utf-8"))
by_id = {chapter["id"]: chapter for chapter in narration["chapters"]}
total_ms = int(timeline.get("durationMs", 0))
for chapter in timeline.get("chapters", []):
total_ms = max(total_ms, int(chapter.get("endMs", 0)) + 800)
total_samples = int(total_ms / 1000 * SAMPLE_RATE)
buffer = bytearray(total_samples * 2)
cues = []
for chapter in timeline.get("chapters", []):
prepared = by_id.get(chapter["id"])
if not prepared:
continue
start_ms = int(chapter.get("startMs", 0))
samples = read_pcm(pathlib.Path(prepared["wav"]))
overlay(buffer, samples, int(start_ms / 1000 * SAMPLE_RATE))
cues.append({
"id": chapter["id"],
"title": prepared["title"],
"startMs": start_ms,
"endMs": start_ms + prepared["durationMs"],
"narration": prepared["narration"],
})
narration_wav = output / "narration.wav"
with wave.open(str(narration_wav), "wb") as handle:
handle.setnchannels(CHANNELS)
handle.setsampwidth(SAMPLE_WIDTH)
handle.setframerate(SAMPLE_RATE)
handle.writeframes(bytes(buffer))
vtt = ["WEBVTT", ""]
for cue in cues:
vtt.extend([f"{timestamp(cue['startMs'])} --> {timestamp(cue['endMs'])}", f"{cue['title']}:{cue['narration']}", ""])
vtt_path = video.with_name(re.sub(r"-\d+p$", "", video.stem) + ".zh-Hans.vtt")
vtt_path.write_text("\n".join(vtt), encoding="utf-8")
video.parent.mkdir(parents=True, exist_ok=True)
webm_seconds = float(run([str(FFPROBE), "-v", "error", "-show_entries", "format=duration",
"-of", "default=nw=1:nk=1", str(output / "screencast.webm")]).stdout.strip())
# -shortest silently cuts to whichever input ends first. On a long screencast
# whose narration bed is shorter, that drops whole trailing chapters without
# any error (measured: 230.56 s webm -> 187.007 s output). Mux to an explicit
# target length instead, then verify the result actually matches it.
target_seconds = min(webm_seconds, total_samples / SAMPLE_RATE)
run([str(FFMPEG), "-y", "-hide_banner", "-loglevel", "error", "-i", str(output / "screencast.webm"),
"-i", str(narration_wav), "-vf", f"scale={scale}", "-c:v", "libx264", "-crf", "23", "-preset", "medium",
"-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", "-t", f"{target_seconds:.3f}", str(video)])
muxed_seconds = float(run([str(FFPROBE), "-v", "error", "-show_entries", "format=duration",
"-of", "default=nw=1:nk=1", str(video)]).stdout.strip())
if abs(muxed_seconds - target_seconds) > 2.0:
raise SystemExit(f"mux duration mismatch: target {target_seconds:.3f}s, got {muxed_seconds:.3f}s")
poster = pathlib.Path(poster_path) if poster_path else video.parent / "poster.jpg"
poster_ms = max(0, min(int(poster_ms), max(0, total_ms - 2000)))
run([str(FFMPEG), "-y", "-hide_banner", "-loglevel", "error", "-ss", f"{poster_ms / 1000:.3f}", "-i", str(video),
"-frames:v", "1", "-vf", f"scale={scale}", str(poster)])
probe = run([str(FFPROBE), "-v", "error", "-show_entries",
"format=duration,size:stream=codec_type,codec_name,width,height", "-of", "json", str(video)])
(output / "video-probe.json").write_text(probe.stdout + "\n", encoding="utf-8")
report = {
"schemaVersion": 1,
"status": "PASS",
"voice": narration.get("voice"),
"video": {"path": str(video), "sha256": sha256(video), "bytes": video.stat().st_size},
"narration": {"path": str(narration_wav), "sha256": sha256(narration_wav), "bytes": narration_wav.stat().st_size},
"vtt": {"path": str(vtt_path), "sha256": sha256(vtt_path), "cues": len(cues)},
"poster": {"path": str(poster), "sha256": sha256(poster)},
"chapters": cues,
}
(output / "video-report.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(json.dumps({"status": "MIXED", "video": str(video), "bytes": video.stat().st_size,
"duration": json.loads(probe.stdout).get("format", {}).get("duration"), "cues": len(cues)}, ensure_ascii=False))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--mode", choices=["prepare", "mix"], required=True)
parser.add_argument("--chapters", default="/home/mes123456/workinf_Blender_Wasm/tools/web/re-pbr-video-chapters.json")
parser.add_argument("--out", default="/home/mes123456/workinf_Blender_Wasm/tests/golden/RE-PBR-VIDEO-001")
parser.add_argument("--video", default="/home/mes123456/workinf_Blender_Wasm/web/app/public/tutorial/pbr/lesson/video/re-pbr-lesson-720p.mp4")
parser.add_argument("--poster-ms", type=int, default=1000)
parser.add_argument("--scale", default="1280:720", help="ffmpeg output scale, e.g. 1920:1080")
parser.add_argument("--poster", default=None, help="poster output path; defaults to <video dir>/poster.jpg")
args = parser.parse_args()
output = pathlib.Path(args.out)
output.mkdir(parents=True, exist_ok=True)
if args.mode == "prepare":
prepare(pathlib.Path(args.chapters), output)
else:
mix(output, pathlib.Path(args.video), args.poster_ms, args.scale, args.poster)
if __name__ == "__main__":
main()