feat/cadence-handoff #3

Merged
pedro-bento merged 3 commits from feat/cadence-handoff into master 2026-10-05 20:33:46 +01:00
2 changed files with 83 additions and 6 deletions
Showing only changes of commit 9fa4a016c5 - Show all commits

View file

@ -47,6 +47,7 @@ import sys
import tempfile import tempfile
import uuid import uuid
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import zipfile
from collections.abc import Callable from collections.abc import Callable
from dataclasses import asdict, dataclass from dataclasses import asdict, dataclass
from pathlib import Path from pathlib import Path
@ -1634,9 +1635,8 @@ def generate_handoff(
transcript_name = f"{output_path.stem}.transcript.json" transcript_name = f"{output_path.stem}.transcript.json"
transcript_path = output_path.with_name(transcript_name) transcript_path = output_path.with_name(transcript_name)
transcript_payload = _build_transcript_payload(track_data_list, subs, language) transcript_payload = _build_transcript_payload(track_data_list, subs, language)
_write_text_atomic( transcript_text = json.dumps(transcript_payload, ensure_ascii=False, indent=2)
transcript_path, json.dumps(transcript_payload, ensure_ascii=False, indent=2) _write_text_atomic(transcript_path, transcript_text)
)
logger.info(f"[*] Edited transcript generated -> {transcript_name}") logger.info(f"[*] Edited transcript generated -> {transcript_name}")
handoff = _build_handoff_manifest( handoff = _build_handoff_manifest(
@ -1648,11 +1648,46 @@ def generate_handoff(
) )
handoff_name = f"{output_path.stem}.handoff.json" handoff_name = f"{output_path.stem}.handoff.json"
handoff_path = output_path.with_name(handoff_name) handoff_path = output_path.with_name(handoff_name)
_write_text_atomic( handoff_text = json.dumps(handoff, ensure_ascii=False, indent=2)
handoff_path, json.dumps(handoff, ensure_ascii=False, indent=2) _write_text_atomic(handoff_path, handoff_text)
)
logger.info(f"[*] Handoff manifest generated -> {handoff_name}") logger.info(f"[*] Handoff manifest generated -> {handoff_name}")
zip_name = f"{output_path.stem}.handoff.zip"
zip_path = output_path.with_name(zip_name)
_write_handoff_zip(
zip_path,
handoff_name=handoff_name,
handoff_text=handoff_text,
transcript_name=transcript_name,
transcript_text=transcript_text,
)
logger.info(f"[*] Handoff bundle generated -> {zip_name}")
def _write_handoff_zip(
zip_path: Path,
*,
handoff_name: str,
handoff_text: str,
transcript_name: str,
transcript_text: str,
) -> None:
"""Atomically bundle the loose manifest and transcript into one ZIP.
The manifest is stored under its ``*.handoff.json`` basename (what the
clipwright backend discovers) and the transcript under the exact basename
referenced by the manifest's ``transcript.path``.
"""
tmp = zip_path.with_suffix(zip_path.suffix + ".tmp")
try:
with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr(handoff_name, handoff_text)
zf.writestr(transcript_name, transcript_text)
os.replace(tmp, zip_path)
except BaseException:
tmp.unlink(missing_ok=True)
raise
def _positive_int(value: str) -> int: def _positive_int(value: str) -> int:
try: try:

View file

@ -1,4 +1,5 @@
import json import json
import zipfile
import cadence import cadence
from cadence import Segment, Word from cadence import Segment, Word
@ -94,6 +95,47 @@ def test_generate_handoff_writes_valid_json(make_track, tmp_path):
assert json.loads(json.dumps(handoff)) == handoff assert json.loads(json.dumps(handoff)) == handoff
def test_generate_handoff_writes_upload_bundle(make_track, tmp_path):
td = make_track(
name="video.mp4",
duration=2.0,
timeline=[Segment(0.0, 2.0, "keep")],
words=[Word("Hello.", 0.2, 0.5), Word("world.", 0.6, 0.9)],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_handoff([td], out, jl_mode="j", jl_frames=2, language="en")
zip_path = tmp_path / "recording.handoff.zip"
assert zip_path.exists()
assert not list(tmp_path.glob("*.tmp"))
with zipfile.ZipFile(zip_path) as zf:
member_names = zf.namelist()
zipped_handoff = json.loads(zf.read("recording.handoff.json"))
zipped_transcript = json.loads(zf.read("recording.transcript.json"))
assert member_names == [
"recording.handoff.json",
"recording.transcript.json",
]
# Manifest points at the transcript member by basename.
transcript_member = zipped_handoff["transcript"]["path"]
assert transcript_member == "recording.transcript.json"
assert transcript_member in member_names
# Bundled manifest matches the loose one written alongside the ZIP.
loose_handoff = json.loads(
(tmp_path / "recording.handoff.json").read_text(encoding="utf-8")
)
loose_transcript = json.loads(
(tmp_path / "recording.transcript.json").read_text(encoding="utf-8")
)
assert zipped_handoff == loose_handoff
assert zipped_transcript == loose_transcript
def test_transcript_words_carry_leading_space(make_track, tmp_path): def test_transcript_words_carry_leading_space(make_track, tmp_path):
td = make_track( td = make_track(
name="video.mp4", name="video.mp4",