alfrednorman
9th June 2024, 16:40
Hi all!
I have a source where the main frame bounces around from left to right, but the frame itself is consistently the same width. This is the code I landed on that seems to work pretty well, I was wondering if anyone sees any issues with it. I'm very new to everything so it'd be nice to learn. Thanks!
import vapoursynth as vs
import functools
core = vs.core
clip = core.dgdecodenv.DGSource(source)
clip = core.wwxd.WWXD(clip)
template = core.std.BlankClip(clip, width=708, height=480)
class Selector:
def __init__(self):
self.avg = 0
self.maxavg = 0
self.maxavg_l = 0
self.total_avg = 0
self.total_frames = 1
def __call__(self, n, f, clip):
if n == 0 or f.props['Scenechange'] == 1:
self.avg = 0
self.maxavg = 0
self.maxavg_l = 0
self.total_avg = 0
self.total_frames = 1
for l in range(0, 12, 2):
cropped = core.std.CropAbs(clip, width=708, height=480, left=l).std.PlaneStats()
self.avg = cropped.get_frame(n).props['PlaneStatsAverage']
self.total_avg = self.avg
for frame in range(n + 1, n + 40):
while f.props['Scenechange'] == 0:
self.avg = cropped.get_frame(frame).props['PlaneStatsAverage']
self.total_avg += self.avg
self.total_frames += 1
avg_avg = self.total_avg / self.total_frames
if avg_avg > self.maxavg:
self.maxavg = avg_avg
self.maxavg_l = l
return core.std.CropAbs(clip, width=708, height=480, left=self.maxavg_l)
else:
return core.std.CropAbs(clip, width=708, height=480, left=self.maxavg_l)
selector = Selector()
clip = core.std.FrameEval(template, functools.partial(selector, clip=clip), prop_src=clip, clip_src=clip)
clip.set_output()
I have a source where the main frame bounces around from left to right, but the frame itself is consistently the same width. This is the code I landed on that seems to work pretty well, I was wondering if anyone sees any issues with it. I'm very new to everything so it'd be nice to learn. Thanks!
import vapoursynth as vs
import functools
core = vs.core
clip = core.dgdecodenv.DGSource(source)
clip = core.wwxd.WWXD(clip)
template = core.std.BlankClip(clip, width=708, height=480)
class Selector:
def __init__(self):
self.avg = 0
self.maxavg = 0
self.maxavg_l = 0
self.total_avg = 0
self.total_frames = 1
def __call__(self, n, f, clip):
if n == 0 or f.props['Scenechange'] == 1:
self.avg = 0
self.maxavg = 0
self.maxavg_l = 0
self.total_avg = 0
self.total_frames = 1
for l in range(0, 12, 2):
cropped = core.std.CropAbs(clip, width=708, height=480, left=l).std.PlaneStats()
self.avg = cropped.get_frame(n).props['PlaneStatsAverage']
self.total_avg = self.avg
for frame in range(n + 1, n + 40):
while f.props['Scenechange'] == 0:
self.avg = cropped.get_frame(frame).props['PlaneStatsAverage']
self.total_avg += self.avg
self.total_frames += 1
avg_avg = self.total_avg / self.total_frames
if avg_avg > self.maxavg:
self.maxavg = avg_avg
self.maxavg_l = l
return core.std.CropAbs(clip, width=708, height=480, left=self.maxavg_l)
else:
return core.std.CropAbs(clip, width=708, height=480, left=self.maxavg_l)
selector = Selector()
clip = core.std.FrameEval(template, functools.partial(selector, clip=clip), prop_src=clip, clip_src=clip)
clip.set_output()