Jotley / website / scripts / video-refresh / native-take.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Record a real Jotley window locally, verify it, then optionally archive it. |
| 3 | |
| 4 | Uses Apple's recorder; does not drive the interface or change app preferences. |
| 5 | Keep temporary recording files on the internal disk so a disconnected media |
| 6 | drive cannot interrupt the movie writer. |
| 7 | """ |
| 8 | import argparse |
| 9 | import datetime |
| 10 | import json |
| 11 | from pathlib import Path |
| 12 | import shutil |
| 13 | import subprocess |
| 14 | import time |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | parser = argparse.ArgumentParser(description=__doc__) |
| 19 | parser.add_argument("name") |
| 20 | parser.add_argument("--seconds", type=int, default=90) |
| 21 | parser.add_argument("--bundle", default="com.protomota.jotley.video-demo") |
| 22 | parser.add_argument("--window-kind", choices=["main", "widget", "menu"], default="main") |
| 23 | parser.add_argument("--directory", type=Path, default=Path("/tmp/jotley-video-refresh-20261023/mac")) |
| 24 | parser.add_argument("--archive", type=Path) |
| 25 | args = parser.parse_args() |
| 26 | if Path(args.name).name != args.name: |
| 27 | parser.error("name must be a filename without directories") |
| 28 | args.directory.mkdir(parents=True, exist_ok=True) |
| 29 | movie = args.directory / (args.name + ".mov") |
| 30 | receipt = movie.with_suffix(".capture.json") |
| 31 | if movie.exists() or receipt.exists(): |
| 32 | parser.error("take already exists; use a new name") |
| 33 | |
| 34 | swift = '''import AppKit |
| 35 | import CoreGraphics |
| 36 | let bundle = CommandLine.arguments.last! |
| 37 | guard let pid = NSRunningApplication.runningApplications(withBundleIdentifier: bundle).first?.processIdentifier else { exit(2) } |
| 38 | let windows = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as! [[String: Any]] |
| 39 | let own = windows.filter { $0[kCGWindowOwnerPID as String] as? Int32 == pid } |
| 40 | print(String(data: try! JSONSerialization.data(withJSONObject: own), encoding: .utf8)!) |
| 41 | ''' |
| 42 | windows = json.loads(subprocess.check_output(["swift", "-e", swift, args.bundle], text=True)) |
| 43 | if args.window_kind == "widget": |
| 44 | windows = [w for w in windows if w["kCGWindowBounds"]["Width"] == 220 |
| 45 | and w["kCGWindowBounds"]["Height"] == 52] |
| 46 | if not windows: |
| 47 | parser.error("native recording widget is not visible; start transcription and activate another app") |
| 48 | elif args.window_kind == "menu": |
| 49 | windows = [w for w in windows if w["kCGWindowLayer"] == 101 |
| 50 | and w["kCGWindowBounds"]["Width"] > 100 |
| 51 | and w["kCGWindowBounds"]["Height"] > 50] |
| 52 | if not windows: |
| 53 | parser.error("native status menu is not open") |
| 54 | window = max(windows, key=lambda w: w["kCGWindowBounds"]["Width"] * w["kCGWindowBounds"]["Height"]) |
| 55 | command = ["screencapture", "-x", "-v", "-o", "-a", "-V", str(args.seconds), "-l" + str(window["kCGWindowNumber"]), str(movie)] |
| 56 | started = time.time() |
| 57 | process = subprocess.Popen(command) |
| 58 | data = {"name": args.name, "bundle": args.bundle, "window_kind": args.window_kind, "window": window, "local_movie": str(movie), "pid": process.pid, |
| 59 | "started_wall": started, "started_utc": datetime.datetime.fromtimestamp(started, datetime.timezone.utc).isoformat(), |
| 60 | "state": "recording", "ui": "Unmodified native app window", "interpolation": False} |
| 61 | receipt.write_text(json.dumps(data, indent=2) + "\n") |
| 62 | print(json.dumps({"state": "recording", "movie": str(movie), "receipt": str(receipt)}), flush=True) |
| 63 | code = process.wait() |
| 64 | if code != 0: |
| 65 | data.update(state="failed", exit_code=code) |
| 66 | receipt.write_text(json.dumps(data, indent=2) + "\n") |
| 67 | raise SystemExit(code) |
| 68 | probe = subprocess.run(["ffprobe", "-v", "error", "-show_format", "-show_streams", "-of", "json", str(movie)], capture_output=True, text=True, check=True) |
| 69 | subprocess.run(["ffmpeg", "-v", "error", "-i", str(movie), "-f", "null", "-"], check=True) |
| 70 | data.update(state="verified", probe=json.loads(probe.stdout), full_decode=True) |
| 71 | if args.archive: |
| 72 | if not args.archive.is_dir(): |
| 73 | data["archive_pending"] = str(args.archive) |
| 74 | else: |
| 75 | target = args.archive / movie.name |
| 76 | if target.exists(): |
| 77 | raise FileExistsError(target) |
| 78 | shutil.copy2(movie, target) |
| 79 | data["archived_movie"] = str(target) |
| 80 | receipt.write_text(json.dumps(data, indent=2) + "\n") |
| 81 | if data.get("archived_movie"): |
| 82 | shutil.copy2(receipt, args.archive / receipt.name) |
| 83 | print(json.dumps({"state": "verified", "movie": str(movie), "archived": data.get("archived_movie")}), flush=True) |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | main() |