Non-clip functions
From Avisynth wiki
(Difference between revisions)
Raffriff42 (Talk | contribs) m (Chr - fix my typo) |
Raffriff42 (Talk | contribs) (all functions in subheads) |
||
Line 3: | Line 3: | ||
== Numeric Functions == | == Numeric Functions == | ||
− | MulDiv(int, int, int) | + | =====MulDiv(int, int, int)===== |
+ | ::v2.56 | ||
− | (m * n + d / 2) / d with 64 bit intermediate result | + | :(m * n + d / 2) / d with 64 bit intermediate result |
− | + | :''Examples'': | |
MulDiv (1, 1, 2) = 1 | MulDiv (1, 1, 2) = 1 | ||
MulDiv (2, 3, 2) = 3 | MulDiv (2, 3, 2) = 3 | ||
− | Floor(float) | + | =====Floor(float)===== |
− | Converts from float to int (round down on any fractional amount). | + | :Converts from float to int (round down on any fractional amount). |
− | + | :''Examples'': | |
Floor(1.2) = 1 | Floor(1.2) = 1 | ||
Floor(1.6) = 1 | Floor(1.6) = 1 | ||
Line 21: | Line 22: | ||
Floor(-1.6) = -2 | Floor(-1.6) = -2 | ||
− | Ceil(float) | + | =====Ceil(float)===== |
− | Converts from float to int (round up on any fractional amount). | + | :Converts from float to int (round up on any fractional amount). |
− | + | :''Examples'': | |
Ceil(1.2) = 2.0 | Ceil(1.2) = 2.0 | ||
Ceil(1.6) = 2.0 | Ceil(1.6) = 2.0 | ||
Line 31: | Line 32: | ||
Ceil(-1.6) = -1 | Ceil(-1.6) = -1 | ||
− | Round(float) | + | =====Round(float)===== |
− | Converts from float to int (round off to nearest integer). | + | :Converts from float to int (round off to nearest integer). |
− | + | :''Examples'': | |
Round(1.2) = 1 | Round(1.2) = 1 | ||
Round(1.6) = 2 | Round(1.6) = 2 | ||
Line 41: | Line 42: | ||
Round(-1.6) = -2 | Round(-1.6) = -2 | ||
− | Sin(float) | + | =====Sin(float)===== |
+ | ::v2 | ||
− | Returns the sine of the argument (assumes it is radians). | + | :Returns the sine of the argument (assumes it is radians). |
− | + | :''Examples'': | |
Sin(Pi()/4) = 0.707 | Sin(Pi()/4) = 0.707 | ||
Sin(Pi()/2) = 1.0 | Sin(Pi()/2) = 1.0 | ||
− | Cos(float) | + | =====Cos(float)===== |
+ | ::v2 | ||
− | Returns the cosine of the argument (assumes it is radians). | + | :Returns the cosine of the argument (assumes it is radians). |
− | + | :''Examples'': | |
Cos(Pi()/4) = 0.707 | Cos(Pi()/4) = 0.707 | ||
Cos(Pi()/2) = 0.0 | Cos(Pi()/2) = 0.0 | ||
− | Pi() | + | =====Pi()===== |
+ | ::v2 | ||
− | Returns the value of the "pi" constant (the ratio of a circle's perimeter to its diameter). | + | :Returns the value of the "pi" constant (the ratio of a circle's perimeter to its diameter). |
− | + | :''Examples'': | |
d = Pi() # d == 3.141593 | d = Pi() # d == 3.141593 | ||
− | Log(float) | + | =====Log(float)===== |
+ | ::v2 | ||
− | Returns the natural (base-e) logarithm of the argument. | + | :Returns the natural (base-e) logarithm of the argument. |
− | + | :''Examples'': | |
Log(1) = 0.0 | Log(1) = 0.0 | ||
Log(10) = 2.30259 | Log(10) = 2.30259 | ||
− | Exp(float) | + | =====Exp(float)===== |
+ | ::v2 | ||
− | Returns the natural (base-e) exponent of the argument. | + | :Returns the natural (base-e) exponent of the argument. |
− | + | :''Examples'': | |
Exp(1) = 2.718282 | Exp(1) = 2.718282 | ||
Exp(0) = 1.0 | Exp(0) = 1.0 | ||
− | Pow(float base, float power) | + | =====Pow(float base, float power)===== |
+ | ::v2 | ||
− | Returns "base" raised to the power indicated by the second argument. | + | :Returns "base" raised to the power indicated by the second argument. |
− | + | :''Examples'': | |
Pow(2, 3) = 8 | Pow(2, 3) = 8 | ||
Pow(3, 2) = 9 | Pow(3, 2) = 9 | ||
Pow(3.45, 1.75) = 8.7334 | Pow(3.45, 1.75) = 8.7334 | ||
− | Sqrt(float) | + | =====Sqrt(float)===== |
+ | ::v2 | ||
− | Returns the square root of the argument. | + | :Returns the square root of the argument. |
− | + | :''Examples'': | |
Sqrt(1) = 1.0 | Sqrt(1) = 1.0 | ||
Sqrt(2) = 1.4142 | Sqrt(2) = 1.4142 | ||
− | Abs(float or integer) | + | =====Abs(float or integer)===== |
+ | ::v2.07 | ||
− | Returns the absolute value (returns float for float, integer for integer). | + | :Returns the absolute value (returns float for float, integer for integer). |
− | + | :''Example'': | |
Abs(-3.8) = 3.8 | Abs(-3.8) = 3.8 | ||
− | Sign(float) | + | =====Sign(float)===== |
+ | ::v2.07 | ||
− | Returns sign of value (1, 0 or -1). | + | :Returns sign of value (1, 0 or -1). |
− | + | :''Examples'': | |
Sign(-3.5) = -1 | Sign(-3.5) = -1 | ||
Sign(3.5) = 1 | Sign(3.5) = 1 | ||
Sign(0) = 0 | Sign(0) = 0 | ||
− | Int(float) | + | =====Int(float)===== |
+ | ::v2.07 | ||
− | Converts from float to int (round towards zero). | + | :Converts from float to int (round towards zero). |
− | + | :''Examples'': | |
Int(1.2) = 1 | Int(1.2) = 1 | ||
Int(1.6) = 1 | Int(1.6) = 1 | ||
Line 123: | Line 134: | ||
Int(-1.6) = -1 | Int(-1.6) = -1 | ||
− | Frac(float) | + | =====Frac(float)===== |
+ | ::v2.07 | ||
− | Returns the fractional portion of the value provided. | + | :Returns the fractional portion of the value provided. |
− | + | :''Examples'': | |
Frac(3.7) = 0.7 | Frac(3.7) = 0.7 | ||
Frac(-1.8) = -0.8 | Frac(-1.8) = -0.8 | ||
− | Float(int) | + | =====Float(int)===== |
+ | ::v2.07 | ||
− | Converts int to float. | + | :Converts int to float. |
− | Value(string) | + | =====Value(string)===== |
+ | ::v2.07 | ||
− | Converts string to numeric value. | + | :Converts string to numeric value. <!-- TODO: determine behavior on invalid input --> |
− | + | :''Example'': | |
Value ("-2.7") = -2.7 | Value ("-2.7") = -2.7 | ||
− | HexValue(string) | + | =====HexValue(string)===== |
+ | ::v2.07 | ||
− | Evaluates string as hexadecimal value. | + | :Evaluates string as hexadecimal value. |
− | + | :''Example'': | |
HexValue ("FF00") = 65280 | HexValue ("FF00") = 65280 | ||
− | Rand([int max] [, bool scale] [, bool seed]) | + | =====Rand([int max] [, bool scale] [, bool seed])===== |
+ | ::v2.07 | ||
− | Returns a random integer value and all parameters are optional. | + | :Returns a random integer value and all parameters are optional. |
− | + | :;max | |
+ | ::sets the maximum value+1 (default 32768) and can be set negative for negative results. It operates either in scaled or modulus mode (default ''scale''=true only if abs(max) > 32768, false otherwise). | ||
− | + | :;''scale'' | |
+ | ::If ''scale''=true, scales the internal random number generator value to the maximum value, while modulus mode (''scale''=false) uses the remainder from an integer divide of the random generator value by the maximum. Modulus mode is preferred for smaller maximums. | ||
− | + | :;''seed'' | |
+ | ::If ''seed''=true, seeds the random number generator with the current time. ''seed'' defaults to false and probably isn't necessary, although it's there just in case. | ||
− | Typically, this function would be used with the Select function for random clips. | + | :Typically, this function would be used with the Select function for random clips. |
− | + | :''Example'': | |
Select(Rand(5), clip1, clip2, clip3, clip4, clip5) | Select(Rand(5), clip1, clip2, clip3, clip4, clip5) | ||
− | Spline(float X, x1, y1, x2, y2, .... [, bool cubic]) | + | =====Spline(float X, x1, y1, x2, y2, .... [, bool cubic])===== |
+ | ::v2.51 | ||
− | Interpolates the Y value at point X using the control points x1/y1, ... There have to be at least 2 x/y-pairs. The interpolation can be cubic (the result is a spline) or linear (the result is a polygon). | + | :Interpolates the Y value at point X using the control points x1/y1, ... There have to be at least 2 x/y-pairs. The interpolation can be cubic (the result is a spline) or linear (the result is a polygon). |
− | + | :''Examples'': | |
Spline(5, 0, 0, 10, 10, 20, 0, false) = 5 | Spline(5, 0, 0, 10, 10, 20, 0, false) = 5 | ||
Spline(5, 0, 0, 10, 10, 20, 0, true) = 7 | Spline(5, 0, 0, 10, 10, 20, 0, true) = 7 | ||
Line 174: | Line 194: | ||
== String Functions == | == String Functions == | ||
− | LCase(string) | + | =====LCase(string)===== |
+ | ::v2.07 | ||
− | Returns lower case of string. | + | :Returns lower case of string. |
− | + | :''Example:'' | |
LCase("AviSynth") = "avisynth" | LCase("AviSynth") = "avisynth" | ||
− | UCase(string) | + | =====UCase(string)===== |
+ | ::v2.07 | ||
− | Returns upper case of string. | + | :Returns upper case of string. |
− | + | :''Example:'' | |
UCase("AviSynth") = "AVISYNTH" | UCase("AviSynth") = "AVISYNTH" | ||
− | StrLen(string) | + | =====StrLen(string)===== |
+ | ::v2.07 | ||
− | Returns length of string. | + | :Returns length of string. |
− | + | :''Example:'' | |
StrLen("AviSynth") = 8 | StrLen("AviSynth") = 8 | ||
− | RevStr(string) | + | =====RevStr(string)===== |
+ | ::v2.07 | ||
− | Returns string backwards. | + | :Returns string backwards. |
− | + | :''Example:'' | |
RevStr("AviSynth") = "htnySivA" | RevStr("AviSynth") = "htnySivA" | ||
− | LeftStr(string, int) | + | =====LeftStr(string, int)===== |
+ | ::v2.07 | ||
− | Returns first int number of characters. | + | :Returns first int number of characters. |
− | + | :''Example:'' | |
LeftStr("AviSynth", 3) = "Avi" | LeftStr("AviSynth", 3) = "Avi" | ||
− | RightStr(string, int) | + | =====RightStr(string, int)===== |
+ | ::v2.07 | ||
− | Returns last int number of characters. | + | :Returns last int number of characters. |
− | + | :''Example:'' | |
RightStr("AviSynth", 5) = "Synth" | RightStr("AviSynth", 5) = "Synth" | ||
− | MidStr(string, int pos [, int length]) | + | =====MidStr(string, int pos [, int length])===== |
+ | ::v2.07 | ||
− | Returns substring starting at ''pos'' for optional ''length'' or to end. ''pos''=1 specifies start. | + | :Returns substring starting at ''pos'' for optional ''length'' or to end. ''pos''=1 specifies start. |
− | + | :''Example:'' | |
MidStr("AviSynth", 3, 2) = "iS" | MidStr("AviSynth", 3, 2) = "iS" | ||
− | FindStr(string, substring) | + | =====FindStr(string, substring)===== |
+ | ::v2.07 | ||
− | Returns position of substring within string. Returns 0 if not found. | + | :Returns position of substring within string. Returns 0 if not found. |
− | + | :''Example:'' | |
Findstr("AviSynth", "syn") = 4 | Findstr("AviSynth", "syn") = 4 | ||
− | String(float / int) | + | =====String(float / int)===== |
+ | ::v2 | ||
− | Converts a number to a string. | + | :Converts a number to a string. |
− | + | :''Example:'' | |
[[Subtitle]]( "Clip height is " + String(last.height) ) | [[Subtitle]]( "Clip height is " + String(last.height) ) | ||
− | Chr(int) | + | =====Chr(int)===== |
+ | ::v2.51 | ||
− | Returns the Windows character in the system code page (for computers in the West, usually [https://en.wikipedia.org/wiki/Windows-1252 Windows-1252]). | + | :Returns the Windows character in the system code page (for computers in the West, usually [https://en.wikipedia.org/wiki/Windows-1252 Windows-1252]). |
− | Valid input range 0-255; values outside that range are wrapped around (modulo 256). | + | :Valid input range 0-255; values outside that range are wrapped around (modulo 256). |
− | + | :''Example:'' | |
Chr(34) # returns the quote character | Chr(34) # returns the quote character | ||
Chr(802) # also returns the quote character (802 % 256 = 34) | Chr(802) # also returns the quote character (802 % 256 = 34) | ||
Chr(-990) # also returns the quote character (-990 % 256 = 34, in unsigned 32 bit math) | Chr(-990) # also returns the quote character (-990 % 256 = 34, in unsigned 32 bit math) | ||
− | Time(string) | + | =====Time(string)===== |
+ | ::v2.51 | ||
− | Returns a string with the current system time formatted as defined by the string.Codes for output formatting: | + | :Returns a string with the current system time formatted as defined by the string. |
+ | |||
+ | :Codes for output formatting: | ||
%a Abbreviated weekday name | %a Abbreviated weekday name | ||
%A Full weekday name | %A Full weekday name | ||
Line 271: | Line 304: | ||
%X Time representation for current locale | %X Time representation for current locale | ||
%y Year without century, as decimal number (00 ? 99) | %y Year without century, as decimal number (00 ? 99) | ||
− | % | + | %Y Year with century, as decimal number |
%z, %Z Time-zone name or abbreviation; no characters if time zone is unknown | %z, %Z Time-zone name or abbreviation; no characters if time zone is unknown | ||
%% Percent sign | %% Percent sign | ||
− | The # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows: | + | :The # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows: |
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% | %#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% | ||
Line 295: | Line 328: | ||
== Version Checking Functions == | == Version Checking Functions == | ||
− | VersionNumber() | + | =====VersionNumber()===== |
+ | ::v2.07 | ||
− | Returns AviSynth version number as float. | + | :Returns AviSynth version number as float. |
− | + | :''Example:'' | |
VersionNumber() = 2.07 | VersionNumber() = 2.07 | ||
− | VersionString() | + | =====VersionString()===== |
+ | ::v2.07 | ||
− | Returns AviSynth version info as string (first line used in Version command). | + | :Returns AviSynth version info as string (first line used in Version command). |
− | + | :''Example:'' | |
VersionString() = "AviSynth 2.08 (avisynth.org) 22 nov. 2002" | VersionString() = "AviSynth 2.08 (avisynth.org) 22 nov. 2002" | ||
== Variable Type and File Checking Functions == | == Variable Type and File Checking Functions == | ||
− | IsBool(var) | + | =====IsBool(var), IsInt(var), IsFloat(var), IsString(var), IsClip(var)===== |
− | + | :Checks if ''var'' is the requested type. | |
− | + | =====Exist(filename)===== | |
+ | ::v2.07 | ||
− | + | :Checks if specified file exists. | |
− | + | =====Defined(var)===== | |
− | + | :returns true if var defined, false otherwise. | |
− | + | =====Default(x,d)===== | |
− | + | :returns x if Defined(x), d otherwise. | |
− | + | :Note: Defined and Default are primarily designed for dealing with optional declared variables in user-defined functions and are used to determine whether an optional variable has been passed. Any completely undeclared variable will generate an error. See "User Defined Functions" later in this section for more information. | |
− | + | =====Eval(string), Apply(func-string, arg, ...)===== | |
− | Eval( | + | :Eval("f(x)") is equivalent to f(x) is equivalent to Apply("f", x)) |
− | + | :You can use Eval for something like ''(here a specific call to [[Resize|BicubicResize]] filter is shown, but you get the idea):'' | |
− | + | ||
− | + | ||
settings = "352, 288" | settings = "352, 288" | ||
Eval( "BicubicResize(" + settings + ")" ) | Eval( "BicubicResize(" + settings + ")" ) | ||
− | ''You can import the text of another script:'' | + | :''You can import the text of another script:'' |
[[Import]] (filename): evals contents of another AviSynth script. | [[Import]] (filename): evals contents of another AviSynth script. | ||
− | + | =====Assert (bool, string error-message)===== | |
− | Assert (bool, string error-message) | + | |
− | + | :For error reporting and catching bad input to user-defined function, you can use (error-message if bool=false) | |
try { | try { | ||
Line 357: | Line 390: | ||
== Control Functions == | == Control Functions == | ||
− | SetMemoryMax (int) | + | =====SetMemoryMax (int)===== |
− | + | :Sets the maximum memory (in MB) that AviSynth uses for its internal Video Frame cache | |
− | + | :From v2.5.8, setting this to zero just returns the current Memory Max value. In the 2.5 series the default Memory Max value is 25% of the free physical memory, with a minimum of 16MB. From rev 2.5.8 RC4, the default Memory Max is also limited to 512MB. | |
− | + | =====SetPlanarLegacyAlignment (clip, bool)===== | |
− | + | :Set alignment mode for planar frames. v2.5.6 | |
+ | |||
+ | :Some older plugins illegally assume the layout of video frames in memory. This special filter forces the memory layout of planar frames to be compatible with prior version of AviSynth. The filter works on the GetFrame() call stack, so it effects filters before it in the script. | ||
+ | |||
+ | :''Example:'' | ||
# Using an older version of Mpeg2Source() | # Using an older version of Mpeg2Source() | ||
Line 375: | Line 412: | ||
... # advanced memory alignment. | ... # advanced memory alignment. | ||
− | SetWorkingDir (string) | + | =====SetWorkingDir (string)===== |
− | + | :Sets the directory from which AviSource, LoadPlugin, etc. are referenced. This is primarily for easy loading of source clips, etc. Does not affect plugin autoloading. Return value: 0 if successful, -1 otherwise. v2 | |
+ | |||
+ | :''Conditional Operations:'' | ||
```[```''var''```=]```''boolean expression'' ? ''iftrue value or operation'' : ''ifelse value or operation'' | ```[```''var''```=]```''boolean expression'' ? ''iftrue value or operation'' : ''ifelse value or operation'' | ||
− | nop(): v2.07 null result primarily for if-then-else operation above where ifelse is not desired | + | =====nop()===== |
+ | |||
+ | :v2.07 null result primarily for if-then-else operation above where ifelse is not desired | ||
+ | |||
:(nop primarily to be used against non-variable / non-clip functions such as import and loadplugin) | :(nop primarily to be used against non-variable / non-clip functions such as import and loadplugin) | ||
− | + | :''Example:'' | |
VersionNumber<2.07 ? [[Import]]("patches.avs") : nop() | VersionNumber<2.07 ? [[Import]]("patches.avs") : nop() | ||
− | Select(int index, val ''item0'' ```[```,''item1''...```]```) | + | =====Select(int index, val ''item0'' ```[```,''item1''...```]```)===== |
+ | ::v2.07 | ||
− | Indexed selection of item0 ... item<n-1> (no particular limit on number of items). Items can be of any type, including clips and technically, item types don't have to match, but that could be problematic. Can be used with Rand() function for the index to have a random clip generator or to manage various versions (i.e.,title, preview, main or perhaps PAL, NTSC, FILM) in the same script. | + | :Indexed selection of item0 ... item<n-1> (no particular limit on number of items). Items can be of any type, including clips and technically, item types don't have to match, but that could be problematic. Can be used with Rand() function for the index to have a random clip generator or to manage various versions (i.e.,title, preview, main or perhaps PAL, NTSC, FILM) in the same script. |
− | + | == User defined functions == | |
− | You can define and call your own functions in AVS scripts as shown below. [[Script_grammar|Script Grammar]] within a function is identical to that of a script at large. The function can return any clip or variable type. The format is generally as follows: | + | :You can define and call your own functions in AVS scripts as shown below. [[Script_grammar|Script Grammar]] within a function is identical to that of a script at large. The function can return any clip or variable type. The format is generally as follows: |
function ''function_name''(```[```''var_type'' ''var_name'' ```[,...]```) | function ''function_name''(```[```''var_type'' ''var_name'' ```[,...]```) | ||
Line 405: | Line 448: | ||
} | } | ||
− | Curled brackets must enclose the function commands, but they can share lines with the function header and / or the first and last function commands. The following is an example of a single line function: | + | :Curled brackets must enclose the function commands, but they can share lines with the function header and / or the first and last function commands. The following is an example of a single line function: |
function rednum(clip c, int "num") { | function rednum(clip c, int "num") { | ||
Line 411: | Line 454: | ||
} | } | ||
− | Variables passed to functions can be made optional by enclosing ''var_name'' in quotes (i.e., int "num" in the previous example). Within the function, you would include a Defined or Default function to determine if ''var_name'' was passed and set the default value or alter your processing accordingly. Optional parameters can also be called using the ''var_name=value'' syntax to skip over other optional parameters. | + | :Variables passed to functions can be made optional by enclosing ''var_name'' in quotes (i.e., int "num" in the previous example). Within the function, you would include a Defined or Default function to determine if ''var_name'' was passed and set the default value or alter your processing accordingly. Optional parameters can also be called using the ''var_name=value'' syntax to skip over other optional parameters. |
function NTSC2PAL(clip c) { | function NTSC2PAL(clip c) { |
Revision as of 03:35, 16 September 2014
The input and output of these functions are not clips, but some other variables used in the script.
Numeric Functions
MulDiv(int, int, int)
- v2.56
- (m * n + d / 2) / d with 64 bit intermediate result
- Examples:
MulDiv (1, 1, 2) = 1 MulDiv (2, 3, 2) = 3
Floor(float)
- Converts from float to int (round down on any fractional amount).
- Examples:
Floor(1.2) = 1 Floor(1.6) = 1 Floor(-1.2) = -2 Floor(-1.6) = -2
Ceil(float)
- Converts from float to int (round up on any fractional amount).
- Examples:
Ceil(1.2) = 2.0 Ceil(1.6) = 2.0 Ceil(-1.2) = -1 Ceil(-1.6) = -1
Round(float)
- Converts from float to int (round off to nearest integer).
- Examples:
Round(1.2) = 1 Round(1.6) = 2 Round(-1.2) = -1 Round(-1.6) = -2
Sin(float)
- v2
- Returns the sine of the argument (assumes it is radians).
- Examples:
Sin(Pi()/4) = 0.707 Sin(Pi()/2) = 1.0
Cos(float)
- v2
- Returns the cosine of the argument (assumes it is radians).
- Examples:
Cos(Pi()/4) = 0.707 Cos(Pi()/2) = 0.0
Pi()
- v2
- Returns the value of the "pi" constant (the ratio of a circle's perimeter to its diameter).
- Examples:
d = Pi() # d == 3.141593
Log(float)
- v2
- Returns the natural (base-e) logarithm of the argument.
- Examples:
Log(1) = 0.0 Log(10) = 2.30259
Exp(float)
- v2
- Returns the natural (base-e) exponent of the argument.
- Examples:
Exp(1) = 2.718282 Exp(0) = 1.0
Pow(float base, float power)
- v2
- Returns "base" raised to the power indicated by the second argument.
- Examples:
Pow(2, 3) = 8 Pow(3, 2) = 9 Pow(3.45, 1.75) = 8.7334
Sqrt(float)
- v2
- Returns the square root of the argument.
- Examples:
Sqrt(1) = 1.0 Sqrt(2) = 1.4142
Abs(float or integer)
- v2.07
- Returns the absolute value (returns float for float, integer for integer).
- Example:
Abs(-3.8) = 3.8
Sign(float)
- v2.07
- Returns sign of value (1, 0 or -1).
- Examples:
Sign(-3.5) = -1 Sign(3.5) = 1 Sign(0) = 0
Int(float)
- v2.07
- Converts from float to int (round towards zero).
- Examples:
Int(1.2) = 1 Int(1.6) = 1 Int(-1.2) = -1 Int(-1.6) = -1
Frac(float)
- v2.07
- Returns the fractional portion of the value provided.
- Examples:
Frac(3.7) = 0.7 Frac(-1.8) = -0.8
Float(int)
- v2.07
- Converts int to float.
Value(string)
- v2.07
- Converts string to numeric value.
- Example:
Value ("-2.7") = -2.7
HexValue(string)
- v2.07
- Evaluates string as hexadecimal value.
- Example:
HexValue ("FF00") = 65280
Rand([int max] [, bool scale] [, bool seed])
- v2.07
- Returns a random integer value and all parameters are optional.
- max
- sets the maximum value+1 (default 32768) and can be set negative for negative results. It operates either in scaled or modulus mode (default scale=true only if abs(max) > 32768, false otherwise).
- scale
- If scale=true, scales the internal random number generator value to the maximum value, while modulus mode (scale=false) uses the remainder from an integer divide of the random generator value by the maximum. Modulus mode is preferred for smaller maximums.
- seed
- If seed=true, seeds the random number generator with the current time. seed defaults to false and probably isn't necessary, although it's there just in case.
- Typically, this function would be used with the Select function for random clips.
- Example:
Select(Rand(5), clip1, clip2, clip3, clip4, clip5)
Spline(float X, x1, y1, x2, y2, .... [, bool cubic])
- v2.51
- Interpolates the Y value at point X using the control points x1/y1, ... There have to be at least 2 x/y-pairs. The interpolation can be cubic (the result is a spline) or linear (the result is a polygon).
- Examples:
Spline(5, 0, 0, 10, 10, 20, 0, false) = 5 Spline(5, 0, 0, 10, 10, 20, 0, true) = 7
String Functions
LCase(string)
- v2.07
- Returns lower case of string.
- Example:
LCase("AviSynth") = "avisynth"
UCase(string)
- v2.07
- Returns upper case of string.
- Example:
UCase("AviSynth") = "AVISYNTH"
StrLen(string)
- v2.07
- Returns length of string.
- Example:
StrLen("AviSynth") = 8
RevStr(string)
- v2.07
- Returns string backwards.
- Example:
RevStr("AviSynth") = "htnySivA"
LeftStr(string, int)
- v2.07
- Returns first int number of characters.
- Example:
LeftStr("AviSynth", 3) = "Avi"
RightStr(string, int)
- v2.07
- Returns last int number of characters.
- Example:
RightStr("AviSynth", 5) = "Synth"
MidStr(string, int pos [, int length])
- v2.07
- Returns substring starting at pos for optional length or to end. pos=1 specifies start.
- Example:
MidStr("AviSynth", 3, 2) = "iS"
FindStr(string, substring)
- v2.07
- Returns position of substring within string. Returns 0 if not found.
- Example:
Findstr("AviSynth", "syn") = 4
String(float / int)
- v2
- Converts a number to a string.
- Example:
Subtitle( "Clip height is " + String(last.height) )
Chr(int)
- v2.51
- Returns the Windows character in the system code page (for computers in the West, usually Windows-1252).
- Valid input range 0-255; values outside that range are wrapped around (modulo 256).
- Example:
Chr(34) # returns the quote character Chr(802) # also returns the quote character (802 % 256 = 34) Chr(-990) # also returns the quote character (-990 % 256 = 34, in unsigned 32 bit math)
Time(string)
- v2.51
- Returns a string with the current system time formatted as defined by the string.
- Codes for output formatting:
%a Abbreviated weekday name %A Full weekday name %b Abbreviated month name %B Full month name %c Date and time representation appropriate for locale %d Day of month as decimal number (01 ? 31) %H Hour in 24-hour format (00 ? 23) %I Hour in 12-hour format (01 ? 12) %j Day of year as decimal number (001 ? 366) %m Month as decimal number (01 ? 12) %M Minute as decimal number (00 ? 59) %p Current locale?s A.M./P.M. indicator for 12-hour clock %S Second as decimal number (00 ? 59) %U Week of year as decimal number, with Sunday as first day of week (00 ? 53) %w Weekday as decimal number (0 ? 6; Sunday is 0) %W Week of year as decimal number, with Monday as first day of week (00 ? 53) %x Date representation for current locale %X Time representation for current locale %y Year without century, as decimal number (00 ? 99) %Y Year with century, as decimal number %z, %Z Time-zone name or abbreviation; no characters if time zone is unknown %% Percent sign
- The # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows:
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% No change; # flag is ignored.
%#c Long date and time representation, appropriate for current locale. For example: ?Tuesday, March 14, 1995, 12:41:29?.
%#x Long date representation, appropriate to current locale. For example: ?Tuesday, March 14, 1995?.
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y Remove leading zeros (if any).
Version Checking Functions
VersionNumber()
- v2.07
- Returns AviSynth version number as float.
- Example:
VersionNumber() = 2.07
VersionString()
- v2.07
- Returns AviSynth version info as string (first line used in Version command).
- Example:
VersionString() = "AviSynth 2.08 (avisynth.org) 22 nov. 2002"
Variable Type and File Checking Functions
IsBool(var), IsInt(var), IsFloat(var), IsString(var), IsClip(var)
- Checks if var is the requested type.
Exist(filename)
- v2.07
- Checks if specified file exists.
Defined(var)
- returns true if var defined, false otherwise.
Default(x,d)
- returns x if Defined(x), d otherwise.
- Note: Defined and Default are primarily designed for dealing with optional declared variables in user-defined functions and are used to determine whether an optional variable has been passed. Any completely undeclared variable will generate an error. See "User Defined Functions" later in this section for more information.
Eval(string), Apply(func-string, arg, ...)
- Eval("f(x)") is equivalent to f(x) is equivalent to Apply("f", x))
- You can use Eval for something like (here a specific call to BicubicResize filter is shown, but you get the idea):
settings = "352, 288" Eval( "BicubicResize(" + settings + ")" )
- You can import the text of another script:
Import (filename): evals contents of another AviSynth script.
Assert (bool, string error-message)
- For error reporting and catching bad input to user-defined function, you can use (error-message if bool=false)
try { AviSource("file.avi") } catch(err_msg) { BlankClip.Subtitle(err_msg) }
Control Functions
SetMemoryMax (int)
- Sets the maximum memory (in MB) that AviSynth uses for its internal Video Frame cache
- From v2.5.8, setting this to zero just returns the current Memory Max value. In the 2.5 series the default Memory Max value is 25% of the free physical memory, with a minimum of 16MB. From rev 2.5.8 RC4, the default Memory Max is also limited to 512MB.
SetPlanarLegacyAlignment (clip, bool)
- Set alignment mode for planar frames. v2.5.6
- Some older plugins illegally assume the layout of video frames in memory. This special filter forces the memory layout of planar frames to be compatible with prior version of AviSynth. The filter works on the GetFrame() call stack, so it effects filters before it in the script.
- Example:
# Using an older version of Mpeg2Source() LoadPlugin("...\Mpeg2Decode.dll") Mpeg2Source("test.d2v") # A plugin that illegally assumes # the layout of memory SetPlanarLegacyAlignment(true) # Set legacy memory alignment for prior statements ConvertToYUY2() # Statements thru to the end of the script have ... # advanced memory alignment.
SetWorkingDir (string)
- Sets the directory from which AviSource, LoadPlugin, etc. are referenced. This is primarily for easy loading of source clips, etc. Does not affect plugin autoloading. Return value: 0 if successful, -1 otherwise. v2
- Conditional Operations:
```[```var```=]```boolean expression ? iftrue value or operation : ifelse value or operation
nop()
- v2.07 null result primarily for if-then-else operation above where ifelse is not desired
- (nop primarily to be used against non-variable / non-clip functions such as import and loadplugin)
- Example:
VersionNumber<2.07 ? Import("patches.avs") : nop()
Select(int index, val item0 ```[```,item1...```]```)
- v2.07
- Indexed selection of item0 ... item<n-1> (no particular limit on number of items). Items can be of any type, including clips and technically, item types don't have to match, but that could be problematic. Can be used with Rand() function for the index to have a random clip generator or to manage various versions (i.e.,title, preview, main or perhaps PAL, NTSC, FILM) in the same script.
User defined functions
- You can define and call your own functions in AVS scripts as shown below. Script Grammar within a function is identical to that of a script at large. The function can return any clip or variable type. The format is generally as follows:
function function_name(```[```var_type var_name ```[,...]```) { function commands . . . return(result) }
- Curled brackets must enclose the function commands, but they can share lines with the function header and / or the first and last function commands. The following is an example of a single line function:
function rednum(clip c, int "num") { return Subtitle(c,string(default(num,0)),text_color=$FF0000) }
- Variables passed to functions can be made optional by enclosing var_name in quotes (i.e., int "num" in the previous example). Within the function, you would include a Defined or Default function to determine if var_name was passed and set the default value or alter your processing accordingly. Optional parameters can also be called using the var_name=value syntax to skip over other optional parameters.
function NTSC2PAL(clip c) { # Fairly good NTSC->PAL conversion. Would be better with Smart Bob. :-) Assert(c.height == 480, "NTSC2PAL: input clip must have 480 scan lines") Bob(c, height=576) ChangeFPS(50) SeparateFields.SelectEvery(4, 0, 3) return Weave } AviSource("ntsc.avi").NTSC2PAL
If you create anything cool, be sure to post it to Shared Script Functions!