#!/usr/bin/python3

# solution from:
# https://forum.videohelp.com/threads/395630-Playing-264-files-not-working#post2571670
#
# Thanks, ljx34, whoever you are. ~Aaron
#

import binascii
import struct
import sys

outdata = open(sys.argv[2], "ab")
with open(sys.argv[1], "rb") as f:
    byte = f.read(1)
    while byte != b"":
        if byte == b"I":
            byte = f.read(1)
            if byte == b"T":
                byte = f.read(1)
                if byte == b"K":
                    byte = f.read(1)
                    if byte == b"A":
                        chunkcount1 = f.read(1)
                        binascii.hexlify(chunkcount1)
                        chunkcount2 = f.read(1)
                        binascii.hexlify(chunkcount2)
                        combinedchunk = chunkcount1 + chunkcount2
                        chunkcount = struct.unpack("<h", combinedchunk)
                        outdata.write(b"ITKA")
                        if int(chunkcount[0]) > 0:
                            x = 0
                            total = int(chunkcount[0]) + 10
                            while x != total:
                                byte = f.read(1)
                                outdata.write(byte)
                                x += 1
        byte = f.read(1)
