OnDemandFilter v0.1

Download: https://horman.net/avisynth/

OnDemandFilter is a deferred loading/unloading filter wrapper that can be used to reduce initial loading times and memory usage.

It works by calling the specified filter only when a video frame or audio is directly requested, and by only keeping a specified number of OnDemandFilter clips open at any one time.

Parameters

OnDemandFilter ( string, array "parameters", clip "vi_clip" )
ParameterDescriptionDefault value
stringThe name of the filter to be called when required
array "parameters"Parameters for the filter (e.g. clip, filename, etc) - see below for expected structure[ ] (empty array)
clip "vi_clip"A placeholder clip to provide video parameters (pixel type, resolution, framerate, audio sample type, etc)none

parameters (array)

This array contains the parameters to be passed to the specified filter. If any member of the array is an array itself, and contains exactly two elements, the first of which is a string which does not begin with a "!" character, then the array is treated as a parameter name/value pair.
OnDemandFilter("Blur", [ input_clip, 1.5 ]) # Calls the internal blur filter on input_clip with a parameter of 1.5 OnDemandFilter("ConvertToY8", [ input_clip, "rec709" ]) # Calls ConvertToY8 on input_clip with a parameter of "rec709" OnDemandFilter("ConvertToY8", [ input_clip, [ "matrix", "rec709" ] ]) # Identical to the previous example, except the matrix parameter is now specified with its name
In the unlikely event that a filter expects a parameter which is a two-element array where the first element is a string, the string should be prefixed with a "!" (which will be removed) to indicate that the array should not be treated as a name/value pair.

vi_clip (clip)

This optional parameter specifies a clip to use as a source for placeholder clip specifications. This reduces initial script loading speed. It's the user's responsibility to ensure that vi_clip matches the deferred filter result:
Video parameterDetails
Pixel typeMust match
ResolutionMust match
Audio sample typeMust match, or have no audio (result will have no audio)
Number of audio channelsMust match, or have no audio (result will have no audio)
Frame rateWill override the filter result's frame rate in all circumstances
Audio sample rateWill override the filter result's sample rate in all circumstances
Number of framesWill override the filter result's number of frames in all circumstances
vi_clip = blankclip(length = 10000000, pixel_type = "yv12", width = 1920, height = 1080, fps = 25, audio_rate = 48000, channels = 2) # the user must ensure these specifications match the output of the deferred filter below OnDemandFilter("FFmpegSource2", [\ "source_file.mp4",\ [ "atrack", -1 ]\ ], vi_clip)
If no vi_clip is provided, the deferred filter will be called immediately to obtain the clip specifications, which will result in no reduction of inital script loading speed. However since the resulting clip can be discarded later if the limit of open clips is reached, OnDemandFilter can still reduce memory usage.

OnDemandFilterLimit

This function specifies how many deferred clips may be open at one time. For example, if a script contains a dissolve between two clips, this value should be at least 2 to ensure that both clips remain loaded throughout the dissolve. It can be specified at any point in a script. The default value is 4.
OnDemandFilterLimit(2) # allow at most 2 OnDemandFilter clips to be loaded at one time

© wonkey_monkey 2025