Spaces:
Sleeping
Fix CLI audio output: inline for single, bundle for zip, schema round-trip
Browse filesrun_pipeline already slices per-segment audio and sets seg.audio_filename,
and guide.html emits src="audio/..." when that field is present. But
the CLI's build command never inlined audio for --format single nor
passed result.audio_paths to bundle_zip() for --format zip — so:
- 'vgm build --format single' produced HTML referencing
audio/segment_0001.mp3 paths that didn't exist next to it,
breaking the 'self-contained HTML' contract.
- 'vgm build --format zip' produced a zip with the HTML but no
audio/ folder, so the audio references 404'd on extraction.
- 'vgm render-from-metadata' had no audio_filename in the metadata
schema at all; round-tripping a previously-built guide silently
lost the audio entirely.
Fixes:
- SegmentMetadata gains audio_filename + migration default. The
save/load round-trip now preserves it (test guard).
- metadata_to_segment loads audio from <frames_dir.parent>/audio/<fn>
when inline_images=True (so render-from-metadata --format single
inlines as data URIs), and otherwise propagates audio_filename
so guide.html emits the relative 'audio/<fn>' reference for the
sibling-folder layout.
- CLI 'build' command:
single → base64-inlines result.audio_paths into seg.audio_data_uri
before render_guide(inline_images=True).
zip → passes result.audio_paths to bundle_zip(audio_paths=...)
so the bundle includes the audio/ folder the HTML refs.
New tests pin the behavior:
- test_html_gen: zip mode emits 'audio/<fn>'; single mode inlines
data URI and never leaks 'audio/<fn>'; metadata round-trip
preserves audio_filename; metadata_to_segment inlines correctly
for both inline_images=True and =False paths.
- test_cli (skipped without sample media): single-format HTML never
references 'audio/' if it has any <audio> elements; zip-format
bundle contains an 'audio/' folder whenever its HTML does.
- app/cli.py +16 -1
- app/pipeline/html_gen.py +17 -1
- app/pipeline/metadata.py +4 -0
- tests/test_cli.py +63 -0
- tests/test_html_gen.py +87 -0
|
@@ -150,10 +150,25 @@ def build(
|
|
| 150 |
html = render_review(**common)
|
| 151 |
else:
|
| 152 |
inline = fmt == "single"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
html = render_guide(inline_images=inline, **common)
|
| 154 |
|
| 155 |
if fmt == "zip":
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
if output.suffix.lower() != ".zip":
|
| 158 |
output = output.with_suffix(".zip")
|
| 159 |
output.write_bytes(zip_bytes)
|
|
|
|
| 150 |
html = render_review(**common)
|
| 151 |
else:
|
| 152 |
inline = fmt == "single"
|
| 153 |
+
if inline:
|
| 154 |
+
# Inline audio data URIs alongside images so the single HTML
|
| 155 |
+
# stays self-contained (no broken audio/foo.mp3 references).
|
| 156 |
+
import base64
|
| 157 |
+
for seg, ap in zip(result.segments, result.audio_paths):
|
| 158 |
+
if ap and ap.exists():
|
| 159 |
+
seg.audio_data_uri = (
|
| 160 |
+
"data:audio/mpeg;base64,"
|
| 161 |
+
+ base64.b64encode(ap.read_bytes()).decode("ascii")
|
| 162 |
+
)
|
| 163 |
html = render_guide(inline_images=inline, **common)
|
| 164 |
|
| 165 |
if fmt == "zip":
|
| 166 |
+
audio_disk_paths = [p for p in result.audio_paths if p is not None]
|
| 167 |
+
zip_bytes = bundle_zip(
|
| 168 |
+
html,
|
| 169 |
+
[f.image_path for f in result.kept_frames],
|
| 170 |
+
audio_paths=audio_disk_paths,
|
| 171 |
+
)
|
| 172 |
if output.suffix.lower() != ".zip":
|
| 173 |
output = output.with_suffix(".zip")
|
| 174 |
output.write_bytes(zip_bytes)
|
|
@@ -204,15 +204,27 @@ def render_review(
|
|
| 204 |
)
|
| 205 |
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def metadata_to_segment(seg_meta: Any, frames_dir: Path, *, inline_images: bool) -> Segment:
|
| 208 |
"""Convert a SegmentMetadata into a render-ready Segment.
|
| 209 |
|
| 210 |
`frames_dir` resolves `image_filename` to a real file when inline_images
|
| 211 |
-
is True (we need to read bytes for the data URI).
|
|
|
|
|
|
|
| 212 |
"""
|
| 213 |
image_path = Path(frames_dir) / seg_meta.image_filename
|
| 214 |
alt_filenames = list(getattr(seg_meta, "alt_image_filenames", []) or [])
|
| 215 |
alt_paths = [Path(frames_dir) / fn for fn in alt_filenames]
|
|
|
|
|
|
|
|
|
|
| 216 |
return Segment(
|
| 217 |
index=seg_meta.index,
|
| 218 |
start_seconds=seg_meta.start_seconds,
|
|
@@ -230,6 +242,10 @@ def metadata_to_segment(seg_meta: Any, frames_dir: Path, *, inline_images: bool)
|
|
| 230 |
alt_image_data_uris=(
|
| 231 |
[_image_data_uri(p) for p in alt_paths if p.exists()] if inline_images else []
|
| 232 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
)
|
| 234 |
|
| 235 |
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
|
| 207 |
+
def _audio_data_uri(audio_path: Path) -> str | None:
|
| 208 |
+
if not audio_path.exists():
|
| 209 |
+
return None
|
| 210 |
+
data = audio_path.read_bytes()
|
| 211 |
+
return "data:audio/mpeg;base64," + base64.b64encode(data).decode("ascii")
|
| 212 |
+
|
| 213 |
+
|
| 214 |
def metadata_to_segment(seg_meta: Any, frames_dir: Path, *, inline_images: bool) -> Segment:
|
| 215 |
"""Convert a SegmentMetadata into a render-ready Segment.
|
| 216 |
|
| 217 |
`frames_dir` resolves `image_filename` to a real file when inline_images
|
| 218 |
+
is True (we need to read bytes for the data URI). The corresponding
|
| 219 |
+
audio clip is looked up in a sibling 'audio/' directory next to
|
| 220 |
+
`frames_dir`, mirroring the on-disk layout the orchestrator writes.
|
| 221 |
"""
|
| 222 |
image_path = Path(frames_dir) / seg_meta.image_filename
|
| 223 |
alt_filenames = list(getattr(seg_meta, "alt_image_filenames", []) or [])
|
| 224 |
alt_paths = [Path(frames_dir) / fn for fn in alt_filenames]
|
| 225 |
+
audio_filename = getattr(seg_meta, "audio_filename", "") or ""
|
| 226 |
+
audio_dir = Path(frames_dir).parent / "audio"
|
| 227 |
+
audio_path = audio_dir / audio_filename if audio_filename else None
|
| 228 |
return Segment(
|
| 229 |
index=seg_meta.index,
|
| 230 |
start_seconds=seg_meta.start_seconds,
|
|
|
|
| 242 |
alt_image_data_uris=(
|
| 243 |
[_image_data_uri(p) for p in alt_paths if p.exists()] if inline_images else []
|
| 244 |
),
|
| 245 |
+
audio_filename=audio_filename,
|
| 246 |
+
audio_data_uri=(
|
| 247 |
+
_audio_data_uri(audio_path) if (inline_images and audio_path is not None) else None
|
| 248 |
+
),
|
| 249 |
)
|
| 250 |
|
| 251 |
|
|
@@ -43,6 +43,8 @@ class SegmentMetadata(BaseModel):
|
|
| 43 |
# Additional candidate frames the reviewer can switch to; the active
|
| 44 |
# choice is whatever's set as `image_filename`.
|
| 45 |
alt_image_filenames: list[str] = Field(default_factory=list)
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
class SourceInfo(BaseModel):
|
|
@@ -97,6 +99,7 @@ def migrate_v1_to_v2(raw: dict[str, Any]) -> dict[str, Any]:
|
|
| 97 |
s.setdefault("math_equations", [])
|
| 98 |
s.setdefault("include", True)
|
| 99 |
s.setdefault("alt_image_filenames", [])
|
|
|
|
| 100 |
narration = s.get("narration", [])
|
| 101 |
if isinstance(narration, str):
|
| 102 |
s["narration"] = [narration] if narration else []
|
|
@@ -138,6 +141,7 @@ def segment_to_metadata(segment: Any) -> SegmentMetadata:
|
|
| 138 |
],
|
| 139 |
include=segment.include,
|
| 140 |
alt_image_filenames=list(getattr(segment, "alt_image_filenames", []) or []),
|
|
|
|
| 141 |
)
|
| 142 |
|
| 143 |
|
|
|
|
| 43 |
# Additional candidate frames the reviewer can switch to; the active
|
| 44 |
# choice is whatever's set as `image_filename`.
|
| 45 |
alt_image_filenames: list[str] = Field(default_factory=list)
|
| 46 |
+
# Sliced narration audio filename (sibling 'audio/' folder to frames_dir).
|
| 47 |
+
audio_filename: str = ""
|
| 48 |
|
| 49 |
|
| 50 |
class SourceInfo(BaseModel):
|
|
|
|
| 99 |
s.setdefault("math_equations", [])
|
| 100 |
s.setdefault("include", True)
|
| 101 |
s.setdefault("alt_image_filenames", [])
|
| 102 |
+
s.setdefault("audio_filename", "")
|
| 103 |
narration = s.get("narration", [])
|
| 104 |
if isinstance(narration, str):
|
| 105 |
s["narration"] = [narration] if narration else []
|
|
|
|
| 141 |
],
|
| 142 |
include=segment.include,
|
| 143 |
alt_image_filenames=list(getattr(segment, "alt_image_filenames", []) or []),
|
| 144 |
+
audio_filename=getattr(segment, "audio_filename", "") or "",
|
| 145 |
)
|
| 146 |
|
| 147 |
|
|
@@ -113,6 +113,69 @@ def test_cli_skip_ocr_and_max_frames(tmp_path):
|
|
| 113 |
assert all(seg.ocr_text == "" for seg in page.segments)
|
| 114 |
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
@requires_media
|
| 117 |
def test_cli_build_writes_html_and_metadata(tmp_path):
|
| 118 |
runner = CliRunner()
|
|
|
|
| 113 |
assert all(seg.ocr_text == "" for seg in page.segments)
|
| 114 |
|
| 115 |
|
| 116 |
+
@requires_media
|
| 117 |
+
def test_cli_build_single_inlines_audio_data_uris(tmp_path):
|
| 118 |
+
"""`build --format single` must inline audio as data URIs so the
|
| 119 |
+
self-contained HTML doesn't reference a missing 'audio/' folder."""
|
| 120 |
+
runner = CliRunner()
|
| 121 |
+
out = tmp_path / "single.html"
|
| 122 |
+
frames_dir = tmp_path / "static"
|
| 123 |
+
result = runner.invoke(
|
| 124 |
+
app,
|
| 125 |
+
[
|
| 126 |
+
"build",
|
| 127 |
+
str(VIDEO),
|
| 128 |
+
str(TRANSCRIPT),
|
| 129 |
+
"--title", "Audio Inline",
|
| 130 |
+
"--output", str(out),
|
| 131 |
+
"--frames-dir", str(frames_dir),
|
| 132 |
+
"--format", "single",
|
| 133 |
+
"--max-frames", "2",
|
| 134 |
+
],
|
| 135 |
+
)
|
| 136 |
+
assert result.exit_code == 0, result.stdout + (result.stderr or "")
|
| 137 |
+
html = out.read_text(encoding="utf-8")
|
| 138 |
+
if "<audio" in html:
|
| 139 |
+
# If ffmpeg sliced anything, every <audio> src must be inlined —
|
| 140 |
+
# never a relative 'audio/foo.mp3' that won't resolve.
|
| 141 |
+
assert "data:audio/mpeg;base64," in html
|
| 142 |
+
assert 'src="audio/' not in html
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@requires_media
|
| 146 |
+
def test_cli_build_zip_includes_audio_folder(tmp_path):
|
| 147 |
+
"""`build --format zip` must bundle the 'audio/' folder so the
|
| 148 |
+
references in the HTML resolve when the zip is extracted."""
|
| 149 |
+
import zipfile
|
| 150 |
+
runner = CliRunner()
|
| 151 |
+
out = tmp_path / "guide.zip"
|
| 152 |
+
frames_dir = tmp_path / "static"
|
| 153 |
+
result = runner.invoke(
|
| 154 |
+
app,
|
| 155 |
+
[
|
| 156 |
+
"build",
|
| 157 |
+
str(VIDEO),
|
| 158 |
+
str(TRANSCRIPT),
|
| 159 |
+
"--title", "Audio Zip",
|
| 160 |
+
"--output", str(out),
|
| 161 |
+
"--frames-dir", str(frames_dir),
|
| 162 |
+
"--format", "zip",
|
| 163 |
+
"--max-frames", "2",
|
| 164 |
+
],
|
| 165 |
+
)
|
| 166 |
+
assert result.exit_code == 0, result.stdout + (result.stderr or "")
|
| 167 |
+
with zipfile.ZipFile(out, "r") as zf:
|
| 168 |
+
names = zf.namelist()
|
| 169 |
+
html_name = next((n for n in names if n.endswith(".html")), None)
|
| 170 |
+
assert html_name is not None
|
| 171 |
+
html = zf.read(html_name).decode("utf-8")
|
| 172 |
+
if "<audio" in html:
|
| 173 |
+
audio_entries = [n for n in names if n.startswith("audio/")]
|
| 174 |
+
assert audio_entries, "html references audio but zip has no audio/ folder"
|
| 175 |
+
for entry in audio_entries:
|
| 176 |
+
assert entry.endswith(".mp3")
|
| 177 |
+
|
| 178 |
+
|
| 179 |
@requires_media
|
| 180 |
def test_cli_build_writes_html_and_metadata(tmp_path):
|
| 181 |
runner = CliRunner()
|
|
@@ -64,6 +64,93 @@ def test_render_guide_inline_images_embeds_data_uri(tmp_path):
|
|
| 64 |
assert "data:image/jpeg;base64," in html
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def test_build_segments_assigns_default_section_titles(tmp_path):
|
| 68 |
img1 = tmp_path / "scene_0000.jpg"
|
| 69 |
img2 = tmp_path / "scene_0001.jpg"
|
|
|
|
| 64 |
assert "data:image/jpeg;base64," in html
|
| 65 |
|
| 66 |
|
| 67 |
+
def test_render_guide_zip_mode_uses_audio_subfolder_path(tmp_path):
|
| 68 |
+
# When not inlining, the template must emit audio/<filename> for each
|
| 69 |
+
# segment that has an audio_filename — bundle_zip writes to that same
|
| 70 |
+
# 'audio/' arcname.
|
| 71 |
+
img = tmp_path / "scene_0000.jpg"
|
| 72 |
+
_make_image(img)
|
| 73 |
+
frame = SceneFrame(0, 0.0, 5.0, img)
|
| 74 |
+
segments = build_segments([frame], [["hello"]], [""], inline_images=False)
|
| 75 |
+
segments[0].audio_filename = "segment_0000.mp3"
|
| 76 |
+
html = render_guide("Zip", segments, inline_images=False)
|
| 77 |
+
assert "audio/segment_0000.mp3" in html
|
| 78 |
+
assert "data:audio/mpeg;base64," not in html
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_render_guide_single_mode_inlines_audio_data_uri(tmp_path):
|
| 82 |
+
img = tmp_path / "scene_0000.jpg"
|
| 83 |
+
_make_image(img)
|
| 84 |
+
frame = SceneFrame(0, 0.0, 5.0, img)
|
| 85 |
+
segments = build_segments([frame], [["hello"]], [""], inline_images=True)
|
| 86 |
+
segments[0].audio_data_uri = "data:audio/mpeg;base64,AAAA"
|
| 87 |
+
html = render_guide("Single", segments, inline_images=True)
|
| 88 |
+
# Inline data URI present, no relative-folder reference leaking through.
|
| 89 |
+
assert "data:audio/mpeg;base64,AAAA" in html
|
| 90 |
+
assert "audio/segment_0000.mp3" not in html
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def test_metadata_round_trip_preserves_audio_filename(tmp_path):
|
| 94 |
+
from app.pipeline.metadata import (
|
| 95 |
+
SegmentMetadata,
|
| 96 |
+
load_metadata,
|
| 97 |
+
page_metadata_from_segments,
|
| 98 |
+
dump_metadata,
|
| 99 |
+
)
|
| 100 |
+
img = tmp_path / "scene_0000.jpg"
|
| 101 |
+
_make_image(img)
|
| 102 |
+
frame = SceneFrame(0, 0.0, 5.0, img)
|
| 103 |
+
segments = build_segments([frame], [["hello"]], [""], inline_images=False)
|
| 104 |
+
segments[0].audio_filename = "segment_0000.mp3"
|
| 105 |
+
page = page_metadata_from_segments(title="T", segments=segments)
|
| 106 |
+
out = tmp_path / "meta.json"
|
| 107 |
+
dump_metadata(out, page)
|
| 108 |
+
loaded = load_metadata(out)
|
| 109 |
+
assert isinstance(loaded.segments[0], SegmentMetadata)
|
| 110 |
+
assert loaded.segments[0].audio_filename == "segment_0000.mp3"
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def test_metadata_to_segment_inlines_audio_when_sibling_audio_dir_present(tmp_path):
|
| 114 |
+
from app.pipeline.html_gen import metadata_to_segment
|
| 115 |
+
from app.pipeline.metadata import SegmentMetadata
|
| 116 |
+
frames_dir = tmp_path / "static"
|
| 117 |
+
frames_dir.mkdir()
|
| 118 |
+
img = frames_dir / "scene_0000.jpg"
|
| 119 |
+
_make_image(img)
|
| 120 |
+
audio_dir = tmp_path / "audio"
|
| 121 |
+
audio_dir.mkdir()
|
| 122 |
+
(audio_dir / "segment_0000.mp3").write_bytes(b"\xff\xfb fake mp3")
|
| 123 |
+
sm = SegmentMetadata(
|
| 124 |
+
index=0, start_seconds=0.0, end_seconds=5.0,
|
| 125 |
+
image_filename="scene_0000.jpg", section_title="t", alt_text="a",
|
| 126 |
+
audio_filename="segment_0000.mp3",
|
| 127 |
+
)
|
| 128 |
+
seg = metadata_to_segment(sm, frames_dir, inline_images=True)
|
| 129 |
+
assert seg.audio_data_uri is not None
|
| 130 |
+
assert seg.audio_data_uri.startswith("data:audio/mpeg;base64,")
|
| 131 |
+
assert seg.audio_filename == "segment_0000.mp3"
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def test_metadata_to_segment_no_audio_when_inline_false(tmp_path):
|
| 135 |
+
from app.pipeline.html_gen import metadata_to_segment
|
| 136 |
+
from app.pipeline.metadata import SegmentMetadata
|
| 137 |
+
frames_dir = tmp_path / "static"
|
| 138 |
+
frames_dir.mkdir()
|
| 139 |
+
(tmp_path / "audio").mkdir()
|
| 140 |
+
(tmp_path / "audio" / "segment_0000.mp3").write_bytes(b"x")
|
| 141 |
+
img = frames_dir / "scene_0000.jpg"
|
| 142 |
+
_make_image(img)
|
| 143 |
+
sm = SegmentMetadata(
|
| 144 |
+
index=0, start_seconds=0.0, end_seconds=5.0,
|
| 145 |
+
image_filename="scene_0000.jpg", section_title="t", alt_text="a",
|
| 146 |
+
audio_filename="segment_0000.mp3",
|
| 147 |
+
)
|
| 148 |
+
seg = metadata_to_segment(sm, frames_dir, inline_images=False)
|
| 149 |
+
# Filename preserved so the template can emit audio/<fn>; no inlining.
|
| 150 |
+
assert seg.audio_filename == "segment_0000.mp3"
|
| 151 |
+
assert seg.audio_data_uri is None
|
| 152 |
+
|
| 153 |
+
|
| 154 |
def test_build_segments_assigns_default_section_titles(tmp_path):
|
| 155 |
img1 = tmp_path / "scene_0000.jpg"
|
| 156 |
img2 = tmp_path / "scene_0001.jpg"
|