ScriptFunctions のバックアップソース(No.5)

*スクリプト関数: [#iea63ce3]

これらの関数の入出力は、クリップではなく、スクリプトの中で使われる別の変数です。

#contents

**数値関数: [#u1ee7bc9]

#hr

 Floor(float)

floatからintへ変換します(小数点以下の端数を切り捨て)。

''例:''

 Floor(1.2) = 1 
 Floor(1.6) = 1 
 Floor(-1.2) = -2 
 Floor(-1.6) = -2 

#hr

 Ceil(float)

floatからintへ変換します(小数点以下の端数を切り上げ)。

''例:''

 Ceil(1.2) = 2.0 
 Ceil(1.6) = 2.0 
 Ceil(-1.2) = -1 
 Ceil(-1.6) = -1 

#hr

 Round(float)

floatからintへ変換します(最も近い整数に四捨五入する)。

''例:''

 Round(1.2) = 1 
 Round(1.6) = 2 
 Round(-1.2) = -1 
 Round(-1.6) = -2 

#hr

 Sin(float) v2

#hr

 Cos(float) v2

#hr

 Pi() v2

#hr

 Log(float) v2

#hr

 Exp(float) v2

#hr

 Pow(float base, float power) v2

#hr

 Sqrt(float) v2

#hr

 Abs(float or integer) v2.07

絶対値を返します(floatにはfloatを、整数には整数を返す)。

''例:''

 Abs(-3.8) = 3.8 

#hr

 Sign(float) v2.07

値の符号を返します(1、0、または-1)。

''例:''

 Sign(-3.5) = -1 
 Sign(3.5) = 1 
 Sign(0) = 0 

#hr

 Int(float) v2.07

floatからintへ変換します(0のほうへ丸める)。

''例:''

 Int(1.2) = 1 
 Int(1.6) = 1 
 Int(-1.2) = -1 
 Int(-1.6) = -1 

#hr

 Frac(float) v2.07

与えられた値の小数点以下の端数分を返します。

''例:''

 Frac(3.7) = 0.7 
 Frac(-1.8) = -0.8 

#hr

 Float(int) v2.07

intをfloatへ変換します。

#hr

 Value(string) v2.07

文字列を数値へ変換します。

#hr

 HexValue(string) v2.07

文字列を16進数の値として評価します。

''例:''

 HexValue ("FF00") = 65280

#hr

 Rand([int max] [, bool scale] [, bool seed]) v2.07

ランダムな整数値を返します。すべてのパラメータはオプションです。maxは最大値+1を設定し(デフォルト 32768)、負の結果のために負の値を設定することができます。scaledモードかmodulus((訳者註: 係数の意。)モードのどちらかで処理します(デフォルトは、maxの絶対値が32768より大きいならscale=true、それ以外はscale=false)。scaledモードは、内部のランダム数ジェネレータの値を最大値(max)にあわせて調整します。一方、modulusモード(scale=false)は、整数の割り算により、最大値でランダムジェネレータの値を割ったときの余りを使用します。最大値が小さいときほど、modulusモードが最適です。seed=trueを使うと、ランダム数ジェネレータに現在の時間をシード(seed)します。デフォルトではfalseとなり、おそらくseed=trueを使用する必要はありません。しかし、万一に備えて、seedパラメータは存在します。一般的に、この関数は、ランダムなクリップ用のSelect関数と一緒に使用されます。

''例:''

 Select(Rand(5), clip1, clip2, clip3, clip4, clip5) 

#hr

 Spline(float X, x1, y1, x2, y2, .... [, bool cubic]) v2.51

コントロールポイント(制御点)x1/y1などを使って、Yの値をX点で補間します。x/yのペアは、少なくとも2つ以上でなければなりません。補間は、3次(cubic)補間(結果はスプライン)か線形(linear)補間(結果は多角形(polygon))になります。

''例:''

 Spline(5, 0, 0, 10, 10, 20, 0, false) = 5 
 Spline(5, 0, 0, 10, 10, 20, 0, true) = 7

#hr

**String Functions: [#s1b75b61]

#hr

 LCase(string) v2.07

Returns lower case of string.

''Example:''

 LCase("AviSynth") = "avisynth" 

#hr

 UCase(string) v2.07

Returns upper case of string.

''Example:''

 UCase("AviSynth") = "AVISYNTH" 

#hr

 StrLen(string) v2.07

Returns length of string.

''Example:''

 StrLen("AviSynth") = 8 

#hr

 RevStr(string) v2.07

Returns string backwards.

''Example:''

 RevStr("AviSynth") = "htnySivA" 

#hr

 LeftStr(string, int) v2.07

Returns first int number of characters.

''Example:''

 LeftStr("AviSynth", 3) = "Avi" 

#hr

 RightStr(string, int) v2.07

Returns last int number of characters.

''Example:''

 LeftStr("AviSynth", 5) = "Synth" 

#hr

 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" 

#hr

 FindStr(string, substring) v2.07

returns position of substring within string. Returns 0 if not found.

''Example:''

 Findstr("AviSynth", "syn") = 4 

#hr

 String(float / int) v2

Converts a number to a string.

''Example:''

 Subtitle( "Clip height is " + String(last.height) ) 

#hr

 Chr(int) v2.51

Returns the ASCII character.

''Example:''

    Chr(34) returns the quote character

#hr

 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)

%YYear 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, %#% # 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).

#hr

**Version Checking Functions: [#k3d35a2c]

#hr

 VersionNumber() v2.07

Returns AviSynth version number as float.

''Example:''

 VersionNumber() = 2.07 

#hr

 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"

#hr

**Variable Type and File Checking Functions: [#sb873dca]

 IsBool(var)

 IsInt(var)

 IsFloat(var)

 IsString(var)

 IsClip(var)

 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:'''

 settings = "352, 288"
 
 Eval( "BicubicResize(" + settings + ")" )

'''You can import the text of another script:'''

 Import (filename): evals contents of another AviSynth script.

'''For error reporting and catching bad input to user-defined function you can use (error-message if bool=false):'''

 Assert (bool, string error-message)

There is a function for checking if an error WILL arise:

 Try {
 
 AviSource("file.avi")
 }
 
 catch(err_msg) {
 
 Blackness.Subtitle(err_msg)
 
 }

**Control Functions: [#u1280d7c]

 SetMemoryMax(int)
: Sets the maximum memory that AviSynth uses (in MB). v2

In some versions there is a default setting of 5MB, which is quite low. If you encounter problems (e.g. low speed) try to set this values to at least 32MB.

 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. ScriptGrammar 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 ShareFunctions!

Back to AviSynthManual