|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Retime an asciicast v2 recording without changing any output bytes. |
| 3 | +
|
| 4 | +- Every gap between events is capped at --max-gap seconds. |
| 5 | +- Each --squeeze START:END:SECONDS window (START/END are substrings of the |
| 6 | + output stream; END is searched after START) is rescaled so that the |
| 7 | + events between them span at most SECONDS. Use it for long progress bars |
| 8 | + and boot waits. |
| 9 | +""" |
| 10 | +import argparse |
| 11 | +import json |
| 12 | +import sys |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + parser = argparse.ArgumentParser() |
| 17 | + parser.add_argument("src") |
| 18 | + parser.add_argument("dst") |
| 19 | + parser.add_argument("--max-gap", type=float, default=1.4) |
| 20 | + parser.add_argument("--squeeze", action="append", default=[]) |
| 21 | + parser.add_argument("--title") |
| 22 | + args = parser.parse_args() |
| 23 | + |
| 24 | + with open(args.src, encoding="utf-8") as handle: |
| 25 | + header = json.loads(handle.readline()) |
| 26 | + events = [json.loads(line) for line in handle if line.strip()] |
| 27 | + |
| 28 | + outputs = [e for e in events if e[1] == "o"] |
| 29 | + times = [e[0] for e in outputs] |
| 30 | + gaps = [times[0]] + [b - a for a, b in zip(times, times[1:])] |
| 31 | + |
| 32 | + # Locate squeeze windows by output substrings (on the cumulative stream). |
| 33 | + stream, offsets = "", [] |
| 34 | + for event in outputs: |
| 35 | + offsets.append(len(stream)) |
| 36 | + stream += event[2] |
| 37 | + |
| 38 | + def index_at(char_pos): |
| 39 | + lo, hi = 0, len(offsets) - 1 |
| 40 | + while lo < hi: |
| 41 | + mid = (lo + hi + 1) // 2 |
| 42 | + if offsets[mid] <= char_pos: |
| 43 | + lo = mid |
| 44 | + else: |
| 45 | + hi = mid - 1 |
| 46 | + return lo |
| 47 | + |
| 48 | + scale = [1.0] * len(gaps) |
| 49 | + cursor = 0 |
| 50 | + for spec in args.squeeze: |
| 51 | + start, end, seconds = spec.rsplit(":", 2) if spec.count(":") >= 2 else (None, None, None) |
| 52 | + start_pos = stream.find(start, cursor) |
| 53 | + end_pos = stream.find(end, start_pos + len(start)) if start_pos >= 0 else -1 |
| 54 | + if start_pos < 0 or end_pos < 0: |
| 55 | + sys.exit(f"squeeze window not found: {spec!r}") |
| 56 | + first, last = index_at(start_pos) + 1, index_at(end_pos) |
| 57 | + span = sum(min(g, args.max_gap) for g in gaps[first:last + 1]) |
| 58 | + factor = min(1.0, float(seconds) / span) if span > 0 else 1.0 |
| 59 | + for i in range(first, last + 1): |
| 60 | + scale[i] = factor |
| 61 | + print(f"squeeze {start!r}..{end!r}: {span:.1f}s -> {span * factor:.1f}s", file=sys.stderr) |
| 62 | + cursor = end_pos |
| 63 | + |
| 64 | + clock, retimed = 0.0, [] |
| 65 | + for event, gap, factor in zip(outputs, gaps, scale): |
| 66 | + clock += min(gap, args.max_gap) * factor |
| 67 | + retimed.append([round(clock, 4), "o", event[2]]) |
| 68 | + |
| 69 | + header.pop("idle_time_limit", None) |
| 70 | + if args.title: |
| 71 | + header["title"] = args.title |
| 72 | + header.pop("env", None) |
| 73 | + header.pop("command", None) |
| 74 | + with open(args.dst, "w", encoding="utf-8") as handle: |
| 75 | + handle.write(json.dumps(header, ensure_ascii=False) + "\n") |
| 76 | + for event in retimed: |
| 77 | + handle.write(json.dumps(event, ensure_ascii=False) + "\n") |
| 78 | + print(f"{len(retimed)} events, {times[-1]:.1f}s -> {clock:.1f}s", file=sys.stderr) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments