Log in

View Full Version : Displaying clip/frame properties


Gavino
1st June 2008, 20:32
Here's a little diagnostic function that will show any specified clip or frame property (a bit like Info or ShowFrameNumber, but you choose the property you want to see).

The 'property' can be the name of any internal (http://avisynth.org/mediawiki/Clip_properties) (or even user-defined) function which takes a clip as a parameter and returns a value (numerical, boolean, or even string, though I can't think of any internal functions that do that). Most usefully, it can be any of the runtime functions (http://avisynth.org/mediawiki/Internal_functions/Runtime_functions), giving a painless way to display their value without having to use the potentially tricky ScriptClip.

# ShowProperty: show a specified clip or frame property
# (Gavino, 1st June 2008)
# Parameters:
# name : name of property required, eg "AverageLuma()", "Framecount", etc (brackets optional)
# x, y : position of output text (optional, defaults as in Subtitle())
# label: text to appear in front of value (optional, default is name followed by ": ")
# Also needs: function Quote(string s) - included below
# Examples:
# ShowProperty("AverageLuma")
# ShowProperty("GetParity()", label="TFF=")
# ShowProperty("UDifferenceFromPrevious", 20, 20, "dU=")
function ShowProperty(clip c, string name, int "x", int "y", string "label") {
xStr = Defined(x) ? ",x="+String(x) : ""
yStr = Defined(y) ? ",y="+String(y) : ""
label = Default(label, name+": ")
command="Subtitle(" + Quote(label) + "+String(" + name + ")" + xStr + yStr + ")"
ScriptClip(c, command)
}

# Quote: convert a string to a literal by adding (triple) quotes
function Quote(string s) {
q = chr(34)
q3 = q+q+q
return q3+s+q3
}

A simple example is
ShowProperty("AverageLuma") # displays (eg) "AverageLuma: 46.390278"
You can also optionally specify the position of the output text (as in Subtitle), and/or a 'label' to identify the value (default label is just the property name followed by ": "). Thus:

ShowProperty("GetParity()", label="TFF=") # -> "TFF=true" (or false!)
ShowProperty("UDifferenceFromPrevious", 20, 30) # display text at (20, 30)
# you can even do this:
ShowProperty("current_frame") # a (poor) substitute for ShowFrameNumber()

(If you don't care how this works, or about the nitty-gritty of AviSynth programming, skip the rest).

The function code, though simple, also illustrates some key points on how to use the Runtime Environment (http://avisynth.org/mediawiki/Runtime_environment) in a user-defined function.

Function parameters (and variables) do not exist (http://avisynth.org/mediawiki/The_script_execution_model/Scope_and_lifetime_of_variables) when the run-time part of the script executes (they have been 'popped off the stack' by then), so you cannot directly write a run-time script containing their names. Instead, the script code must be constructed dynamically (via string addition), incorporating the values of the relevant parameters.
To get the value of a numeric parameter into the run-time script, you need to convert it using the String function.
For a string parameter, the most usual case is that it represents the name of something (like a filter to be executed), and its value is copied 'as is'. However, in other situations (it's just a piece of text supplied by the function user), you need to add quotes to its value to turn it into a literal. (Notice the difference between the way the parameters name and label are processed in ShowProperty.)

The first two points are pretty widely-known, but the last one is something I haven't explicitly seen elsewhere.