-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFillDuplicateFrames.py
268 lines (233 loc) · 10.8 KB
/
FillDuplicateFrames.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import vapoursynth as vs
from vapoursynth import core
'''
call using:
from FillDuplicateFrames import FillDuplicateFrames
fdf = FillDuplicateFrames(clip, debug=True, thresh=0.001, method='SVP')
//fdf = FillDuplicateFrames(clip, debug=True, thresh=0.001, method='MV')
//fdf = FillDuplicateFrames(clip, debug=True, thresh=0.001, method='RIFE')
clip = fdf.out
Replaces duplicate frames with interpolations.
v0.0.3
0.0.4 removed and added back RGBH support or RIFE
0.0.3 allow to set device_index for RIFE and support RGBH input for RIFE
0.0.4 removed RGBH since RIFE ncnn does not support it
0.0.5 add general sceneThr
0.0.6 add rifeModel parameter
0.0.7 add mode: FillDuplicate|FillDrops|Replace, add: rifeTTA, rifeUHD
'''
class FillDuplicateFrames:
# constructor
def __init__(self, clip: vs.VideoNode, mode='FillDuplicate', thresh: float=0.001, method: str='SVP', sceneThr: float=0.15, rifeModel: int=22, rifeTTA=False, rifeUHD=False, frames = [], debug: bool=False, device_index: int=0):
# calculte stats
self.thresh = thresh
self.debug = debug
self.method = method
self.smooth = None
self.sceneThr = sceneThr
self.device_index = device_index
self.rifeModel = rifeModel
self.rifeTTA = rifeTTA
self.rifeUHD = rifeUHD
self.clip = core.std.PlaneStats(clip, clip[0]+clip)
self.mode = mode
self.frames = frames
if sceneThr > 0 and method.lower() == 'rife':
clip = core.misc.SCDetect(clip=clip,threshold=sceneThr)
if method == 'Replace' and not frames:
raise ValueError(f'FillDuplicateFrames: "frames" needs to be set when using \'{self.method}\'!')
def interpolate(self, n, f):
if self.mode == 'FillDuplicate':
out = self.get_current_or_interpolate(n)
elif self.mode == 'FillDrops':
out = self.get_current_or_interpolate_for_fill(n)
elif self.mode == 'Replace':
out = self.replaceFrame(n)
else:
raise ValueError(f'FillDuplicateFrames: Unknown mode \'{self.mode}\'!')
if self.debug:
return out.text.Text(text="avg: "+str(f.props['PlaneStatsDiff']),alignment=8)
return out
def interpolateWithRIFE(self, clip, n, start, end):
if clip.format.id != vs.RGBS:
raise ValueError(f'FillDuplicateFrames: "clip" needs to be RGBS when using \'{self.method}\'!')
num = end - start
self.smooth = core.rife.RIFE(clip, model=self.rifeModel, factor_num=num, tta=self.rifeTTA,uhd=self.rifeUHD,gpu_id=self.device_index)
self.smooth_start = start
self.smooth_end = end
return self.smooth[n-start]
def interpolateWithMV(self, clip, n, start, end):
num = end - start
sup = core.mv.Super(clip, pel=2, hpad=0, vpad=0)
bvec = core.mv.Analyse(sup, blksize=16, isb=True, chroma=True, search=3, searchparam=1)
fvec = core.mv.Analyse(sup, blksize=16, isb=False, chroma=True, search=3, searchparam=1)
self.smooth = core.mv.FlowFPS(clip, sup, bvec, fvec, num=num, den=1, mask=2)
self.smooth_start = start
self.smooth_end = end
out = self.smooth[n-start]
if self.debug:
return out.text.Text(text="MV",alignment=9)
return out
def interpolateWithSVP(self, clip, n, start, end):
if clip.format.id != vs.YUV420P8:
raise ValueError(f'FillDuplicateFrames: "clip" needs to be YUV420P8 when using \'{self.method}\'!')
if self.method.lower() == 'svp_gpu' or self.method == 'SVP':
super = core.svp1.Super(clip,"{gpu:1}")
else: # self.method == 'SVPCPU':
super = core.svp1.Super(clip,"{gpu:0}")
vectors = core.svp1.Analyse(super["clip"],super["data"],clip,"{}")
num = end - start
self.smooth = core.svp2.SmoothFps(clip,super["clip"],super["data"],vectors["clip"],vectors["data"],f"{{rate:{{num:{num},den:1,abs:true}}}}")
self.smooth_start = start
self.smooth_end = end
out = self.smooth[n-start]
if self.debug:
return out.text.Text(text="SVP",alignment=9)
return out
def get_current_or_interpolate(self, n):
if self.is_not_duplicate(n):
if self.potential_scene_change(n):
if self.debug:
return self.clip[n].text.Text(text="Input (scene change - 1)", alignment=9)
elif self.debug:
#current non dublicate selected
return self.clip[n].text.Text(text="Input (1)", alignment=9)
return self.clip[n]
#dublicate frame, frame is interpolated
for start in reversed(range(n+1)):
if self.is_not_duplicate(start):
break
else: #there are all black frames preceding n, return current n frame // will be executed then for-look does not end with a break
if self.debug:
return self.clip[n].text.Text(text="Input (2)", alignment=9)
return self.clip[n]
for end in range(n, len(self.clip)):
if self.potential_scene_change(end):
#there are all duplicate frames to the end, return current n frame
if self.debug:
return self.clip[n].text.Text(text="Input(before scene change)", alignment=9)
return self.clip[n]
if self.is_not_duplicate(end):
break
else:
#there are all duplicate frames to the end, return current n frame
if self.debug:
return self.clip[n].text.Text(text="Input(3)", alignment=9)
return self.clip[n]
#does interpolated smooth clip exist for requested n frame? Use n frame from it.
if self.smooth is not None and start >= self.smooth_start and end <= self.smooth_end:
if self.debug:
return self.smooth[n-start].text.Text(text=self.method, alignment=9)
return self.smooth[n-start]
#interpolating two frame clip into end-start+1 fps
clip = self.clip[start] + self.clip[end]
clip = clip.std.AssumeFPS(fpsnum=1, fpsden=1)
if self.method.lower() == 'svp' or self.method == 'SVPcpu' or self.method == 'svp_gpu':
return self.interpolateWithSVP(clip, n, start, end)
elif self.method.lower() == 'rife':
return self.interpolateWithRIFE(clip, n, start, end)
elif self.method.lower() == 'mv':
return self.interpolateWithMV(clip, n, start, end)
else:
raise ValueError(f'FillDuplicateFrames: {self.mode} "method" \'{self.method}\' is not supported atm.')
def get_current_or_interpolate_for_fill(self, n):
if n == 0 or n >= self.clip.num_frames -1:
if self.debug:
return self.clip[n].text.Text(text="Input (0)", alignment=9)
return self.clip[n]
if self.is_not_duplicate(n):
if self.potential_scene_change(n):
if self.debug:
return self.clip[n].text.Text(text="Input (scene change - 1)", alignment=9)
elif self.debug:
#current non dublicate selected
return self.clip[n].text.Text(text="Input (1)", alignment=9)
return self.clip[n]
start = n-1
# previous frame is duplicate => nothing can be done
if not self.is_not_duplicate(start):
if self.debug:
return self.clip[n].text.Text(text="Input (2)", alignment=9)
return self.clip[n]
# previous frame is scene change => nothing can be done
if self.potential_scene_change(start):
if self.debug:
return self.clip[n].text.Text(text="Input(before scene change)", alignment=9)
return self.clip[n]
end = n+1
# next frame is duplicate => nothing can be done
if not self.is_not_duplicate(start):
if self.debug:
return self.clip[n].text.Text(text="Input (2)", alignment=9)
return self.clip[n]
# nex frame is scene change => nothing can be done
if self.potential_scene_change(start):
if self.debug:
return self.clip[n].text.Text(text="Input(before scene change)", alignment=9)
return self.clip[n]
#does interpolated smooth clip exist for requested n frame? Use n frame from it.
if self.smooth is not None and start >= self.smooth_start and end <= self.smooth_end:
if self.debug:
return self.smooth[n-start].text.Text(text=self.method, alignment=9)
return self.smooth[n-start]
#interpolating two frame clip into end-start+1 fps
clip = self.clip[start] + self.clip[end]
clip = clip.std.AssumeFPS(fpsnum=1, fpsden=1)
if self.method == 'svp_gpu' or self.method == 'svp':
return self.interpolateWithSVP(clip, n, start, end)
elif self.method.lower() == 'rife':
return self.interpolateWithRIFE(clip, n, start, end)
elif self.method.lower() == 'mv':
return self.interpolateWithMV(clip, n, start, end)
else:
raise ValueError(f'FillDuplicateFrames: {self.mode} "method" \'{self.method}\' is not supported atm.')
def replaceFrame(self, n):
if not n in self.frames or n == 0 or n >= self.clip.num_frames -1:
if self.debug:
return self.clip[n].text.Text(text="Input (0)", alignment=9)
return self.clip[n]
start = n-1
# previous frame is duplicate => nothing can be done
if not self.is_not_duplicate(start):
if self.debug:
return self.clip[n].text.Text(text="Input (1)", alignment=9)
return self.clip[n]
# previous frame is scene change => nothing can be done
if self.potential_scene_change(start):
if self.debug:
return self.clip[n].text.Text(text="Input(before scene change)", alignment=9)
return self.clip[n]
end = n+1
# next frame is duplicate => nothing can be done
if not self.is_not_duplicate(start):
if self.debug:
return self.clip[n].text.Text(text="Input (2)", alignment=9)
return self.clip[n]
# nex frame is scene change => nothing can be done
if self.potential_scene_change(start):
if self.debug:
return self.clip[n].text.Text(text="Input(before scene change)", alignment=9)
return self.clip[n]
#does interpolated smooth clip exist for requested n frame? Use n frame from it.
if self.smooth is not None and start >= self.smooth_start and end <= self.smooth_end:
if self.debug:
return self.smooth[n-start].text.Text(text=self.method, alignment=9)
return self.smooth[n-start]
#interpolating two frame clip into end-start+1 fps
clip = self.clip[start] + self.clip[end]
clip = clip.std.AssumeFPS(fpsnum=1, fpsden=1)
if self.method.lower() == 'svp_gpu' or self.method.lower() == 'svp':
return self.interpolateWithSVP(clip, n, start, end)
elif self.method.lower() == 'rife':
return self.interpolateWithRIFE(clip, n, start, end)
elif self.method.lower() == 'mv':
return self.interpolateWithMV(clip, n, start, end)
else:
raise ValueError(f'FillDuplicateFrames: {self.mode} "method" \'{self.method}\' is not supported atm.')
def is_not_duplicate(self, n):
return self.clip.get_frame(n).props['PlaneStatsDiff'] > self.thresh
def potential_scene_change(self, n):
return self.sceneThr > 0 and self.clip.get_frame(n).props['PlaneStatsDiff'] > self.sceneThr
@property
def out(self):
return core.std.FrameEval(self.clip, self.interpolate, prop_src=self.clip)