Script variables

From Avisynth

Jump to: navigation, search

A variable name can be a practically of any length character string (more than 4000 characters in Avisynth 2.56 and later) that contains (English) letters, digits, and underscores (_), but no other characters. The name cannot start with a digit.

You may use characters from your language system codepage (locale) in strings and file names (ANSI 8 bit only, not Unicode).

A variable's placement in an expression is determined by the AviSynth Syntax.

Variables can have one of the following types:

  • clip
A video clip containing video and / or audio. At least one variable of type clip must be used and returned by the script.
  • string
A sequence of characters representing text. Strings are surrounded either by "quotation marks" or by """three quotes""". A text string can contain any character except the terminating quotation mark or double-apostrophe.
The manual used to mention TeX-style quotes, but it has been confirmed that AviSynth doesn't work this way since v1.03. If you need to put a quotation mark inside a string, you need to use Python-style """three quotes""". For example:
Subtitle("""AVISynth is as they say "l33t".""") 
Alternatively, you can use Windows extended-ASCII curly-quotes instead of straight quotes to get around this limitation.
  • int
Integers are entered as a sequence of digits, optionally with a + or - at the beginning. Integers can be given in hexadecimal by preceding them with a "$" character. For example $FF as well as $ff (case does not matter) are equal to 255.
  • float
Floating-point numbers are entered as a sequence of digits with a decimal point (.) somewhere in it and an optional + or -. For example, +1. is treated as a floating-point number. Note that exponent-style notation is not supported.
  • bool
Boolean values must be either true or false. In addition they can be yes or no, but you should avoid to use them in your scripts (they remain for compatibility purposes only).
  • val
A generic type. It is applicable only inside a used defined script function's argument list, in order to be able to declare an argument variable to be of any type (int, float, bool, string, or clip). You must then explicitly test for its type (using the boolean functions) and take appropriate actions.

Variables can be either local (binded to the local scope of the executing script block) or global. Global variables are binded to the global script environment's scope and can be accessed by all Internal functions, User defined script functions, runtime environment scripts and the main script also.

To define and / or assign a value to a global variable you must precede its name with the keyword global at the left side of the assignment. The keyword is not needed (actually it is not allowed) in order to read the value of a global variable. Examples:

global canvas = BlankClip(length=200, pixel_type="yv12")
global stroke_intensity = 0.7
...
global canvas = Overlay(canvas, pen, opacity=stroke_intensity, mask=brush)

To declare a variable, simply type the variable name, followed by '=' (an equals sign), followed by its initial value. The type must not be declared; it is inferred by the value assigned to it. The only place where it is allowed (in fact, required) to declare a variable's type is in user defined script function's argument lists. Examples:

b = false      # this declares a variable named 'b' of type 'bool' and initializes it to 'false'
x = $100       # type int (initial value is in hexadecimal)
y = 256        # type int (initial value is in decimal)
global f = 0.0 # type float declared globally
...
function my_recolor_filter(clip c, int new_color, float amount, val "userdata") { ... }

Back to AviSynth Syntax.

Personal tools