Skip to content

Commit

Permalink
Use with x.get_frame instead of x.get_frame globally
Browse files Browse the repository at this point in the history
This should help prevent some minor memory leaks. Since it only happens upon init, the memory leaks ultimately shouldn't get worse over time (unless called in i.e. a frame eval). However, since a lot of functions call `get_prop`, this might negatively impact memory for scripts that depend on a lot of these functions.
  • Loading branch information
LightArrowsEXE committed Feb 3, 2025
1 parent d71fb67 commit f70384e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion vstools/enums/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ def from_clip(cls: type[SarSelf], clip: HoldsPropValueT) -> SarSelf:
if isinstance(clip, vs.RawFrame):
props = clip.props
elif isinstance(clip, vs.RawNode):
props = clip.get_frame(0).props
with clip.get_frame(0) as frame:
props = frame.props
else:
props = clip


return cls(get_prop(props, '_SARNum', int, None, 1), get_prop(props, '_SARDen', int, None, 1))

@classmethod
Expand Down
4 changes: 3 additions & 1 deletion vstools/exceptions/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ def check(func: FuncExceptT, to_check: vs.VideoNode) -> None:
"""

try:
to_check.get_frame(0)
with to_check.get_frame(0):
pass
except vs.Error as e:
if 'no path between colorspaces' in str(e):

raise InvalidColorspacePathError(func, e)
raise

Expand Down
4 changes: 3 additions & 1 deletion vstools/functions/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ def video_heuristics(
heuristics = dict[str, PropEnum]()

if props is True:
props_dict = clip.get_frame(0).props
with clip.get_frame(0) as frame:
props_dict = frame.props
else:
props_dict = props or None


def try_or_fallback(prop_type: type[SelfPropEnum]) -> SelfPropEnum:
try:
assert props_dict
Expand Down
3 changes: 2 additions & 1 deletion vstools/utils/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def get_var_infos(frame: vs.VideoNode | vs.VideoFrame) -> tuple[vs.VideoFormat,
if isinstance(frame, vs.VideoNode) and not (
frame.width and frame.height and frame.format
):
frame = frame.get_frame(0)
with frame.get_frame(0) as frame:
pass

assert frame.format

Expand Down
8 changes: 4 additions & 4 deletions vstools/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def match_clip(
clip = clip.resize.Bicubic(format=ref.format.id, matrix=Matrix.from_video(ref))

if matrices:
ref_frame = ref.get_frame(0)
clip = clip.std.SetFrameProps(
_Matrix=Matrix(ref_frame), _Transfer=Transfer(ref_frame), _Primaries=Primaries(ref_frame)
)
with ref.get_frame(0) as ref_frame:
clip = clip.std.SetFrameProps(
_Matrix=Matrix(ref_frame), _Transfer=Transfer(ref_frame), _Primaries=Primaries(ref_frame)
)

return clip.std.AssumeFPS(fpsnum=ref.fps.numerator, fpsden=ref.fps.denominator)

Expand Down
4 changes: 3 additions & 1 deletion vstools/utils/props.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ def get_prop(
if isinstance(obj, vs.RawFrame):
props = obj.props
elif isinstance(obj, vs.RawNode):
props = obj.get_frame(0).props
with obj.get_frame(0) as frame:
props = frame.props
else:
props = obj


prop: Any = MISSING

try:
Expand Down
6 changes: 4 additions & 2 deletions vstools/utils/vs_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ def clear_cache() -> None:
try:
for output in get_outputs().values():
if isinstance(output, VideoOutputTuple):
output.clip.get_frame(0)
with output.clip.get_frame(0):
pass
break
except Exception:
core.std.BlankClip().get_frame(0)
with core.std.BlankClip().get_frame(0):
pass
core.max_cache_size = cache_size
except Exception:
...
Expand Down

0 comments on commit f70384e

Please sign in to comment.