Grammar

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(Multiple Avisynth statements on a single line)
m (chaining - correction)
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
All basic AviSynth scripting statements have one of these forms:  
+
<div style="max-width:62em" >
 +
==== Statements, Expressions, Types and Operators ====
 +
All basic AviSynth scripting '''statements''' have one of these ''forms'':  
  
# ''variable_name'' = ''expression''  
+
# {{FuncArg|variable_name}} '''=''' {{FuncArg|expression}}
# ''expression''
+
#: {{FuncArg|expression}} is evaluated and the result is assigned to {{FuncArg|variable_name}}.
# '''return''' ''expression''
+
# {{FuncArg|expression}}
 +
#: {{FuncArg|expression}} is evaluated and the result, if a ''clip'', is assigned to the special variable '''Last'''.
 +
# '''return''' {{FuncArg|expression}}
 +
#: {{FuncArg|expression}} is evaluated and is used as the '''return value''' of the active ''script block'' (function), or the entire script.
 +
#:As a shorthand, if '''return''' is not present in the final executable statement of a script (or script block), it is ''implied'' &ndash; the statement is treated as if '''return''' was present.
  
(Two higher-level constructs also exist - the [[User_defined_script_functions|function declaration]] and the [[Control_structures#The_try..catch_statement|try..catch statement]].)
+
Most of the time the '''result''' of an expression will be a video clip; however an expression's result can be any type supported by AviSynth (clip, int, float, bool, string) and this is how ''utility functions'' such as [[Internal_functions|Internal script functions]] operate.
  
In the first case, ''expression'' is evaluated and the result is assigned to ''variable_name''. In the second case, ''expression'' is evaluated and the result, if a clip, is assigned to the special variable '''last'''. In the third case, ''expression'' is evaluated and is used as the "return value" of the active script block--that is either a function or the entire script. In the latter case, the return value is typically the video clip that will be seen by the application which opens the AVS file. As a shorthand, a bare expression as the final statement in a script (or script block) is treated as if the keyword '''return''' was present.
+
If the return value of the script is a ''clip'', which is the normal case, it can be "played" as a video by a [[Frameserver|frameserving]] client.
  
Most of the time the result of an expression will be a video clip; however an expression's result can be of any type supported by the scripting language (clip, int, float, bool, string) and this is how utility functions such as [[Internal_functions|internal script functions]] operate.
+
{{BoldColor|black|120|►}} Two higher-level constructs also exist - the '''function declaration''' (dicussed below) and the '''[[Control_structures#The_try..catch_statement|try..catch statement]]'''.
 +
 +
{{BoldColor|black|120|►}}  [[GScript]], a [[External_filters#Support_filters|support plugin]], extends the language to include '''if&ndash;else''', '''for''' and '''while''' loops; {{AvsPluscon}} has [[GScript#Avisynth.2B|native support of GScript]].
  
An ''expression'' can have one of these forms:
 
  
# ''numeric_constant'', ''string_constant'' or ''boolean_constant''
+
An '''Expression''' can have one of these forms:  
# ''variable_name'' or ''clip_property''
+
# ''Function''(''args'')
+
# ''expression''.''Function''(''args'')
+
# ''expression1'' '''operator''' ''expression2''
+
# ''bool_expression'' ? ''expression1'' : ''expression2''
+
  
In the first case, the value of the ''expression'' is the value of the constant.  In the second case, the values correspond to [[Clip_properties|clip properties]] or [[Script_variables|script variables]] (which must have been previously initialized). In the third case, the value is the return value of an AVS function (see below). The fourth case is an alternate syntax (called "OOP notation") which is equivalent to ''Function''(''expression'', ''args'').   
+
# {{FuncArg|constant}}
 +
#: The ''value'' of the expression is the value of the (numeric, string or boolean) {{FuncArg|constant}}&mdash;a ''literal value'' such as '42'.
 +
# {{FuncArg|variable_name}} or {{FuncArg|clip_property_name}}
 +
#: The ''value'' equals the value of the specified [[Script_variables|{{FuncArg|variable}}]] or [[Clip_properties|{{FuncArg|clip property}}]] (possibly [[Internal_functions/Defined#Defined|undefined]]).
 +
# {{FuncArg|function_name}} '''(''' {{FuncArg|argument_list}} ''')'''
 +
#: The ''value'' is the return value of a {{FuncArg|function}} [[#Functions|(see below)]].  
 +
# {{FuncArg|expression}} <b>.</b> {{FuncArg|function_name}} '''(''' {{FuncArg|argument_list}} ''')'''
 +
#: This alternate syntax is called "''OOP notation''" in AviSynth:
 +
#:{{FuncArg|''expression''}} <b>.</b> {{FuncArg|function_name}} '''(''' {{FuncArg|argument_list}} ''')''' is equivalent to
 +
#:{{FuncArg|function_name}} '''(''' {{FuncArg|''expression''}} ''',''' {{FuncArg|argument_list}} ''')'''.   
 +
# {{FuncArg|expression1}} '''operator''' {{FuncArg|expression2}}
 +
#: AviSynth operators are discussed [[Operators|'''here''']]; in addition to the normal math operators you would expect, there are several ''special'' operators; for example,
 +
#:*'''strings''' can be ''concatenated'' (joined together) with '+', and ''compared'' with relational operators like '>='.
 +
#:*'''video clips''' can be ''spliced'' (joined together) with '+':
 +
#::''a'' '''+''' ''b'' is equivalent to [[Splice|UnalignedSplice]](''a'', ''b'')
 +
#::''a'' '''++''' ''b'' is equivalent to [[Splice|AlignedSplice]](''a'', ''b'').
 +
#:The value of the expression will depend on the specific operator; for example,
 +
#:* the value of ''string'' '''+''' ''string'' is the concatenated string
 +
#:* the value of ''string'' '''==''' ''string'' is a ''boolean'' (true/false) value (it is a ''boolean expression'').
 +
# {{FuncArg|expression1}} <b>?</b> {{FuncArg|expression2}} <b>:</b> {{FuncArg|expression3}}
 +
#: Supports conditional execution with the [[Ternary_operator|''ternary operator'']];
 +
#:*If the value of {{FuncArg|expression1}} evaluates to ''true'', the value of the whole expression is the value of {{FuncArg|expression2}}; otherwise it is the value of {{FuncArg|expression3}}
 +
#:*If {{FuncArg|expression1}} is true and {{FuncArg|expression2}} is a {{FuncArg|function}}, that function will be ''executed''. This is the main method of [[Control_structures|code branching]] in AviSynth.
 +
#:For more on ternary operators, see [http://www.cplusplus.com/articles/1AUq5Di1/ ''The Conditional (or Ternary) Operator''] (cplusplus.com)
 +
{{BoldColor|black|120|►}} Incidentally, Avisynth '''ignores case''': ''avisource'' or ''aViSouRCe'' is just as good as ''AVISource''.
  
The final two cases show that one can manipulate expressions using all of the usual arithmetic and logical [[operators]] (from C) as you'd expect on ints, floats and bools, as well as execute code conditionally with the ternary operator.  Strings can be compared with relational operators and concatenated with '+'.  The following operators are also defined on video clips: '''a + b''' is equivalent to [[Splice|UnalignedSplice]](''a'', ''b''), and '''a ++ b''' is equivalent to [[Splice|AlignedSplice]](''a'', ''b'').
+
{{BoldColor|black|120|►}} As a special case, [[Operators|comparisons ]] may be ''chained'':
 +
:<code>(3 <= x <= 5)</code> can be used where normally something like
 +
:<code>(3 <= x && x <= 5)</code> would be required.<sup>[http://forum.doom9.org/showthread.php?p=1783132#post1783132 (doom9)]</sup>
  
The functions in AviSynth's scripting language are, by and large, video filters. Although a function can return any type it chooses (this is a useful feature for creating utility code to reuse in scripts; you can define your own [[User_defined_script_functions|script functions]]) functions which do '''not''' return a '''clip''' are always limited to intermediate processing of variables to pass as arguments to filters (functions that ''do'' return a clip). The script should always return a clip as its final value. After all, AviSynth is a video processing application.
 
  
Functions can take up to sixty arguments (hope that's enough), and the return value can be of any type supported by the scripting language (clip, int, float, bool, string). Functions always produce a new value and never modify an existing one. What that means is that all arguments to a function are passed "by value" and not "by reference"; in order to alter a variable's value in AviSynth script language you must assign to it a new value.
+
{{HiddenAnchor|Functions}}{{HiddenAnchor|Filters}}{{HiddenAnchor|Arguments}}
 +
==== Functions, Filters and Arguments ====
 +
All {{FuncArg|functions}} take one or more ''inputs'', do some processing, and return a value. The return value can be of any [[Script_variables|Avisynth type]]: ''clip'', ''int'', ''float'', ''bool'' or ''string''. A [[Filter|{{FuncArg|filter}}]] in AviSynth is simply a function that processes audio/video information, normally returning a ''clip''.  
  
To see the syntax of the function call for each built-in filter, view the [[Internal_filters|internal filters]]. There are also built-in [[Internal_functions|internal functions]] that perform common operations on non-clip variables.
+
A function must have a ''declaration'': the <tt>function</tt> keyword, a ''name'', an ''argument list'' (see below), an opening brace ''''{'''', one or more {{FuncArg|statements}}, and a closing brace ''''}'''':
 +
<div {{BoxWidthIndent|48|1}} >
 +
''function'' UsefulFunction(''int'' a, ''int'' b)
 +
{
 +
    ''return'' a + b
 +
}
  
''Args'' is a list of function arguments separated by commas. The list can be empty. Each argument must be an expression whose type (eg text string, integer, floating-point number, boolean value or video clip) matches the one expected by the function. If the function expects a video clip as its first argument, and that argument is not supplied, then the clip in the special variable ''last'' will be used.
+
''function'' UsefulFilter(''clip'' C)  
 +
{
 +
    C
 +
    [[Invert]]
 +
    ''return'' Last
 +
}
 +
</div>
  
AviSynth functions can take named arguments. The named arguments can be specified in any order, and the filter will choose default values for any that you leave off. This makes certain filters much easier to use. For example, you can now write '''[[Subtitle]]("Hello, World!", text_color=$00FF00, x=100, y=200)''' instead of '''[[Subtitle]]("Hello, World!", 100, 200, 0, 999999, "Arial", 24, $00FF00)'''. [[Colors]] can be specified in hexadecimal as in the example above or in decimal. In both cases it should be specified as RGB value, even if the clip itself is YUV.
+
Functions always produce a new value and never modify an existing one. What that means is that all arguments to a function are passed ''by&nbsp;value'' and not ''by&nbsp;reference''; in order to alter a variable's value in AviSynth, you must assign it a new value.
  
If no arguments are being passed to the function, you can also make the function call without parentheses, e.g. '''FilterName'''. The primary reason for this is to retain compatibility with old scripts. However, it's sometimes convenient to leave off the parentheses when there's no possibility of confusion.  
+
Not all functions are typed into your script (or another script that has been [[Import|imported]]); some, [[Internal_filters|listed here]], are built into AviSynth itself, while others, [[External_filters|listed here]], are packaged in external [[Plugins|plugins]]. There are also many useful non-clip-returning internal functions, [[Internal_functions|described here]]. More about creating your own user-defined functions can be found [[User_defined_script_functions|here]].
 +
 
 +
Functions get their input from {{FuncArg|arguments}} (and [[Global|global variables]]). An argument must have a ''[[Script_variables|type]]'' and a ''name''. An {{FuncArg|argument_list}} is a list of arguments, separated by commas. The list can contain up to sixty arguments (hope that's enough). When the function is called, argument values must be supplied (ignoring optional '''named arguments''' for the moment &ndash; see below). Each argument the caller supplies must be an expression whose ''type'' matches the declared type in the argument list.
 +
<div {{BoxWidthIndent|48|1}} >
 +
function UsefulFunction(int a, int b)
 +
{
 +
    return a + b
 +
}
 +
[[ColorBars]]
 +
 +
## All these function calls result in a script that fails to load;
 +
## the error message is:
 +
##    ''Invalid arguments to function "UsefulFunction"''
 +
#c = UsefulFunction()              ## not enough arguments
 +
#c = UsefulFunction("ted", "alice") ## wrong argument types
 +
#c = UsefulFunction(5, 3.141)      ## 2nd argument type wrong
 +
 +
## This call works correctly - both arguments match the declaration:
 +
u = UsefulFunction(5, 11)
 +
[[Subtitle]]([[Internal_functions#String|String]](u))
 +
return Last
 +
</div>
 +
 
 +
If the function expects a video clip as its first argument, and that argument is not supplied, then the clip in the special variable '''Last''' will be used.  
 +
<div {{BoxWidthIndent|48|1}} >
 +
[[ColorBars]]
 +
[[Invert]]() ## built-in filter expects a clip argument
 +
</div>
 +
 
 +
The list can be '''empty'''. If so, you can omit the '''parentheses''':
 +
<div {{BoxWidthIndent|48|1}} >
 +
AviSource("my.avi").AssumeFrameBased.AssumeTFF.SeparateFields
 +
</div>
 +
versus
 +
<div {{BoxWidthIndent|48|1}} >
 +
AviSource("my.avi").AssumeFrameBased().AssumeTFF().SeparateFields()
 +
</div>
 +
 
 +
 
 +
Function definitions may specify an additional argument type: ''var'', which accepts any of the AviSynth types. It is up to the function to determine the argument's actual type using functions such as [[Internal_functions#IsClip|IsClip]], before attempting to use it.
 +
 
 +
Any ''variable_name'' which has never been assigned a value is an '''undefined''' variable. Undefined variables may be passed to functions, which in turn can determine their status with the [[Internal_functions/Defined#Defined|Defined]] function.
 +
 
 +
 
 +
Functions can take '''named arguments'''. Named arguments can be specified in any order, and they are always ''optional''&ndash;the function should set default values for any that you leave off (the [[Internal_functions#Default|Default]] function is useful for this). This makes certain filters much easier to use. For example, instead of
 +
<div {{BoxWidthIndent|68|1}} >
 +
[[Subtitle]]("Hello, World!", 100, 200, 0, Framecount-1, "Arial", 18, $FFFF00, $0, 7, 0, 0, 0, 0, false)
 +
</div>
 +
you can say
 +
<div {{BoxWidthIndent|48|1}} >
 +
Subtitle("Hello, World!", x=100, y=200)
 +
</div>
 +
The result is the same; all missing arguments are set by default.
 +
 
 +
By the way, '''[[Colors|colors]]''' can be specified in hexadecimal as in the example above, or in decimal. They may be specified as an [[RGB]] value, even if the clip itself is [[YUV]]. They may also be specified by name using one of the [[Preset_colors|preset colors]], eg ''color_yellow''.
  
Avisynth ignores anything from a # character to the end of that line. This can be used to add '''comments''' to a script.
 
<pre># comment</pre>
 
  
In v2.58 it is possible to add '''block''' and '''nested block''' comments in the following way:
+
==== Comments ====
 +
Avisynth ignores anything from a '<tt>#</tt>' character to the end of that line. This can be used to add '''comments''' to a script.
 +
<div {{BoxWidthIndent|48|1}} >
 +
# this is a comment
 +
</div>
  
# block comment:
+
It is possible to add '''block''' and '''nested block''' comments in the following way:
 +
<div {{BoxWidthIndent|48|1}} >
 
  /*  
 
  /*  
  comment 1
+
  this is a
  comment 2
+
  block comment
 
  */
 
  */
 +
</div>
  
  # nested block comments:
+
<div {{BoxWidthIndent|48|1}} >
[* [* a meaningful example will follow later :) *] *]
+
  /* this is a
 +
    block comment
 +
  [* this is
 +
      a nested comment
 +
      [* and another one *]
 +
  *]  
 +
*/
 +
</div>
  
Avisynth ignores anything from an <nowiki>__END__</nowiki> keyword (with double underscores) to the end of the script file.
 
This can be used to disable some last commands of script.
 
<pre>Version()
 
__END__
 
ReduceBy2()
 
Result is not reduced and we can write any text here
 
</pre>
 
  
Avisynth '''ignores case''': aViSouRCe is just as good as AVISource.
+
Avisynth ignores anything from the <tt><nowiki>__END__</nowiki></tt> keyword (with double underscores) to the end of the script file.
 +
<div {{BoxWidthIndent|48|1}} >
 +
Version()
 +
__END__
 +
ReduceBy2()
 +
Result is not reduced and we can write any text here
 +
</div>
  
Multiple Avisynth statements on a single line can be achieved in the context of OOP notation or embedding filters as parameters of another function such as:
 
  
 +
==== Line breaks and continuation ====
 +
'''Multiple statements on a single line''' can be made with OOP-style (dot) notation, or by embedding filters as arguments to another function:
 +
<div {{BoxWidthIndent|48|1}} >
 
  [[AviSource]]("c:\video.avi").[[Trim]](0, 499)
 
  [[AviSource]]("c:\video.avi").[[Trim]](0, 499)
 
  -or-
 
  -or-
 
  [[AudioDub]](AviSource("c:\video.avi"), [[AviSource|WavSource]]("c:\audio.wav"))
 
  [[AudioDub]](AviSource("c:\video.avi"), [[AviSource|WavSource]]("c:\audio.wav"))
 +
</div>
  
Multiple unrelated statements can also be made:
+
The [[Wikipedia:Parsing|parser]] recognises it has reached the end of a ''statement'' when the next symbol is not a valid continuation of what it parsed so far; the next symbol it encounters begins a new statement. Although it is conventional to use '''newlines''' to separate statements (and good practice for readability), the grammar is such that it is only strictly necessary if the following statement starts with a unary minus (or plus) operator. See [http://forum.doom9.org/showthread.php?p=1621382#post1621382 Multiple statements on a single line] @ doom9.org
  
 +
For example:
 +
<div {{BoxWidthIndent|48|1}} >
 
  x = 1  y = 2  z = 3  
 
  x = 1  y = 2  z = 3  
 +
</div>
  
The Avisynth parser recognises it has reached the end of a statement when the next symbol is not a valid continuation of what it already has. Although it is conventional to use newlines to separate statements (and good practice for readability), the syntax is such that it is only strictly necessary if the following statement starts with a unary minus (or plus) operator. See [http://forum.doom9.org/showthread.php?p=1621382#post1621382 Multiple statements on a single line] @ doom9.org
 
  
Avisynth statements can be split across multiple lines by placing a backslash ("\") either as the last non-space character of the line being extended, or as the first non-space character on the next line.
+
Statements can be '''split across multiple lines''' by placing a backslash ('<tt>\</tt>') either as the last non-space character of the line being extended, or as the first non-space character on the next line.
  
 
Line splitting examples (both valid and equal):
 
Line splitting examples (both valid and equal):
 
+
<div {{BoxWidthIndent|48|1}} >
 
  Subtitle("Hello, World!", 100, 200, 0, \
 
  Subtitle("Hello, World!", 100, 200, 0, \
 
   999999, "Arial", 24, $00FF00)
 
   999999, "Arial", 24, $00FF00)
 
+
</div>
 
-or-
 
-or-
 
+
<div {{BoxWidthIndent|48|1}} >
 
  Subtitle("Hello, World!", 100, 200, 0,
 
  Subtitle("Hello, World!", 100, 200, 0,
 
   \ 999999, "Arial", 24, $00FF00)
 
   \ 999999, "Arial", 24, $00FF00)
 +
</div>
  
When splitting across multiple lines you may ''place comments only at the end of the last line''. Mixing comments with backslashes at an intermediate line of the line-split will either produce an error message or result at hard to trace bugs.
 
  
Example of a not-signaled bug by improper mixing of comments and line separation:
+
When splitting across multiple lines you may place '#'-style '''comments''' ''only at the end of the last line''. Mixing comments with backslashes at an intermediate line of the line-split will either produce an error message or result in hard-to-trace bugs.
  
 +
Example of a silent bug (no error is raised) by improper mixing of comments and line separation:
 +
<div {{BoxWidthIndent|48|1}} >
 
  [[ColorBars]]
 
  [[ColorBars]]
 
  [[ShowFrameNumber]]
 
  [[ShowFrameNumber]]
  Trim(0,9) # select some frames  \
+
  Trim(0,9) '''#''' select some frames  '''\'''
 
   + Trim(20,29)
 
   + Trim(20,29)
 +
</div>
 +
 +
The above example does not return frames [0..9, 20..29] as intended because the '<tt>\</tt>' is masked by the '<tt>#</tt>' character before it; thus the line continuation never happens.
 +
 +
However you may use block comments in this situation:
 +
<div {{BoxWidthIndent|48|1}} >
 +
[[ColorBars]]
 +
[[ShowFrameNumber]]
 +
Trim(0,9) '''[*''' select some frames '''*] \'''
 +
  + Trim(20,29)
 +
</div>
 +
 +
====For More Information====
 +
For a longer explanation, see '''[[The_full_AviSynth_grammar|The Full AviSynth Grammar]]'''.
 +
  
The above example does not return frames [0..9,20..29] as intended because the "\" is masked by the "#" character before it; thus the line continuation never happens.
+
</div>
  
 
----
 
----

Latest revision as of 16:28, 16 November 2017

Contents

[edit] Statements, Expressions, Types and Operators

All basic AviSynth scripting statements have one of these forms:

  1. variable_name = expression
    expression is evaluated and the result is assigned to variable_name.
  2. expression
    expression is evaluated and the result, if a clip, is assigned to the special variable Last.
  3. return expression
    expression is evaluated and is used as the return value of the active script block (function), or the entire script.
    As a shorthand, if return is not present in the final executable statement of a script (or script block), it is implied – the statement is treated as if return was present.

Most of the time the result of an expression will be a video clip; however an expression's result can be any type supported by AviSynth (clip, int, float, bool, string) and this is how utility functions such as Internal script functions operate.

If the return value of the script is a clip, which is the normal case, it can be "played" as a video by a frameserving client.

Two higher-level constructs also exist - the function declaration (dicussed below) and the try..catch statement.

GScript, a support plugin, extends the language to include if–else, for and while loops; AVS+ has native support of GScript.


An Expression can have one of these forms:

  1. constant
    The value of the expression is the value of the (numeric, string or boolean) constant—a literal value such as '42'.
  2. variable_name or clip_property_name
    The value equals the value of the specified variable or clip property (possibly undefined).
  3. function_name ( argument_list )
    The value is the return value of a function (see below).
  4. expression . function_name ( argument_list )
    This alternate syntax is called "OOP notation" in AviSynth:
    expression . function_name ( argument_list ) is equivalent to
    function_name ( expression , argument_list ).
  5. expression1 operator expression2
    AviSynth operators are discussed here; in addition to the normal math operators you would expect, there are several special operators; for example,
    • strings can be concatenated (joined together) with '+', and compared with relational operators like '>='.
    • video clips can be spliced (joined together) with '+':
    a + b is equivalent to UnalignedSplice(a, b)
    a ++ b is equivalent to AlignedSplice(a, b).
    The value of the expression will depend on the specific operator; for example,
    • the value of string + string is the concatenated string
    • the value of string == string is a boolean (true/false) value (it is a boolean expression).
  6. expression1 ? expression2 : expression3
    Supports conditional execution with the ternary operator;
    • If the value of expression1 evaluates to true, the value of the whole expression is the value of expression2; otherwise it is the value of expression3
    • If expression1 is true and expression2 is a function, that function will be executed. This is the main method of code branching in AviSynth.
    For more on ternary operators, see The Conditional (or Ternary) Operator (cplusplus.com)

Incidentally, Avisynth ignores case: avisource or aViSouRCe is just as good as AVISource.

As a special case, comparisons may be chained:

(3 <= x <= 5) can be used where normally something like
(3 <= x && x <= 5) would be required.(doom9)


[edit] Functions, Filters and Arguments

All functions take one or more inputs, do some processing, and return a value. The return value can be of any Avisynth type: clip, int, float, bool or string. A filter in AviSynth is simply a function that processes audio/video information, normally returning a clip.

A function must have a declaration: the function keyword, a name, an argument list (see below), an opening brace '{', one or more statements, and a closing brace '}':

function UsefulFunction(int a, int b) 
{
    return a + b
} 
function UsefulFilter(clip C) 
{ 
    C
    Invert
    return Last
}

Functions always produce a new value and never modify an existing one. What that means is that all arguments to a function are passed by value and not by reference; in order to alter a variable's value in AviSynth, you must assign it a new value.

Not all functions are typed into your script (or another script that has been imported); some, listed here, are built into AviSynth itself, while others, listed here, are packaged in external plugins. There are also many useful non-clip-returning internal functions, described here. More about creating your own user-defined functions can be found here.

Functions get their input from arguments (and global variables). An argument must have a type and a name. An argument_list is a list of arguments, separated by commas. The list can contain up to sixty arguments (hope that's enough). When the function is called, argument values must be supplied (ignoring optional named arguments for the moment – see below). Each argument the caller supplies must be an expression whose type matches the declared type in the argument list.

function UsefulFunction(int a, int b) 
{
    return a + b
} 
ColorBars

## All these function calls result in a script that fails to load;
## the error message is: 
##    Invalid arguments to function "UsefulFunction"
#c = UsefulFunction()               ## not enough arguments
#c = UsefulFunction("ted", "alice") ## wrong argument types
#c = UsefulFunction(5, 3.141)       ## 2nd argument type wrong

## This call works correctly - both arguments match the declaration:
u = UsefulFunction(5, 11)
Subtitle(String(u))
return Last

If the function expects a video clip as its first argument, and that argument is not supplied, then the clip in the special variable Last will be used.

ColorBars
Invert() ## built-in filter expects a clip argument

The list can be empty. If so, you can omit the parentheses:

AviSource("my.avi").AssumeFrameBased.AssumeTFF.SeparateFields

versus

AviSource("my.avi").AssumeFrameBased().AssumeTFF().SeparateFields()


Function definitions may specify an additional argument type: var, which accepts any of the AviSynth types. It is up to the function to determine the argument's actual type using functions such as IsClip, before attempting to use it.

Any variable_name which has never been assigned a value is an undefined variable. Undefined variables may be passed to functions, which in turn can determine their status with the Defined function.


Functions can take named arguments. Named arguments can be specified in any order, and they are always optional–the function should set default values for any that you leave off (the Default function is useful for this). This makes certain filters much easier to use. For example, instead of

Subtitle("Hello, World!", 100, 200, 0, Framecount-1, "Arial", 18, $FFFF00, $0, 7, 0, 0, 0, 0, false)

you can say

Subtitle("Hello, World!", x=100, y=200)

The result is the same; all missing arguments are set by default.

By the way, colors can be specified in hexadecimal as in the example above, or in decimal. They may be specified as an RGB value, even if the clip itself is YUV. They may also be specified by name using one of the preset colors, eg color_yellow.


[edit] Comments

Avisynth ignores anything from a '#' character to the end of that line. This can be used to add comments to a script.

# this is a comment

It is possible to add block and nested block comments in the following way:

/* 
this is a
block comment
*/
/* this is a
    block comment
  [* this is
     a nested comment
     [* and another one *]
  *] 
*/


Avisynth ignores anything from the __END__ keyword (with double underscores) to the end of the script file.

Version()
__END__
ReduceBy2()
Result is not reduced and we can write any text here


[edit] Line breaks and continuation

Multiple statements on a single line can be made with OOP-style (dot) notation, or by embedding filters as arguments to another function:

AviSource("c:\video.avi").Trim(0, 499)
-or-
AudioDub(AviSource("c:\video.avi"), WavSource("c:\audio.wav"))

The parser recognises it has reached the end of a statement when the next symbol is not a valid continuation of what it parsed so far; the next symbol it encounters begins a new statement. Although it is conventional to use newlines to separate statements (and good practice for readability), the grammar is such that it is only strictly necessary if the following statement starts with a unary minus (or plus) operator. See Multiple statements on a single line @ doom9.org

For example:

x = 1  y = 2  z = 3 


Statements can be split across multiple lines by placing a backslash ('\') either as the last non-space character of the line being extended, or as the first non-space character on the next line.

Line splitting examples (both valid and equal):

Subtitle("Hello, World!", 100, 200, 0, \
  999999, "Arial", 24, $00FF00)

-or-

Subtitle("Hello, World!", 100, 200, 0,
  \ 999999, "Arial", 24, $00FF00)


When splitting across multiple lines you may place '#'-style comments only at the end of the last line. Mixing comments with backslashes at an intermediate line of the line-split will either produce an error message or result in hard-to-trace bugs.

Example of a silent bug (no error is raised) by improper mixing of comments and line separation:

ColorBars
ShowFrameNumber
Trim(0,9) # select some frames  \
  + Trim(20,29)

The above example does not return frames [0..9, 20..29] as intended because the '\' is masked by the '#' character before it; thus the line continuation never happens.

However you may use block comments in this situation:

ColorBars
ShowFrameNumber
Trim(0,9) [* select some frames *] \
  + Trim(20,29)

[edit] For More Information

For a longer explanation, see The Full AviSynth Grammar.



Back to AviSynth_Syntax.

Personal tools