#!/usr/bin/env python3 """Capture real speech on a ready paired device using Xcode's on-device video. Requires the isolated signed capture build and the user's device-readiness reply. Uses internal storage while recording, so external-drive disconnects cannot lose an active movie. Does not install over the user's shipping Jotley application. """ import argparse import json import plistlib from pathlib import Path import re import subprocess import time def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('device', choices=['iphone', 'ipad']) parser.add_argument('--udid', required=True) parser.add_argument('--test-run', type=Path, required=True) parser.add_argument('--audio', type=Path, required=True) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() args.output.mkdir(parents=True, exist_ok=False) result = args.output / 'result.xcresult' log = args.output / 'test.log' command = ['xcodebuild', 'test-without-building', '-xctestrun', str(args.test_run), '-destination', f'platform=iOS,id={args.udid}', '-parallel-testing-enabled', 'NO', '-collect-test-diagnostics', 'never', '-only-testing:Jotley-iOSUITests/JotleyVideoCaptureUITests/testCaptureRealLiveTranscription', '-resultBundlePath', str(result)] config = plistlib.loads(args.test_run.read_bytes()) capture_formats = sorted({t.get('PreferredScreenCaptureFormat', 'screenshots') for c in config['TestConfigurations'] for t in c['TestTargets']}) receipt = {'device': args.device, 'udid': args.udid, 'test_run': str(args.test_run), 'state': 'capturing', 'source_commit': '89422fb2bbfb839f24eb4ba85f4a34be1ff51020', 'speech': 'Real physical microphone and Apple Speech; no simulated speech hooks', 'capture_formats': capture_formats, 'started_wall': time.time()} receipt_path = args.output / 'capture.json' receipt_path.write_text(json.dumps(receipt, indent=2) + '\n') audio = None with log.open('w') as output: process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT) deadline = time.monotonic() + 360 try: while process.poll() is None: content = log.read_text() ready = re.search(r'CAPTURE-MARK live-ready ([0-9.]+)', content) if ready and audio is None and time.time() >= float(ready[1]) + 1: receipt['audio_start_wall'] = time.time() receipt['demo_audio'] = str(args.audio) audio = subprocess.Popen(['afplay', str(args.audio)]) receipt_path.write_text(json.dumps(receipt, indent=2) + '\n') if time.monotonic() > deadline: process.terminate() raise TimeoutError('Physical capture exceeded six minutes') time.sleep(0.15) finally: if audio and audio.poll() is None: audio.terminate() content = log.read_text() receipt['marks'] = {name: float(wall) for name, wall in re.findall(r'CAPTURE-MARK ([\w-]+) ([0-9.]+)', content)} receipt['test_exit_code'] = process.returncode receipt['one_test_executed'] = bool(re.search(r'Executed 1 test,', content)) receipt['state'] = 'passed' if process.returncode == 0 and receipt['one_test_executed'] else 'failed' attachments = args.output / 'attachments' if result.exists(): export = subprocess.run(['xcrun', 'xcresulttool', 'export', 'attachments', '--path', str(result), '--output-path', str(attachments)], capture_output=True, text=True) (args.output / 'attachments-export.log').write_text(export.stdout + export.stderr) receipt['attachments_export_exit_code'] = export.returncode movies = [] for movie in attachments.glob('*.mp4'): probe = subprocess.run(['ffprobe', '-v', 'error', '-show_format', '-show_streams', '-of', 'json', str(movie)], capture_output=True, text=True) decode = subprocess.run(['ffmpeg', '-v', 'error', '-i', str(movie), '-f', 'null', '-'], capture_output=True, text=True) movies.append({'file': str(movie), 'probe': json.loads(probe.stdout) if probe.returncode == 0 else None, 'full_decode': decode.returncode == 0}) receipt['movies'] = movies receipt_path.write_text(json.dumps(receipt, indent=2) + '\n') print(json.dumps(receipt, indent=2), flush=True) raise SystemExit(0 if receipt['state'] == 'passed' else process.returncode or 2) if __name__ == '__main__': main()