AviSynth - What it is, how it works and why you need it.


AviSynth is, in my humble opinion, the most significant addidition to Windows digital video editing in recent memory. It it incredibly powerful and, once you get used to it, it is incredibly convenient for doing all kinds of wonderful video manipulation tasks that would be very difficult to do otherwise.

There aren't any problems with AviSynth in my mind but there is a problem with the way it is perceived. Many people don't really know what it is, what it does or why any of this is actually useful. Others feel that they don't need it because their method has worked for them and they are sticking to it... but I say if you give this program a chance you will soon wonder how you used to live without it.

Demystifying AviSynth

AviSynth isn't a program in the way you may usually think of programs. You don't run an .exe and use some graphical interface to change settings or anything - that's not the way AviSynth works. AviSynth is a video processing machine that works in the background. It's a trickster that makes video editing/playback software  think that it is reading a video file when in reality the footage is being generated frame by frame to your design thanks to Avisynth.

All you do is write a simple file with some commands on it and you give this file to a Video Editing program to read. When the Video Program looks at the file it doesn't see the silly commands you wrote: it sees video. The video the program sees is what you described in your simple file.

Let me demonstrate the process by using resizing as an example:
  1. Take Video 'X' whose resolution is too small
  2. Make a simple avisynth script that says "This is a bigger verison of Video X"
  3. Give avisynth script to editing program
  4. Editing program looks at the script file and instead sees footage
  5. Every time the program looks at a frame, avisynth takes Video X, makes the frame bigger and then gives it to the editing program.
So, instead of having to make a brand new video which is bigger than the old one, AviSynth simply makes each frame bigger when the editing program wants to see that frame. This all happens behind the scenes so that the effect is that you have your bigger video but no need to re-encode or make a new file.

Of course this sort of thing you can do in your editing program - you can tell the editing program to scale up the footage. This is just the start though - you can put all sorts of commands in these scripts such as:
  1. Take some dvd footage that an editing program can't read
  2. Decode it
  3. Look for the Telecine (interlacing) pattern
  4. Restore the original frames and remove interlacing
  5. Trim out a scene
  6. Crop off the junk from the edges
  7. Filter the footage so it looks cleaner
  8. Change the frame rate
  9. Give it to the editing program
And all of this can be done before the video program even sees the footage - and all in one place. You don't need one program to decode the dvd footage, then another to remove interlacing and then another to make it look better. AviSynth does all these things and a LOT more besides all in a simple script file that you write.

AviSynth has become essential to Windows digital video editing because of this flexibility and also because of its immense power. The kind of deinterlacing and telecine removal that you can do in avisynth is something that you could potentially pay thousands to do as part of your professional video editing program. The ability to decode mpeg2 is something that many editing programs have these days but few can do it quite so accurately as AviSynth. Combine this with its ability to convert types of footage, and do all the other digital manipulations it does, then it's easy to understand why this is such as useful tool.

And all of this is done in a mere few lines of text. That's the real beauty - you need to change something, you change the script. You want to add something else, you add it to the script. It's convenience in excess... you just need to work out how to use it.

Getting Avisynth to do its thing.

Avisynth uses scripts which you have to write and it is these scripts which tell avisynth what to do and what video to produce when this script is given to a video editing program. Now although these scripts are pretty simple things it is very different form what most people do - it's more like programming than video editing - but you'll pick it up pretty quick. Also, these instructions give you basic way of doing these things but VirtualDubMod will help you do them twice as fast if not more, but it's best to learn the simple long way first.

The first thing you'll want to do is to go to a folder. Right-click and choose New > Text Document

A file will appear called "New Text Document" and if you double click it will open up in a text editor, probably Notepad or Wordpad. Now, in order to make Avisynth files we need the ability to rename the file extension. If the file you created is shown as "New Text Document.txt" then this is a good thing. If all you can see is "New Text Document" then you will need to change your folder display options

Go to Tools in your directory window and choose folder options...  select the 'view' tab and deselect "Hide file extenstions for known file types". This should allow you to see the file as "New Text Document.txt"

["I recommend having people select "Display full path in the address bar" while they are in the folder options as it makes the copy and pasting of file locations much easier when creating avisynth scripts." - Corran]

