Non-clip functions

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
m (String(var [, format-string]): delete previous note, add link to other String( ))
(make redirect)
 
Line 1: Line 1:
The input and output of these functions are not clips, but some other variables used in the script.
+
#REDIRECT [[Internal_functions]]
  
== 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. <!-- TODO: determine behavior on invalid input -->
 
 
:''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(var [, String format_string])=====
 
::v2
 
 
:Converts a variable to a string. String arguments are passed along unchanged; booleans are converted to "true" or "false"; numbers (ints or floats) are formatted as described below; all other value types are converted to the empty string.
 
 
:'''format_string''': optional, sets number of decimal places; must be of the form <code>"%'''x'''.'''y'''f"</code>
 
::* where '''x''' is the minimum width in characters (including decimal point)
 
::* and '''y''' is the number of digits to the right of the decimal point.
 
::* format_string must start with '''%''' and end with '''f'''.
 
::* other [http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm sprintf] formatting flags are available;  see [[Internal_functions/Conversion_functions|Conversion functions]].
 
::* if ''var'' is not a number and a format string is given, output is the empty string.
 
 
:The format string is also useful for '''right-justifying numbers''' - enforcing a minimum width, padded with leading spaces.
 
 
:''Examples:''
 
## floating point examples
 
Subtitle( "PI=" + String(PI, "%0.0f") )    # "PI=3"
 
Subtitle( "PI=" + String(PI, "%2.0f") )    # "PI= 3"
 
Subtitle( "PI=" + String(PI, "%3.2f") )    # "PI=3.14"
 
Subtitle( "PI=" + String(PI, "%0.5f") )    # "PI=3.14159"
 
Subtitle( "PI=" + String(PI, "%6.3f") )    # "PI= 3.142"
 
## integer examples
 
Subtitle( "'" + String(32, "%0f") + "'" )  # result: '32.000000'
 
Subtitle( "'" + String(32, "%0.0f") + "'" ) # result: '32'
 
Subtitle( "'" + String(32, "%3.0f") + "'" ) # result: ' 32'
 
Subtitle( "'" + String(32, "%8.0f") + "'" ) # result: '      32'
 
 
=====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]).
 
 
: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)
 
 
=====Hex(int)=====
 
::v2.07
 
 
: Converts a numerical value to its hexadecimal value. See [[Colors]] for more information on specifying colors.
 
: ''Examples:''
 
Hex (10824234) = "A52A2A"
 
 
=====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 [[Resize|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 ==
 
 
('''TODO''' - move to another page?)
 
 
=====Ternary expression (condition ? a : b)=====
 
 
:Syntax:
 
::[''var''=] ''boolean expression'' '''?''' ''if-true value or operation'' '''<nowiki>:</nowiki>''' ''if-else value or operation''
 
 
:''Examples:''
 
 
(IsRGB) '''?''' RGBAdjust(analyze=true) ''':''' ColorYUV(analyze=true)
 
ConvertToYV12(matrix=(Height>500) '''?''' "Rec709" ''':''' "Rec601")
 
HasAudio '''?''' Last ''':''' AudioDub(WavSource("music.wav"))
 
 
=====Nop()=====
 
 
:::v2.07
 
:Null operation; primarily for if-then-else operation above where an 'else' value is not desired - for example, conditional [[Import]] or [[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|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_functions|Shared Script Functions]]!
 
  
 
[[Category:AviSynth Syntax]]
 
[[Category:AviSynth Syntax]]

Latest revision as of 03:00, 17 September 2014

  1. REDIRECT Internal_functions
Personal tools