Previous response had warnings:
WARNINGS: - Extra text detected before JSON object
- Extra text detected after JSON object

New Terminal Output:

root@d7299c7f5982:/app# cat > /app/termzip_ansi.py << 'ENDOFFILE'
extend(b'\r')
                        output.extend(f'\x1b[{col}C'.encode())
                    else:
                        output.extend(f'\x1b[{dist}C'.encode())
            cur_col = col

    for frame in frames:
        # Find rows with changes and their change runs
        for r in range(height):
            row_start = r * width
            c = 0
            while c < width:
                if prev_frame[row_start + c] != frame[row_start + c]:
                    move_to(r, c)
                    while c < width and prev_frame[row_start + c] != frame[row_start + c]:
                        output.append(frame[row_start + c])
                        c += 1
                        cur_col += 1
                        if cur_col >= width:
                            cur_col = 0
                            cur_row += 1
                else:
                    c += 1
        output.append(0x1e)
        prev_frame = frame

    sys.stdout.buffer.write(output)

if __name__ == '__main__':
    main()
ENDOFFILE
> #!/usr/bin/env python3
> import struct
> import sys
>
> def read_tza(filename):
>     with open(filename, 'rb') as f:
>         magic = f.read(4)
>         if magic != b'TZA1':
>             raise ValueError(f'Invalid magic: {magic}')
>         width, height = struct.unpack('<HH', f.read(4))
>         n_frames, max_bytes = struct.unpack('<II', f.read(8))
>         frames = []
>         for _ in range(n_frames):
>             frame_data = f.read(width * height)
>             frames.append(frame_data)
>         return width, height, n_frames, max_bytes, frames
>
> def main():
>     if len(sys.argv) != 2:
>         sys.stderr.write(f'Usage: {sys.argv[0]} <frames.tza>\n')
>         sys.exit(1)
>
>     filename = sys.argv[1]
>     width, height, n_frames, max_bytes, frames = read_tza(filename)
>
>     prev_frame = b' ' * (width * height)
>     output = bytearray()
>
>     cur_row, cur_col = 0, 0
>
>     def move_to(row, col):
>         nonlocal cur_row, cur_col, output
>         if row == cur_row and col == cur_col:
>             return
>
>         # Move to target row
>         if row > cur_row:
>             # Use LF to move down
>             output.extend(b'\n' * (row - cur_row))
>             cur_row = row
>         elif row < cur_row:
>             # Use CUU to move up
>             output.extend(f'\x1b[{cur_row - row}A'.encode())
>             cur_row = row
>
>         # Move to target column
>         if col != cur_col:
>             if col == 0:
>                 output.extend(b'\r')
>             elif col < cur_col:
>                 output.extend(b'\b' * (cur_col - col))
>             else:
>                 # col > cur_col
>                 dist = col - cur_col
>                 if dist <= 3:
>                     # Just use CUF
>                     output.extend(f'\x1b[{dist}C'.encode())
>                 else:
>                     # CR + CUF might be shorter
>                     cr_cost = 1 + len(f'\x1b[{col}C')
>                     cuf_cost = len(f'\x1b[{dist}C')
>                     if cr_cost < cuf_cost:
>                         output.extend(b'\r')
>                         output.extend(f'\x1b[{col}C'.encode())
>                     else:
>                         output.extend(f'\x1b[{dist}C'.encode())
>             cur_col = col
>
>     for frame in frames:
>         # Find rows with changes and their change runs
>         for r in range(height):
>             row_start = r * width
>             c = 0
>             while c < width:
>                 if prev_frame[row_start + c] != frame[row_start + c]:
>                     move_to(r, c)
>                     while c < width and prev_frame[row_start + c] != frame[row_start + c]:
>                         output.append(frame[row_start + c])
>                         c += 1
>                         cur_col += 1
>                         if cur_col >= width:
>                             cur_col = 0
>                             cur_row += 1
>                 else:
>                     c += 1
>         output.append(0x1e)
>         prev_frame = frame
>
>     sys.stdout.buffer.write(output)
>
> if __name__ == '__main__':
>     main()
> ENDOFFILE
root@d7299c7f5982:/app# python3 /app/termzip_ansi.py /app/frames.tza | wc -c
464142
root@d7299c7f5982:/app#
