diff --git a/cadence.py b/cadence.py index e51ad42..0ff9f71 100755 --- a/cadence.py +++ b/cadence.py @@ -47,6 +47,7 @@ import sys import tempfile import uuid import xml.etree.ElementTree as ET +import zipfile from collections.abc import Callable from dataclasses import asdict, dataclass from pathlib import Path @@ -1634,9 +1635,8 @@ def generate_handoff( transcript_name = f"{output_path.stem}.transcript.json" transcript_path = output_path.with_name(transcript_name) transcript_payload = _build_transcript_payload(track_data_list, subs, language) - _write_text_atomic( - transcript_path, json.dumps(transcript_payload, ensure_ascii=False, indent=2) - ) + transcript_text = json.dumps(transcript_payload, ensure_ascii=False, indent=2) + _write_text_atomic(transcript_path, transcript_text) logger.info(f"[*] Edited transcript generated -> {transcript_name}") handoff = _build_handoff_manifest( @@ -1648,11 +1648,46 @@ def generate_handoff( ) handoff_name = f"{output_path.stem}.handoff.json" handoff_path = output_path.with_name(handoff_name) - _write_text_atomic( - handoff_path, json.dumps(handoff, ensure_ascii=False, indent=2) - ) + handoff_text = json.dumps(handoff, ensure_ascii=False, indent=2) + _write_text_atomic(handoff_path, handoff_text) 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: try: diff --git a/tests/test_handoff.py b/tests/test_handoff.py index ba103a8..38a5297 100644 --- a/tests/test_handoff.py +++ b/tests/test_handoff.py @@ -1,4 +1,5 @@ import json +import zipfile import cadence 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 +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): td = make_track( name="video.mp4",