AfterDawn.com

What Is a Script


If you're familiar with any kind of scripting you should catch on to AviSynth right away. If not, it should be only slightly more difficult. Like pretty much any type of script, an AviSynth script is basically a list of instructions. The instructions range from opening a video file to changing the resolution, and even converting from PAL to NTSC. Scripts are stored in text files that end with an extension of .AVS. There are specialized tools for script editing that will create files with this extension for you. Otherwise you can use a general purpose text editor (like Notepad) and change the extension to .AVS when you save it.


Script Reading 101


Before you try to write an AviSynth script, it's helpful to learn how to read them first. This will give you a taste of the syntax (computer grammar) and general flow of a script. In fact we're not even going to start with a script that would work in AviSynth because we aren't interested in what it does yet. For now we'll just concentrate on how it's written. This example, and in fact the entire guide, is intended to be a basic introduction to AviSynth. There are much more complex things that can be done with AviSynth that won't be covered here.

Basic Script

# Lines that start with the # character aren't parsed by AviSynth.
# Instead they're used for human readable comments. These can
# include instructions for using the script, explanations of
# specific elements, writing credits, legal notices, or just neat
# designs
#
#########################################
#***************************************#
#********** Sample Script 1.0 **********#
#******* By Rich Fiscus (Vurbal) *******#
#********* © 2007 Afterdawn.com ********#
#***************************************#
#########################################
#
# Load a third party plugin
# Not necessary for plugins stored in the AviSynth plugins folder
# Located at C:\Program Files\AviSynth 2.5\plugins
LoadPlugin("D:\Plugins\pluginName.dll")

# Load muxed video/audio source
MuxedSource("Muxed Filename.ext")

# Load separate video and audio sources
# Uncomment the code in this section
# To combine video and audio from separate files
#
# Load Video Source
# Vid=VideoSource("D:\Folder Name\Video Filename.ext")
#
# Load audio source
# Aud=AudioSource("D:\Folder Name\Audio Filename.ext")
#
# Combine Video and Audio
# AudioDub(Vid, Aud)

# Load a second video/audio combination
# Uncomment the code in this section
# To combine video and audio from separate files without variables
#
# clipOne=AudioDub(VideoSourceFilter("D:\Folder Name\Video Filename.ext"), /
# AudioSourceFilter("D:\Folder Name\Audio Filename.ext"))

# Do Something
SomeFilter(SomeArg="something", OtherArg=3, BoolArg=true)

# Concatenate Filters
SomeFilter().SomeOtherFilter()


Reading This Script

Although nearly all the filters used in this script are made up, it can still be used to understand how a script is constructed. The first thing to understand is that each filter represents a single instruction. Instead of starting with the code you should try to construct a sentence describing what you want to do. The sentence will then be translated into one or more filters. For example, we start with the instruction open a file. This translates to
MuxedSource("Muxed Filename.ext")

MuxedSource is an imaginary filter for opening a file. In a real script you'll need to use a real source filter to open your video. The value in parentheses (a file path and name) is an argument. Arguments are additional information that may be required in order for a filter to work. In this case the filename is required because it must know what file to open.

# Comments

This script may be used to open files in a variety of different ways. Since the most basic way to get video and audio is from a single file, that's the default for the script. However, by adding a # to the beginning of the MuxedSource line you would comment it out. The text following a comment mark (#) isn't read by AviSynth, but can be helpful to document scripts for a variety of reasons. Comments can be entire lines of text or added after code.

Loading External Plugins

LoadPlugin("D:\Plugins\pluginName.dll")
Since much of AviSynth's functionality stems directly from its open source roots and active developer community. Plugins are routinely developed and updated to add advanced features from high quality deinterlacing to IVTC. In the AviSynth installation folder is another folder called plugins. In order to make the filters in third party packages available, simply copy the DLL file(s) into this plugins folder and they'll automatically load for every script. If you don't have the DLL file in this directory you can load it via the LoadPlugin filter, which takes a single argument of the path to the filter.

Variables

