from Crypto.Cipher import DES3
import sys
key = bytearray.fromhex("00000000000000000000000000000000")
des = DES3.new(key,DES3.MODE_ECB)
if len(sys.argv) != 2:
    print("Name of file to fix required")
    exit(1)
with open( sys.argv[1]+"fixed.ts", "wb", buffering=1024*1024) as outfile:
    with open( sys.argv[1], "rb", buffering=1024*1024) as infile:
        rec = infile.read(192)
        while rec:
            if (rec[7] & 0x30 ) == 0x10 :
                decoded = des.decrypt(rec[8:])
                outrec = rec[:8]+decoded
            else:
                start = 9+rec[8]
                stop = 8*int((len(rec)-start)/8)+start
                decoded = des.decrypt(rec[start:stop])
                outrec = rec[:start]+decoded+rec[stop:]
            outfile.write(outrec)
            rec = infile.read(192)
exit(0)