Now open up the file in your text editor and add the following line (note that capitalisation doesn't matter, it's only ever used in the guides for readibility):
Colorbars(720,480)
Save the file and go back to your folder. Now we have a text file with a command in it (I'll explain what the command does in a moment). However, this isn't much use to us like this - it needs to be an Avisynth File. To do this you need to rename the fie so it has a ".avs" extension not a ".txt" one. So, rename the file to be "colorbars.avs" This process might seem a little longwinded at first, but it becomes second nature and as I say there are programs out there to really make this a whole lot faster, which you will be introduced to later.

When you have your colorbars.avs file, you will want to play it in Windows Media Player. It is possible that when you installed Avisynth that you registered .avs files to be played in Windows Media Player - if so you can double click the file to see what it does. Otherwise you will have to load your media player and drop this file into it manually.

When you play this file you should see an image of some colorbars and hear a nasty beeeeeeeeeeeep noise. Congratulations, you have sucessfully made a working avisynth script. This script was a very simple one line command that asked Avisynth to produce a video with some coloured bars at a resolution of 720x480. This is how avisynth works, you give a command and it produces video. Let's try changing the video we have. Although it is no longer a .txt file you can still load the colorbars.avs file into a text editor like notepad or wordpad. Open up the file into a text editor and we will see if we can change things.

Firstly, that sound is really annoying so we should tell Avisynth to get rid of it. Add the following command to the end of your script:
KillAudio()
The two parenthesis do not have anything in them as this command does not have any options that you need to enter, but you should always write the parenthesis anyway.

720x480 is a little big for just playing around with too, so why not resize it. Add another line to your script:
BicubicResize(320,240)
We should now have a scipt that says:
Colorbars(720,480)
KillAudio()
BicubicResize(320,240)
Save the script and play it again in Windows Media Player. That should be smaller and much less painful on the ears. The movie is a bit on the long side though - we'll never get to the end at this rate. So, why not shorten it?
colorbars(720,480)
KillAudio()
BicubicResize(320,240)
Trim(0,300)
The Trim command takes only the frames from frame number 0 (start) and 300. There - it only lasts about 10 seconds now.

So you can start to see now how you make avisynth scripts. These commands happen in order, one by one, and it is sometimes very important that something happens before or something else. In this script, putting KillAudio() before the colorbars line would have been useless because there was no audio until the colorbars command was executed so it would not have done anything.

Using Footage as a Source

OK so we've done the very basics, now it's time to play with some real footage and see what you can really do with this program. Let's take, for example, the MPEG1 file of Kevin Caldwell's "Believe" - if you don't have this video then it's worth your while seeing. Sadly it's in 352x240 and it's not flagged to be played back in 4:3, so when I watch it poor Akari-chan looks all fat. Well, what can be done about this? Well, I could simply run it through AVISynth!

Make a new text file just as before and then rename this blank file to blahblah.avs, then open it in notepad. I'll name mine "Believe.avs".

Now within this text file put the following lines making sure to change the location of the file to your own:

DirectShowSource("D:\believe.mpg")
BicubicResize(320,240)

Then I can open the file in Windows Media Player, and presto! The video is now playing back in 320x240 resolution and everyone's back to normal. Neat, huh? So how did I do it?

Well, the first line here is basically your "load this file" command. It's actually a filter that creates a stream of video or audio which can be played with but for all practical purposes it's a way of opening the file. This command is specifically made for opening things like MPEGs and other files that aren't AVI files or don't have their own special command to load them.

To load an AVI you'd use AVISource instead of DirectShowSource.

Remember that after commands you have a pair of parentheses that can contain instructions. In this case, the instruction is the path to a file, which should always be in quotes.

There are lots of commands that do various things. File source commands are things like AVISource, DirectShowSource, MPEG2Source, WAVSource, etc. These load files and then shunt their video/audio streams into AVISynth. 

Then there are custom commands which are things like Version (which creates a video clip displaying the version number), BlankClip (which generates, you guessed it, a blank clip of a solid color with silent audio), and MessageClip (which produces a clip that has a certain message in text).

Then we have commands that actually modify clips or streams that have been loaded. In this case, BicubicResize is a filter which resizes a video clip to a new resolution using a bicubic resampling algorithm. You notice that in the brackets for BicubicResize the extra information is a new horizontal resolution and a new vertical resolution. There are other optional arguments that change the strength of certain parameters and lots of other things and you can get a description for each command and its settings in the help documentation. You should look at this to familiarize yourself with the kinds of commands AVISynth has on offer.

In fact, you should probably do that now. The best way to get to know AviSynth is to read the manual. Avisynth comes with extensive documentation that will be located in your Start Menu. Here you will find lists of all the command that avisynth can do and lots of other good things besides. There is also a copious amount of information at the main avisynth website www.avisynth.org

This said, there is even more to Avisynth than its default commands.

Avisynth Plugins

The AMVapp installer calls these Avisynth Filters simply to distinguish between them and the Premiere AVS Plugin which is a totally different thing. Avisynth plugins contain functions that people have written as additions to the core AviSynth tools. These plugins are essential to getting the most out of AviSynth as they give it abilities it would never usually have.

If you have installed the AMVapp or any 2.5x version of Avisynth, you will have a folder called "Avisynth 2.5\plugins\" and this is the default plugin folder. When plugins are put into this folder they are automatically loaded into avisynth so you can use the functions straight away in scripts.

If you do not have a default plugin folder registered or if you have a plugin that is not in this folder, you have to load them manually. This should be done at the very start of a script before any commands, e.g.:
LoadPlugin("C:\mpeg2dec\mpeg2dec3.dll")
Thankfully for you, you will not need to do this if you have the AMVapp installed. The AMVapp has a large variety of plugins and custom script functions, all of which do not need to be loaded manually.

The most important plugin commands that you may need to become familiar with are:

Mpeg2Source - This allows dvd2avi project files to be used to import mpeg2 (dvd) footage into AviSynth.
Telecide - This filter findsthe original progressive frames of a telecined video source (inverse telecine)
Decimate - This removes duplicate frames discovered in the IVTC process and reduces a video stream to its original framerate.

You will see exactly how to use these command and many more like it in the guides that are to follow.

Avisynth, Colourspaces and Conversions

You will notice that when you start using lots of functions that some of them work in some colour formats and some do not. This is something you get used to pretty fast but you also need to be cautious when converting from one colourspace to another and try not to do it too often.
YV12 - This is the colourspace used by DVDs, Mpeg1, Divx and basically most distribution formats. It is a way of storing YUV colour that has one chroma sample for a 2x2 square of pixels and hence must have a resolution in multiples of 4.

YUY2 - This is the colourspace used by some capture cards. It is another YUV format but this time there is one chroma sample for ever pair of pixels horizontally and hence must have a horizontal resolution which is a multiple of 2.

RGB24 - This is the basic Red Green Blue colourspace. One sample of each colour per pixel - can be at any resolution.

RGB32 - This is just like RGB24 but it also has an alpha channel which can be used for things like transparencies. Very handy for people doing Adobe After Effects work or exporting footage from 3D programs.
Conversions between colourspaces should be avoided as much as possible - especially conversions between YV12/YUY2 and the RGB colourspaces. You can convert from RGB24 to RGB32 and back as much as you like (except you will lose your alpha channel if you are using it).

So, if you get an error that a certain filter needs a certain colourspace then you need to add a conversion line such as
ConvertToYUY2() or ConvertToYUY2(interlaced=true)
However, try and think if this conversion is really necessary. When transferring DVD footage to an editing program you are going to have to convert to RGB32 at some point as that is the native editing format of most editing software.  Likewise when you are converting an edited amv that you have made to a divx you will need to convert it to YV12 at some point. Knowing this means you can use certain filters at certain times without having to do an unnecessary conversion. We will get back to this idea later - just remember that some commands only work in some colour formats and some programs only edit in some colour formats too.

Avisynth Includes

The final info you will need to know about Avisynth before we get onto other programs is that there are some functions which are not plugins but are loaded into avisynth when it starts. These are scripts that are contained in the plugins folder and have the extension ".avsi"

There are a number of these and they all contain interesting functions which will be described in greater detail as they are required. You can also define your own functions in these files - just follow the instructions in the AviSynth documenation on creating custom functions. These .avsi commands are automatically "included" when you make a new avs file so some of these avsi files are used for global settings. SetMemoryMax.avsi, for example, dictates how much memory each AviSynth script is allowed to use - this is to avoid crashes in editing programs when using many scripts.

That should be all you need to know about using AviSynth for now. Next we are going to look at a program which is not only invaluable for encoding video but also for making avisynth script use so much easier....