Once the file is open, the clip (up to 1 video stream and up to 1 audio stream) is assigned to a variable called last. Although you don't need to understand variables for simple operations like cutting and joining clips, for more advance operations they are likely to be necessary. A variable is a label that can represent a piece of data such as text or a number. In AviSynth a variable can also represent a clip. When using a filter you can generally assume that the clip assigned to last is being processed unless a specific clip (like clipOne later in the script) is used as an argument. For example
BicubicResize(480, 576)
is equivalent to
last = BicubicResize(last, 480, 576)
In English this equates to 'Resize the clip last to a resolution of 480x576 and then assign this new clip to last in place of the original. While it may seem odd that last is both the source for the filter and destination for the output, if you understand the order variables are evaluated (replaced with clips) in it makes perfect sense. You can think of it in terms of the math you learned as a child. Evaluate what's in parentheses first. First the clip assigned to last before the filter runs goes in at one end and is returned to last again when processing is complete. By replacing last (in either place) with either another variable or another filter you can change this default behavior.

Make sure not to confuse the following two lines:
var = BicubicResize(480, 576)
BicubicResize(var, 480, 576)
The first resizes last and assigns it to var. The second resizes var and assigns it to last. This is where thinking in terms of actions rather than filters comes in handy. Both use the same variables and filter, but they do very different things.

Separate Video and Audio Sources

# Vid = VideoSource("D:\Folder Name\Video Filename.ext")
#
# Load audio source
# Aud = AudioSource("D:\Folder Name\Audio Filename.ext")
#
# Combine Video and Audio
# AudioDub(Vid, Aud)
The first filter chain (series of filters) example for using separate video and audio files to produce a clip involves user defined variables. One variable (vid) has the video, while another (aud) represents the audio. Even though one is video and the other audio, they're both clips. The AudioDub filter is the actual filter you'd use to combine (mux) these clips together. The muxed clip is assigned to last. If you left the original MuxedSource line active (ie no # at the beginning), the last variable would refer to that clip before AudioDub set it equal to this new clip.

Using Filters as Arguments

The next filter chain uses the same filters, but this time instead of inserting them sequentially and assigning them to user defined variables, the VideoSource and AudioSource filters are used directly as arguments for the AudioDub filter. Unlike the previous example, this is explicitly assigned to the user defined variable clipOne:
clipOne = AudioDub(VideoSourceFilter("D:\Folder Name\Video Filename.ext"), \
AudioSourceFilter("D:\Folder Name\Audio Filename.ext"))
Because this is a long line, I split it across two lines in the script by adding \ to the end of the first line. It could instead be added to the beginning of the continued line:
clipOne = AudioDub(VideoSourceFilter("D:\Folder Name\Video Filename.ext"),
\ AudioSourceFilter("D:\Folder Name\Audio Filename.ext"))
You could also leave it intact as a single line:
clipOne = AudioDub(VideoSourceFilter("D:\Folder Name\Video Filename.ext"), AudioSourceFilter("D:\Folder Name\Audio Filename.ext"))
Although this makes for a somewhat shorter script, it's not as easy to decipher quickly. Most experienced scripters prefer using variables for clarity.

Other Filters

Additional filters can be used to select frames, deinterlace, change color properties, and do a wide variety of other tasks. The main thing to remember is how arguments work.
# Do Something
SomeFilter(SomeArg = "text", OtherArg = 3, BoolArg = true)
Some arguments are named, meaning they require a variable name be used (SomeArg=), while others simply require the desired value. Notice that there are three distinct types of arguments here. The first is text, which must be surrounded by quotes. The next, OtherArg, is numeric, and therefore doesn't use quotes. The last one is Boolean, and although the text True is used, it's really a numeric value (True = 1, False = 0) so it doesn't need quotes either. Any or all of these arguments could possibly be used without a parameter name. For example, if only one argument uses a Boolean value you might be able to rewrite the line like this:
# Do Something
SomeFilter(SomeArg = "something", OtherArg = 3, true)
Whether an argument requires a name or not depends on the range of arguments used, and sometimes on the author of a plugin.

Concatenating Filters

Sometimes it makes sense to put multiple filters on the same line. You can do this with AviSynth's concatenation feature, which works by putting . between filters on the same line:
SomeFilter().SomeOtherFilter()
This can be useful for combining filter chains that are always used in combination, both for readability and ease of editing later on.
previous  | next
Written by: Rich Fiscus