-
Notifications
You must be signed in to change notification settings - Fork 6
/
vidsort.py
executable file
·200 lines (185 loc) · 5.91 KB
/
vidsort.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
#!/usr/bin/env python
from PIL import Image, ImageFont, ImageStat, ImageFile, ImageOps, ImageDraw, ImageChops
from multiprocessing import Pool
import multiprocessing
from random import Random
random=Random()
import sys, os, math, gc
poolSize=10
pad=0
fmt=""
##############################################################################
# From: https://stackoverflow.com/a/16071616/1966871
def fun(f, q_in, q_out):
while True:
i, x = q_in.get()
if i is None:
break
q_out.put((i, f(x)))
def parmap(f, X, nprocs=multiprocessing.cpu_count()):
q_in = multiprocessing.Queue(1)
q_out = multiprocessing.Queue()
proc = [multiprocessing.Process(target=fun, args=(f, q_in, q_out))
for _ in range(nprocs)]
for p in proc:
p.daemon = True
p.start()
sent = [q_in.put((i, x)) for i, x in enumerate(X)]
[q_in.put((None, None)) for _ in range(nprocs)]
res = [q_out.get() for _ in range(len(sent))]
[p.join() for p in proc]
return [x for i, x in sorted(res)]
###############################################################################
def invertMask(pixel):
return abs(pixel-255)
def avg(l):
ax=0
for i in l:
ax+=i
return ax/len(l)
def chunkDivide(l, chunkMax):
ret=[]
i=0
while(len(l)>i):
j=i+chunkMax
if(chunkMax>=len(l)):
chunkMax=len(l)-1
ret.append(l[i:j])
i=j
return ret
def mindex(l):
i=0
m=l[0]
for j in range(0, len(l)):
if(l[j]<m):
m=l[j]
i=j
return (m, i)
cacheMaxItems=750
cache={}
def deCache(framenum, outdir):
while(len(cache.keys())>=cacheMaxItems):
del cache[random.choice(cache.keys())]
gc.collect()
num=str(framenum)
if(not(num in cache)):
try:
cache[num]=(Image.open(outdir+"/"+fmt.format(framenum)+".jpg")).convert("RGB")
except:
return None
return cache[num]
def imgDelta(a, b):
if(a==None or b==None):
return 2**64
merge=ImageChops.difference(a, b).convert("L")
return avg(ImageStat.Stat(merge).mean)
def vid2frames(vidfile, fps, outdir):
global pad, fmt
cmd=("mplayer -vo jpeg"+
":outdir="+outdir+
" -fps "+str(fps)+
" -ao pcm:file=\""+outdir+
"/audio.wav\" \""+vidfile+"\"")
print(cmd)
os.system(cmd)
framecount=0
for item in os.listdir(outdir):
if item.find(".jpg")>0:
pad=len(item)-4
framecount+=1
fmt="{0:0>"+str(pad)+"}"
return framecount
def frames2vid(vidfile, fps, framenums, outdir, audiofile):
def fileIfExists(x):
path=outdir+"/"+fmt.format(x)+".jpg"
if(os.path.exists(path)):
return "\"mf://"+path+"\""
else: return ""
os.system("mencoder -oac copy -ovc lavc -mf fps="+str(fps)+
" -o \""+vidfile+"\" "+
" ".join(map(fileIfExists, framenums)))
os.system("mencoder -oac copy -ovc copy " +
" -audiofile \""+outdir+"/"+audiofile+"\" "+
"-o \"new_"+vidfile+"\" \""+vidfile+"\"")
os.system("mv \"new_"+vidfile+"\" \""+vidfile+"\"")
def frame2audio(outdir, duration, idx, outfile):
os.system("sox \""+outdir+"/audio.wav\" \""+outdir+"/"+outfile+"\" trim "+str(duration*idx)+" "+str(duration*(idx+1)))
def frames2audio(outdir, fps, framenums):
spf=1.0/fps
frame2audio(outdir, spf, framenums[0], "ax.wav")
for frame in framenums[1:]:
frame2audio(outdir, spf, frame, "item.wav")
os.system("sox \""+outdir+"/ax.wav\" \""+outdir+"/item.wav\" ax2.wav")
os.system("mv ax2.wav \""+outdir+"/ax.wav\"")
os.system("mv \""+outdir+"/ax.wav\" \""+outdir+"/processed_audio.wav\"")
def findBestMatchFrame(needle, haystack, outdir):
global pool
if(len(haystack)==1):
return haystack[0]
sys.stdout.write("|")
needleF=deCache(needle, outdir)
sys.stdout.write("\b-")
sys.stdout.flush()
def deltaDecache(x):
sys.stdout.write("\b"+random.choice(["|", "/", "-", "\\"]))
sys.stdout.flush()
return imgDelta(needleF, deCache(x, outdir))
deltas=parmap(deltaDecache, haystack)
sys.stdout.write("\b")
sys.stdout.flush()
return mindex(deltas)[1]
#return mindex2(pool.map(lambda y: pool.map(lambda x: imgDelta(needleF, deCache(x, outdir)), y), chunkDivide(haystack, poolSize)))
def findMatchChain(initialFrame, frames, outdir):
numFrames=len(frames)
print("Caching frames...")
framesToCache=frames
if(len(framesToCache)>cacheMaxItems):
framesToCache=frames[:cacheMaxItems]
parmap(lambda x: deCache(x, outdir), framesToCache)
print("Starting frame path identification...")
outFrames=[initialFrame]
try:
frames.remove(0)
except:
pass
try:
del frames[initialFrame]
except:
pass
plugged=False
while(len(frames)>0):
sys.stdout.write(".")
percent=(len(frames)*100)/numFrames
if(percent%10 == 0):
if(not plugged):
sys.stdout.write(str(percent)+"%")
plugged=True
else:
plugged=False
frameSample=frames
if(len(frames)>1000):
frameSample=random.sample(frames, 1000)
match=findBestMatchFrame(outFrames[-1], frameSample, outdir)
try:
del cache[str(outFrames[-1])]
except:
pass
try:
del frames[match]
except:
outFrames.append(match)
outFrames.extend(frames)
return outFrames
outFrames.append(match)
gc.collect()
return outFrames
def main():
os.system("rm -rf ~/.vidsort")
os.system("mkdir ~/.vidsort")
outdir=os.getenv("HOME")+"/.vidsort"
frameCount=vid2frames(sys.argv[1], 25, outdir)
frames=findMatchChain(1, range(1, frameCount), outdir)
frames2audio(outdir, 25, frames)
frames2vid("out.avi", 25, frames, outdir, "processed_audio.wav")
if(__name__=="__main__"):
main()