Introduction to AVISynth

If you've been around this site's guides or forums, or even some video descriptions, you've probably heard of AVISynth, sometimes just abbreviated as AVS. AVISynth is one of the most powerful tools for manipulating digital video that you can find - even more powerful than VirtualDub for manipulating footage (although AVISynth can't capture like VDub) and its ability to edit together footage matches that of programs like Windows Movie Maker. What's the downside? Well it's a little difficult to use for some people.

AVISynth is a frameserver program, which means it serves frames of video (and in some cases audio) to other programs. AVISynth is centered around AVS files, or specially formatted text files that act as a script which AVISynth executes. These scripts can then be read by other programs like Windows Media Player, VirtualDub, Premiere, TMPGEnc, and many other programs just like ordinary video streams. This allows you to basically read any video file (or produce one on-the-fly), modify it, and then transparently open it in another program.

For those who haven't gotten it yet: This is very, very cool.

For instance, say I've got the MPEG1 file of Kevin Caldwell's "Believe" that's been floating around the net for ages. 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!

Making AVISynth Files

First I'll create an AVS file using Notepad - these are just text files containing a few commends. The text files have the extension .avs so you can then run these in programs and it will appear as footage. OK, make a .avs text document. For those who don't understand this part, what I do is in windows I right-click, choose New then "New Text Document". This should create a file called "New Text Document.txt". If you do not see the TXT at the end, then you need to go to Tools->Folder Options in the Explorer menu and turn off "Hide File Extensions for Known Types" under the View tab.

When you've done that you then rename this blank file to blahblah.avs, then open it in notepad. I'll name mine "Believe.avs".

Now within this text file I'll put the following lines:

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 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 an AVI you'd use AVISource.

After each command you have a pair of parentheses that 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 streams into AVISynth. These are what you'll usually start with unless you have to load a plugin first (see later)

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.

So now I've got this neat file which plays back Believe in its correct aspect ratio. What else can I do?

Well lets say that, hypothetically, I find that whole beginning part to be really boring. I want action and good editing, not lip-synching! Well, instead of seeking past it everytime I watch the video, I could trim it out! Let's say that the good part starts at frame 1000, or around 33 seconds into the video. I can add the following line to my script:

Trim(1000,0)

Trim takes the current stream and trims bits off the ends. It takes two arguments, a start frame, and an end frame. I told it to start at frame 1000 (in other words, trim out everything before that frame) and go until the end of the file - 0 is a special value for the Trim filter which means "the last frame of the clip."

Now when I playback the AVS file in Windows Media Player, the clip is 33 seconds shorter and I don't have to skip through the intro every time I want to watch it.

Importing DVD2AVI Project Files for using dvd footage

What if I wanted to open a project file I made with DVD2AVI? AVISynth can't open D2V files natively, so what can I do? Well, you use something called the MPEG2DEC.DLL plugin! MPEG2DEC is a plugin that allows AVISynth to read these files.

If you have installed the AMVapp or if you have Avisynth 2.06 or later installed, then by having the mpeg2dec.dll file in the default plugin directory it will load automatically whenever you use one of its commands. If you are making avisynth scripts the old way then you will have to load the plugin manually using (for example)

LoadPlugin("D:\mpeg2dec.dll")

If you have the AMVapp installed, you should skip that and just use the command

MPEG2Source("D:\mydvd.d2v")

MPEG2Source is not natively part of AVISynth, but with the plugin you can use it. Note that if you add a loadplugin line for a plugin that has been autoloaded, the script probably won't work ^^;;

Important new mpeg2dec options

The copy of mpeg2dec that is supplied with the AMVapp is probably a little bit different to the one you have used when following the old guides. The AMVapp version is actually mpeg2dec3.dll which includes some very excellent post-processing features.

The advantage of this is that you can use it to clean up your footage intellegently, straight from the source. If you've ever adjusted the quality settings on divx playback or used the post-processing in ffdshow then you will know that these can erally improve a picture quality during the decode.

I highly recommend using the post processing settings either during a render from Premiere using an avisynth source or when creating clips. The new options in the current release are:

cpu - Sets the amount of Post-processing cleanup with values from 1 to 6, 6 being the highest.

iDCT : iDCT algorithm used - can increase performance by selecting the optino that fits your cpu (optional)

>0 : Default value (in .d2v file)
>1 : 32 bit MMX
>2 : 32 bit SSEMMX
>3 : 64 bit FPU
>4 : 64 bit IEEE-1180 Reference
>5 : 32 bit SSE2 (for P4)

- lumoff : offset of luma [-256;256] (default = 0)

- lumgain : gain of luma [0;256] (default = 128)

- interlaced : choose between Field-Based/Frame-Based PP (default=false)

So, to enable these options you would use a command like this:

MPEG2Source("D:\mydvd.d2v",cpu=6,interlaced=true)

I recommend seeing which cpu value looks best for you, sometimes 6 can smooth too much. This is a great way to clean up a source with very little, if any, detail removal.

Another bonus of this version of mpeg2dec is that it seemingly works with both old and new versions of DVD2AVI.