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'
> #!/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
>
>         if row > cur_row:
>             output.extend(b'\n' * (row - cur_row))
>             cur_row = row
>             cur_col = 0
>         elif row < cur_row:
>             output.extend(f'\x1b[{cur_row - row}A'.encode())
>             cur_row = row
>
>         if col != cur_col:
>             if col == 0:
>                 output.extend(b'\r')
>             elif col < cur_col:
>                 output.extend(b'\b' * (cur_col - col))
>             else:
>                 dist = col - cur_col
>                 cost_cuf = len(f'\x1b[{dist}C')
>                 cost_cr_cuf = 1 + len(f'\x1b[{col}C')
>                 if cost_cr_cuf < cost_cuf:
>                     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:
>         i = 0
>         while i < width * height:
>             if prev_frame[i] != frame[i]:
>                 start_row = i // width
>                 start_col = i % width
>                 move_to(start_row, start_col)
>                 while i < width * height and prev_frame[i] != frame[i]:
>                     output.append(frame[i])
>                     i += 1
>                     cur_col += 1
>                     if cur_col >= width:
>                         cur_col = 0
>                         cur_row += 1
>             else:
>                 i += 1
>
>         # Check if we can use EL for trailing spaces on last modified row
>         if cur_row < height and cur_col < width:
>             row_start = cur_row * width
>             if all(frame[row_start + cc] == ord(' ') for cc in range(cur_col, width)):
>                 output.extend(b'\x1b[0K')
>                 cur_col = width
>
>         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
340344
root@d7299c7f5982:/app#
