Spaces:
Sleeping
Phase A: unified metadata schema + vgm CLI
Browse filesAdd a pydantic-backed PageMetadata schema (app/pipeline/metadata.py)
with KeyTerm and MathEquation models, plus dump/load/migrate helpers.
Existing browser-editor `.edits.json` is upgraded to schema v2 by
migrate_v1_to_v2 — old saves still load.
Extract the FastAPI handler body into a shared run_pipeline helper in
app/pipeline/orchestrator.py so the same orchestration is reused by the
new vgm CLI (typer-based). Subcommands: build, export-metadata,
render-from-metadata; the last one re-renders HTML from the JSON
exported by either the editor or the CLI.
Segment now carries optional key_terms / math_equations / include
fields and a new render_from_metadata function turns a PageMetadata
back into renderable Segments. render_guide / render_review accept
subtitle, module, mathjax, and mathjax_src so subsequent phases (math
equations, key terms) just need template + UI work.
Verified: all 30 pytest tests pass, including a round-trip test that
runs the full pipeline on smaller.m4v + M14_L3_S3.srt and confirms
render-from-metadata is byte-identical across two runs.
https://claude.ai/code/session_01G3XpEehfr9DnX9L7BahQ4L
- app/cli.py +192 -0
- app/main.py +33 -39
- app/pipeline/__init__.py +29 -1
- app/pipeline/html_gen.py +85 -1
- app/pipeline/metadata.py +161 -0
- app/pipeline/orchestrator.py +119 -0
- pyproject.toml +20 -0
- requirements.txt +1 -0
- tests/test_cli.py +110 -0
- tests/test_metadata.py +167 -0
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""`vgm` command-line interface.
|
| 2 |
+
|
| 3 |
+
Three subcommands:
|
| 4 |
+
- `build` — full pipeline: video (+ optional transcript) → HTML / zip / review HTML.
|
| 5 |
+
- `export-metadata` — run the pipeline and dump the unified metadata JSON.
|
| 6 |
+
- `render-from-metadata` — re-render HTML from a previously exported metadata JSON.
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import logging
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from typing import Optional
|
| 13 |
+
|
| 14 |
+
import typer
|
| 15 |
+
|
| 16 |
+
from .pipeline import bundle_zip, dump_metadata, load_metadata, render_guide, render_review
|
| 17 |
+
from .pipeline.html_gen import metadata_to_segment, render_from_metadata
|
| 18 |
+
from .pipeline.orchestrator import PipelineError, PipelineInputs, run_pipeline
|
| 19 |
+
|
| 20 |
+
app = typer.Typer(
|
| 21 |
+
add_completion=False,
|
| 22 |
+
help="VideoGuideMaker — generate WCAG-ready study guides from video + transcript.",
|
| 23 |
+
no_args_is_help=True,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
log = logging.getLogger("videoguidemaker.cli")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _setup_logging(verbose: bool) -> None:
|
| 30 |
+
logging.basicConfig(
|
| 31 |
+
level=logging.DEBUG if verbose else logging.INFO,
|
| 32 |
+
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _resolve_format(fmt: str, output: Path) -> str:
|
| 37 |
+
fmt = fmt.lower()
|
| 38 |
+
if fmt not in ("review", "single", "zip", "guide"):
|
| 39 |
+
raise typer.BadParameter("format must be one of: review, single, zip, guide")
|
| 40 |
+
return fmt
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _safe_filename(title: str) -> str:
|
| 44 |
+
safe = "".join(c if c.isalnum() or c in "-_ " else "-" for c in title).strip()
|
| 45 |
+
safe = safe.replace(" ", "-")
|
| 46 |
+
return safe or "study-guide"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.command()
|
| 50 |
+
def build(
|
| 51 |
+
video: Path = typer.Argument(..., exists=True, dir_okay=False, readable=True),
|
| 52 |
+
transcript: Optional[Path] = typer.Argument(None, exists=False, dir_okay=False),
|
| 53 |
+
title: str = typer.Option("Untitled Study Guide", "--title"),
|
| 54 |
+
subtitle: Optional[str] = typer.Option(None, "--subtitle"),
|
| 55 |
+
module: Optional[str] = typer.Option(None, "--module"),
|
| 56 |
+
output: Path = typer.Option(Path("study_guide.html"), "--output", "-o"),
|
| 57 |
+
frames_dir: Path = typer.Option(Path("frames"), "--frames-dir"),
|
| 58 |
+
auto_transcribe: bool = typer.Option(False, "--auto-transcribe"),
|
| 59 |
+
whisper_model: str = typer.Option("small", "--whisper-model"),
|
| 60 |
+
threshold: float = typer.Option(27.0, "--threshold"),
|
| 61 |
+
face_threshold: float = typer.Option(0.12, "--face-threshold"),
|
| 62 |
+
lang: str = typer.Option("en", "--lang"),
|
| 63 |
+
fmt: str = typer.Option("single", "--format", help="review | single | zip | guide"),
|
| 64 |
+
export_metadata: Optional[Path] = typer.Option(None, "--export-metadata"),
|
| 65 |
+
verbose: bool = typer.Option(False, "--verbose", "-v"),
|
| 66 |
+
) -> None:
|
| 67 |
+
"""Run the full pipeline: video + transcript → HTML."""
|
| 68 |
+
_setup_logging(verbose)
|
| 69 |
+
fmt = _resolve_format(fmt, output)
|
| 70 |
+
|
| 71 |
+
inputs = PipelineInputs(
|
| 72 |
+
video_path=video,
|
| 73 |
+
transcript_path=transcript if transcript and transcript.exists() else None,
|
| 74 |
+
frames_dir=frames_dir,
|
| 75 |
+
title=title,
|
| 76 |
+
subtitle=subtitle,
|
| 77 |
+
module=module,
|
| 78 |
+
lang=lang,
|
| 79 |
+
threshold=threshold,
|
| 80 |
+
face_threshold=face_threshold,
|
| 81 |
+
auto_transcribe=auto_transcribe,
|
| 82 |
+
whisper_model=whisper_model,
|
| 83 |
+
inline_images=(fmt in ("single", "review")),
|
| 84 |
+
)
|
| 85 |
+
try:
|
| 86 |
+
result = run_pipeline(inputs)
|
| 87 |
+
except PipelineError as exc:
|
| 88 |
+
typer.secho(f"error: {exc}", fg=typer.colors.RED, err=True)
|
| 89 |
+
raise typer.Exit(2)
|
| 90 |
+
|
| 91 |
+
common = dict(
|
| 92 |
+
title=title,
|
| 93 |
+
segments=result.segments,
|
| 94 |
+
lang=lang,
|
| 95 |
+
subtitle=subtitle,
|
| 96 |
+
module=module,
|
| 97 |
+
meta_lines=result.page.meta_lines or None,
|
| 98 |
+
eyebrow=result.page.eyebrow,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if fmt == "review":
|
| 102 |
+
html = render_review(**common)
|
| 103 |
+
else:
|
| 104 |
+
inline = fmt == "single"
|
| 105 |
+
html = render_guide(inline_images=inline, **common)
|
| 106 |
+
|
| 107 |
+
if fmt == "zip":
|
| 108 |
+
zip_bytes = bundle_zip(html, [f.image_path for f in result.kept_frames])
|
| 109 |
+
if output.suffix.lower() != ".zip":
|
| 110 |
+
output = output.with_suffix(".zip")
|
| 111 |
+
output.write_bytes(zip_bytes)
|
| 112 |
+
else:
|
| 113 |
+
output.write_text(html, encoding="utf-8")
|
| 114 |
+
|
| 115 |
+
typer.echo(f"wrote {output}")
|
| 116 |
+
|
| 117 |
+
if export_metadata:
|
| 118 |
+
dump_metadata(export_metadata, result.page)
|
| 119 |
+
typer.echo(f"wrote {export_metadata}")
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
@app.command("export-metadata")
|
| 123 |
+
def export_metadata_cmd(
|
| 124 |
+
video: Path = typer.Argument(..., exists=True, dir_okay=False, readable=True),
|
| 125 |
+
transcript: Optional[Path] = typer.Argument(None, exists=False, dir_okay=False),
|
| 126 |
+
title: str = typer.Option("Untitled Study Guide", "--title"),
|
| 127 |
+
subtitle: Optional[str] = typer.Option(None, "--subtitle"),
|
| 128 |
+
module: Optional[str] = typer.Option(None, "--module"),
|
| 129 |
+
output: Path = typer.Option(Path("study_guide_metadata.json"), "--output", "-o"),
|
| 130 |
+
frames_dir: Path = typer.Option(Path("frames"), "--frames-dir"),
|
| 131 |
+
auto_transcribe: bool = typer.Option(False, "--auto-transcribe"),
|
| 132 |
+
whisper_model: str = typer.Option("small", "--whisper-model"),
|
| 133 |
+
threshold: float = typer.Option(27.0, "--threshold"),
|
| 134 |
+
face_threshold: float = typer.Option(0.12, "--face-threshold"),
|
| 135 |
+
lang: str = typer.Option("en", "--lang"),
|
| 136 |
+
verbose: bool = typer.Option(False, "--verbose", "-v"),
|
| 137 |
+
) -> None:
|
| 138 |
+
"""Run the pipeline and dump the metadata JSON only (no HTML)."""
|
| 139 |
+
_setup_logging(verbose)
|
| 140 |
+
inputs = PipelineInputs(
|
| 141 |
+
video_path=video,
|
| 142 |
+
transcript_path=transcript if transcript and transcript.exists() else None,
|
| 143 |
+
frames_dir=frames_dir,
|
| 144 |
+
title=title,
|
| 145 |
+
subtitle=subtitle,
|
| 146 |
+
module=module,
|
| 147 |
+
lang=lang,
|
| 148 |
+
threshold=threshold,
|
| 149 |
+
face_threshold=face_threshold,
|
| 150 |
+
auto_transcribe=auto_transcribe,
|
| 151 |
+
whisper_model=whisper_model,
|
| 152 |
+
inline_images=False,
|
| 153 |
+
)
|
| 154 |
+
try:
|
| 155 |
+
result = run_pipeline(inputs)
|
| 156 |
+
except PipelineError as exc:
|
| 157 |
+
typer.secho(f"error: {exc}", fg=typer.colors.RED, err=True)
|
| 158 |
+
raise typer.Exit(2)
|
| 159 |
+
dump_metadata(output, result.page)
|
| 160 |
+
typer.echo(f"wrote {output} ({len(result.page.segments)} segments, frames in {frames_dir})")
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
@app.command("render-from-metadata")
|
| 164 |
+
def render_from_metadata_cmd(
|
| 165 |
+
metadata_json: Path = typer.Argument(..., exists=True, dir_okay=False, readable=True),
|
| 166 |
+
output: Path = typer.Option(Path("study_guide.html"), "--output", "-o"),
|
| 167 |
+
frames_dir: Optional[Path] = typer.Option(
|
| 168 |
+
None, "--frames-dir",
|
| 169 |
+
help="Override the frames_dir recorded in the metadata JSON.",
|
| 170 |
+
),
|
| 171 |
+
fmt: str = typer.Option("single", "--format", help="review | single | guide"),
|
| 172 |
+
verbose: bool = typer.Option(False, "--verbose", "-v"),
|
| 173 |
+
) -> None:
|
| 174 |
+
"""Re-render HTML from a previously exported metadata JSON."""
|
| 175 |
+
_setup_logging(verbose)
|
| 176 |
+
fmt = _resolve_format(fmt, output)
|
| 177 |
+
if fmt == "zip":
|
| 178 |
+
raise typer.BadParameter("zip format requires source frames; use 'build' instead.")
|
| 179 |
+
page = load_metadata(metadata_json)
|
| 180 |
+
|
| 181 |
+
resolved_frames_dir = (
|
| 182 |
+
frames_dir
|
| 183 |
+
if frames_dir is not None
|
| 184 |
+
else (metadata_json.parent / page.frames_dir).resolve()
|
| 185 |
+
)
|
| 186 |
+
html = render_from_metadata(page, Path(resolved_frames_dir), mode=fmt)
|
| 187 |
+
output.write_text(html, encoding="utf-8")
|
| 188 |
+
typer.echo(f"wrote {output}")
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
if __name__ == "__main__": # pragma: no cover
|
| 192 |
+
app()
|
|
@@ -11,17 +11,8 @@ from fastapi.responses import HTMLResponse, Response
|
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
from fastapi.templating import Jinja2Templates
|
| 13 |
|
| 14 |
-
from .pipeline import
|
| 15 |
-
|
| 16 |
-
bundle_zip,
|
| 17 |
-
detect_and_extract,
|
| 18 |
-
filter_instructor_frames,
|
| 19 |
-
ocr_frame,
|
| 20 |
-
parse_transcript,
|
| 21 |
-
render_guide,
|
| 22 |
-
render_review,
|
| 23 |
-
)
|
| 24 |
-
from .pipeline.html_gen import build_segments
|
| 25 |
|
| 26 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
| 27 |
log = logging.getLogger("videoguidemaker")
|
|
@@ -51,6 +42,8 @@ async def process(
|
|
| 51 |
threshold: float = Form(27.0),
|
| 52 |
face_threshold: float = Form(0.12),
|
| 53 |
lang: str = Form("en"),
|
|
|
|
|
|
|
| 54 |
) -> Response:
|
| 55 |
video_suffix = Path(video.filename or "").suffix.lower()
|
| 56 |
transcript_suffix = Path(transcript.filename or "").suffix.lower()
|
|
@@ -71,38 +64,39 @@ async def process(
|
|
| 71 |
with transcript_path.open("wb") as f:
|
| 72 |
shutil.copyfileobj(transcript.file, f)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if format == "review":
|
| 99 |
-
|
| 100 |
-
html = render_review(title, segments, lang=lang)
|
| 101 |
return Response(content=html, media_type="text/html; charset=utf-8")
|
| 102 |
|
| 103 |
inline = format == "single"
|
| 104 |
-
|
| 105 |
-
html = render_guide(title, segments, lang=lang, inline_images=inline)
|
| 106 |
|
| 107 |
if inline:
|
| 108 |
filename = _safe_filename(title) + ".html"
|
|
@@ -111,7 +105,7 @@ async def process(
|
|
| 111 |
media_type="text/html; charset=utf-8",
|
| 112 |
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
|
| 113 |
)
|
| 114 |
-
zip_bytes = bundle_zip(html, [f.image_path for f in
|
| 115 |
filename = _safe_filename(title) + ".zip"
|
| 116 |
return Response(
|
| 117 |
content=zip_bytes,
|
|
|
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
from fastapi.templating import Jinja2Templates
|
| 13 |
|
| 14 |
+
from .pipeline import bundle_zip, render_guide, render_review
|
| 15 |
+
from .pipeline.orchestrator import PipelineError, PipelineInputs, run_pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
| 18 |
log = logging.getLogger("videoguidemaker")
|
|
|
|
| 42 |
threshold: float = Form(27.0),
|
| 43 |
face_threshold: float = Form(0.12),
|
| 44 |
lang: str = Form("en"),
|
| 45 |
+
subtitle: str | None = Form(None),
|
| 46 |
+
module: str | None = Form(None),
|
| 47 |
) -> Response:
|
| 48 |
video_suffix = Path(video.filename or "").suffix.lower()
|
| 49 |
transcript_suffix = Path(transcript.filename or "").suffix.lower()
|
|
|
|
| 64 |
with transcript_path.open("wb") as f:
|
| 65 |
shutil.copyfileobj(transcript.file, f)
|
| 66 |
|
| 67 |
+
inputs = PipelineInputs(
|
| 68 |
+
video_path=video_path,
|
| 69 |
+
transcript_path=transcript_path,
|
| 70 |
+
frames_dir=frames_dir,
|
| 71 |
+
title=title,
|
| 72 |
+
subtitle=subtitle or None,
|
| 73 |
+
module=module or None,
|
| 74 |
+
lang=lang,
|
| 75 |
+
threshold=threshold,
|
| 76 |
+
face_threshold=face_threshold,
|
| 77 |
+
inline_images=(format in ("single", "review")),
|
| 78 |
+
)
|
| 79 |
+
try:
|
| 80 |
+
result = run_pipeline(inputs)
|
| 81 |
+
except PipelineError as exc:
|
| 82 |
+
raise HTTPException(422, str(exc))
|
| 83 |
+
|
| 84 |
+
common = dict(
|
| 85 |
+
title=title,
|
| 86 |
+
segments=result.segments,
|
| 87 |
+
lang=lang,
|
| 88 |
+
subtitle=subtitle or None,
|
| 89 |
+
module=module or None,
|
| 90 |
+
meta_lines=result.page.meta_lines or None,
|
| 91 |
+
eyebrow=result.page.eyebrow,
|
| 92 |
+
)
|
| 93 |
|
| 94 |
if format == "review":
|
| 95 |
+
html = render_review(**common)
|
|
|
|
| 96 |
return Response(content=html, media_type="text/html; charset=utf-8")
|
| 97 |
|
| 98 |
inline = format == "single"
|
| 99 |
+
html = render_guide(inline_images=inline, **common)
|
|
|
|
| 100 |
|
| 101 |
if inline:
|
| 102 |
filename = _safe_filename(title) + ".html"
|
|
|
|
| 105 |
media_type="text/html; charset=utf-8",
|
| 106 |
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
|
| 107 |
)
|
| 108 |
+
zip_bytes = bundle_zip(html, [f.image_path for f in result.kept_frames])
|
| 109 |
filename = _safe_filename(title) + ".zip"
|
| 110 |
return Response(
|
| 111 |
content=zip_bytes,
|
|
@@ -20,7 +20,14 @@ __all__ = [
|
|
| 20 |
"Segment",
|
| 21 |
"render_guide",
|
| 22 |
"render_review",
|
|
|
|
| 23 |
"bundle_zip",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
_EXPORTS = {
|
|
@@ -35,7 +42,14 @@ _EXPORTS = {
|
|
| 35 |
"Segment": "html_gen",
|
| 36 |
"render_guide": "html_gen",
|
| 37 |
"render_review": "html_gen",
|
|
|
|
| 38 |
"bundle_zip": "html_gen",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
|
|
@@ -47,7 +61,21 @@ def __getattr__(name: str) -> Any:
|
|
| 47 |
|
| 48 |
|
| 49 |
if TYPE_CHECKING: # pragma: no cover
|
| 50 |
-
from .html_gen import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
from .ocr import ocr_frame
|
| 52 |
from .filter import filter_instructor_frames, is_instructor_frame
|
| 53 |
from .scenes import SceneFrame, detect_and_extract
|
|
|
|
| 20 |
"Segment",
|
| 21 |
"render_guide",
|
| 22 |
"render_review",
|
| 23 |
+
"render_from_metadata",
|
| 24 |
"bundle_zip",
|
| 25 |
+
"PageMetadata",
|
| 26 |
+
"SegmentMetadata",
|
| 27 |
+
"KeyTerm",
|
| 28 |
+
"MathEquation",
|
| 29 |
+
"load_metadata",
|
| 30 |
+
"dump_metadata",
|
| 31 |
]
|
| 32 |
|
| 33 |
_EXPORTS = {
|
|
|
|
| 42 |
"Segment": "html_gen",
|
| 43 |
"render_guide": "html_gen",
|
| 44 |
"render_review": "html_gen",
|
| 45 |
+
"render_from_metadata": "html_gen",
|
| 46 |
"bundle_zip": "html_gen",
|
| 47 |
+
"PageMetadata": "metadata",
|
| 48 |
+
"SegmentMetadata": "metadata",
|
| 49 |
+
"KeyTerm": "metadata",
|
| 50 |
+
"MathEquation": "metadata",
|
| 51 |
+
"load_metadata": "metadata",
|
| 52 |
+
"dump_metadata": "metadata",
|
| 53 |
}
|
| 54 |
|
| 55 |
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
if TYPE_CHECKING: # pragma: no cover
|
| 64 |
+
from .html_gen import ( # noqa: F401
|
| 65 |
+
Segment,
|
| 66 |
+
bundle_zip,
|
| 67 |
+
render_from_metadata,
|
| 68 |
+
render_guide,
|
| 69 |
+
render_review,
|
| 70 |
+
)
|
| 71 |
+
from .metadata import ( # noqa: F401
|
| 72 |
+
KeyTerm,
|
| 73 |
+
MathEquation,
|
| 74 |
+
PageMetadata,
|
| 75 |
+
SegmentMetadata,
|
| 76 |
+
dump_metadata,
|
| 77 |
+
load_metadata,
|
| 78 |
+
)
|
| 79 |
from .ocr import ocr_frame
|
| 80 |
from .filter import filter_instructor_frames, is_instructor_frame
|
| 81 |
from .scenes import SceneFrame, detect_and_extract
|
|
@@ -4,12 +4,14 @@ from __future__ import annotations
|
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
import zipfile
|
| 7 |
-
from dataclasses import dataclass
|
| 8 |
from pathlib import Path
|
|
|
|
| 9 |
|
| 10 |
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
| 11 |
|
| 12 |
TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "templates"
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
@dataclass
|
|
@@ -23,6 +25,9 @@ class Segment:
|
|
| 23 |
ocr_text: str
|
| 24 |
alt_draft: str
|
| 25 |
section_title: str
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def _format_timestamp(seconds: float) -> str:
|
|
@@ -111,6 +116,10 @@ def _default_meta_lines(segments: list[Segment]) -> list[str]:
|
|
| 111 |
]
|
| 112 |
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
def render_guide(
|
| 115 |
title: str,
|
| 116 |
segments: list[Segment],
|
|
@@ -121,6 +130,10 @@ def render_guide(
|
|
| 121 |
eyebrow: str = DEFAULT_EYEBROW,
|
| 122 |
meta_lines: list[str] | None = None,
|
| 123 |
description: str | None = None,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
) -> str:
|
| 125 |
template = _env().get_template("guide.html")
|
| 126 |
return template.render(
|
|
@@ -132,6 +145,10 @@ def render_guide(
|
|
| 132 |
eyebrow=eyebrow,
|
| 133 |
meta_lines=meta_lines if meta_lines is not None else _default_meta_lines(segments),
|
| 134 |
description=description,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
|
|
@@ -142,6 +159,10 @@ def render_review(
|
|
| 142 |
lang: str = "en",
|
| 143 |
eyebrow: str = DEFAULT_EYEBROW,
|
| 144 |
meta_lines: list[str] | None = None,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
) -> str:
|
| 146 |
"""Render an in-browser editor page. Images must be inlined as data URIs
|
| 147 |
so the page is self-contained — saved/reopened HTML keeps working."""
|
|
@@ -154,6 +175,69 @@ def render_review(
|
|
| 154 |
segments=segments,
|
| 155 |
eyebrow=eyebrow,
|
| 156 |
meta_lines=meta_lines if meta_lines is not None else _default_meta_lines(segments),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
|
| 159 |
|
|
|
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
import zipfile
|
| 7 |
+
from dataclasses import dataclass, field
|
| 8 |
from pathlib import Path
|
| 9 |
+
from typing import Any
|
| 10 |
|
| 11 |
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
| 12 |
|
| 13 |
TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "templates"
|
| 14 |
+
MATHJAX_CDN_URL = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
|
| 15 |
|
| 16 |
|
| 17 |
@dataclass
|
|
|
|
| 25 |
ocr_text: str
|
| 26 |
alt_draft: str
|
| 27 |
section_title: str
|
| 28 |
+
key_terms: list[Any] = field(default_factory=list)
|
| 29 |
+
math_equations: list[Any] = field(default_factory=list)
|
| 30 |
+
include: bool = True
|
| 31 |
|
| 32 |
|
| 33 |
def _format_timestamp(seconds: float) -> str:
|
|
|
|
| 116 |
]
|
| 117 |
|
| 118 |
|
| 119 |
+
def _needs_mathjax(segments: list[Segment]) -> bool:
|
| 120 |
+
return any(seg.math_equations for seg in segments)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
def render_guide(
|
| 124 |
title: str,
|
| 125 |
segments: list[Segment],
|
|
|
|
| 130 |
eyebrow: str = DEFAULT_EYEBROW,
|
| 131 |
meta_lines: list[str] | None = None,
|
| 132 |
description: str | None = None,
|
| 133 |
+
subtitle: str | None = None,
|
| 134 |
+
module: str | None = None,
|
| 135 |
+
mathjax: bool | None = None,
|
| 136 |
+
mathjax_src: str = MATHJAX_CDN_URL,
|
| 137 |
) -> str:
|
| 138 |
template = _env().get_template("guide.html")
|
| 139 |
return template.render(
|
|
|
|
| 145 |
eyebrow=eyebrow,
|
| 146 |
meta_lines=meta_lines if meta_lines is not None else _default_meta_lines(segments),
|
| 147 |
description=description,
|
| 148 |
+
subtitle=subtitle,
|
| 149 |
+
module=module,
|
| 150 |
+
mathjax=_needs_mathjax(segments) if mathjax is None else mathjax,
|
| 151 |
+
mathjax_src=mathjax_src,
|
| 152 |
)
|
| 153 |
|
| 154 |
|
|
|
|
| 159 |
lang: str = "en",
|
| 160 |
eyebrow: str = DEFAULT_EYEBROW,
|
| 161 |
meta_lines: list[str] | None = None,
|
| 162 |
+
subtitle: str | None = None,
|
| 163 |
+
module: str | None = None,
|
| 164 |
+
mathjax: bool = True,
|
| 165 |
+
mathjax_src: str = MATHJAX_CDN_URL,
|
| 166 |
) -> str:
|
| 167 |
"""Render an in-browser editor page. Images must be inlined as data URIs
|
| 168 |
so the page is self-contained — saved/reopened HTML keeps working."""
|
|
|
|
| 175 |
segments=segments,
|
| 176 |
eyebrow=eyebrow,
|
| 177 |
meta_lines=meta_lines if meta_lines is not None else _default_meta_lines(segments),
|
| 178 |
+
subtitle=subtitle,
|
| 179 |
+
module=module,
|
| 180 |
+
mathjax=mathjax,
|
| 181 |
+
mathjax_src=mathjax_src,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def metadata_to_segment(seg_meta: Any, frames_dir: Path, *, inline_images: bool) -> Segment:
|
| 186 |
+
"""Convert a SegmentMetadata into a render-ready Segment.
|
| 187 |
+
|
| 188 |
+
`frames_dir` resolves `image_filename` to a real file when inline_images
|
| 189 |
+
is True (we need to read bytes for the data URI).
|
| 190 |
+
"""
|
| 191 |
+
image_path = Path(frames_dir) / seg_meta.image_filename
|
| 192 |
+
return Segment(
|
| 193 |
+
index=seg_meta.index,
|
| 194 |
+
start_seconds=seg_meta.start_seconds,
|
| 195 |
+
end_seconds=seg_meta.end_seconds,
|
| 196 |
+
image_filename=seg_meta.image_filename,
|
| 197 |
+
image_data_uri=_image_data_uri(image_path) if inline_images else None,
|
| 198 |
+
narration=list(seg_meta.narration),
|
| 199 |
+
ocr_text=seg_meta.ocr_text,
|
| 200 |
+
alt_draft=seg_meta.alt_text,
|
| 201 |
+
section_title=seg_meta.section_title,
|
| 202 |
+
key_terms=list(seg_meta.key_terms),
|
| 203 |
+
math_equations=list(seg_meta.math_equations),
|
| 204 |
+
include=seg_meta.include,
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def render_from_metadata(
|
| 209 |
+
page: Any,
|
| 210 |
+
frames_dir: Path,
|
| 211 |
+
*,
|
| 212 |
+
mode: str = "guide",
|
| 213 |
+
image_dir_name: str = "images",
|
| 214 |
+
mathjax_src: str = MATHJAX_CDN_URL,
|
| 215 |
+
) -> str:
|
| 216 |
+
"""Render either guide or review HTML directly from a PageMetadata.
|
| 217 |
+
|
| 218 |
+
`mode` selects "guide" (linked images), "single" (inlined images,
|
| 219 |
+
same template as guide), or "review" (in-browser editor).
|
| 220 |
+
"""
|
| 221 |
+
inline = mode in ("single", "review")
|
| 222 |
+
segments = [
|
| 223 |
+
metadata_to_segment(s, frames_dir, inline_images=inline) for s in page.segments
|
| 224 |
+
]
|
| 225 |
+
common = dict(
|
| 226 |
+
title=page.title,
|
| 227 |
+
lang=page.lang,
|
| 228 |
+
eyebrow=page.eyebrow,
|
| 229 |
+
meta_lines=page.meta_lines or None,
|
| 230 |
+
subtitle=page.subtitle,
|
| 231 |
+
module=page.module,
|
| 232 |
+
mathjax_src=mathjax_src,
|
| 233 |
+
)
|
| 234 |
+
if mode == "review":
|
| 235 |
+
return render_review(segments=segments, **common)
|
| 236 |
+
return render_guide(
|
| 237 |
+
segments=segments,
|
| 238 |
+
inline_images=inline,
|
| 239 |
+
image_dir_name=image_dir_name,
|
| 240 |
+
**common,
|
| 241 |
)
|
| 242 |
|
| 243 |
|
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unified metadata JSON spec for VideoGuideMaker.
|
| 2 |
+
|
| 3 |
+
The same schema backs both the browser editor's Save/Load JSON and the
|
| 4 |
+
CLI's --export-metadata / --metadata round-trip, so users can edit either
|
| 5 |
+
way and re-render produces an identical document.
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from typing import Any
|
| 12 |
+
|
| 13 |
+
from pydantic import BaseModel, Field
|
| 14 |
+
|
| 15 |
+
CURRENT_SCHEMA_VERSION = 2
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class KeyTerm(BaseModel):
|
| 19 |
+
term: str
|
| 20 |
+
definition: str
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class MathEquation(BaseModel):
|
| 24 |
+
latex: str
|
| 25 |
+
aria_label: str
|
| 26 |
+
label: str | None = None
|
| 27 |
+
caption: str | None = None
|
| 28 |
+
inline: bool = False
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class SegmentMetadata(BaseModel):
|
| 32 |
+
index: int
|
| 33 |
+
start_seconds: float
|
| 34 |
+
end_seconds: float
|
| 35 |
+
image_filename: str
|
| 36 |
+
section_title: str
|
| 37 |
+
alt_text: str
|
| 38 |
+
narration: list[str] = Field(default_factory=list)
|
| 39 |
+
ocr_text: str = ""
|
| 40 |
+
key_terms: list[KeyTerm] = Field(default_factory=list)
|
| 41 |
+
math_equations: list[MathEquation] = Field(default_factory=list)
|
| 42 |
+
include: bool = True
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class SourceInfo(BaseModel):
|
| 46 |
+
video_filename: str | None = None
|
| 47 |
+
duration_seconds: float = 0.0
|
| 48 |
+
auto_transcribed: bool = False
|
| 49 |
+
whisper_model: str | None = None
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class PageMetadata(BaseModel):
|
| 53 |
+
schema_version: int = CURRENT_SCHEMA_VERSION
|
| 54 |
+
title: str
|
| 55 |
+
subtitle: str | None = None
|
| 56 |
+
module: str | None = None
|
| 57 |
+
eyebrow: str = "Auto-Generated Study Guide"
|
| 58 |
+
lang: str = "en"
|
| 59 |
+
meta_lines: list[str] = Field(default_factory=list)
|
| 60 |
+
source: SourceInfo = Field(default_factory=SourceInfo)
|
| 61 |
+
frames_dir: str = "frames"
|
| 62 |
+
segments: list[SegmentMetadata] = Field(default_factory=list)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def migrate_v1_to_v2(raw: dict[str, Any]) -> dict[str, Any]:
|
| 66 |
+
"""Adapt a pre-schema-versioned editor save into the v2 shape.
|
| 67 |
+
|
| 68 |
+
The browser editor's prior `.edits.json` had: {title, eyebrow, segments:[{
|
| 69 |
+
index, section_title, alt_text, narration, ocr_text, include}]} — no
|
| 70 |
+
schema_version, no key_terms / math_equations / page-level meta_lines /
|
| 71 |
+
source / frames_dir / start_seconds / end_seconds / image_filename.
|
| 72 |
+
"""
|
| 73 |
+
out = dict(raw)
|
| 74 |
+
out.setdefault("schema_version", CURRENT_SCHEMA_VERSION)
|
| 75 |
+
out.setdefault("subtitle", None)
|
| 76 |
+
out.setdefault("module", None)
|
| 77 |
+
out.setdefault("eyebrow", "Auto-Generated Study Guide")
|
| 78 |
+
out.setdefault("lang", "en")
|
| 79 |
+
out.setdefault("meta_lines", [])
|
| 80 |
+
out.setdefault("source", {})
|
| 81 |
+
out.setdefault("frames_dir", "frames")
|
| 82 |
+
out.setdefault("segments", [])
|
| 83 |
+
|
| 84 |
+
migrated_segments: list[dict[str, Any]] = []
|
| 85 |
+
for seg in out["segments"]:
|
| 86 |
+
s = dict(seg)
|
| 87 |
+
s.setdefault("start_seconds", 0.0)
|
| 88 |
+
s.setdefault("end_seconds", 0.0)
|
| 89 |
+
s.setdefault("image_filename", f"scene_{int(s.get('index', 0)):04d}.jpg")
|
| 90 |
+
s.setdefault("section_title", f"Segment {int(s.get('index', 0)) + 1}")
|
| 91 |
+
s.setdefault("alt_text", "")
|
| 92 |
+
s.setdefault("ocr_text", "")
|
| 93 |
+
s.setdefault("key_terms", [])
|
| 94 |
+
s.setdefault("math_equations", [])
|
| 95 |
+
s.setdefault("include", True)
|
| 96 |
+
narration = s.get("narration", [])
|
| 97 |
+
if isinstance(narration, str):
|
| 98 |
+
s["narration"] = [narration] if narration else []
|
| 99 |
+
migrated_segments.append(s)
|
| 100 |
+
out["segments"] = migrated_segments
|
| 101 |
+
return out
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def load_metadata(path: str | Path) -> PageMetadata:
|
| 105 |
+
raw = json.loads(Path(path).read_text(encoding="utf-8"))
|
| 106 |
+
if raw.get("schema_version", 1) < CURRENT_SCHEMA_VERSION:
|
| 107 |
+
raw = migrate_v1_to_v2(raw)
|
| 108 |
+
return PageMetadata.model_validate(raw)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def dump_metadata(path: str | Path, page: PageMetadata) -> None:
|
| 112 |
+
payload = page.model_dump(mode="json")
|
| 113 |
+
Path(path).write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def segment_to_metadata(segment: Any) -> SegmentMetadata:
|
| 117 |
+
"""Adapt an html_gen.Segment dataclass into a SegmentMetadata.
|
| 118 |
+
|
| 119 |
+
Imported lazily to avoid a circular dependency (html_gen also imports
|
| 120 |
+
metadata for render_from_metadata).
|
| 121 |
+
"""
|
| 122 |
+
return SegmentMetadata(
|
| 123 |
+
index=segment.index,
|
| 124 |
+
start_seconds=segment.start_seconds,
|
| 125 |
+
end_seconds=segment.end_seconds,
|
| 126 |
+
image_filename=segment.image_filename,
|
| 127 |
+
section_title=segment.section_title,
|
| 128 |
+
alt_text=segment.alt_draft,
|
| 129 |
+
narration=list(segment.narration),
|
| 130 |
+
ocr_text=segment.ocr_text,
|
| 131 |
+
key_terms=[KeyTerm(**kt) if isinstance(kt, dict) else kt for kt in segment.key_terms],
|
| 132 |
+
math_equations=[
|
| 133 |
+
MathEquation(**me) if isinstance(me, dict) else me for me in segment.math_equations
|
| 134 |
+
],
|
| 135 |
+
include=segment.include,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def page_metadata_from_segments(
|
| 140 |
+
title: str,
|
| 141 |
+
segments: list[Any],
|
| 142 |
+
*,
|
| 143 |
+
subtitle: str | None = None,
|
| 144 |
+
module: str | None = None,
|
| 145 |
+
eyebrow: str = "Auto-Generated Study Guide",
|
| 146 |
+
lang: str = "en",
|
| 147 |
+
meta_lines: list[str] | None = None,
|
| 148 |
+
source: SourceInfo | None = None,
|
| 149 |
+
frames_dir: str = "frames",
|
| 150 |
+
) -> PageMetadata:
|
| 151 |
+
return PageMetadata(
|
| 152 |
+
title=title,
|
| 153 |
+
subtitle=subtitle,
|
| 154 |
+
module=module,
|
| 155 |
+
eyebrow=eyebrow,
|
| 156 |
+
lang=lang,
|
| 157 |
+
meta_lines=meta_lines or [],
|
| 158 |
+
source=source or SourceInfo(),
|
| 159 |
+
frames_dir=frames_dir,
|
| 160 |
+
segments=[segment_to_metadata(s) for s in segments],
|
| 161 |
+
)
|
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared pipeline orchestrator used by both the FastAPI server and the CLI.
|
| 2 |
+
|
| 3 |
+
This is intentionally I/O-light at the boundary: callers hand in already-on-disk
|
| 4 |
+
paths (video, transcript, frames_dir) and get back a list of build-ready
|
| 5 |
+
SceneFrames + their narration/OCR data, plus a populated PageMetadata. The
|
| 6 |
+
HTML rendering step is left to callers so they can pick guide/review/single.
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import logging
|
| 11 |
+
from dataclasses import dataclass
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
from .filter import filter_instructor_frames
|
| 15 |
+
from .metadata import PageMetadata, SourceInfo, page_metadata_from_segments
|
| 16 |
+
from .ocr import ocr_frame
|
| 17 |
+
from .scenes import detect_and_extract
|
| 18 |
+
from .transcript import Cue, align_segments, parse_transcript
|
| 19 |
+
from .types import SceneFrame
|
| 20 |
+
from .html_gen import Segment, build_segments
|
| 21 |
+
|
| 22 |
+
log = logging.getLogger("videoguidemaker.orchestrator")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@dataclass
|
| 26 |
+
class PipelineInputs:
|
| 27 |
+
video_path: Path
|
| 28 |
+
transcript_path: Path | None
|
| 29 |
+
frames_dir: Path
|
| 30 |
+
title: str
|
| 31 |
+
subtitle: str | None = None
|
| 32 |
+
module: str | None = None
|
| 33 |
+
eyebrow: str = "Auto-Generated Study Guide"
|
| 34 |
+
lang: str = "en"
|
| 35 |
+
threshold: float = 27.0
|
| 36 |
+
face_threshold: float = 0.12
|
| 37 |
+
skip_ocr: bool = False
|
| 38 |
+
auto_transcribe: bool = False
|
| 39 |
+
whisper_model: str = "small"
|
| 40 |
+
inline_images: bool = False
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@dataclass
|
| 44 |
+
class PipelineResult:
|
| 45 |
+
page: PageMetadata
|
| 46 |
+
segments: list[Segment]
|
| 47 |
+
kept_frames: list[SceneFrame]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class PipelineError(RuntimeError):
|
| 51 |
+
"""Raised when the pipeline cannot produce a usable study guide."""
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _load_cues(inputs: PipelineInputs) -> tuple[list[Cue], bool]:
|
| 55 |
+
"""Return (cues, used_auto_transcribe)."""
|
| 56 |
+
if inputs.transcript_path and inputs.transcript_path.exists():
|
| 57 |
+
log.info("Parsing transcript: %s", inputs.transcript_path)
|
| 58 |
+
return parse_transcript(inputs.transcript_path), False
|
| 59 |
+
if not inputs.auto_transcribe:
|
| 60 |
+
raise PipelineError(
|
| 61 |
+
"No transcript provided. Pass a VTT/SRT file or enable auto_transcribe."
|
| 62 |
+
)
|
| 63 |
+
try:
|
| 64 |
+
from .whisper_asr import transcribe_to_cues # noqa: WPS433 (optional dep)
|
| 65 |
+
except ImportError as exc: # pragma: no cover (covered via CLI integration)
|
| 66 |
+
raise PipelineError(
|
| 67 |
+
"auto_transcribe requires the optional 'asr' extra: pip install -e .[asr]"
|
| 68 |
+
) from exc
|
| 69 |
+
log.info("Auto-transcribing %s with whisper model=%s", inputs.video_path, inputs.whisper_model)
|
| 70 |
+
cues = transcribe_to_cues(inputs.video_path, model_size=inputs.whisper_model)
|
| 71 |
+
return cues, True
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def run_pipeline(inputs: PipelineInputs) -> PipelineResult:
|
| 75 |
+
log.info("Detecting scenes (threshold=%s)", inputs.threshold)
|
| 76 |
+
frames = detect_and_extract(inputs.video_path, inputs.frames_dir, threshold=inputs.threshold)
|
| 77 |
+
log.info("Extracted %d scene frames", len(frames))
|
| 78 |
+
|
| 79 |
+
kept, dropped = filter_instructor_frames(frames, face_area_threshold=inputs.face_threshold)
|
| 80 |
+
log.info("Kept %d, dropped %d instructor frames", len(kept), len(dropped))
|
| 81 |
+
|
| 82 |
+
if not kept:
|
| 83 |
+
raise PipelineError(
|
| 84 |
+
"No usable visual segments found. Try lowering scene-change sensitivity "
|
| 85 |
+
"or raising the instructor face threshold."
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
cues, auto_transcribed = _load_cues(inputs)
|
| 89 |
+
log.info("Loaded %d cues (auto_transcribed=%s)", len(cues), auto_transcribed)
|
| 90 |
+
|
| 91 |
+
boundaries = [f.start_seconds for f in kept]
|
| 92 |
+
total_duration = max(f.end_seconds for f in kept)
|
| 93 |
+
narrations = align_segments(cues, boundaries, total_duration)
|
| 94 |
+
|
| 95 |
+
if inputs.skip_ocr:
|
| 96 |
+
log.info("Skipping OCR (--skip-ocr)")
|
| 97 |
+
ocr_texts = ["" for _ in kept]
|
| 98 |
+
else:
|
| 99 |
+
log.info("Running OCR on %d frames", len(kept))
|
| 100 |
+
ocr_texts = [ocr_frame(f.image_path) for f in kept]
|
| 101 |
+
|
| 102 |
+
segments = build_segments(kept, narrations, ocr_texts, inline_images=inputs.inline_images)
|
| 103 |
+
|
| 104 |
+
page = page_metadata_from_segments(
|
| 105 |
+
title=inputs.title,
|
| 106 |
+
segments=segments,
|
| 107 |
+
subtitle=inputs.subtitle,
|
| 108 |
+
module=inputs.module,
|
| 109 |
+
eyebrow=inputs.eyebrow,
|
| 110 |
+
lang=inputs.lang,
|
| 111 |
+
source=SourceInfo(
|
| 112 |
+
video_filename=inputs.video_path.name,
|
| 113 |
+
duration_seconds=total_duration,
|
| 114 |
+
auto_transcribed=auto_transcribed,
|
| 115 |
+
whisper_model=inputs.whisper_model if auto_transcribed else None,
|
| 116 |
+
),
|
| 117 |
+
frames_dir=str(inputs.frames_dir.name),
|
| 118 |
+
)
|
| 119 |
+
return PipelineResult(page=page, segments=segments, kept_frames=kept)
|
|
@@ -3,6 +3,26 @@ name = "videoguidemaker"
|
|
| 3 |
version = "0.1.0"
|
| 4 |
description = "Generate WCAG 2.2 AA-ready HTML study guides from instructional videos"
|
| 5 |
requires-python = ">=3.10"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
[tool.pytest.ini_options]
|
| 8 |
testpaths = ["tests"]
|
|
|
|
| 3 |
version = "0.1.0"
|
| 4 |
description = "Generate WCAG 2.2 AA-ready HTML study guides from instructional videos"
|
| 5 |
requires-python = ">=3.10"
|
| 6 |
+
dependencies = [
|
| 7 |
+
"fastapi>=0.115",
|
| 8 |
+
"uvicorn[standard]>=0.32",
|
| 9 |
+
"python-multipart>=0.0.12",
|
| 10 |
+
"jinja2>=3.1",
|
| 11 |
+
"webvtt-py>=0.5",
|
| 12 |
+
"scenedetect[opencv]>=0.6.5",
|
| 13 |
+
"opencv-python-headless>=4.10",
|
| 14 |
+
"pytesseract>=0.3.13",
|
| 15 |
+
"Pillow>=10.4",
|
| 16 |
+
"numpy>=1.26",
|
| 17 |
+
"pydantic>=2.9",
|
| 18 |
+
"typer>=0.12",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
[project.optional-dependencies]
|
| 22 |
+
asr = ["faster-whisper>=1.0"]
|
| 23 |
+
|
| 24 |
+
[project.scripts]
|
| 25 |
+
vgm = "app.cli:app"
|
| 26 |
|
| 27 |
[tool.pytest.ini_options]
|
| 28 |
testpaths = ["tests"]
|
|
@@ -9,3 +9,4 @@ pytesseract==0.3.13
|
|
| 9 |
Pillow==10.4.0
|
| 10 |
numpy==1.26.4
|
| 11 |
pydantic==2.9.2
|
|
|
|
|
|
| 9 |
Pillow==10.4.0
|
| 10 |
numpy==1.26.4
|
| 11 |
pydantic==2.9.2
|
| 12 |
+
typer==0.25.1
|
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CLI integration tests.
|
| 2 |
+
|
| 3 |
+
These tests exercise the CLI surface without spawning a subprocess by
|
| 4 |
+
invoking typer's CliRunner directly. They rely on the existing
|
| 5 |
+
smaller.m4v + M14_L3_S3.srt media in the repo root for the slow path,
|
| 6 |
+
but they're skipped if those files aren't present so the suite stays
|
| 7 |
+
green in environments that only check out source.
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import pytest
|
| 14 |
+
from typer.testing import CliRunner
|
| 15 |
+
|
| 16 |
+
from app.cli import app
|
| 17 |
+
from app.pipeline.metadata import load_metadata
|
| 18 |
+
|
| 19 |
+
REPO_ROOT = Path(__file__).resolve().parents[1]
|
| 20 |
+
VIDEO = REPO_ROOT / "smaller.m4v"
|
| 21 |
+
TRANSCRIPT = REPO_ROOT / "M14_L3_S3.srt"
|
| 22 |
+
|
| 23 |
+
requires_media = pytest.mark.skipif(
|
| 24 |
+
not (VIDEO.exists() and TRANSCRIPT.exists()),
|
| 25 |
+
reason="sample media (smaller.m4v / M14_L3_S3.srt) not present",
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_cli_help_lists_subcommands():
|
| 30 |
+
runner = CliRunner()
|
| 31 |
+
result = runner.invoke(app, ["--help"])
|
| 32 |
+
assert result.exit_code == 0
|
| 33 |
+
out = result.stdout
|
| 34 |
+
assert "build" in out
|
| 35 |
+
assert "export-metadata" in out
|
| 36 |
+
assert "render-from-metadata" in out
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@requires_media
|
| 40 |
+
def test_cli_export_then_render_is_idempotent(tmp_path):
|
| 41 |
+
runner = CliRunner()
|
| 42 |
+
meta_path = tmp_path / "study_guide_metadata.json"
|
| 43 |
+
frames_dir = tmp_path / "frames"
|
| 44 |
+
|
| 45 |
+
result = runner.invoke(
|
| 46 |
+
app,
|
| 47 |
+
[
|
| 48 |
+
"export-metadata",
|
| 49 |
+
str(VIDEO),
|
| 50 |
+
str(TRANSCRIPT),
|
| 51 |
+
"--title", "M14 L3 S3",
|
| 52 |
+
"--subtitle", "Prompting",
|
| 53 |
+
"--module", "Module 14",
|
| 54 |
+
"--output", str(meta_path),
|
| 55 |
+
"--frames-dir", str(frames_dir),
|
| 56 |
+
],
|
| 57 |
+
)
|
| 58 |
+
assert result.exit_code == 0, result.stdout + (result.stderr or "")
|
| 59 |
+
assert meta_path.exists()
|
| 60 |
+
|
| 61 |
+
page = load_metadata(meta_path)
|
| 62 |
+
assert page.title == "M14 L3 S3"
|
| 63 |
+
assert page.subtitle == "Prompting"
|
| 64 |
+
assert page.module == "Module 14"
|
| 65 |
+
assert len(page.segments) > 0
|
| 66 |
+
assert page.segments[0].image_filename.startswith("scene_")
|
| 67 |
+
|
| 68 |
+
# Now re-render twice and confirm bytes are identical.
|
| 69 |
+
out1 = tmp_path / "out1.html"
|
| 70 |
+
out2 = tmp_path / "out2.html"
|
| 71 |
+
for out in (out1, out2):
|
| 72 |
+
result = runner.invoke(
|
| 73 |
+
app,
|
| 74 |
+
[
|
| 75 |
+
"render-from-metadata",
|
| 76 |
+
str(meta_path),
|
| 77 |
+
"--output", str(out),
|
| 78 |
+
"--frames-dir", str(frames_dir),
|
| 79 |
+
"--format", "single",
|
| 80 |
+
],
|
| 81 |
+
)
|
| 82 |
+
assert result.exit_code == 0, result.stdout + (result.stderr or "")
|
| 83 |
+
assert out.exists()
|
| 84 |
+
assert out1.read_bytes() == out2.read_bytes()
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@requires_media
|
| 88 |
+
def test_cli_build_writes_html_and_metadata(tmp_path):
|
| 89 |
+
runner = CliRunner()
|
| 90 |
+
out = tmp_path / "guide.html"
|
| 91 |
+
meta = tmp_path / "meta.json"
|
| 92 |
+
frames_dir = tmp_path / "frames"
|
| 93 |
+
|
| 94 |
+
result = runner.invoke(
|
| 95 |
+
app,
|
| 96 |
+
[
|
| 97 |
+
"build",
|
| 98 |
+
str(VIDEO),
|
| 99 |
+
str(TRANSCRIPT),
|
| 100 |
+
"--title", "Build Test",
|
| 101 |
+
"--output", str(out),
|
| 102 |
+
"--frames-dir", str(frames_dir),
|
| 103 |
+
"--format", "single",
|
| 104 |
+
"--export-metadata", str(meta),
|
| 105 |
+
],
|
| 106 |
+
)
|
| 107 |
+
assert result.exit_code == 0, result.stdout + (result.stderr or "")
|
| 108 |
+
assert out.exists()
|
| 109 |
+
assert meta.exists()
|
| 110 |
+
assert "<title>Build Test</title>" in out.read_text(encoding="utf-8")
|
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
from app.pipeline.html_gen import (
|
| 8 |
+
Segment,
|
| 9 |
+
metadata_to_segment,
|
| 10 |
+
render_from_metadata,
|
| 11 |
+
render_guide,
|
| 12 |
+
)
|
| 13 |
+
from app.pipeline.metadata import (
|
| 14 |
+
CURRENT_SCHEMA_VERSION,
|
| 15 |
+
KeyTerm,
|
| 16 |
+
MathEquation,
|
| 17 |
+
PageMetadata,
|
| 18 |
+
SegmentMetadata,
|
| 19 |
+
dump_metadata,
|
| 20 |
+
load_metadata,
|
| 21 |
+
migrate_v1_to_v2,
|
| 22 |
+
page_metadata_from_segments,
|
| 23 |
+
segment_to_metadata,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _make_image(path: Path) -> None:
|
| 28 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 29 |
+
Image.new("RGB", (32, 24), (123, 200, 50)).save(path, "JPEG")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _sample_segment(idx: int = 0) -> Segment:
|
| 33 |
+
return Segment(
|
| 34 |
+
index=idx,
|
| 35 |
+
start_seconds=float(idx * 5),
|
| 36 |
+
end_seconds=float(idx * 5 + 5),
|
| 37 |
+
image_filename=f"scene_{idx:04d}.jpg",
|
| 38 |
+
image_data_uri=None,
|
| 39 |
+
narration=["First paragraph.", "Second paragraph."],
|
| 40 |
+
ocr_text="ocr text",
|
| 41 |
+
alt_draft="An image showing a slide.",
|
| 42 |
+
section_title=f"Section {idx + 1}",
|
| 43 |
+
key_terms=[KeyTerm(term="Photon", definition="A particle of light.")],
|
| 44 |
+
math_equations=[
|
| 45 |
+
MathEquation(latex="E=mc^2", aria_label="E equals m c squared"),
|
| 46 |
+
],
|
| 47 |
+
include=True,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_pagemetadata_roundtrips_through_json(tmp_path):
|
| 52 |
+
page = page_metadata_from_segments(
|
| 53 |
+
title="Round-Trip",
|
| 54 |
+
segments=[_sample_segment(0), _sample_segment(1)],
|
| 55 |
+
subtitle="A subtitle",
|
| 56 |
+
module="Module 7",
|
| 57 |
+
meta_lines=["2 segments", "Source: 0:10"],
|
| 58 |
+
)
|
| 59 |
+
out = tmp_path / "meta.json"
|
| 60 |
+
dump_metadata(out, page)
|
| 61 |
+
|
| 62 |
+
loaded = load_metadata(out)
|
| 63 |
+
assert loaded.schema_version == CURRENT_SCHEMA_VERSION
|
| 64 |
+
assert loaded.title == "Round-Trip"
|
| 65 |
+
assert loaded.subtitle == "A subtitle"
|
| 66 |
+
assert loaded.module == "Module 7"
|
| 67 |
+
assert len(loaded.segments) == 2
|
| 68 |
+
assert loaded.segments[0].key_terms[0].term == "Photon"
|
| 69 |
+
assert loaded.segments[0].math_equations[0].latex == "E=mc^2"
|
| 70 |
+
assert loaded.segments[0].narration == ["First paragraph.", "Second paragraph."]
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_segment_to_and_from_metadata(tmp_path):
|
| 74 |
+
seg = _sample_segment(3)
|
| 75 |
+
seg_meta = segment_to_metadata(seg)
|
| 76 |
+
assert isinstance(seg_meta, SegmentMetadata)
|
| 77 |
+
assert seg_meta.index == 3
|
| 78 |
+
assert seg_meta.alt_text == "An image showing a slide."
|
| 79 |
+
assert seg_meta.key_terms[0].definition == "A particle of light."
|
| 80 |
+
|
| 81 |
+
img = tmp_path / seg.image_filename
|
| 82 |
+
_make_image(img)
|
| 83 |
+
rebuilt = metadata_to_segment(seg_meta, tmp_path, inline_images=False)
|
| 84 |
+
assert isinstance(rebuilt, Segment)
|
| 85 |
+
assert rebuilt.section_title == seg.section_title
|
| 86 |
+
assert rebuilt.alt_draft == seg.alt_draft
|
| 87 |
+
assert rebuilt.narration == seg.narration
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def test_migrate_v1_to_v2_fills_defaults():
|
| 91 |
+
v1 = {
|
| 92 |
+
"title": "Old Save",
|
| 93 |
+
"eyebrow": "Legacy",
|
| 94 |
+
"segments": [
|
| 95 |
+
{
|
| 96 |
+
"index": 0,
|
| 97 |
+
"section_title": "Intro",
|
| 98 |
+
"alt_text": "Slide",
|
| 99 |
+
"narration": "Single string narration.",
|
| 100 |
+
"ocr_text": "OCR",
|
| 101 |
+
"include": True,
|
| 102 |
+
}
|
| 103 |
+
],
|
| 104 |
+
}
|
| 105 |
+
v2 = migrate_v1_to_v2(v1)
|
| 106 |
+
page = PageMetadata.model_validate(v2)
|
| 107 |
+
assert page.schema_version == CURRENT_SCHEMA_VERSION
|
| 108 |
+
assert page.subtitle is None
|
| 109 |
+
assert page.module is None
|
| 110 |
+
assert page.frames_dir == "frames"
|
| 111 |
+
seg = page.segments[0]
|
| 112 |
+
assert seg.narration == ["Single string narration."]
|
| 113 |
+
assert seg.key_terms == []
|
| 114 |
+
assert seg.math_equations == []
|
| 115 |
+
assert seg.image_filename == "scene_0000.jpg"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def test_migrate_v1_via_load_metadata_with_no_schema_version_key(tmp_path):
|
| 119 |
+
v1_json = tmp_path / "v1.json"
|
| 120 |
+
v1_json.write_text(
|
| 121 |
+
json.dumps(
|
| 122 |
+
{
|
| 123 |
+
"title": "Old",
|
| 124 |
+
"segments": [
|
| 125 |
+
{"index": 0, "narration": ["one", "two"], "section_title": "S", "alt_text": "A"}
|
| 126 |
+
],
|
| 127 |
+
}
|
| 128 |
+
),
|
| 129 |
+
encoding="utf-8",
|
| 130 |
+
)
|
| 131 |
+
page = load_metadata(v1_json)
|
| 132 |
+
assert page.schema_version == CURRENT_SCHEMA_VERSION
|
| 133 |
+
assert page.segments[0].narration == ["one", "two"]
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def test_render_from_metadata_idempotent(tmp_path):
|
| 137 |
+
img0 = tmp_path / "scene_0000.jpg"
|
| 138 |
+
img1 = tmp_path / "scene_0001.jpg"
|
| 139 |
+
_make_image(img0)
|
| 140 |
+
_make_image(img1)
|
| 141 |
+
|
| 142 |
+
page = page_metadata_from_segments(
|
| 143 |
+
title="Idempotent",
|
| 144 |
+
segments=[_sample_segment(0), _sample_segment(1)],
|
| 145 |
+
subtitle="Sub",
|
| 146 |
+
module="Mod 3",
|
| 147 |
+
)
|
| 148 |
+
page.frames_dir = str(tmp_path)
|
| 149 |
+
|
| 150 |
+
html_a = render_from_metadata(page, frames_dir=tmp_path, mode="single")
|
| 151 |
+
html_b = render_from_metadata(page, frames_dir=tmp_path, mode="single")
|
| 152 |
+
assert html_a == html_b
|
| 153 |
+
assert "Idempotent" in html_a
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def test_render_from_metadata_links_images_in_guide_mode(tmp_path):
|
| 157 |
+
img0 = tmp_path / "scene_0000.jpg"
|
| 158 |
+
_make_image(img0)
|
| 159 |
+
page = page_metadata_from_segments(
|
| 160 |
+
title="Linked",
|
| 161 |
+
segments=[_sample_segment(0)],
|
| 162 |
+
)
|
| 163 |
+
page.frames_dir = str(tmp_path)
|
| 164 |
+
html = render_from_metadata(page, frames_dir=tmp_path, mode="guide")
|
| 165 |
+
# In guide mode (non-inline), images are referenced by path.
|
| 166 |
+
assert 'src="images/scene_0000.jpg"' in html
|
| 167 |
+
assert "data:image/jpeg;base64," not in html
|