-
Notifications
You must be signed in to change notification settings - Fork 2
/
tomato.py
248 lines (194 loc) · 6.76 KB
/
tomato.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/python
import argparse, os, re, random, struct
from itertools import chain
print " _ _ "
print "| | | | "
print "| |_ ___ _ __ ___ __ _| |_ ___ "
print "| __/ _ \| '_ ` _ \ / _` | __/ _ \ "
print "| || (_) | | | | | | (_| | || (_) |"
print " \__\___/|_| |_| |_|\__,_|\__\___/ "
print "tomato.py v1.3 last update 12.10.2017"
print "\\\\ Audio Video Interleave index breaker"
print " "
print "\"je demande a ce qu'on tienne pour un cretin"
print "celui qui se refuserait encore, par exemple,"
print "a voir un cheval galoper sur une tomate.\""
print " - Andre Breton"
print " "
print "___________________________________"
print " "
#################
### ARGUMENTS ###
#################
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument("-i", "--input", help="input file")
parser.add_argument('-m', "--mode", action='store', dest='modevalue',help='choose mode, one of:\nshuffle irep ikill bloom pulse reverse invert')
parser.add_argument('-ss', action='store', dest='ignoredframes',help='var3', default=1)
parser.add_argument('-t', action='store', dest='glitchedframes',help='var4', default=0)
parser.add_argument('-c', action='store', dest='countframes',help='var1', default=1)
parser.add_argument('-n', action='store', dest='positframes',help='var2', default=1)
parser.add_argument('-l','--lim', help='fraction of file to search before giving up, default: 4', default=4)
parser.add_argument("file", help="input file")
args = parser.parse_args()
fileout = args.file
filein = args.input
mode = args.modevalue
ignoredframes = args.ignoredframes
glitchedframes = args.glitchedframes
countframes = args.countframes
positframes = args.positframes
#######################
### HELPER FUNCTION ###
#######################
def constrain(val, min_val, max_val):
return min(max_val, max(min_val, val))
####################
### OPENING FILE ###
####################
with open(filein,'rb') as rd:
print "Opening File\n"
filesize = os.path.getsize(filein)
chunk = 1024
lim = args.lim
idx = ''
with open(fileout,'wb') as wr:
print "Streaming File\n"
for pos in xrange(filesize-chunk, filesize-filesize/1, -chunk): # move backwards through data
rd.seek(pos)
buffer = rd.read(chunk)
if buffer.find(b'idx1') >= 0:
split = buffer.split(b'idx1', 1)
rd.seek(0)
wr.write(rd.read(pos)) # start the read at the beginning again,
wr.write(split[0]) # spit out data up to this point plus the stuff before idx
rd.seek(len(split[0])+4,1)
idx = rd.read()
break
if len(idx) == 0:
print('Could not locate index!\n')
raise SystemExit # quit
print "Getting list of Tomatoes\n"
## get the length of the index and store it
idx, index_length = idx[4:], idx[:4]
## get the first iframe and store it
n = 16
first_frame, idx = idx[:n], idx[n:]
check = bytearray()
check.extend(first_frame)
#print([i for i in check])
## put all frames in array ignoring sound frames
regex = re.compile(b'.*wb.*')
## option for ignoring n first frames (with -ss)
ignored_bytes = (int(ignoredframes) - 1) * n
ignored_idx = idx[:ignored_bytes]
idx = idx[ignored_bytes:]
## option for only glitching t frames (with -t)
if glitchedframes != 0 :
end_bytes = int(glitchedframes) * n
end_idx = idx[end_bytes:]
idx = idx[:end_bytes]
else :
end_idx = ""
## unignored part of the index to act upon
idx = [idx[i:i+n] for i in range(0, len(idx), n) if not re.match(regex,idx[i:i+n])]
## calculate number of frames
number_of_frames = len(idx)
print "Ready for the serious shitz\n"
#########################
### OPERATIONS TO IDX ###
#########################
if mode == "void":
print "### MODE - VOID"
print "##################\n"
print "not doing shit"
if mode == "shuffle":
print "### MODE - RANDOM"
print "##################\n"
idx = random.sample(idx,number_of_frames)
if mode == "jiggle":
print "### MODE - JIGGLE"
print "##################\n"
l = len(idx)-1
amount = int(positframes)
idx = [idx[constrain(x+int(random.gauss(0,amount)),0,l)] for x in range(0,len(idx))]
if mode == "ikill":
print "### MODE - IKILL"
print "##################\n"
regex = re.compile(b'.*dc\x10.*')
idx = [x for x in idx if not re.match(regex,x)]
if mode == "irep":
print "### MODE - IREP"
print "##################\n"
nidx = []
last = None
regex = re.compile(b'.*dc\x10.*')
for x in idx:
if not last: last = x
if not re.match(regex,x):
nidx.append(x)
last = x
else:
nidx.append(last)
idx = nidx
if mode == "bloom":
print "### MODE - BLOOM"
print "##################\n"
## bloom options
repeat = int(countframes)
frame = int(positframes)
## split list
lista = idx[:frame]
listb = idx[frame:]
## rejoin list with bloom
idx = lista + ([idx[frame]]*repeat) + listb
if mode == "pulse":
print "### MODE - PULSE"
print "##################\n"
pulselen = int(countframes)
pulseryt = int(positframes)
idx = [[x for j in range(pulselen)] if not i%pulseryt else x for i,x in enumerate(idx)]
idx = [item for sublist in idx for item in sublist] #flattens the list
idx = ''.join(idx)
idx = [idx[i:i+n] for i in range(0, len(idx), n)]
if mode == "overlapped":
print "### MODE - OVERLAPPED"
print "##################\n"
pulselen = int(countframes)
pulseryt = int(positframes)
idx = [idx[i:i+pulselen] for i in range(0,len(idx),pulseryt)]
idx = [item for sublist in idx for item in sublist]
if mode == "reverse":
print "### MODE - REVERSE"
print "##################\n"
idx = idx[::-1]
if mode == "invert":
print "### MODE - INVERT"
print "##################\n"
idx = sum(zip(idx[2::1], idx[::1]), ())
########################
### FIX INDEX LENGTH ###
########################
print "old index size : " + str(number_of_frames + 1 + int(ignoredframes) + (len(end_idx)/16)) + " frames"
index_length = len(idx)*16 + 16 + ignored_bytes
if end_idx : index_length = index_length + len(end_idx)
print "new index size : " + str((index_length/16)) + " frames\n"
## convert it to packed data
index_length = struct.pack('<I',index_length)
###################
### SAVING FILE ###
###################
print "Saving new file\n"
## rejoin the whole thing
data = b''.join(b'idx1' + index_length + first_frame + ignored_idx + b''.join(idx) + end_idx)
wr = open(fileout, 'ab')
wr.write(data)
wr.close()
print "Your file has been saved <3\n"
print "Prefer VLC to view your unstable video file"
print "But don't forget to bake it ! :)"
#############
### DEBUG ###
#############
## creates a seperate file with the index
# f3 = open('index.avi', 'wb')
# f3.write(''.join(idx))