Retire Python reporter and cross-language parity checks
Beffroi/beffroi-tools/pipeline/head This commit looks good
Details
Beffroi/beffroi-tools/pipeline/head This commit looks good
Details
This commit is contained in:
parent
de804a1afe
commit
a22de66e47
31
AGENTS.md
31
AGENTS.md
|
|
@ -8,20 +8,13 @@ with quick-start examples and links to detailed documentation in `docs/`.
|
|||
|
||||
- `crates/pptx-reporter/` is the Rust reporter: shared core, CLI and egui desktop interface.
|
||||
PDF compilation and DejaVu fonts are embedded. Discover FFprobe beside the app,
|
||||
under `bin/`, or on PATH. Keep report/JSON/Ontime behavior compatible with the
|
||||
Python reference; `scripts/parity.py` verifies real-media parity.
|
||||
The document exporter is outside the Rust migration scope.
|
||||
under `bin/`, or on PATH. It is the sole PPTX video reporter; the Python
|
||||
implementation and cross-language parity harness are retired. Preserve the
|
||||
established report/JSON/Ontime behavior with Rust regression tests in
|
||||
`crates/pptx-reporter/tests/`. Apply trims, retain looping/unsupported timing
|
||||
in reports, and omit those cases from Ontime and playback totals.
|
||||
The document exporter remains Python.
|
||||
|
||||
- `tools/pptx_videos.py` discovers and extracts embedded PPTX videos, maps them to
|
||||
slides, probes metadata with FFmpeg's `ffprobe`, and generates timing reports
|
||||
in Markdown, plain text, JSON, HTML, Typst, PDF, and Ontime 4 JSON. Markdown
|
||||
and Ontime are the defaults. PDF reports compile through the Typst CLI.
|
||||
Its output root contains `videos/` for media and `reports/` for reports and
|
||||
Typst sources; report media links are relative to `reports/`.
|
||||
- `tools/ontime.py` maps video references into an importable Ontime rundown.
|
||||
- `tools/playback.py` reads per-shape trims and flags looping/unsupported timing.
|
||||
Keep these cases in reports but omit them from Ontime and playback totals.
|
||||
`tests/test_playback.py` covers the XML and effective-duration behavior.
|
||||
- `tools/progress.py` provides shared transient terminal progress and plain logs
|
||||
for redirected output; `tests/test_progress.py` checks display cleanup.
|
||||
- `tools/exporter.py` uses DeckCraft to export PPTX presentations to PDF or PNG slides,
|
||||
|
|
@ -33,9 +26,7 @@ with quick-start examples and links to detailed documentation in `docs/`.
|
|||
output or replace existing files.
|
||||
- `tests/test_exporter.py` covers exporter validation, no-op behavior, conversion
|
||||
failures, and overwrite behavior.
|
||||
- `tests/test_pptx_videos.py` covers slide discovery, extraction, temporary probing,
|
||||
report formatting, and error handling.
|
||||
- `Jenkinsfile` runs Python/Rust tests, parity, lint, docs and coverage on
|
||||
- `Jenkinsfile` runs Python/Rust tests, lint, docs and coverage on
|
||||
`rust_no_docker`. The explicitly requested badge workflow archives exactly
|
||||
three SVGs and publishes them only from main during cleanup. CI never creates
|
||||
releases or uploads release binaries. Cross and `rust_realease` are separate,
|
||||
|
|
@ -49,8 +40,8 @@ with quick-start examples and links to detailed documentation in `docs/`.
|
|||
- `tools/` and `tests/`: Python prototypes and their regression tests.
|
||||
- `crates/<tool>/`: each Rust port owns its source, assets, build script and tests.
|
||||
- Root Cargo workspace: shared version/metadata, lockfile, profiles and target directory.
|
||||
- Keep Python references while parity is useful; add future Rust tools as workspace
|
||||
members and include every member in CI checks and coverage.
|
||||
- Retire Python prototypes once their Rust replacements are validated. Add future
|
||||
Rust tools as workspace members and include every member in CI checks and coverage.
|
||||
|
||||
## Working conventions
|
||||
|
||||
|
|
@ -65,8 +56,8 @@ with quick-start examples and links to detailed documentation in `docs/`.
|
|||
- Keep tools in `tools/`, tests in `tests/`, and specialized documentation in
|
||||
`docs/`. Update the relevant docs when options or workflows change; keep the
|
||||
README's examples and links current.
|
||||
- Run checks appropriate to the change before committing. Exporter regression
|
||||
tests and video-report tests run with
|
||||
- Run checks appropriate to the change before committing. Python exporter, CI and packaging
|
||||
tests run with
|
||||
`python3 -m unittest discover -s tests -v`. For rendering changes,
|
||||
test with the actual external tools when available and inspect visual output.
|
||||
Report any checks that could not be completed.
|
||||
|
|
|
|||
|
|
@ -59,9 +59,6 @@ pipeline {
|
|||
python3 -m unittest discover -s tests -v
|
||||
cargo test --locked --workspace --all-features
|
||||
cargo build --locked --workspace --all-features
|
||||
export PATH="$PWD/downloads/ci-tools/bin:$PATH"
|
||||
python3 scripts/ci_tools.py
|
||||
python3 scripts/parity.py target/debug/pptx-reporter
|
||||
python3 scripts/ci.py tests-passed
|
||||
'''
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ opening reports, and reading video timings.
|
|||
## More documentation
|
||||
|
||||
- [Reporter](docs/rust-reporter.md) · [Windows quick start](docs/windows-quickstart.txt)
|
||||
- [Report options and Python reference](docs/pptx-videos.md)
|
||||
- [Report formats and timing semantics](docs/pptx-videos.md)
|
||||
- [Document exports](docs/exporter.md)
|
||||
- [Development](docs/development.md) · [CI and coverage](docs/ci.md)
|
||||
- [Cross builds and portable releases](docs/releases.md)
|
||||
|
|
|
|||
25
docs/ci.md
25
docs/ci.md
|
|
@ -3,23 +3,18 @@
|
|||
Jenkins must use a Multibranch Pipeline supplying `BRANCH_NAME`, with the
|
||||
`rust_no_docker` worker, Python 3.11+ and Rust.
|
||||
SCM credentials, webhooks and the publication mount are administrator setup.
|
||||
CI checks formatting, Clippy, rustdoc, Python/Rust tests, real-media parity and
|
||||
CI checks formatting, Clippy, rustdoc, Python/Rust tests and
|
||||
whole-workspace Rust coverage. It never creates tags or release artifacts.
|
||||
|
||||
Parity reuses FFmpeg, FFprobe and Typst from PATH when available. Otherwise
|
||||
`scripts/ci_tools.py` fetches checksum-verified FFmpeg/FFprobe on Linux x86-64
|
||||
and installs locked `typst-cli 0.13.1` with Cargo under ignored
|
||||
`downloads/ci-tools/`. No sudo is needed; missing tools require network access
|
||||
and a first-time Typst build can take several minutes. To provision locally:
|
||||
The Python reporter and cross-language parity checks are retired. CI uses the
|
||||
Rust reporter's own regression tests and embedded PDF engine, plus Python
|
||||
exporter and CI/packaging tests. It does not provision FFmpeg, FFprobe or the
|
||||
Typst CLI. FFprobe remains an application runtime dependency and is fetched
|
||||
separately when packaging portable releases.
|
||||
|
||||
```sh
|
||||
export PATH="$PWD/downloads/ci-tools/bin:$PATH"
|
||||
python3 scripts/ci_tools.py
|
||||
```
|
||||
|
||||
A test, provisioning or parity failure still fails the build, but does not skip
|
||||
the independent coverage stage. The test-passed marker requires all of them to
|
||||
succeed; fresh measured coverage can therefore accompany a failing test badge.
|
||||
A test failure still fails the build but does not skip the independent coverage
|
||||
stage. The test-passed marker requires all required tests to succeed; fresh
|
||||
measured coverage can therefore accompany a failing test badge.
|
||||
|
||||
## Measurement
|
||||
|
||||
|
|
@ -44,7 +39,7 @@ unrounded covered/count totals, logs the measurement and floor before PASS/FAIL,
|
|||
and retains tool failure status even when a valid measurement exists.
|
||||
|
||||
`init` removes stale output. CI creates `.tests-passed-${BUILD_NUMBER}` only
|
||||
after all required tests and parity pass. A valid measurement creates
|
||||
after all required tests pass. A valid measurement creates
|
||||
`.coverage-ready-${BUILD_NUMBER}` even below the floor. Cleanup preserves that
|
||||
measurement; unavailable coverage becomes neutral `-%`. Temporary
|
||||
`coverage/summary.json`, diagnostics and markers are deleted after processing.
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@
|
|||
| `tools/` | Python prototypes and document utilities |
|
||||
| `tests/` | Python regression and CI/packaging tests |
|
||||
| `crates/<tool>/` | Rust ports, each with its own source, tests and assets |
|
||||
| `scripts/` | Shared CI, parity and release helpers |
|
||||
| `scripts/` | Shared CI and release helpers |
|
||||
| `docs/` | Tool usage and specialized workflows |
|
||||
|
||||
Prototype in Python, then add a Rust workspace member when the workflow is
|
||||
stable. Keep the Python implementation as a parity reference while useful.
|
||||
stable. Retire the Python implementation once the Rust port is validated.
|
||||
The PPTX reporter is now Rust-only; the document exporter remains Python.
|
||||
Avoid speculative shared crates; extract common code when multiple tools need it.
|
||||
|
||||
The root `Cargo.toml` owns workspace metadata, the shared version and build
|
||||
|
|
@ -31,13 +32,14 @@ cargo test --locked --workspace --all-features
|
|||
RUSTDOCFLAGS="-D warnings" cargo doc --locked --workspace --all-features --no-deps
|
||||
python3 -m unittest discover -s tests -v
|
||||
cargo build --locked --workspace --all-features
|
||||
python3 scripts/parity.py target/debug/pptx-reporter
|
||||
```
|
||||
|
||||
Parity requires FFmpeg, FFprobe and the Typst CLI for the Python reference.
|
||||
It compares JSON/Ontime for real media with trims, loops, custom timing and
|
||||
orphan media, and generates every report format. Python exporter tests mock
|
||||
external document applications. Inspect rendering changes visually as well.
|
||||
Rust regression tests cover archive parsing, trimming, loops, report formats,
|
||||
Ontime, CLI errors and GUI state; both PDF layouts compile with the embedded
|
||||
engine. Probe-process tests use a controlled stub. No external FFmpeg, FFprobe
|
||||
or Typst CLI is required for CI. The application still needs FFprobe at runtime.
|
||||
Python exporter tests mock external document applications. Inspect rendering
|
||||
changes visually as well.
|
||||
|
||||
`cargo build -p pptx-reporter --no-default-features` builds the CLI without the
|
||||
GUI or PDF engine; add `--features pdf` to restore embedded PDFs.
|
||||
|
|
|
|||
|
|
@ -1,25 +1,26 @@
|
|||
# Report options and Python reference
|
||||
# Report formats and timing semantics
|
||||
|
||||
Report semantics apply to both implementations. Commands below use the Python
|
||||
reference; for the desktop app and Rust CLI see [Reporter](rust-reporter.md).
|
||||
Only the Python implementation needs the external Typst CLI for PDFs.
|
||||
Use the Rust CLI below, or see the [illustrated desktop guide](user-guide.md).
|
||||
Run source commands from the repository root. With a prebuilt CLI, replace
|
||||
`cargo run --locked --bin pptx-reporter --` with `pptx-reporter` (or
|
||||
`pptx-reporter.exe` on Windows). The Python reporter has been retired.
|
||||
|
||||
Extract videos and create Markdown and Ontime timing reports:
|
||||
|
||||
```sh
|
||||
python3 tools/pptx_videos.py presentation.pptx
|
||||
cargo run --locked --bin pptx-reporter -- presentation.pptx
|
||||
```
|
||||
|
||||
Generate one or more report formats without keeping newly extracted videos:
|
||||
|
||||
```sh
|
||||
python3 tools/pptx_videos.py presentation.pptx --timings-only --report html,pdf
|
||||
python3 tools/pptx_videos.py presentation.pptx --report all
|
||||
cargo run --locked --bin pptx-reporter -- presentation.pptx --timings-only --report html,pdf
|
||||
cargo run --locked --bin pptx-reporter -- presentation.pptx --report all
|
||||
```
|
||||
|
||||
`--report` accepts comma-separated formats and can be repeated. Markdown and
|
||||
Ontime are included by default; an explicit `--report` replaces this selection.
|
||||
Use `--report all` for every format, including both PDF layouts (requires Typst)
|
||||
Use `--report all` for every format, including both built-in PDF layouts
|
||||
and Ontime.
|
||||
The output root is `<presentation-stem>_videos`, or the directory supplied with
|
||||
`-o`. Extracted media goes in `videos/`; all reports and Typst sources go in
|
||||
|
|
@ -39,7 +40,7 @@ relative to the report directory too. `--timings-only` reuses media in `videos/`
|
|||
when available, otherwise probes temporary copies without creating that folder.
|
||||
`--extract-only` creates no report folder.
|
||||
|
||||
PDF uses the Typst CLI and retains `timings.typ`
|
||||
PDF uses the embedded Typst engine and retains `timings.typ`
|
||||
for editing. HTML and Typst/PDF include colored summary cards and pills;
|
||||
Markdown and TXT use aligned tables. JSON includes numeric durations in seconds,
|
||||
sizes in bytes, and slide references.
|
||||
|
|
@ -47,7 +48,7 @@ sizes in bytes, and slide references.
|
|||
For a compact portrait PDF with slide numbers, media names, and duration pills:
|
||||
|
||||
```sh
|
||||
python3 tools/pptx_videos.py presentation.pptx --timings-only --report pdf-dense
|
||||
cargo run --locked --bin pptx-reporter -- presentation.pptx --timings-only --report pdf-dense
|
||||
```
|
||||
|
||||
This creates `timings-dense.pdf` and its editable `timings-dense.typ` source.
|
||||
|
|
@ -58,8 +59,7 @@ from the start and end. Looping videos carry an amber **Looping** pill next to
|
|||
their name and a dash instead of a finite duration.
|
||||
Use `--report pdf,pdf-dense` to produce both layouts.
|
||||
|
||||
Timings require `ffprobe` (from FFmpeg); PDF additionally requires `typst` on
|
||||
`PATH`. Use `--extract-only` to extract without a report. Presentations with no
|
||||
Timings require `ffprobe` (from FFmpeg); PDF generation needs no external CLI. Use `--extract-only` to extract without a report. Presentations with no
|
||||
embedded videos produce no output.
|
||||
|
||||
## Playback durations and trimming
|
||||
|
|
@ -103,7 +103,7 @@ and [animation timing properties](https://learn.microsoft.com/en-us/dotnet/api/d
|
|||
## Ontime import
|
||||
|
||||
```sh
|
||||
python3 tools/pptx_videos.py presentation.pptx --timings-only --report ontime
|
||||
cargo run --locked --bin pptx-reporter -- presentation.pptx --timings-only --report ontime
|
||||
```
|
||||
|
||||
This writes `timings.ontime.json`, a native **Ontime 4** project. It is separate
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Supply missing parity tools locally, without sudo or changing the worker."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import platform
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
from .fetch_ffprobe import fetch
|
||||
except ImportError:
|
||||
from fetch_ffprobe import fetch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
TYPST_VERSION = "0.13.1"
|
||||
|
||||
|
||||
def prepare():
|
||||
install = ROOT / "downloads/ci-tools"
|
||||
bindir = install / "bin"
|
||||
bindir.mkdir(parents=True, exist_ok=True)
|
||||
os.environ["PATH"] = str(bindir) + os.pathsep + os.environ.get("PATH", "")
|
||||
for program in ("ffmpeg", "ffprobe"):
|
||||
if not shutil.which(program):
|
||||
if platform.system() != "Linux" or platform.machine().lower() not in ("x86_64", "amd64"):
|
||||
raise RuntimeError(f"Install {program} on PATH; automatic CI provisioning needs Linux x86-64")
|
||||
binary, _ = fetch("x86_64-unknown-linux-gnu", program=program)
|
||||
link = bindir / program
|
||||
link.unlink(missing_ok=True)
|
||||
link.symlink_to(binary.resolve())
|
||||
subprocess.run([program, "-version"], check=True, stdout=subprocess.DEVNULL, timeout=30)
|
||||
print(f"Parity dependency ready: {program}", flush=True)
|
||||
if not shutil.which("typst"):
|
||||
subprocess.run(["cargo", "install", "typst-cli", "--version", TYPST_VERSION,
|
||||
"--locked", "--root", str(install)], check=True)
|
||||
subprocess.run(["typst", "--version"], check=True, timeout=30)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
prepare()
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Fetch verified FFprobe for releases; share validation with CI FFmpeg setup."""
|
||||
"""Fetch verified FFprobe binaries for release packaging, never into source control."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
|
|
@ -62,11 +62,9 @@ def validate_binary(path, target):
|
|||
raise ValueError(f"Unsupported target: {target}")
|
||||
|
||||
|
||||
def unpack(archive, destination, target, program="ffprobe"):
|
||||
"""Copy the selected program and notices without extracting archive paths."""
|
||||
if program not in ("ffprobe", "ffmpeg"):
|
||||
raise ValueError("Unsupported FFmpeg program")
|
||||
executable = program + (".exe" if "windows" in target else "")
|
||||
def unpack(archive, destination, target):
|
||||
"""Copy only FFprobe and upstream notices; never extract archive paths directly."""
|
||||
executable = "ffprobe.exe" if "windows" in target else "ffprobe"
|
||||
notices = destination / "licenses"
|
||||
notices.mkdir()
|
||||
found = False
|
||||
|
|
@ -84,7 +82,7 @@ def unpack(archive, destination, target, program="ffprobe"):
|
|||
raise ValueError(f"Archive member too large: {name}")
|
||||
if is_binary:
|
||||
if found or path.parent.name != "bin":
|
||||
raise ValueError(f"Expected exactly one bin/{executable} executable")
|
||||
raise ValueError("Expected exactly one bin/ffprobe executable")
|
||||
found = True
|
||||
output = destination / executable
|
||||
else:
|
||||
|
|
@ -109,19 +107,17 @@ def unpack(archive, destination, target, program="ffprobe"):
|
|||
continue
|
||||
member(info.name, info.size, info.isfile(), lambda i=info: bundle.extractfile(i))
|
||||
if not found or not licensed:
|
||||
raise ValueError(f"Archive must include {program} and its upstream license")
|
||||
raise ValueError("Archive must include FFprobe and its upstream license")
|
||||
binary = destination / executable
|
||||
validate_binary(binary, target)
|
||||
binary.chmod(0o755)
|
||||
return binary, notices
|
||||
|
||||
|
||||
def fetch(target, cache=None, release="latest", expected_sha256=None, program="ffprobe"):
|
||||
if program not in ("ffprobe", "ffmpeg"):
|
||||
raise ValueError("Unsupported FFmpeg program")
|
||||
def fetch(target, cache=None, release="latest", expected_sha256=None):
|
||||
if target not in ASSETS:
|
||||
raise ValueError(f"Unsupported target: {target}")
|
||||
cache = Path(cache) if cache is not None else ROOT / "downloads" / program
|
||||
cache = Path(cache) if cache is not None else ROOT / "downloads" / "ffprobe"
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.TemporaryDirectory(prefix=".fetch-", dir=cache) as temporary:
|
||||
temporary = Path(temporary)
|
||||
|
|
@ -142,9 +138,7 @@ def fetch(target, cache=None, release="latest", expected_sha256=None, program="f
|
|||
if not url.startswith("https://github.com/BtbN/FFmpeg-Builds/releases/download/"):
|
||||
raise ValueError("Unexpected upstream download URL")
|
||||
folder = cache / target / digest
|
||||
if program != "ffprobe":
|
||||
folder = cache / program / target / digest
|
||||
binary = folder / (program + (".exe" if "windows" in target else ""))
|
||||
binary = folder / ("ffprobe.exe" if "windows" in target else "ffprobe")
|
||||
notices = folder / "licenses"
|
||||
if folder.exists():
|
||||
if folder.is_symlink():
|
||||
|
|
@ -160,7 +154,7 @@ def fetch(target, cache=None, release="latest", expected_sha256=None, program="f
|
|||
if binary.name not in receipt["files"] or receipt["archive_sha256"] != digest:
|
||||
raise ValueError("Invalid FFprobe cache receipt")
|
||||
validate_binary(binary, target)
|
||||
print(f"Using verified {program} cache: {binary}", flush=True)
|
||||
print(f"Using verified FFprobe cache: {binary}", flush=True)
|
||||
return binary, notices
|
||||
archive = temporary / asset["name"]
|
||||
print(f"Downloading {asset['name']} ({asset['size'] / 1048576:.1f} MiB)", flush=True)
|
||||
|
|
@ -169,7 +163,7 @@ def fetch(target, cache=None, release="latest", expected_sha256=None, program="f
|
|||
raise ValueError("FFprobe archive SHA-256 mismatch; nothing installed")
|
||||
stage = temporary / "bundle"
|
||||
stage.mkdir()
|
||||
staged_binary, staged_notices = unpack(archive, stage, target, program)
|
||||
staged_binary, staged_notices = unpack(archive, stage, target)
|
||||
native = platform.machine().lower() in ("amd64", "x86_64") and (
|
||||
(os.name == "nt" and "windows" in target)
|
||||
or (platform.system() == "Linux" and "linux" in target))
|
||||
|
|
@ -178,8 +172,8 @@ def fetch(target, cache=None, release="latest", expected_sha256=None, program="f
|
|||
result = subprocess.run([str(staged_binary.resolve()), "-version"], check=True,
|
||||
capture_output=True, text=True, timeout=30)
|
||||
lines = result.stdout.splitlines()
|
||||
if not lines or not lines[0].startswith(f"{program} version "):
|
||||
raise ValueError(f"Downloaded executable did not identify itself as {program}")
|
||||
if not lines or not lines[0].startswith("ffprobe version "):
|
||||
raise ValueError("Downloaded executable did not identify itself as FFprobe")
|
||||
version = lines[0]
|
||||
receipt = {
|
||||
"provider": "https://github.com/BtbN/FFmpeg-Builds",
|
||||
|
|
@ -192,7 +186,7 @@ def fetch(target, cache=None, release="latest", expected_sha256=None, program="f
|
|||
(staged_notices / "provenance.json").write_text(json.dumps(receipt, indent=2) + "\n")
|
||||
folder.parent.mkdir(parents=True, exist_ok=True)
|
||||
stage.rename(folder)
|
||||
print(f"Verified {program}: {binary}", flush=True)
|
||||
print(f"Verified FFprobe: {binary}", flush=True)
|
||||
return binary, notices
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,65 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Compare Rust results to the Python reference using real media and both PDF layouts."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import zipfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
from tests.test_playback import PlaybackTests, picture, timing
|
||||
|
||||
|
||||
def main():
|
||||
binary = Path(sys.argv[1]).resolve()
|
||||
for tool in ("ffmpeg", "ffprobe", "typst"):
|
||||
if not shutil.which(tool):
|
||||
raise SystemExit(f"Parity check requires {tool} on PATH.")
|
||||
with tempfile.TemporaryDirectory(prefix="reporter-parity-") as temporary:
|
||||
root = Path(temporary)
|
||||
clip = root / "clip.mp4"
|
||||
subprocess.run(["ffmpeg", "-v", "error", "-f", "lavfi", "-i", "color=c=blue:s=32x24:r=10",
|
||||
"-t", "10", "-c:v", "mpeg4", str(clip)], check=True)
|
||||
fixture = PlaybackTests()
|
||||
fixture.setUp()
|
||||
try:
|
||||
source = fixture.deck([
|
||||
(picture("2", '<p14:trim st="1500" end="2s"/>') + picture("3", '<p14:trim end="1s"/>'),
|
||||
timing("2", 'dur="indefinite"')),
|
||||
(picture("2", '<p14:trim st="1s"/>'), timing("2", 'repeatCount="indefinite"')),
|
||||
(picture("2"), timing("2", 'spd="200000"')),
|
||||
(picture("2", '<p14:trim st="-1"/>'), ''),
|
||||
(picture("2"), timing("99", 'repeatCount="indefinite"')),
|
||||
(picture("2"), timing("2", children='<p:endCondLst><p:cond evt="onClick"/></p:endCondLst>')),
|
||||
(picture("2"), timing("2", media_attrs='numSld="2"')),
|
||||
])
|
||||
presentation = root / "Présentation & <review>.pptx"
|
||||
with zipfile.ZipFile(source) as old, zipfile.ZipFile(presentation, "w") as new:
|
||||
for name in old.namelist():
|
||||
new.writestr(name, clip.read_bytes() if name.endswith(".mp4") else old.read(name))
|
||||
new.writestr("ppt/media/orphan.mp4", clip.read_bytes())
|
||||
for runner, output in [([sys.executable, str(ROOT / "tools/pptx_videos.py")], root / "python"),
|
||||
([str(binary)], root / "rust")]:
|
||||
subprocess.run([*runner, str(presentation), "--timings-only", "--report", "all",
|
||||
"-o", str(output)], check=True)
|
||||
assert not (output / "videos").exists()
|
||||
for filename in ("timings.json", "timings.ontime.json"):
|
||||
expected = json.loads((root / "python/reports" / filename).read_text())
|
||||
actual = json.loads((root / "rust/reports" / filename).read_text())
|
||||
if actual != expected:
|
||||
raise AssertionError(f"{filename} differs:\nPython: {expected}\nRust: {actual}")
|
||||
for name in ("timings.pdf", "timings-dense.pdf"):
|
||||
assert (root / "rust/reports" / name).read_bytes().startswith(b"%PDF")
|
||||
# Optional retained review samples, only at an explicitly supplied location.
|
||||
if len(sys.argv) > 2:
|
||||
shutil.copytree(root / "rust", Path(sys.argv[2]), dirs_exist_ok=True)
|
||||
print("PASS: Python/Rust report and Ontime JSON match; all formats and both PDFs generated.")
|
||||
finally:
|
||||
fixture.doCleanups()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from scripts import ci_tools
|
||||
|
||||
|
||||
class CIToolsTests(unittest.TestCase):
|
||||
def test_installed_tools_are_checked_without_downloads(self):
|
||||
with tempfile.TemporaryDirectory() as temp, patch.object(ci_tools, "ROOT", Path(temp)), \
|
||||
patch.dict(os.environ), patch.object(ci_tools.shutil, "which", return_value="/usr/bin/tool"), \
|
||||
patch.object(ci_tools, "fetch") as fetch, patch.object(ci_tools.subprocess, "run") as run:
|
||||
ci_tools.prepare()
|
||||
fetch.assert_not_called()
|
||||
self.assertEqual([call.args[0][0] for call in run.call_args_list], ["ffmpeg", "ffprobe", "typst"])
|
||||
|
||||
def test_missing_tools_are_provisioned_locally_and_typst_is_pinned(self):
|
||||
with tempfile.TemporaryDirectory() as temp, patch.object(ci_tools, "ROOT", Path(temp)), \
|
||||
patch.dict(os.environ), patch.object(ci_tools.shutil, "which", return_value=None), \
|
||||
patch.object(ci_tools.platform, "system", return_value="Linux"), \
|
||||
patch.object(ci_tools.platform, "machine", return_value="x86_64"), \
|
||||
patch.object(ci_tools.subprocess, "run") as run:
|
||||
binary = Path(temp) / "downloaded"
|
||||
binary.write_bytes(b"fixture")
|
||||
with patch.object(ci_tools, "fetch", return_value=(binary, Path(temp))) as fetch:
|
||||
ci_tools.prepare()
|
||||
self.assertEqual([c.kwargs["program"] for c in fetch.call_args_list], ["ffmpeg", "ffprobe"])
|
||||
self.assertEqual((Path(temp) / "downloads/ci-tools/bin/ffmpeg").resolve(), binary)
|
||||
install = next(c.args[0] for c in run.call_args_list if c.args[0][0] == "cargo")
|
||||
self.assertIn("--locked", install)
|
||||
self.assertEqual(install[install.index("--version") + 1], "0.13.1")
|
||||
|
||||
def test_download_failure_propagates(self):
|
||||
with tempfile.TemporaryDirectory() as temp, patch.object(ci_tools, "ROOT", Path(temp)), \
|
||||
patch.dict(os.environ), patch.object(ci_tools.shutil, "which", return_value=None), \
|
||||
patch.object(ci_tools.platform, "system", return_value="Linux"), \
|
||||
patch.object(ci_tools.platform, "machine", return_value="x86_64"), \
|
||||
patch.object(ci_tools, "fetch", side_effect=ValueError("checksum mismatch")):
|
||||
with self.assertRaisesRegex(ValueError, "checksum mismatch"):
|
||||
ci_tools.prepare()
|
||||
|
|
@ -66,27 +66,6 @@ class FetchTests(unittest.TestCase):
|
|||
fetcher.fetch(WINDOWS, cache)
|
||||
self.assertFalse(list(cache.glob(".fetch-*")))
|
||||
|
||||
def test_ci_ffmpeg_selection_and_cache_are_separate(self):
|
||||
content = bundle([("build/bin/ffprobe.exe", pe()), ("build/bin/ffmpeg.exe", pe()),
|
||||
("build/LICENSE", b"notice")])
|
||||
digest = hashlib.sha256(content).hexdigest()
|
||||
name = fetcher.ASSETS[WINDOWS]
|
||||
release = {"id": 1, "assets": [{"id": 2, "name": name, "digest": "sha256:" + digest,
|
||||
"size": len(content), "browser_download_url":
|
||||
"https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/" + name}]}
|
||||
def download(url, destination, limit=None):
|
||||
destination.write_bytes(json.dumps(release).encode() if url.startswith(fetcher.API) else content)
|
||||
with tempfile.TemporaryDirectory() as temp, patch.object(fetcher, "download", side_effect=download), \
|
||||
patch.object(fetcher.platform, "machine", return_value="test-host"):
|
||||
encoder, _ = fetcher.fetch(WINDOWS, Path(temp), program="ffmpeg")
|
||||
probe, _ = fetcher.fetch(WINDOWS, Path(temp))
|
||||
self.assertEqual(encoder.name, "ffmpeg.exe")
|
||||
self.assertEqual(probe.name, "ffprobe.exe")
|
||||
self.assertNotEqual(encoder.parent, probe.parent)
|
||||
self.assertEqual(fetcher.fetch(WINDOWS, Path(temp), program="ffmpeg")[0], encoder)
|
||||
with self.assertRaisesRegex(ValueError, "Unsupported"):
|
||||
fetcher.fetch(WINDOWS, Path(temp), program="other")
|
||||
|
||||
def test_checksum_mismatch_leaves_no_installed_files(self):
|
||||
name = fetcher.ASSETS[WINDOWS]
|
||||
release = {"id": 1, "assets": [{"id": 2, "name": name, "digest": "sha256:" + "0" * 64,
|
||||
|
|
|
|||
|
|
@ -1,167 +0,0 @@
|
|||
import contextlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import zipfile
|
||||
|
||||
from tools import pptx_videos as videos
|
||||
from tools.playback import P14, time_offset
|
||||
|
||||
|
||||
def picture(shape_id, trim=''):
|
||||
return f'''
|
||||
<p:pic><p:nvPicPr><p:cNvPr id="{shape_id}" name="Video"/>
|
||||
<p:nvPr><a:videoFile r:link="legacy"/>
|
||||
<p:extLst><p:ext uri="media"><p14:media r:embed="media">{trim}</p14:media></p:ext></p:extLst>
|
||||
</p:nvPr></p:nvPicPr></p:pic>'''
|
||||
|
||||
|
||||
def timing(shape_id, attrs='', children='', media_attrs=''):
|
||||
return f'''<p:video><p:cMediaNode {media_attrs}>
|
||||
<p:cTn id="{shape_id}" {attrs}>{children}</p:cTn>
|
||||
<p:tgtEl><p:spTgt spid="{shape_id}"/></p:tgtEl>
|
||||
</p:cMediaNode></p:video>'''
|
||||
|
||||
|
||||
class PlaybackTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
|
||||
def deck(self, slides):
|
||||
path = self.root / 'trimmed.pptx'
|
||||
with zipfile.ZipFile(path, 'w') as archive:
|
||||
archive.writestr('ppt/media/shared.mp4', b'media')
|
||||
for index, (shapes, animation) in enumerate(slides, 1):
|
||||
archive.writestr(f'ppt/slides/slide{index}.xml', f'''
|
||||
<p:sld xmlns:p="{videos.P_NS}" xmlns:p14="{P14}"
|
||||
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="{videos.R_NS}">
|
||||
<p:cSld><p:spTree>{shapes}</p:spTree></p:cSld>
|
||||
<p:timing><p:tnLst>{animation}</p:tnLst></p:timing>
|
||||
</p:sld>''')
|
||||
archive.writestr(f'ppt/slides/_rels/slide{index}.xml.rels', f'''
|
||||
<Relationships xmlns="{videos.REL_NS}">
|
||||
<Relationship Id="legacy" Target="../media/shared.mp4" Type="example/video"/>
|
||||
<Relationship Id="media" Target="../media/shared.mp4" Type="example/media"/>
|
||||
</Relationships>''')
|
||||
return path
|
||||
|
||||
def report(self, slides, duration=10):
|
||||
path = self.deck(slides)
|
||||
with zipfile.ZipFile(path) as archive:
|
||||
parts, uses, external = videos.discover(archive)
|
||||
settings = videos.playback_settings(archive, parts)
|
||||
return videos.report_data(path, [('ppt/media/shared.mp4', 'shared.mp4', 5, duration, '32×24', 'mpeg4')],
|
||||
uses, external, self.root, settings)
|
||||
|
||||
def test_time_offset_units_and_invalid_values(self):
|
||||
for value, expected in [('1500', 1.5), ('1.25s', 1.25), ('1min', 60), ('0.5h', 1800),
|
||||
('250ms', .25), ('1000µs', .001), ('1000μs', .001), ('1000000ns', .001)]:
|
||||
self.assertAlmostEqual(time_offset(value), expected)
|
||||
for value in ('-1', 'NaN', 'Infinity', '1:30', '2seconds', ''):
|
||||
with self.subTest(value=value), self.assertRaises(ValueError):
|
||||
time_offset(value)
|
||||
|
||||
def test_trim_end_is_amount_removed_not_end_timestamp(self):
|
||||
data = self.report([(picture('2', '<p14:trim st="1500" end="2s"/>'),
|
||||
timing('2', 'dur="indefinite"'))])
|
||||
self.assertEqual(len(data['playbacks']), 1) # legacy + p14 references are deduplicated
|
||||
playback = data['playbacks'][0]
|
||||
self.assertEqual(playback['duration_seconds'], 6.5)
|
||||
self.assertEqual(playback['original_duration_seconds'], 10)
|
||||
self.assertEqual(playback['trim_start_seconds'], 1.5)
|
||||
self.assertEqual(playback['trim_end_seconds'], 2)
|
||||
self.assertEqual(playback['playback_status'], 'trimmed')
|
||||
event = next(iter(json.loads(videos.render_report(data, 'ontime'))['rundowns']['default']['entries'].values()))
|
||||
self.assertEqual(event['duration'], 6500)
|
||||
|
||||
def test_distinct_trims_on_same_slide_and_other_slides(self):
|
||||
data = self.report([
|
||||
(picture('2', '<p14:trim st="1s"/>') + picture('3', '<p14:trim end="2s"/>'), ''),
|
||||
(picture('2', '<p14:trim st="1s" end="2s"/>'), ''),
|
||||
])
|
||||
self.assertEqual([p['duration_seconds'] for p in data['playbacks']], [9, 8, 7])
|
||||
self.assertEqual(data['embedded_videos'], 1)
|
||||
self.assertEqual(data['total_size_bytes'], 5)
|
||||
self.assertEqual(data['total_duration_seconds'], 24)
|
||||
self.assertEqual(data['total_media_duration_seconds'], 10)
|
||||
events = json.loads(videos.render_report(data, 'ontime'))['rundowns']['default']['entries']
|
||||
self.assertEqual(len(events), 3)
|
||||
self.assertEqual([e['duration'] for e in events.values()], [9000, 8000, 7000])
|
||||
|
||||
def test_loop_remains_in_all_reports_but_not_ontime_or_total(self):
|
||||
data = self.report([
|
||||
(picture('2'), timing('2', 'repeatCount="indefinite" dur="indefinite"')),
|
||||
(picture('2', '<p14:trim st="1s"/>'), timing('2', 'dur="indefinite"')),
|
||||
])
|
||||
self.assertEqual(data['playbacks'][0]['playback_status'], 'looping')
|
||||
self.assertIsNone(data['playbacks'][0]['duration_seconds'])
|
||||
self.assertEqual(data['total_duration_seconds'], 9)
|
||||
for format in ('md', 'txt', 'html', 'typst', 'pdf-dense'):
|
||||
with self.subTest(format=format):
|
||||
self.assertIn('Looping', videos.render_report(data, format))
|
||||
project = json.loads(videos.render_report(data, 'ontime'))
|
||||
events = list(project['rundowns']['default']['entries'].values())
|
||||
self.assertEqual(len(events), 1)
|
||||
self.assertEqual(events[0]['cue'], 'Slide 2')
|
||||
self.assertEqual(events[0]['timeStart'], 0)
|
||||
self.assertEqual(events[0]['duration'], 9000)
|
||||
|
||||
def test_only_loops_produce_empty_ontime_rundown(self):
|
||||
data = self.report([(picture('2'), timing('2', 'repeatCount="2000"'))])
|
||||
project = json.loads(videos.render_report(data, 'ontime'))
|
||||
self.assertEqual(project['rundowns']['default']['order'], [])
|
||||
self.assertEqual(project['rundowns']['default']['entries'], {})
|
||||
|
||||
def test_unsupported_timing_is_not_exported(self):
|
||||
cases = [
|
||||
timing('2', 'spd="200000"'),
|
||||
timing('2', 'autoRev="1"'),
|
||||
timing('2', 'dur="3000"'),
|
||||
timing('2', children='<p:endCondLst><p:cond evt="onClick"/></p:endCondLst>'),
|
||||
timing('2', media_attrs='numSld="2"'),
|
||||
]
|
||||
for animation in cases:
|
||||
with self.subTest(animation=animation):
|
||||
data = self.report([(picture('2'), animation)])
|
||||
self.assertEqual(data['playbacks'][0]['playback_status'], 'custom-timing')
|
||||
self.assertIsNone(data['playbacks'][0]['duration_seconds'])
|
||||
project = json.loads(videos.render_report(data, 'ontime'))
|
||||
self.assertEqual(project['rundowns']['default']['order'], [])
|
||||
|
||||
def test_inherited_repeat_and_unrelated_timing(self):
|
||||
repeated = '<p:par><p:cTn repeatCount="indefinite"><p:childTnLst>' + timing('2') + '</p:childTnLst></p:cTn></p:par>'
|
||||
self.assertEqual(self.report([(picture('2'), repeated)])['playbacks'][0]['playback_status'], 'looping')
|
||||
unrelated = timing('99', 'repeatCount="indefinite"')
|
||||
self.assertEqual(self.report([(picture('2'), unrelated)])['playbacks'][0]['duration_seconds'], 10)
|
||||
|
||||
def test_invalid_trim_is_visible_and_excluded(self):
|
||||
for trim in ('st="-1"', 'end="NaN"', 'st="8s" end="3s"', 'st="10s"'):
|
||||
with self.subTest(trim=trim):
|
||||
data = self.report([(picture('2', f'<p14:trim {trim}/>'), '')])
|
||||
self.assertEqual(data['playbacks'][0]['playback_status'], 'invalid-trim')
|
||||
self.assertIn('Invalid trim', videos.render_report(data, 'md'))
|
||||
project = json.loads(videos.render_report(data, 'ontime'))
|
||||
self.assertEqual(project['rundowns']['default']['entries'], {})
|
||||
|
||||
def test_end_to_end_timings_only_uses_trim_without_modifying_media(self):
|
||||
source = self.deck([(picture('2', '<p14:trim st="1s" end="2s"/>'), '')])
|
||||
with patch('sys.argv', ['pptx_videos.py', str(source), '--timings-only', '--report', 'json,ontime']), \
|
||||
patch.object(videos.shutil, 'which', return_value='/ffprobe'), \
|
||||
patch.object(videos, 'probe', return_value=(10, '32×24', 'mpeg4')), \
|
||||
contextlib.redirect_stdout(io.StringIO()):
|
||||
self.assertEqual(videos.main(), 0)
|
||||
output = self.root / 'trimmed_videos'
|
||||
self.assertFalse((output / 'videos').exists())
|
||||
data = json.loads((output / 'reports/timings.json').read_text())
|
||||
self.assertEqual(data['videos'][0]['duration_seconds'], 10)
|
||||
self.assertEqual(data['playbacks'][0]['duration_seconds'], 7)
|
||||
self.assertEqual(data['playbacks'][0]['original_duration_seconds'], 10)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -1,275 +0,0 @@
|
|||
from collections import defaultdict
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import zipfile
|
||||
|
||||
from tools import pptx_videos as videos
|
||||
|
||||
|
||||
class VideoTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
|
||||
def invoke(self, source, *args):
|
||||
with patch('sys.argv', ['pptx_videos.py', str(source), *args]), \
|
||||
contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
|
||||
return videos.main()
|
||||
|
||||
def deck(self, name='sample.pptx', media=True):
|
||||
path = self.root / name
|
||||
with zipfile.ZipFile(path, 'w') as archive:
|
||||
if media:
|
||||
archive.writestr('ppt/media/clip.mp4', b'video fixture')
|
||||
return path
|
||||
|
||||
def test_discovery_uses_presentation_order_and_relationships(self):
|
||||
source = self.root / 'ordered.pptx'
|
||||
with zipfile.ZipFile(source, 'w') as archive:
|
||||
archive.writestr('ppt/presentation.xml', f'''
|
||||
<p:presentation xmlns:p="{videos.P_NS}" xmlns:r="{videos.R_NS}">
|
||||
<p:sldIdLst><p:sldId r:id="second"/><p:sldId r:id="first"/></p:sldIdLst>
|
||||
</p:presentation>''')
|
||||
archive.writestr('ppt/_rels/presentation.xml.rels', f'''
|
||||
<Relationships xmlns="{videos.REL_NS}">
|
||||
<Relationship Id="first" Target="slides/slide1.xml"/>
|
||||
<Relationship Id="second" Target="slides/slide2.xml"/>
|
||||
</Relationships>''')
|
||||
for number in (1, 2):
|
||||
archive.writestr(f'ppt/slides/slide{number}.xml', f'''
|
||||
<p:sld xmlns:p="{videos.P_NS}" xmlns:r="{videos.R_NS}">
|
||||
<videoFile r:link="video"/><videoFile r:link="remote"/>
|
||||
</p:sld>''')
|
||||
archive.writestr(f'ppt/slides/_rels/slide{number}.xml.rels', f'''
|
||||
<Relationships xmlns="{videos.REL_NS}">
|
||||
<Relationship Id="video" Target="../media/clip{number}.mp4" Type="example/video"/>
|
||||
<Relationship Id="remote" Target="https://example.invalid/video" TargetMode="External" Type="example/video"/>
|
||||
</Relationships>''')
|
||||
archive.writestr(f'ppt/media/clip{number}.mp4', b'video')
|
||||
archive.writestr('ppt/media/audio.mp3', b'audio')
|
||||
with zipfile.ZipFile(source) as archive:
|
||||
parts, uses, external = videos.discover(archive)
|
||||
self.assertEqual(parts, ['ppt/media/clip1.mp4', 'ppt/media/clip2.mp4'])
|
||||
self.assertEqual(uses['ppt/media/clip2.mp4'], {1})
|
||||
self.assertEqual(uses['ppt/media/clip1.mp4'], {2})
|
||||
self.assertEqual(external, {1, 2})
|
||||
|
||||
def test_empty_deck_creates_no_output_and_requires_no_tools(self):
|
||||
source = self.deck(media=False)
|
||||
with patch.object(videos.shutil, 'which', side_effect=AssertionError('No tools needed')):
|
||||
self.assertEqual(self.invoke(source, '--timings-only', '--report', 'pdf'), 0)
|
||||
self.assertFalse((self.root / 'sample_videos').exists())
|
||||
|
||||
def test_timings_only_probes_temporary_copy_then_removes_it(self):
|
||||
source = self.deck()
|
||||
probed = []
|
||||
|
||||
def probe(path):
|
||||
self.assertEqual(path.read_bytes(), b'video fixture')
|
||||
probed.append(path)
|
||||
return 1.25, '1920×1080', 'h264'
|
||||
|
||||
with patch.object(videos.shutil, 'which', return_value='/ffprobe'), patch.object(videos, 'probe', side_effect=probe):
|
||||
self.assertEqual(self.invoke(source, '--timings-only', '--report', 'json,md'), 0)
|
||||
output = self.root / 'sample_videos'
|
||||
self.assertFalse((output / 'videos').exists())
|
||||
self.assertEqual(len(probed), 1)
|
||||
self.assertFalse(probed[0].exists())
|
||||
data = json.loads((output / 'reports' / 'timings.json').read_text())
|
||||
self.assertEqual(data['videos'][0]['duration_seconds'], 1.25)
|
||||
self.assertIsNone(data['videos'][0]['extracted_file'])
|
||||
self.assertNotIn('[clip.mp4]', (output / 'reports' / 'timings.md').read_text())
|
||||
|
||||
def test_extract_only_does_not_probe_or_write_reports(self):
|
||||
source = self.deck()
|
||||
with patch.object(videos, 'probe', side_effect=AssertionError('No probing')):
|
||||
self.assertEqual(self.invoke(source, '--extract-only'), 0)
|
||||
output = self.root / 'sample_videos'
|
||||
self.assertEqual([p.name for p in output.iterdir()], ['videos'])
|
||||
self.assertEqual((output / 'videos' / 'clip.mp4').read_bytes(), b'video fixture')
|
||||
|
||||
def test_separate_folders_and_relative_media_links(self):
|
||||
source = self.root / 'linked.pptx'
|
||||
with zipfile.ZipFile(source, 'w') as archive:
|
||||
archive.writestr('ppt/media/clip #1.mp4', b'video fixture')
|
||||
output = self.root / 'custom output'
|
||||
media = output / 'videos' / 'clip #1.mp4'
|
||||
reports = output / 'reports'
|
||||
with patch.object(videos.shutil, 'which', return_value='/ffprobe'), \
|
||||
patch.object(videos, 'probe', return_value=(1.25, '32×24', 'mpeg4')) as probe:
|
||||
self.assertEqual(self.invoke(source, '-o', str(output), '--report', 'md,html,json'), 0)
|
||||
probe.assert_called_once_with(media)
|
||||
probe.reset_mock()
|
||||
self.assertEqual(self.invoke(source, '-o', str(output), '--timings-only', '--report', 'json'), 0)
|
||||
probe.assert_called_once_with(media)
|
||||
self.assertEqual({p.name for p in output.iterdir()}, {'videos', 'reports'})
|
||||
self.assertEqual(media.read_bytes(), b'video fixture')
|
||||
self.assertIn('](../videos/clip%20%231.mp4)', (reports / 'timings.md').read_text())
|
||||
self.assertIn('href="../videos/clip%20%231.mp4"', (reports / 'timings.html').read_text())
|
||||
data = json.loads((reports / 'timings.json').read_text())
|
||||
self.assertEqual(data['videos'][0]['extracted_file'], '../videos/clip #1.mp4')
|
||||
self.assertEqual((reports / data['videos'][0]['extracted_file']).resolve(), media.resolve())
|
||||
|
||||
def test_all_reports_generate_every_format_once(self):
|
||||
source = self.deck()
|
||||
|
||||
def compile_pdf(command, **kwargs):
|
||||
self.assertEqual(command[:2], ['typst', 'compile'])
|
||||
Path(command[-1]).write_bytes(b'%PDF-test')
|
||||
|
||||
with patch.object(videos.shutil, 'which', return_value='/tool'), \
|
||||
patch.object(videos, 'probe', return_value=(1.25, '32×24', 'mpeg4')), \
|
||||
patch.object(videos.subprocess, 'run', side_effect=compile_pdf) as compiler:
|
||||
self.assertEqual(self.invoke(source, '--timings-only', '--report', ' ALL,md', '--report', 'pdf'), 0)
|
||||
self.assertEqual(compiler.call_count, 2)
|
||||
self.assertEqual(
|
||||
{p.name for p in (self.root / 'sample_videos' / 'reports').iterdir()},
|
||||
{'timings.html', 'timings.typ', 'timings.pdf', 'timings.md',
|
||||
'timings.txt', 'timings.json', 'timings.ontime.json',
|
||||
'timings-dense.typ', 'timings-dense.pdf'},
|
||||
)
|
||||
|
||||
def test_default_reports_include_ontime_and_explicit_choice_overrides(self):
|
||||
source = self.deck()
|
||||
with patch.object(videos.shutil, 'which', return_value='/ffprobe'), \
|
||||
patch.object(videos, 'probe', return_value=(1.25, '32×24', 'mpeg4')):
|
||||
self.assertEqual(self.invoke(source, '--timings-only'), 0)
|
||||
output = self.root / 'sample_videos' / 'reports'
|
||||
self.assertEqual({p.name for p in output.iterdir()}, {'timings.md', 'timings.ontime.json'})
|
||||
other = self.deck(name='explicit.pptx')
|
||||
self.assertEqual(self.invoke(other, '--timings-only', '--report', 'json,ontime'), 0)
|
||||
output = self.root / 'explicit_videos' / 'reports'
|
||||
self.assertEqual({p.name for p in output.iterdir()}, {'timings.json', 'timings.ontime.json'})
|
||||
other = self.deck(name='markdown.pptx')
|
||||
self.assertEqual(self.invoke(other, '--timings-only', '--report', 'md'), 0)
|
||||
self.assertEqual({p.name for p in (self.root / 'markdown_videos' / 'reports').iterdir()}, {'timings.md'})
|
||||
|
||||
def test_ontime_expands_shared_videos_in_slide_order(self):
|
||||
project = json.loads(videos.render_report(self.report(), 'ontime'))
|
||||
self.assertEqual(project['settings']['version'], '4.0.0')
|
||||
rundown = project['rundowns']['default']
|
||||
events = [rundown['entries'][event_id] for event_id in rundown['order']]
|
||||
self.assertEqual([event['cue'] for event in events], ['Slide 2', 'Slide 10', 'Slide 12', 'Unreferenced'])
|
||||
self.assertEqual([event['duration'] for event in events], [1000, 2500, 1000, 0])
|
||||
self.assertEqual([event['timeStart'] for event in events], [0, 1000, 3500, 4500])
|
||||
self.assertEqual([event['linkStart'] for event in events], [False, True, True, True])
|
||||
self.assertEqual([event['skip'] for event in events], [False, False, False, True])
|
||||
self.assertTrue(all(event['endAction'] == 'none' for event in events))
|
||||
self.assertEqual(len(set(rundown['order'])), 4)
|
||||
self.assertEqual(project, json.loads(videos.render_report(self.report(), 'ontime')))
|
||||
|
||||
def test_ontime_skips_unknown_duration_and_preserves_milliseconds(self):
|
||||
data = self.report()
|
||||
data['playbacks'][0]['duration_seconds'] = None
|
||||
data['playbacks'][1]['duration_seconds'] = 2.345
|
||||
events = list(json.loads(videos.render_report(data, 'ontime'))['rundowns']['default']['entries'].values())
|
||||
self.assertTrue(events[0]['skip'])
|
||||
self.assertIn('Duration unknown', events[0]['note'])
|
||||
self.assertEqual(events[1]['duration'], 2345)
|
||||
self.assertEqual(events[1]['timeStart'], 0)
|
||||
self.assertFalse(events[1]['linkStart'])
|
||||
|
||||
def test_ontime_rejects_invalid_durations_without_invalid_json(self):
|
||||
for seconds in (float('nan'), float('inf'), -1, 86400):
|
||||
with self.subTest(seconds=seconds):
|
||||
data = self.report()
|
||||
data['playbacks'][0]['duration_seconds'] = seconds
|
||||
project = json.loads(videos.render_report(data, 'ontime'))
|
||||
event = next(iter(project['rundowns']['default']['entries'].values()))
|
||||
self.assertEqual(event['duration'], 0)
|
||||
self.assertTrue(event['skip'])
|
||||
|
||||
def report(self):
|
||||
rows = [
|
||||
('a', 'clip.mp4', 1024, 2.5, '1920×1080', 'h264'),
|
||||
('b', 'A <clip> #1.mp4', 512, 1.0, '32×24', 'mpeg4'),
|
||||
('c', 'unused.mp4', 128, None, 'Unknown', 'Unknown'),
|
||||
]
|
||||
return videos.report_data(Path('Review & plans.pptx'), rows,
|
||||
defaultdict(set, a={10}, b={2, 12}), {5}, self.root)
|
||||
|
||||
def test_reports_sort_numerically_and_preserve_unknown_duration(self):
|
||||
data = self.report()
|
||||
self.assertEqual([v['slides'] for v in data['videos']], [[2, 12], [10], []])
|
||||
self.assertEqual(data['total_duration_seconds'], 4.5)
|
||||
self.assertEqual(data['total_media_duration_seconds'], 3.5)
|
||||
self.assertEqual(data['known_durations'], 2)
|
||||
decoded = json.loads(videos.render_report(data, 'json'))
|
||||
self.assertIsNone(decoded['videos'][-1]['duration_seconds'])
|
||||
self.assertEqual(decoded['external_media_slides'], [5])
|
||||
|
||||
def test_text_tables_align_and_put_duration_second(self):
|
||||
for format in ('md', 'txt'):
|
||||
with self.subTest(format=format):
|
||||
lines = [line for line in videos.render_report(self.report(), format).splitlines() if line.startswith('|')]
|
||||
self.assertEqual(lines[0].split('|')[2].strip(), 'Duration')
|
||||
columns = {tuple(i for i, char in enumerate(line) if char == '|') for line in lines}
|
||||
self.assertEqual(len(columns), 1)
|
||||
self.assertIn('Unreferenced', lines[-1])
|
||||
|
||||
def test_visual_reports_escape_document_data(self):
|
||||
data = self.report()
|
||||
rendered = videos.render_report(data, 'html')
|
||||
self.assertIn('Review & plans.pptx', rendered)
|
||||
self.assertIn('A <clip> #1.mp4', rendered)
|
||||
self.assertNotIn('<clip>', rendered)
|
||||
data['presentation'] = 'Quotes " and #panic("oops").pptx'
|
||||
rendered = videos.render_report(data, 'typst')
|
||||
self.assertIn(videos.typst_string(data['presentation']), rendered)
|
||||
self.assertEqual(videos.typst_string('a\\b"\n'), '"a\\\\b\\"\\u{a}"')
|
||||
|
||||
def test_pdf_generation_keeps_source_and_invokes_typst(self):
|
||||
def compile_pdf(command, **kwargs):
|
||||
self.assertEqual(command[:2], ['typst', 'compile'])
|
||||
self.assertTrue(Path(command[2]).is_file())
|
||||
Path(command[3]).write_bytes(b'%PDF-test')
|
||||
|
||||
with patch.object(videos.subprocess, 'run', side_effect=compile_pdf) as run, contextlib.redirect_stdout(io.StringIO()):
|
||||
videos.write_reports(self.report(), ['pdf'], self.root)
|
||||
run.assert_called_once()
|
||||
self.assertTrue((self.root / 'timings.typ').is_file())
|
||||
self.assertEqual((self.root / 'timings.pdf').read_bytes(), b'%PDF-test')
|
||||
|
||||
def test_dense_pdf_has_portrait_media_table_and_discreet_title(self):
|
||||
source = videos.render_report(self.report(), 'pdf-dense')
|
||||
self.assertNotIn('flipped: true', source)
|
||||
self.assertIn('columns: (30mm, 1fr, auto)', source)
|
||||
self.assertIn('table.header(strong("Slide(s)"), strong("Media"), strong("Duration"))', source)
|
||||
self.assertIn('"2", "A <clip> #1.mp4", pill("00:00:01.000"', source)
|
||||
self.assertIn('"12", "A <clip> #1.mp4", pill("00:00:01.000"', source)
|
||||
self.assertIn('"Unreferenced", "unused.mp4", pill("Unknown"', source)
|
||||
self.assertIn('#par(text(size: 10pt, fill: rgb("#626b80"), "Video timings — Review & plans.pptx"))', source)
|
||||
for unwanted in ('metric(', 'Embedded videos', 'Total duration', 'Resolution', 'Codec'):
|
||||
self.assertNotIn(unwanted, source)
|
||||
self.assertIn('text(size: 7pt', source)
|
||||
self.assertIn('trimming', source)
|
||||
|
||||
def test_dense_pdf_compiles_its_own_source(self):
|
||||
with patch.object(videos.subprocess, 'run') as compiler, contextlib.redirect_stdout(io.StringIO()):
|
||||
videos.write_reports(self.report(), ['pdf-dense'], self.root)
|
||||
command = compiler.call_args.args[0]
|
||||
self.assertEqual(command, ['typst', 'compile', str(self.root / 'timings-dense.typ'),
|
||||
str(self.root / 'timings-dense.pdf')])
|
||||
self.assertFalse((self.root / 'timings.typ').exists())
|
||||
|
||||
def test_failed_probe_returns_unknown_metadata(self):
|
||||
failure = subprocess.CalledProcessError(1, ['ffprobe'])
|
||||
with patch.object(videos.subprocess, 'run', side_effect=failure):
|
||||
self.assertEqual(videos.probe(self.root / 'bad.mp4'), (None, 'Unknown', 'Unknown'))
|
||||
|
||||
def test_invalid_archive_fails_without_output(self):
|
||||
source = self.root / 'bad.pptx'
|
||||
source.write_bytes(b'not a zip')
|
||||
self.assertEqual(self.invoke(source, '--timings-only'), 1)
|
||||
self.assertFalse((self.root / 'bad_videos').exists())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
"""Create an Ontime 4 project from a video timing report."""
|
||||
import math
|
||||
from uuid import NAMESPACE_URL, uuid5
|
||||
|
||||
|
||||
def ontime_project(data):
|
||||
"""Expand shared videos into slide cues; retain uncertain cues as skipped."""
|
||||
occurrences = [
|
||||
(slide, video)
|
||||
for video in data.get('playbacks', data['videos'])
|
||||
if video.get('ontime_eligible', True)
|
||||
for slide in (video['slides'] or [None])
|
||||
]
|
||||
occurrences.sort(key=lambda item: (item[0] if item[0] is not None else math.inf,
|
||||
item[1]['name'], item[1]['archive_path']))
|
||||
entries = {}
|
||||
elapsed = 0
|
||||
has_previous = False
|
||||
for slide, video in occurrences:
|
||||
seconds = video['duration_seconds']
|
||||
valid = seconds is not None and math.isfinite(seconds) and 0 <= seconds <= 86399
|
||||
duration = round(seconds * 1000) if valid else 0
|
||||
skipped = not valid or slide is None
|
||||
cue = f'Slide {slide}' if slide is not None else 'Unreferenced'
|
||||
note = f'{data["presentation"]} — {video["archive_path"]}.'
|
||||
if not valid:
|
||||
note += ' Duration unknown or outside Ontime limits; review before enabling.'
|
||||
if slide is None:
|
||||
note += ' No slide reference; review before enabling.'
|
||||
event_id = uuid5(NAMESPACE_URL, f'{data["presentation"]}/{video["archive_path"]}#{slide}:{video.get("shape_id", "")}').hex
|
||||
entries[event_id] = {
|
||||
'id': event_id, 'type': 'event', 'cue': cue, 'title': video['name'],
|
||||
'note': note, 'duration': duration, 'timeStart': elapsed % 86400000,
|
||||
'timeEnd': (elapsed + duration) % 86400000,
|
||||
'timeStrategy': 'lock-duration', 'linkStart': has_previous,
|
||||
'timerType': 'count-down', 'endAction': 'none',
|
||||
'skip': skipped, 'colour': '#b7791f' if skipped else '#5744b5',
|
||||
'timeWarning': min(120000, duration // 5),
|
||||
'timeDanger': min(60000, duration // 10),
|
||||
'countToEnd': False, 'flag': False, 'custom': {}, 'triggers': [],
|
||||
}
|
||||
if not skipped:
|
||||
elapsed += duration
|
||||
has_previous = True
|
||||
description = (
|
||||
'Video cues in slide order, one per supported video occurrence. '
|
||||
'Times start at midnight and are linked; set the first cue to your show start. '
|
||||
'PowerPoint trims are applied. Loops and unsupported timing are omitted. '
|
||||
'Unknown durations and unreferenced media are skipped pending review.'
|
||||
)
|
||||
if data['external_media_slides']:
|
||||
description += ' External media was not exported (slides: ' + ', '.join(map(str, data['external_media_slides'])) + ').'
|
||||
return {
|
||||
'settings': {'version': '4.0.0'},
|
||||
'project': {'title': data['presentation'], 'description': description},
|
||||
'rundowns': {'default': {
|
||||
'id': 'default', 'title': 'Video timings', 'entries': entries,
|
||||
'order': list(entries), 'flatOrder': list(entries), 'revision': 0,
|
||||
}},
|
||||
'customFields': {}, 'urlPresets': [], 'viewSettings': {},
|
||||
'automation': {'enabledAutomations': False, 'enabledOscIn': False,
|
||||
'triggers': [], 'automations': {}},
|
||||
}
|
||||
|
|
@ -1,121 +0,0 @@
|
|||
"""Read per-shape PowerPoint video trims and flag unsupported timing."""
|
||||
import math
|
||||
import re
|
||||
|
||||
P = 'http://schemas.openxmlformats.org/presentationml/2006/main'
|
||||
P14 = 'http://schemas.microsoft.com/office/powerpoint/2010/main'
|
||||
R = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
|
||||
|
||||
|
||||
def time_offset(value):
|
||||
"""Office ST_UniversalTimeOffset: default unit is milliseconds."""
|
||||
match = re.fullmatch(r'(\d+(?:\.\d+)?)(h|min|s|ms|[μµ]s|ns)?', value.strip())
|
||||
if not match:
|
||||
raise ValueError(f'Invalid time offset: {value}')
|
||||
seconds = float(match[1]) * {
|
||||
None: .001, 'h': 3600, 'min': 60, 's': 1,
|
||||
'ms': .001, 'μs': .000001, 'µs': .000001, 'ns': .000000001,
|
||||
}[match[2]]
|
||||
if not math.isfinite(seconds):
|
||||
raise ValueError('Non-finite time offset')
|
||||
return seconds
|
||||
|
||||
|
||||
def shape_playbacks(root, relationships, video_parts, slide):
|
||||
parents = {child: parent for parent in root.iter() for child in parent}
|
||||
result = []
|
||||
for shape in root.iter():
|
||||
if shape.tag not in (f'{{{P}}}pic', f'{{{P}}}sp', f'{{{P}}}graphicFrame'):
|
||||
continue
|
||||
identity = shape.find(f'.//{{{P}}}cNvPr')
|
||||
shape_id = identity.get('id') if identity is not None else None
|
||||
parts = {}
|
||||
for el in shape.iter():
|
||||
if el.tag.rsplit('}', 1)[-1] not in ('videoFile', 'media'):
|
||||
continue
|
||||
for attr in (f'{{{R}}}embed', f'{{{R}}}link'):
|
||||
part = relationships.get(el.get(attr), (None, ''))[0]
|
||||
if part in video_parts:
|
||||
parts.setdefault(part, [])
|
||||
if el.tag == f'{{{P14}}}media':
|
||||
parts[part].extend(el.findall(f'{{{P14}}}trim'))
|
||||
for part, trims in parts.items():
|
||||
item = {'archive_path': part, 'slide': slide, 'shape_id': shape_id,
|
||||
'trim_start_seconds': 0.0, 'trim_end_seconds': 0.0,
|
||||
'playback_status': 'normal', 'playback_notes': []}
|
||||
try:
|
||||
values = {(time_offset(t.get('st', '0')), time_offset(t.get('end', '0'))) for t in trims}
|
||||
if len(values) > 1:
|
||||
raise ValueError('Conflicting trims')
|
||||
if values:
|
||||
item['trim_start_seconds'], item['trim_end_seconds'] = values.pop()
|
||||
except ValueError:
|
||||
item['playback_status'] = 'invalid-trim'
|
||||
item['playback_notes'].append('Invalid or conflicting trim settings.')
|
||||
if shape_id is not None:
|
||||
for media in root.iter(f'{{{P}}}cMediaNode'):
|
||||
target = media.find(f'{{{P}}}tgtEl/{{{P}}}spTgt')
|
||||
if target is None or target.get('spid') != shape_id:
|
||||
continue
|
||||
# The media node and its ancestor timing containers can repeat.
|
||||
timing = list(media.findall(f'{{{P}}}cTn'))
|
||||
parent = parents.get(media)
|
||||
while parent is not None:
|
||||
if parent.tag == f'{{{P}}}cTn':
|
||||
timing.append(parent)
|
||||
parent = parents.get(parent)
|
||||
for node in timing:
|
||||
repeat = node.get('repeatCount')
|
||||
repeated = repeat is not None and repeat not in ('1000', '1000.0', '100%')
|
||||
if repeated or node.get('repeatDur') not in (None, '0'):
|
||||
item['playback_status'] = 'looping'
|
||||
item['playback_notes'].append('Repeated playback; not exported to Ontime.')
|
||||
unsupported = (
|
||||
node.get('spd') not in (None, '100000', '100%')
|
||||
or node.get('autoRev') in ('1', 'true')
|
||||
or node.get('accel') not in (None, '0', '0%')
|
||||
or node.get('decel') not in (None, '0', '0%')
|
||||
or node.get('tmFilter') is not None
|
||||
or node.find(f'{{{P}}}endCondLst') is not None
|
||||
)
|
||||
if unsupported:
|
||||
if item['playback_status'] != 'looping':
|
||||
item['playback_status'] = 'custom-timing'
|
||||
item['playback_notes'].append('Custom speed, reverse, or end conditions are not simulated.')
|
||||
if media.get('numSld') not in (None, '1'):
|
||||
if item['playback_status'] != 'looping':
|
||||
item['playback_status'] = 'custom-timing'
|
||||
item['playback_notes'].append('Playback across slides is not simulated.')
|
||||
# Natural media duration is commonly encoded as "indefinite".
|
||||
# A finite timing cap needs timeline evaluation, not trim maths.
|
||||
for node in media.findall(f'{{{P}}}cTn'):
|
||||
if node.get('dur') not in (None, 'indefinite', 'media'):
|
||||
if item['playback_status'] != 'looping':
|
||||
item['playback_status'] = 'custom-timing'
|
||||
item['playback_notes'].append('Explicit animation duration is not simulated.')
|
||||
item['playback_notes'] = list(dict.fromkeys(item['playback_notes']))
|
||||
if item not in result:
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
def effective_playback(video, settings):
|
||||
result = {**video, **settings}
|
||||
result['slides'] = [settings['slide']] if settings['slide'] is not None else []
|
||||
original = video['duration_seconds']
|
||||
result['original_duration_seconds'] = original
|
||||
start, end = settings['trim_start_seconds'], settings['trim_end_seconds']
|
||||
status = settings['playback_status']
|
||||
result['duration_seconds'] = None
|
||||
if status == 'normal':
|
||||
if original is None:
|
||||
status = 'unknown'
|
||||
elif start + end >= original and (start or end):
|
||||
status = 'invalid-trim'
|
||||
result['playback_notes'] = [*settings['playback_notes'], 'Trim removes the whole media or exceeds its duration.']
|
||||
else:
|
||||
result['duration_seconds'] = round(original - start - end, 9)
|
||||
status = 'trimmed' if start or end else 'normal'
|
||||
result['playback_status'] = status
|
||||
result['ontime_eligible'] = status in ('normal', 'trimmed', 'unknown')
|
||||
return result
|
||||
|
|
@ -1,552 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Extract embedded PPTX videos and report their slide references and metadata."""
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
import math
|
||||
import posixpath
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import zipfile
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
from xml.etree import ElementTree as ET
|
||||
|
||||
if __package__:
|
||||
from .ontime import ontime_project
|
||||
from .progress import Progress, status
|
||||
from .playback import effective_playback, shape_playbacks
|
||||
else:
|
||||
from ontime import ontime_project
|
||||
from progress import Progress, status
|
||||
from playback import effective_playback, shape_playbacks
|
||||
|
||||
VIDEO_EXTS = {'.mp4', '.m4v', '.mov', '.avi', '.wmv', '.webm', '.mpg', '.mpeg', '.mkv', '.asf', '.3gp', '.ts'}
|
||||
REL_NS = 'http://schemas.openxmlformats.org/package/2006/relationships'
|
||||
R_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
|
||||
P_NS = 'http://schemas.openxmlformats.org/presentationml/2006/main'
|
||||
REPORT_FORMATS = ('html', 'typst', 'pdf', 'pdf-dense', 'md', 'txt', 'json', 'ontime')
|
||||
HEADERS = ('Slide(s)', 'Duration', 'Video', 'Size', 'Resolution', 'Codec', 'Original', 'Playback')
|
||||
REPORT_NOTES = (
|
||||
'Duration applies PowerPoint trimming per video occurrence. Original is the media file duration.',
|
||||
'Videos linked externally are not downloaded. Slide numbers follow presentation order.',
|
||||
)
|
||||
|
||||
|
||||
def xml(z, name):
|
||||
try:
|
||||
return ET.fromstring(z.read(name))
|
||||
except (KeyError, ET.ParseError):
|
||||
return None
|
||||
|
||||
|
||||
def rels(z, source):
|
||||
directory, basename = posixpath.split(source)
|
||||
path = posixpath.join(directory, '_rels', basename + '.rels')
|
||||
root = xml(z, path)
|
||||
if root is None:
|
||||
return {}
|
||||
result = {}
|
||||
for rel in root.findall(f'{{{REL_NS}}}Relationship'):
|
||||
rid = rel.get('Id')
|
||||
target = rel.get('Target', '')
|
||||
if not rid:
|
||||
continue
|
||||
if rel.get('TargetMode', '').lower() == 'external':
|
||||
result[rid] = (None, rel.get('Type', ''))
|
||||
else:
|
||||
# Targets can be package-root absolute or relative to the source part.
|
||||
target = target.split('#', 1)[0].split('?', 1)[0]
|
||||
resolved = posixpath.normpath(target.lstrip('/') if target.startswith('/') else posixpath.join(directory, target))
|
||||
result[rid] = (resolved, rel.get('Type', ''))
|
||||
return result
|
||||
|
||||
|
||||
def slide_paths(z):
|
||||
root = xml(z, 'ppt/presentation.xml')
|
||||
mapping = rels(z, 'ppt/presentation.xml')
|
||||
if root is not None:
|
||||
ordered = []
|
||||
for slide in root.findall(f'.//{{{P_NS}}}sldId'):
|
||||
target = mapping.get(slide.get(f'{{{R_NS}}}id'), (None, ''))[0]
|
||||
if target:
|
||||
ordered.append(target)
|
||||
if ordered:
|
||||
return ordered
|
||||
return sorted((n for n in z.namelist() if re.fullmatch(r'ppt/slides/slide\d+\.xml', n)), key=lambda n: int(re.search(r'slide(\d+)', n).group(1)))
|
||||
|
||||
|
||||
def discover(z):
|
||||
"""Map actual archive media parts to presentation-order slide numbers."""
|
||||
names = set(z.namelist())
|
||||
videos = sorted((n for n in names if n.startswith('ppt/media/') and Path(n).suffix.lower() in VIDEO_EXTS))
|
||||
uses = defaultdict(set)
|
||||
external = set()
|
||||
for index, slide in enumerate(slide_paths(z), 1):
|
||||
root = xml(z, slide)
|
||||
if root is None:
|
||||
continue
|
||||
mapping = rels(z, slide)
|
||||
# Only relationships referenced by videoFile or p14:media elements;
|
||||
# excludes audio-only attachments and poster images.
|
||||
for el in root.iter():
|
||||
tag = el.tag.rsplit('}', 1)[-1]
|
||||
if tag not in ('videoFile', 'media'):
|
||||
continue
|
||||
for attr in (f'{{{R_NS}}}link', f'{{{R_NS}}}embed'):
|
||||
rid = el.get(attr)
|
||||
if not rid or rid not in mapping:
|
||||
continue
|
||||
target, rel_type = mapping[rid]
|
||||
if tag == 'media' and not rel_type.endswith('/media'):
|
||||
continue
|
||||
if target is None:
|
||||
if tag == 'videoFile' or rel_type.endswith('/media'):
|
||||
external.add(index)
|
||||
elif target in names and Path(target).suffix.lower() in VIDEO_EXTS:
|
||||
uses[target].add(index)
|
||||
return videos, uses, external
|
||||
|
||||
|
||||
def size_fmt(n):
|
||||
for unit in ('B', 'KiB', 'MiB', 'GiB', 'TiB'):
|
||||
if n < 1024 or unit == 'TiB':
|
||||
return f'{n:.0f} {unit}' if unit == 'B' else f'{n:.2f} {unit}'
|
||||
n /= 1024
|
||||
|
||||
|
||||
def playback_settings(z, videos):
|
||||
settings = defaultdict(list)
|
||||
for index, slide in enumerate(slide_paths(z), 1):
|
||||
root = xml(z, slide)
|
||||
if root is not None:
|
||||
for item in shape_playbacks(root, rels(z, slide), set(videos), index):
|
||||
settings[item['archive_path']].append(item)
|
||||
return settings
|
||||
|
||||
|
||||
def time_fmt(seconds):
|
||||
if seconds is None:
|
||||
return 'Unknown'
|
||||
millis = round(seconds * 1000)
|
||||
h, rem = divmod(millis, 3600000)
|
||||
m, rem = divmod(rem, 60000)
|
||||
s, ms = divmod(rem, 1000)
|
||||
return f'{h:02}:{m:02}:{s:02}.{ms:03}'
|
||||
|
||||
|
||||
def probe(path):
|
||||
try:
|
||||
p = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration:stream=codec_type,width,height,codec_name', '-of', 'json', str(path)], capture_output=True, text=True, check=True)
|
||||
data = json.loads(p.stdout)
|
||||
duration = data.get('format', {}).get('duration')
|
||||
duration = float(duration) if duration not in (None, 'N/A') else None
|
||||
if duration is not None and (not math.isfinite(duration) or duration < 0):
|
||||
duration = None
|
||||
streams = data.get('streams', [])
|
||||
video = next((s for s in streams if s.get('codec_type') == 'video'), {})
|
||||
resolution = f"{video['width']}×{video['height']}" if video.get('width') and video.get('height') else 'Unknown'
|
||||
return duration, resolution, video.get('codec_name', 'Unknown')
|
||||
except (subprocess.CalledProcessError, OSError, ValueError, json.JSONDecodeError):
|
||||
return None, 'Unknown', 'Unknown'
|
||||
|
||||
|
||||
def esc(s):
|
||||
return str(s).replace('|', '\\|').replace('\n', ' ')
|
||||
|
||||
|
||||
def aligned_table(cells, markdown=True):
|
||||
headers = HEADERS
|
||||
widths = [max(3, *(len(row[i]) for row in [headers, *cells])) for i in range(len(headers))]
|
||||
right_aligned = {1, 3, 6}
|
||||
|
||||
def render(row):
|
||||
return '| ' + ' | '.join(
|
||||
value.rjust(widths[i]) if i in right_aligned else value.ljust(widths[i])
|
||||
for i, value in enumerate(row)
|
||||
) + ' |'
|
||||
|
||||
separators = [
|
||||
'-' * (width - 1) + ':' if markdown and i in right_aligned else '-' * width
|
||||
for i, width in enumerate(widths)
|
||||
]
|
||||
return [render(headers), render(separators), *(render(row) for row in cells)]
|
||||
|
||||
|
||||
def report_formats(value):
|
||||
formats = [item.strip().lower() for item in value.split(',')]
|
||||
if any(item not in (*REPORT_FORMATS, 'all') for item in formats):
|
||||
raise argparse.ArgumentTypeError('Choose comma-separated formats from: ' + ', '.join((*REPORT_FORMATS, 'all')))
|
||||
return list(dict.fromkeys(
|
||||
format for item in formats for format in (REPORT_FORMATS if item == 'all' else (item,))
|
||||
))
|
||||
|
||||
|
||||
def report_data(source, rows, uses, external, out, settings=None):
|
||||
videos = []
|
||||
for part, name, size, duration, resolution, codec in sorted(
|
||||
rows, key=lambda row: (min(uses[row[0]], default=float('inf')), row[1])):
|
||||
videos.append({
|
||||
'slides': sorted(uses[part]), 'duration_seconds': duration,
|
||||
'name': name, 'size_bytes': size, 'resolution': resolution,
|
||||
'codec': codec, 'archive_path': part,
|
||||
'extracted_file': f'../videos/{name}' if (out / 'videos' / name).is_file() else None,
|
||||
})
|
||||
playbacks = []
|
||||
for video in videos:
|
||||
occurrences = list((settings or {}).get(video['archive_path'], []))
|
||||
covered = {item['slide'] for item in occurrences}
|
||||
# Retain references without recognized shape metadata, and orphan media.
|
||||
for slide in (video['slides'] or [None]):
|
||||
if slide not in covered:
|
||||
occurrences.append({'slide': slide, 'shape_id': None,
|
||||
'trim_start_seconds': 0.0, 'trim_end_seconds': 0.0,
|
||||
'playback_status': 'normal', 'playback_notes': []})
|
||||
playbacks.extend(effective_playback(video, item) for item in occurrences)
|
||||
playbacks.sort(key=lambda item: (item['slide'] if item['slide'] is not None else math.inf,
|
||||
item['name'], item['shape_id'] or ''))
|
||||
return {
|
||||
'presentation': source.name,
|
||||
'embedded_videos': len(videos),
|
||||
'total_size_bytes': sum(v['size_bytes'] for v in videos),
|
||||
'total_duration_seconds': sum(v['duration_seconds'] for v in playbacks if v['duration_seconds'] is not None),
|
||||
'total_media_duration_seconds': sum(v['duration_seconds'] for v in videos if v['duration_seconds'] is not None),
|
||||
'known_durations': sum(v['duration_seconds'] is not None for v in videos),
|
||||
'known_playback_durations': sum(v['duration_seconds'] is not None for v in playbacks),
|
||||
'playbacks': playbacks,
|
||||
'videos': videos, 'external_media_slides': sorted(external),
|
||||
'notes': list(REPORT_NOTES),
|
||||
}
|
||||
|
||||
|
||||
def typst_string(value):
|
||||
# Encode as a literal string, never as executable Typst markup.
|
||||
return '"' + ''.join(
|
||||
'\\' + c if c in ('\\', '"') else f'\\u{{{ord(c):x}}}' if ord(c) < 32 else c
|
||||
for c in value
|
||||
) + '"'
|
||||
|
||||
|
||||
def trim_detail(video):
|
||||
start = video.get('trim_start_seconds', 0)
|
||||
end = video.get('trim_end_seconds', 0)
|
||||
if not (start or end):
|
||||
return ''
|
||||
return f'Trim removed · start {time_fmt(start)} · end {time_fmt(end)}'
|
||||
|
||||
|
||||
def typst_media(video, dense=False):
|
||||
"""Keep playback annotations with the media, without adding table columns."""
|
||||
detail = trim_detail(video)
|
||||
if not detail and video['playback_status'] != 'looping':
|
||||
return typst_string(video['name'])
|
||||
content = '#' + typst_string(video['name'])
|
||||
if video['playback_status'] == 'looping':
|
||||
colors = '"#fff0ce", "#805300"' if dense else 'bg: "#fff0ce", fg: "#805300"'
|
||||
content += f' #pill("Looping", {colors})'
|
||||
if detail:
|
||||
content += '\n#par(leading: 2pt, text(size: 7pt, fill: rgb("#626b80"), ' + typst_string(detail) + '))'
|
||||
return '[' + content + ']'
|
||||
|
||||
|
||||
def render_report(data, format):
|
||||
if format == 'ontime':
|
||||
return json.dumps(ontime_project(data), ensure_ascii=False, indent=2, allow_nan=False) + '\n'
|
||||
title = f'Video timings — {data["presentation"]}'
|
||||
report_rows = data['playbacks']
|
||||
metrics = [
|
||||
('Embedded videos', str(data['embedded_videos'])),
|
||||
('Total duration', time_fmt(data['total_duration_seconds'])),
|
||||
('Total size', size_fmt(data['total_size_bytes'])),
|
||||
('Durations available', f'{data["known_playback_durations"]} / {len(report_rows)}'),
|
||||
]
|
||||
summary = [
|
||||
f'Embedded videos: {data["embedded_videos"]}',
|
||||
f'Total size: {size_fmt(data["total_size_bytes"])}',
|
||||
f'Total duration: {time_fmt(data["total_duration_seconds"])} '
|
||||
f'({data["known_playback_durations"]}/{len(report_rows)} occurrences with known playback duration)',
|
||||
]
|
||||
notes = list(data['notes'])
|
||||
if data['external_media_slides']:
|
||||
notes.append('Slides with external media links: ' +
|
||||
', '.join(map(str, data['external_media_slides'])) + ' (not extracted).')
|
||||
labels = {'looping': 'Looping', 'custom-timing': 'Custom timing', 'invalid-trim': 'Invalid trim'}
|
||||
cells = [
|
||||
[', '.join(map(str, v['slides'])) or 'Unreferenced',
|
||||
labels.get(v['playback_status'], time_fmt(v['duration_seconds'])), v['name'], size_fmt(v['size_bytes']),
|
||||
v['resolution'], v['codec'], time_fmt(v['original_duration_seconds']), v['playback_status']]
|
||||
for v in report_rows
|
||||
]
|
||||
if format == 'json':
|
||||
return json.dumps(data, ensure_ascii=False, indent=2, allow_nan=False) + '\n'
|
||||
if format in ('md', 'txt'):
|
||||
for row, video in zip(cells, report_rows):
|
||||
detail = trim_detail(video)
|
||||
if detail:
|
||||
row[7] += '; ' + detail
|
||||
row[:] = [esc(value) if format == 'md' else value.replace('\n', ' ') for value in row]
|
||||
if format == 'md' and video['extracted_file']:
|
||||
row[2] = f'[{row[2]}]({quote(video["extracted_file"])})'
|
||||
lines = [('# ' if format == 'md' else '') + title, '']
|
||||
lines += ['- ' + line for line in summary]
|
||||
lines += ['', *aligned_table(cells, markdown=format == 'md'), '']
|
||||
lines += [('> ' if format == 'md' else '') + note for note in notes]
|
||||
return '\n'.join(lines) + '\n'
|
||||
if format == 'html':
|
||||
table_rows = []
|
||||
for row, video in zip(cells, report_rows):
|
||||
row = [html.escape(value) for value in row]
|
||||
row[0] = ' '.join(f'<span class="pill slide">{slide}</span>' for slide in video['slides']) or '<span class="pill muted">Unreferenced</span>'
|
||||
duration_class = 'duration' if video['duration_seconds'] is not None else 'warning'
|
||||
row[1] = f'<span class="pill {duration_class}">{row[1]}</span>'
|
||||
row[5] = f'<span class="pill muted">{row[5]}</span>'
|
||||
if video['extracted_file']:
|
||||
row[2] = f'<a href="{html.escape(quote(video["extracted_file"]))}">{row[2]}</a>'
|
||||
if video['playback_status'] == 'looping':
|
||||
row[1] = '—'
|
||||
row[2] += ' <span class="pill warning loop">Looping</span>'
|
||||
detail = trim_detail(video)
|
||||
if detail:
|
||||
row[2] += f'<small class="trim">{html.escape(detail)}</small>'
|
||||
table_rows.append('<tr>' + ''.join(f'<td>{value}</td>' for value in row) + '</tr>')
|
||||
return (
|
||||
'<!doctype html>\n<html lang="en"><head><meta charset="utf-8">\n'
|
||||
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
|
||||
f'<title>{html.escape(title)}</title>\n'
|
||||
'<style>*{box-sizing:border-box}html{background:#f3f5fa}'
|
||||
'body{font:15px/1.6 system-ui,sans-serif;max-width:1280px;margin:48px auto;padding:0 28px;color:#172033}'
|
||||
'.eyebrow{color:#5744b5;font-size:12px;font-weight:750;letter-spacing:.16em;text-transform:uppercase}'
|
||||
'h1{font-size:clamp(28px,4vw,42px);letter-spacing:-.04em;line-height:1.2;margin:10px 0}'
|
||||
'.subtitle{color:#626b80;overflow-wrap:anywhere;margin:0 0 28px}'
|
||||
'.metrics{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:14px;margin:28px 0}'
|
||||
'.metric{background:white;border:1px solid #e2e6f0;border-radius:16px;padding:18px 20px}'
|
||||
'.metric span{display:block;font-size:12px;color:#626b80;margin-bottom:8px}'
|
||||
'.metric strong{font-size:23px;letter-spacing:-.03em;font-variant-numeric:tabular-nums}'
|
||||
'.metric:nth-child(2){background:#eeeafd;border-color:#ded6fa;color:#49359b}'
|
||||
'.table{overflow-x:auto;background:white;border:1px solid #e2e6f0;border-radius:16px;box-shadow:0 8px 30px #17203306}'
|
||||
'table{border-collapse:collapse;width:100%;font-variant-numeric:tabular-nums}'
|
||||
'th,td{padding:15px 18px;text-align:left;border-bottom:1px solid #edf0f6}'
|
||||
'th{background:#f8f9fc;color:#626b80;font-size:11px;text-transform:uppercase;letter-spacing:.07em}'
|
||||
'tr:last-child td{border-bottom:0}tbody tr:hover{background:#faf9ff}'
|
||||
'td:nth-child(2),td:nth-child(4){text-align:right;white-space:nowrap}'
|
||||
'th:nth-child(2),th:nth-child(4){text-align:right}'
|
||||
'td:nth-child(3){overflow-wrap:anywhere;min-width:180px;font-weight:600}'
|
||||
'a{color:#5744b5;text-decoration:none}a:hover{text-decoration:underline}a:focus-visible{outline:2px solid #5744b5}'
|
||||
'.pill{display:inline-block;border-radius:999px;padding:3px 10px;font-size:12px;font-weight:600;margin:2px 0}'
|
||||
'.slide{background:#eeeafd;color:#49359b}.duration{background:#e3f4ee;color:#176349}'
|
||||
'.warning{background:#fff0ce;color:#805300}.muted{background:#edf0f6;color:#566176}'
|
||||
'.trim{display:block;color:#626b80;font-size:11px;font-weight:400;margin-top:4px}.loop{margin-left:6px}'
|
||||
'footer{color:#626b80;font-size:12px;margin-top:24px;border-left:3px solid #dcd5f6;padding-left:16px}'
|
||||
'footer p{margin:5px 0}@media(max-width:760px){body{margin:24px auto;padding:0 16px}.metrics{grid-template-columns:repeat(2,minmax(0,1fr))}.metric strong{font-size:18px}}'
|
||||
'@media print{html{background:white}body{margin:0;padding:0;max-width:none}.table{overflow:visible;box-shadow:none}.metrics,.metric,tr{break-inside:avoid}thead{display:table-header-group}*{print-color-adjust:exact}}</style></head><body>\n'
|
||||
'<header><div class="eyebrow">Presentation media report</div><h1>Video timings</h1>'
|
||||
f'<p class="subtitle">{html.escape(data["presentation"])}</p></header><section class="metrics" aria-label="Summary">' +
|
||||
''.join(f'<div class="metric"><span>{html.escape(label)}</span><strong>{html.escape(value)}</strong></div>' for label, value in metrics) +
|
||||
'</section><div class="table"><table aria-label="Video timings by slide"><thead><tr>' +
|
||||
''.join(f'<th scope="col">{heading}</th>' for heading in HEADERS) +
|
||||
'</tr></thead><tbody>\n' + '\n'.join(table_rows) +
|
||||
'</tbody></table></div><footer>' +
|
||||
''.join(f'<p>{html.escape(note)}</p>' for note in notes) +
|
||||
'</footer></body></html>\n'
|
||||
)
|
||||
if format == 'pdf-dense':
|
||||
lines = [
|
||||
'#set page(paper: "a4", margin: 14mm,',
|
||||
' footer: context align(right, text(size: 7pt, fill: rgb("#626b80"), counter(page).display("1"))))',
|
||||
'#set text(font: "DejaVu Sans", size: 9pt, fill: rgb("#172033"))',
|
||||
'#set par(justify: false)',
|
||||
'#let pill(body, bg, fg) = box(fill: rgb(bg), radius: 20pt,',
|
||||
' inset: (x: 7pt, y: 2pt), text(size: 8pt, fill: rgb(fg), body))',
|
||||
'#par(text(size: 10pt, fill: rgb("#626b80"), ' + typst_string(title) + '))',
|
||||
'#v(4pt)',
|
||||
'#table(',
|
||||
' columns: (30mm, 1fr, auto),',
|
||||
' align: (left + horizon, left + horizon, right + horizon),',
|
||||
' inset: (x: 8pt, y: 4pt),',
|
||||
' stroke: (top: none, left: none, right: none, bottom: 0.4pt + rgb("#e2e6f0")),',
|
||||
' fill: (x, y) => if y == 0 { rgb("#eeeafd") } else if calc.even(y) { rgb("#f8f9fc") },',
|
||||
' table.header(strong("Slide(s)"), strong("Media"), strong("Duration")),',
|
||||
]
|
||||
for row, video in zip(cells, report_rows):
|
||||
bg, fg = ('#e3f4ee', '#176349') if video['duration_seconds'] is not None else ('#fff0ce', '#805300')
|
||||
duration = '"—"' if video['playback_status'] == 'looping' else f'pill({typst_string(row[1])}, "{bg}", "{fg}")'
|
||||
lines.append(f' {typst_string(row[0])}, {typst_media(video, dense=True)}, {duration},')
|
||||
lines += [')', '#v(6pt)']
|
||||
lines += ['#par(text(size: 7pt, fill: rgb("#626b80"), ' + typst_string(note) + '))' for note in notes]
|
||||
return '\n'.join(lines) + '\n'
|
||||
if format == 'typst':
|
||||
lines = [
|
||||
'#set page(paper: "a4", flipped: true, margin: 16mm,',
|
||||
' footer: context align(right, text(size: 8pt, fill: rgb("#626b80"), counter(page).display("1"))))',
|
||||
'#set text(font: "DejaVu Sans", size: 9pt, fill: rgb("#172033"))',
|
||||
'#set par(justify: false)',
|
||||
'#let pill(body, bg: "#eeeafd", fg: "#49359b") = box(',
|
||||
' fill: rgb(bg), radius: 20pt, inset: (x: 7pt, y: 3pt),',
|
||||
' text(size: 8pt, weight: "medium", fill: rgb(fg), body))',
|
||||
'#let metric(label, value) = block(width: 100%, fill: rgb("#f3f1fc"),',
|
||||
' radius: 8pt, inset: 12pt)[',
|
||||
' #text(size: 8pt, fill: rgb("#626b80"), label)',
|
||||
' #v(5pt)',
|
||||
' #text(size: 17pt, weight: "bold", fill: rgb("#49359b"), value)',
|
||||
']',
|
||||
'#text(size: 8pt, weight: "bold", fill: rgb("#5744b5"), tracking: 1pt)[PRESENTATION MEDIA REPORT]',
|
||||
'#v(5pt)',
|
||||
'#text(size: 28pt, weight: "bold")[Video timings]',
|
||||
'#par(text(fill: rgb("#626b80"), ' + typst_string(data['presentation']) + '))',
|
||||
'#v(8pt)',
|
||||
'#grid(columns: (1fr, 1fr, 1fr, 1fr), gutter: 10pt,',
|
||||
]
|
||||
lines += [' metric(' + typst_string(label) + ', ' + typst_string(value) + '),' for label, value in metrics]
|
||||
lines += [')']
|
||||
lines += [
|
||||
'#v(16pt)',
|
||||
'#table(',
|
||||
' columns: (22mm, auto, 1fr, auto, auto, auto, auto, auto),',
|
||||
' align: (left + horizon, right + horizon, left + horizon, right + horizon, left + horizon, left + horizon, right + horizon, left + horizon),',
|
||||
' inset: 5pt,',
|
||||
' stroke: (top: none, left: none, right: none, bottom: 0.4pt + rgb("#e2e6f0")),',
|
||||
' fill: (x, y) => if y == 0 { rgb("#eeeafd") } else if calc.even(y) { rgb("#f8f9fc") },',
|
||||
' table.header(' + ', '.join('text(size: 8pt, weight: "bold", fill: rgb("#49359b"), ' + typst_string(h) + ')' for h in HEADERS) + '),',
|
||||
]
|
||||
for row, video in zip(cells, report_rows):
|
||||
values = [typst_string(value) for value in row]
|
||||
values[0] = '[' + ' '.join('#pill(' + typst_string(str(slide)) + ')' for slide in video['slides']) + ']' if video['slides'] else 'text(size: 8pt, fill: rgb("#626b80"), "Unreferenced")'
|
||||
bg, fg = ('#e3f4ee', '#176349') if video['duration_seconds'] is not None else ('#fff0ce', '#805300')
|
||||
values[1] = f'pill({values[1]}, bg: "{bg}", fg: "{fg}")'
|
||||
if video['playback_status'] == 'looping':
|
||||
values[1] = '"—"'
|
||||
values[2] = typst_media(video)
|
||||
values[5] = f'pill({values[5]}, bg: "#edf0f6", fg: "#566176")'
|
||||
lines.append(' ' + ', '.join(values) + ',')
|
||||
lines += [')', '#v(10pt)']
|
||||
lines += ['#par(text(size: 8pt, fill: rgb("#626b80"), ' + typst_string(note) + '))' for note in notes]
|
||||
return '\n'.join(lines) + '\n'
|
||||
raise ValueError(f'Unsupported report format: {format}')
|
||||
|
||||
|
||||
def write_reports(data, formats, out):
|
||||
# Keep the source beside the PDF so it can be edited and compiled again.
|
||||
if 'pdf' in formats and 'typst' not in formats:
|
||||
formats = [*formats, 'typst']
|
||||
for format in formats:
|
||||
if format == 'pdf':
|
||||
continue
|
||||
extension = {'typst': 'typ', 'ontime': 'ontime.json'}.get(format, format)
|
||||
report = out / ('timings-dense.typ' if format == 'pdf-dense' else f'timings.{extension}')
|
||||
status(f'Writing report: {report}')
|
||||
report.write_text(render_report(data, format), encoding='utf-8')
|
||||
status(f'Report: {report}', 'success', persist=True)
|
||||
for format in ('pdf', 'pdf-dense'):
|
||||
if format not in formats:
|
||||
continue
|
||||
stem = 'timings-dense' if format == 'pdf-dense' else 'timings'
|
||||
report = out / f'{stem}.pdf'
|
||||
status(f'Compiling PDF with Typst: {report}')
|
||||
subprocess.run(['typst', 'compile', str((out / f'{stem}.typ').resolve()),
|
||||
str(report.resolve())], check=True, capture_output=True, text=True)
|
||||
status(f'Report: {report}', 'success', persist=True)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
parser.add_argument('presentation', type=Path, help='Source .pptx file')
|
||||
parser.add_argument('-o', '--output', type=Path, help='Output directory (default: <presentation-stem>_videos)')
|
||||
mode = parser.add_mutually_exclusive_group()
|
||||
mode.add_argument('--extract-only', action='store_true', help='Extract embedded videos without a report')
|
||||
mode.add_argument('--timings-only', action='store_true', help='Report video timings without keeping newly extracted videos')
|
||||
parser.add_argument('--overwrite', action='store_true', help='Overwrite already extracted files')
|
||||
parser.add_argument('--report', type=report_formats, action='append', metavar='FORMAT[,FORMAT...]',
|
||||
help='Report formats: html, typst, pdf, pdf-dense, md, txt, json, ontime, or all. Repeatable; default: md,ontime. PDFs require typst and keep their .typ sources.')
|
||||
args = parser.parse_args()
|
||||
if args.extract_only and args.report:
|
||||
parser.error('--report cannot be combined with --extract-only.')
|
||||
formats = list(dict.fromkeys(format for group in (args.report or [['md', 'ontime']]) for format in group))
|
||||
source = args.presentation
|
||||
if not source.is_file():
|
||||
parser.error(f'Not a file: {source}')
|
||||
out = args.output or source.with_name(source.stem + '_videos')
|
||||
video_dir = out / 'videos'
|
||||
report_dir = out / 'reports'
|
||||
try:
|
||||
with Progress() as progress, zipfile.ZipFile(source) as z:
|
||||
status(f'Scanning presentation: {source}')
|
||||
videos, uses, external = discover(z)
|
||||
settings = playback_settings(z, videos) if not args.extract_only else {}
|
||||
if external:
|
||||
status(f'External media on slide(s) {", ".join(map(str, sorted(external)))} will not be downloaded.', 'warning')
|
||||
if not videos:
|
||||
status('No embedded video files found; no output created.', 'warning')
|
||||
return 0
|
||||
status(f'Found {len(videos)} embedded video(s), {size_fmt(sum(z.getinfo(part).file_size for part in videos))} in the presentation.')
|
||||
if not args.extract_only and shutil.which('ffprobe') is None:
|
||||
parser.error('ffprobe is required for timings. Install ffmpeg or use --extract-only.')
|
||||
if not args.extract_only and {'pdf', 'pdf-dense'}.intersection(formats) and shutil.which('typst') is None:
|
||||
parser.error('typst is required for PDF reports. Install the Typst CLI or choose --report typst to generate source only.')
|
||||
mode_label = 'extraction only' if args.extract_only else 'timings only' if args.timings_only else 'extraction and timings'
|
||||
status(f'Mode: {mode_label}. Output: {out}')
|
||||
if not args.timings_only:
|
||||
video_dir.mkdir(parents=True, exist_ok=True)
|
||||
rows = []
|
||||
total_bytes = 0
|
||||
progress.set(0, len(videos))
|
||||
for index, part in enumerate(videos, 1):
|
||||
name = posixpath.basename(part)
|
||||
dest = video_dir / name
|
||||
slides = ', '.join(map(str, sorted(uses[part]))) or 'unreferenced'
|
||||
status(f'[{index}/{len(videos)}] {name} — {size_fmt(z.getinfo(part).file_size)} — slide(s): {slides}')
|
||||
if not args.timings_only:
|
||||
if dest.exists() and not args.overwrite:
|
||||
status(f' Keeping existing file: {dest}')
|
||||
else:
|
||||
status(f' Extracting to: {dest}')
|
||||
with z.open(part) as src, dest.open('wb') as dst:
|
||||
shutil.copyfileobj(src, dst)
|
||||
status(' Extraction complete.', 'success')
|
||||
if args.extract_only:
|
||||
progress.set(index)
|
||||
continue
|
||||
if not dest.is_file():
|
||||
# A seekable temporary file supports media whose metadata
|
||||
# cannot be read reliably from a pipe.
|
||||
with tempfile.TemporaryDirectory(prefix='pptx-videos-') as tmp:
|
||||
temporary_video = Path(tmp) / name
|
||||
status(' Extracting a temporary copy for probing...')
|
||||
with z.open(part) as src, temporary_video.open('wb') as dst:
|
||||
shutil.copyfileobj(src, dst)
|
||||
size = temporary_video.stat().st_size
|
||||
status(' Probing temporary video with ffprobe...')
|
||||
duration, resolution, codec = probe(temporary_video)
|
||||
status(' Temporary copy removed.')
|
||||
else:
|
||||
size = dest.stat().st_size
|
||||
status(f' Probing with ffprobe: {dest}')
|
||||
duration, resolution, codec = probe(dest)
|
||||
total_bytes += size
|
||||
status(f' Original duration: {time_fmt(duration)} | Resolution: {resolution} | Codec: {codec}',
|
||||
'success' if duration is not None else 'warning')
|
||||
if duration is None:
|
||||
status(' ffprobe could not determine the duration; continuing with available metadata.', 'warning')
|
||||
rows.append((part, name, size, duration, resolution, codec))
|
||||
progress.set(index)
|
||||
if args.extract_only:
|
||||
status(f'Done: {len(videos)} video(s) processed. Output: {video_dir}', 'success', persist=True)
|
||||
return 0
|
||||
report_dir.mkdir(parents=True, exist_ok=True)
|
||||
data = report_data(source, rows, uses, external, out, settings)
|
||||
write_reports(data, formats, report_dir)
|
||||
status(f'Done: {data["known_playback_durations"]}/{len(data["playbacks"])} playback durations available, {time_fmt(data["total_duration_seconds"])} total, {size_fmt(total_bytes)}.', 'success', persist=True)
|
||||
return 0
|
||||
except subprocess.CalledProcessError as e:
|
||||
status(f'Typst compilation failed: {e.stderr.strip()}', 'error')
|
||||
return 1
|
||||
except (zipfile.BadZipFile, OSError, ET.ParseError) as e:
|
||||
status(str(e), 'error')
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Loading…
Reference in New Issue